Skip to content

Instantly share code, notes, and snippets.

@redwrasse
redwrasse / dual_perceptron.py
Created February 28, 2018 07:39
dual perceptron tensorflow
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))])
@redwrasse
redwrasse / kernel_perceptron.py
Created February 28, 2018 07:44
kernel perceptron tensorflow
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)),
@redwrasse
redwrasse / gd.py
Last active July 16, 2020 04:02
gradient descent analytically implemented for linear regression
# 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,
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
@redwrasse
redwrasse / binary_undirected_graphical_models.py
Last active February 20, 2020 16:58
undirected graphical models over binary variables
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
@redwrasse
redwrasse / mc_pi.py
Last active July 27, 2020 02:12
monte carlo approximation of pi
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.
@redwrasse
redwrasse / convolution_apis.py
Created March 30, 2020 00:45
example convolution apis
# 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
@redwrasse
redwrasse / conv_check.py
Last active July 15, 2020 22:34
verifies output length and partial derivatives of standard and left-padded 1d convolutions
"""
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
@redwrasse
redwrasse / parameterized_mu_sigma.py
Last active May 18, 2020 18:37
supervised learning with mse loss with Gaussian parameterized by both mu and sigma
"""
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
@redwrasse
redwrasse / cc_statistics.py
Created April 7, 2020 00:05
statistics for a simple communication channel with bit-repetition encoding
"""
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))