Skip to content

Instantly share code, notes, and snippets.

View jakelevi1996's full-sized avatar

jakelevi1996

View GitHub Profile
@jakelevi1996
jakelevi1996 / Mutable types, immutable types, names, and objects.md
Last active October 17, 2018 16:45
Mutable types, immutable types, names, and objects

Mutable types, immutable types, names, and objects

The following statements are quotes from Jeff Knupp's blog-post "Is Python call-by-value or call-by-reference? Neither."

In Python a variable is not an alias for a location in memory. Rather, it is simply a binding to a Python object.

  • Changes to a mutable object are visible through all names bound to it. Python's lists are an example of mutable objects.
  • The value of immutable objects can not be modified after they are created. They can be used to compute the values of new objects.

When you think about it, this dichotomy is necessary because, again, everything is an object in Python. If integers were not immutable I could change the meaning of the number '2' throughout my program.

@jakelevi1996
jakelevi1996 / Self-contained, single-script example of training a neural network in TensorFlow.md
Last active October 17, 2018 18:45
Self-contained, single-script example of training a neural network in TensorFlow

Self-contained, single-script example of training a neural network in TensorFlow

Shown below is the code for a self-contained, single-script example of a TensorFlow program; the steps it goes through are as follows:

  1. create_data_set():
    1. Create a randomly generated data-set, consisting of a training set, and a uniform grid for evaluating the model
  2. train_and_eval():
    1. Define a model
    2. Train the model on the training data (while keeping a log of summary operations)
    3. Save the trained model
    4. Restore the trained model, and evaluate it on the evaluation data
@jakelevi1996
jakelevi1996 / Running TensorFlow Sessions inside sub-processes.md
Last active July 4, 2022 19:13
Running TensorFlow Sessions inside sub-processes

Running TensorFlow Sessions inside sub-processes

In some applications of machine learning/TensorFlow, it is desirable to start multiple processes, and have separate training procedures running concurrently in each of those processes. A useful Python method for achieving this is the multiprocessing.pool.Pool.map() method (or the equivalent starmap() method when the target function takes multiple arguments; see the section "Process Pools" from the description of the multiprocessing module in the Python Library Reference).

The Pool.map() method takes a target-function and a list (or more generally an iterable) of arguments, and returns an equivalent iterable of the results of the function evaluated on each member of the argument-list (which is similar to the [built-in Python function map()

@jakelevi1996
jakelevi1996 / Distributed MapReduce in TensorFlow.md
Last active October 18, 2018 22:46
Distributed MapReduce in TensorFlow

Distributed MapReduce in TensorFlow

Detailed description and tidying up of code to come later; for now:

import tensorflow as tf
import numpy as np
from multiprocessing.pool import Pool
from time import time
@jakelevi1996
jakelevi1996 / Gist "To do" list.md
Last active February 17, 2020 17:05
Gist "To do" list

Gist "To do" list

Here is my to-do list for Gists/things I intend to write Gists about:

  • Generic Bayesian linear regression: as basis functions, define some classes with __call__ methods (EG Gaussian, polynomials) which take appropriate parameters to their __init__ method (EG location, scale, polynomial order); a given Bayesian linear regression solution should accept a list of basis functions, whose outputs for each data point can be calculated with the built-in map function, or numpy.vectorise, and compare the output with the MAP and ML solutions, EG:
import numpy as np

class Sigmoid:
    def __init__(self, loc): self.loc = loc
 def __call__(self, x): return 1.0 / (1.0 + np.exp(self.loc - x))
@jakelevi1996
jakelevi1996 / Checking and reducing a series of homogeneous objects.md
Last active December 19, 2018 14:12
Checking and reducing a series of homogeneous objects

Checking and reducing a series of homogeneous objects

Ever had a bunch of values or objects in a Python program which are all supposed to be equal (for example lengths of lists that are being iterated through concurrently), and need to arbitrarily pick one, while wanting to assert that they're all the same?

Here is an elegent solution to the problem, based on the built-in Python data-type known as a set:

def check_reduce(*arg_list):
    arg_set = set(arg_list)
@jakelevi1996
jakelevi1996 / Speed tests.md
Last active December 31, 2019 01:02
Speed tests

Speed tests

Shown below is some code to perform generic speed tests and save a log-log graph of the results (in this example, eigenvalue decomposition for SPD matrices is being tested). The resulting graph is shown below the code:

import numpy as np
import matplotlib.pyplot as plt
import scipy as sp
from time import perf_counter
@jakelevi1996
jakelevi1996 / Automating Tensorboard.md
Last active May 18, 2019 19:12
Automating Tensorboard

Automating Tensorboard

In order to run Tensorboard, you have to open a command window, type a command which resembles tensorboard --logdir ., open a browser, and then navigate to the appropriate URL; altogether this procedure is extremely difficult and entirely repulsive. The following batch script, saved as run tensorboard.bat, automates the process:

start /b tensorboard --logdir .
start /b http://Jakes-Laptop:6006

Note that you shouldn't save the script simply as tensorboard.bat ([source][1]).

@jakelevi1996
jakelevi1996 / Listing primes.md
Last active July 31, 2020 12:13
Listing primes

Listing primes

An efficient way to calculate all primes less than a certain threshold is to use a prime number sieve. This method can also be used to calculate the nth prime, as shown below:

import numpy as np

def prime_number_sieve(n_elements=100):
    sieve = np.ones(n_elements, dtype=int)
    sieve[0] = sieve[1] = sieve[4::2] = 0
@jakelevi1996
jakelevi1996 / Pointers in C.md
Last active September 24, 2023 06:39
Pointers in C (including function pointers)

Pointers in C (including function pointers)

In the C programming language, pointers are used to refer to the memory locations in which variables are stored. Some code snippets are shown below which demonstrate why they are useful and how to properly use them; the full pointers.c program and its output are included at the end.

Accessing a memory location using &

In C, the & operator is used for accessing the memory address of a variable; for example:

int x = 10;