Skip to content

Commit

Permalink
update ob5
Browse files Browse the repository at this point in the history
  • Loading branch information
camilomarino committed Dec 2, 2020
1 parent c822af3 commit a596e10
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 10 deletions.
2 changes: 1 addition & 1 deletion ob5/ej1.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class DecreasingStep:
def __init__(self, const, *k1, **k2):
self.const = const
def step(self, k):
return self.const/k
return self.const/(k+5)
def __str__(self):
return fr'$\alpha_k = {self.const}/k$'

Expand Down
71 changes: 62 additions & 9 deletions ob5/ej2.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-

import numpy as np
np.random.seed(5)
import matplotlib.pyplot as plt

gatos = np.loadtxt('Gatos.asc').astype(np.uint8)
Expand All @@ -12,7 +13,7 @@ def vect2img(x):


plt.figure()
plt.imshow(vect2img(gatos[:, 1]))
plt.imshow(vect2img(gatos[:, 0]))
#%%

plt.figure()
Expand All @@ -35,11 +36,13 @@ def ReLU(x, epsilon):
else:
return epsilon * x

def F(x, y, a):
def F(x, y, a, n):
if a.T@x <= 0:
value = epsilon
#print('chico'+ ' '+ n)
else:
value = 1
#print('grande'+ ' '+ n)
return 2 * (-y + ReLU(a.T@x, epsilon)) * value * x

# gatos : label=0
Expand All @@ -50,17 +53,22 @@ def F(x, y, a):
step = ConstantStep(1e-9)

a = np.zeros((gatos.shape[0],1))

ases = list()
ases.append(a)
#%%
from tqdm import tqdm
K = 20
N_EPOCHS = 50
for n in range(N_EPOCHS):
N_EPOCHS = 100
for n in tqdm(range(N_EPOCHS)):
for k in range(K):
gato = gatos[:, k:k+1]
conejo = conejos[:, k:k+1]

a = a - step.step()*F(gato, gato_label, a)
a = a - step.step()*F(conejo, conejo_label, a)
a = a - step.step()*F(gato, gato_label, a, 'gato')
#print(np.unique(a))
#ases.append(a)
a = a - step.step()*F(conejo, conejo_label, a, 'conejo')
#print(np.unique(a))
#ases.append(a)

#%%
# Train
Expand Down Expand Up @@ -91,4 +99,49 @@ def F(x, y, a):
for k in range(20,30):
conejo = conejos[:, k:k+1]
v = a.T@conejo
print(v, label(v))
print(v, label(v))

#%%
plt.figure(figsize=(7,7))
for k in range(20):
plt.scatter(gatos[:,k]@a, k, c='g', label='gatos')
plt.scatter(conejos[:,k]@a, k, c='r', label='gatos')
plt.axvline(0.5)
plt.legend(['linea divisoria', 'gatos', 'conejos'])
plt.title('Ejemplos de train')
plt.xlabel(r'$a^Tx$')
plt.ylabel('Indice')
plt.grid(True)
plt.savefig('figures/ej2_train')

#%%
plt.figure(figsize=(7,7))
for k in range(20,30):
plt.scatter(gatos[:,k]@a, k, c='g', label='gatos', marker='o')
plt.scatter(conejos[:,k]@a, k, c='r', label='gatos', marker='x')
plt.axvline(0.5)
plt.legend(['linea divisoria', 'gatos', 'conejos'])
plt.title('Ejemplos de validación')
plt.xlabel(r'$a^Tx$')
plt.ylabel('Indice')
plt.grid(True)
plt.savefig('figures/ej2_val')

#%%
def a2img(a):
v = vect2img(a)
v -= v.min()
v /= v.max()
v *= 255
v = v.astype(np.uint8)
return v


import imageio
plt.figure()
plt.imshow(v:=a2img(a))
#plt.title(fr'$a$')
plt.axis(False)
imageio.imsave('figures/ej2_a.png', v)


Binary file modified ob5/figures/ej1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ob5/figures/ej2_a.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ob5/figures/ej2_test.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ob5/figures/ej2_train.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ob5/figures/ej2_val.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit a596e10

Please sign in to comment.