This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import numpy | |
| import torch | |
| import torchvision | |
| import matplotlib.pyplot as plt | |
| def relu(x): | |
| return numpy.maximum(0, x) | |
| def relu_derivative(x): | |
| return (x > 0).astype(float) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import numpy as np | |
| from time import sleep | |
| from itertools import count | |
| from pdb import set_trace | |
| import matplotlib.pyplot as plt | |
| def softmax(x): | |
| y = np.exp(x - x.max(-1, keepdims=True)) | |
| return y / y.sum(-1, keepdims=True) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import torch | |
| import torchvision | |
| import matplotlib.pyplot as plt | |
| import matplotlib.patches as patches | |
| from matplotlib.backend_bases import MouseEvent | |
| import torch | |
| def onclick(event): | |
| x = torch.tensor([event.ydata, event.xdata]).float() | |
| d = (y - x).float().norm(dim=-1) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from itertools import cycle, count | |
| import string | |
| alphabet = string.ascii_lowercase + string.ascii_uppercase + ' ' + string.digits + string.punctuation | |
| encode = dict(zip(alphabet, count())).get | |
| decode = dict(zip(count(), alphabet)).get | |
| add = lambda a, b: decode((encode(b) + encode(a)) % len(alphabet)) | |
| sub = lambda a, b: decode((encode(b) - encode(a)) % len(alphabet)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # https://www.sciencedirect.com/science/article/pii/0304397588900461 | |
| def stackermann(i, n): | |
| stack = [] | |
| stack.append(i) | |
| stack.append(n) | |
| while len(stack) > 1: | |
| n = stack.pop() | |
| i = stack.pop() | |
| if i == 0: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import numpy as np | |
| import sympy.ntheory | |
| import time | |
| # https://mathoverflow.net/a/227408/74578 | |
| def mobius_transform(sequence): | |
| sequence = sequence.copy() | |
| for i in range(1, len(sequence)//2+1): | |
| sequence[i+i-1::i] -= sequence[i-1] | |
| return sequence |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import numpy as np | |
| import gurobipy | |
| import cvxopt | |
| from multiprocessing import Pool | |
| from timeit import default_timer as timer | |
| gurobipy.setParam('OutputFlag', 0) | |
| def max_min_gurobi(game): | |
| model = gurobipy.Model() | |
| v = model.addMVar(1) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from functools import partial | |
| import jax | |
| import optax | |
| from jax import numpy as jnp, random | |
| def pseudo_gradient( | |
| f, | |
| x, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import jax | |
| from jax import numpy as jnp | |
| from jax.flatten_util import ravel_pytree | |
| def centered_finite_differences(f, x, eps): | |
| x, unravel = ravel_pytree(x) | |
| def helper(index): | |
| xp = x.at[index].add(eps) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import argparse | |
| import jax | |
| from jax import lax, numpy as jnp, random | |
| from matplotlib import pyplot as plt, rcParams | |
| def newton_raphson(f, x, iters): | |
| """Use the Newton-Raphson method to find a root of the given function.""" |
OlderNewer