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
def dual_perceptron(): | |
import numpy as np | |
import tensorflow as tf | |
c1 = np.random.randn(50, 75) + 1 | |
c2 = np.random.randn(50, 75) - 1 | |
X = np.vstack([c1, c2]) | |
Y = np.concatenate([np.ones((50, 1)), 1 - np.zeros((50, 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
def kernel_perceptron(): | |
import numpy as np | |
import tensorflow as tf | |
c1 = np.random.randn(50, 75) + 1 | |
c2 = np.random.randn(50, 75) - 1 | |
X = np.vstack([c1, c2]) | |
Y = np.concatenate([np.ones((50, 1)), 1 - np.zeros((50, 1))]) | |
alpha = tf.Variable(tf.random_normal((100, 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
# gradient descent for linear regression, with gradients given analytically | |
""" | |
Math: | |
loss = (1 / N) sum_n { (y_n - y_n*)^2} := (1 / N) sum_n { z_n^2 } | |
y_n*_i = A_ik x_n_k + b_i | |
partial_{A_jk} y_n*_i = del_ij x_n_k | |
partial_{b_j} y_n*_i = del_ij | |
Hence, |
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 itertools | |
""" | |
an example training an undirected graphical model | |
over a small number of binary variables, brute force | |
computing the partition function. | |
The model has a pairwise energy function E = w_ij x_i x_j + a_i x_i | |
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 itertools | |
""" | |
an example training an undirected graphical model | |
over a small number of binary variables, brute force | |
computing the partition function. | |
The model has a pairwise energy function E = w_ij x_i x_j + a_i x_i | |
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 random | |
def compute_pi(): | |
sm = 0. | |
n = 10**7 | |
for i in range(n): | |
x1, x2 = 2 * (random.uniform(0, 1) - 0.5), 2 * (random.uniform(0, 1) - 0.5) | |
r = x1**2 + x2**2 | |
if r <= 1.0: | |
sm += 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
# example convolution apis | |
import tensorflow as tf | |
from tensorflow import keras | |
# a 2d convolution with 10 filters and a '5 by 5' | |
# kernel size on a image of 32 by 32 with 3 color dimensions | |
# so output 'image' dimensions are each 32 - 5 + 1 = 28 |
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
""" | |
verifies expected output length and partial derivatives of | |
a) standard 1d convolution | |
b) left-padded 1d convolution | |
""" | |
import torch | |
import torch.nn.functional as F |
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
""" | |
supervised learning with mse loss with Gaussian parameterized by both mu and sigma | |
""" | |
import numpy as np | |
import torch | |
N = 200 # number of training points | |
d = 5 # dimension of training inputs |
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
""" | |
modeling a simple communication channel | |
with bit repetition as the encoder and | |
majority vote as the decoder. | |
""" | |
import math | |
def bin_coeff(n, k): | |
return math.factorial(n) / (math.factorial(k) * math.factorial(n - k)) |