Skip to content

Instantly share code, notes, and snippets.

@carlosgmartin
carlosgmartin / vigenere.py
Created June 20, 2019 06:36
Python implementation of the Vigenère cipher
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))
@carlosgmartin
carlosgmartin / hausdorff.py
Last active June 20, 2019 06:37
Demo of Hausdorff distance between a point and a fuzzy set
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)
@carlosgmartin
carlosgmartin / qlearning_parallel.py
Last active June 20, 2019 06:39
Q learning with multiple simultaneous agents
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)
@carlosgmartin
carlosgmartin / numpy-neuralnet
Created September 16, 2018 19:19
Pure-numpy neural network on CIFAR10 dataset
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)