You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Instantly share code, notes, and snippets.
Jake Levi
jakelevi1996
PhD student at the University of Oxford, studying Autonomous Intelligent Machines and Systems (AIMS)
Recursively flatten an irregularly nested list in Python
Recursively flatten an irregularly nested list in Python
As described in my answer to a question on Stack Overflow, here is some code to recursively flatten an irregularly nested list in Python using generator functions:
Python: converting a list of dictionaries to a dictionary of lists
Python: converting a list of dictionaries to a dictionary of lists
Shown below is a method for converting a list of dictionaries to a dictionary of lists in Python, when the original list of dictionaries share some (but not all) of the same keys:
Single script example of using TensorRT and TensorFlow
Single script example of using TensorRT and TensorFlow
This Gist demonstrates a self-contained, single-script example of how to define a simple Keras CNN model, train it on MNIST, convert to TensorRT format, and then perform GPU inference on the Jetson Nano. The script is shown below, followed by the console output.
TODO: fix errors; maybe ask question on Stack Overflow and raise issue on Nvidia website
NB, according to this answer on Stack Overflow, the error messages can be removed by limiting the GPU memory to 2GB (after importing Tensorflow but before importing anything else) as follows:
sounddevice is a Python module for playing and recording sounds using physical audio devices connected to a computer. The documentation is available here. It can be installed with the command python -m pip install sounddevice (use python3 instead of python on Linux).
soundfile is a Python module for reading and writing audio files. The documentation is available here. It can be installed with the command python -m pip install soundfile (use python3 instead of python on Linux).
This Gist demonstrates simple usage of the sounddevice and soundfile modules. Two simple triads (one major and one minor) are created using pure tones (sinusoids). They are played through the speakers using sounddevice and then saved as WAV files using soundfile (currently, MP3 files are not supported by soundfile because they are not supported by the cross-plat
The TensorFlow Datasets module provides easy access to many useful machine learning data sets. It can be installed from pip using the following command:
pip install tensorflow-datasets
There are many datasets available from this module in various categories, including audio, image classification, object detection, text, translation, and more. The full list of data sets and categories is available here. One useful example is the imagenette data-set, a small version of imagenet with 10 classes, 9,469 training examples and 3,925 validation examples, available in 3 different resolutions, requiring 100 MB, 330 MB, and 1.5 GB respectively. Below is an example script which downloads imagenette if it isn't available already, and displays images from it one by one using matplotlib:
timeplot: a Python module for plotting time complexity
timeplot: a Python module for plotting time complexity
Here is the source code (and some usage examples and their resulting images, shown below) for timeplot: a Python function/module which accepts a dictionary of string:function pairs, and plots the time complexity of each function over a specified range of input sizes (the key-strings are used as legend entries). Each function must accept an integer size, and return a floating-point value for the time-taken for the function's main operation (not including set-up code). Usage examples and images shown below include comparing different matrix operations, comparing strategies for modifying an iterable while iterating over it, and comparing the efficiency of operations on python lists vs dicts. The images are shown first, followed by the modules that generated them using timeplot.
The Python built-in function property can be used as a decorator to create read-only attributes of a class, as described in the Python documentation for built-in functions. To make a property read-only, simply do not define a setter method for that property, in which case an AttributeError will be raised when trying to assign a value to that property, as demonstrated below:
Python provides a timeit module for timing statements and function calls. Below are some examples of using timeit, along with some results to demonstrate the efficiency of various different operations in Python. Using this approach can be useful before implementing a new feature when there are a few different options for how how to implement that feature, in order to gauge which approach will be the most efficient.
Different types of function calls
When using timeit, there are various different options for choosing how to time a function, EG using default arguments, wrapping the function in a lambda expression, or putting the statement in a string, which supposedly reduces the overhead. This example demonstrates the consequences for execution time of those different choices, as well as using the repeat function from the timeit module, which calls the function repeatedly, and returns a list of the results; as stated in [the d
Householder transformations and the QR decomposition
Householder transformations and the QR decomposition
This Gist demonstrates some code for generating Householder transformations, and using it to perform QR decompositions, including pytest unit tests. This is prototype code, and could be made more efficient in various ways; for example, when performing the QR decomposition, calculating v (the Householder vector) could be made a lot more efficient by using the fact that y is always a one-hot vector, and the memory efficiency can be improved by normalising v such that the first non-zero element is unity, and storing the rest of the vector in the corresponding column of R, as described in chapter 5 of Matrix Computations by Golub and Van Loan (4th edition).