In [1]:
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt

Softmax


In [5]:
def softmax(scores):
    exps = np.exp(scores)
    return exps / exps.sum()

In [53]:
a = np.array([2., -1, 1])
plt.stem(a)
plt.xlim([-1, len(a)]);



In [54]:
a2 = softmax(a)
plt.stem(a2)
plt.xlim([-1, len(a)])
print("Softmax probabilities sum to", np.sum(a2))


Softmax probabilities sum to 1.0

Dense (similarities)


In [24]:
prototypes_raw = np.array([
    [3, 1, 1],
    [1, 3, 1],
    [1, 1, 3]], dtype=float)
prototype_magnitudes = np.linalg.norm(prototypes_raw, axis=1, keepdims=True)
prototypes = prototypes_raw / prototype_magnitudes
prototypes


Out[24]:
array([[ 0.90453403,  0.30151134,  0.30151134],
       [ 0.30151134,  0.90453403,  0.30151134],
       [ 0.30151134,  0.30151134,  0.90453403]])

In [25]:
def similarities(vec, prototypes):
    return np.dot(prototypes, vec)
similarities([1, 2, 3], prototypes)
similarities([3, 2, 1], prototypes)


Out[25]:
array([ 3.61813613,  3.01511345,  2.41209076])

In [26]:
softmax(similarities([1, 2, 3], prototypes))


Out[26]:
array([ 0.16213016,  0.29631473,  0.54155511])

Nonlinearity


In [27]:
def relu(vector):
    return np.maximum(vector, 0)
x = np.linspace(-2, 2, 100)
plt.plot(x, relu(x))


Out[27]:
[<matplotlib.lines.Line2D at 0x1083080b8>]

In [28]:
def tanh(vector):
    return np.tanh(vector)
plt.plot(x, tanh(x));


Out[28]:
[<matplotlib.lines.Line2D at 0x10825def0>]

Convolution


In [ ]:
from scipy.misc import imsave, fromimage, toimage
from PIL import Image, ImageOps

In [31]:
DATA = '../data/'
WIDTH = HEIGHT = 224
def load_and_crop_image(filename, target_size):
    return ImageOps.fit(Image.open(filename), target_size)

In [43]:
gatos = np.array([fromimage(load_and_crop_image(DATA+'/gatos/cat.{:04}.jpg'.format(i), (WIDTH, HEIGHT))) for i in range(1,100)])
gatos_nocolor = np.mean(gatos, axis=3)

In [44]:
img = gatos_nocolor[0]
toimage(img)


Out[44]:

In [45]:
import scipy.signal

Cambia filt hasta que la imagen parece como lo siguiente


In [49]:
filt = np.array([
        [1, 0, -1],
        [1, 0, -1],
        [1, 0, -1]
    ])
convolved = scipy.signal.convolve2d(img, filt)
toimage(convolved)


Out[49]:

In [49]:
filt = np.array([
        [1, 0, -1],
        [1, 0, -1],
        [1, 0, -1]
    ])
convolved = scipy.signal.convolve2d(img, filt)
toimage(convolved)


Out[49]:

In [ ]: