Skip to content

Instantly share code, notes, and snippets.

@redwrasse
redwrasse / patternmatchautomata.c
Last active July 16, 2020 03:16
String pattern matching automaton in c
/*----------------------------
Collection of states {s_i}. Sequence of inputs (say characters) {c_j}. State transition function T: (s_i, c_j) |-> s_k
. For pattern matching we would say one such state corresponds to a ‘match’. For string matching allowed state transitions
are increments until match found or ‘back to the beginning’ aka start over.
-----------------------------*/
#include <stdio.h>
#include "string.h"
@redwrasse
redwrasse / base_to_decimal.py
Last active May 19, 2020 18:17
base to decimal and vice versa conversions
# base_to_decimal.py
"""
a base n to decimal converter and vice versa,
as well as base to base converter
Not all decimals can be expressed in a certain base as a
finite sequence; these are handled with an exception after
exceeding TERMINATION_LENGTH
characters allowed are 0-9 followed by A-Z
@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))
@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 / 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 / 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 / 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 / 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
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 / 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,