Skip to content

Commit

Permalink
Ob 5 scripts agregados
Browse files Browse the repository at this point in the history
  • Loading branch information
camilomarino committed Nov 30, 2020
1 parent e227207 commit c822af3
Show file tree
Hide file tree
Showing 3 changed files with 165 additions and 0 deletions.
71 changes: 71 additions & 0 deletions ob5/ej1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# -*- coding: utf-8 -*-

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


class ConstantStep:
def __init__(self, const):
self.const = const
def step(self, *k1, **k2):
return self.const
def __str__(self):
return fr'$\alpha_k = {self.const}$'
class DecreasingStep:
def __init__(self, const, *k1, **k2):
self.const = const
def step(self, k):
return self.const/k
def __str__(self):
return fr'$\alpha_k = {self.const}/k$'

A = np.array([[2, 1],
[1, 2]])
theta_0 = np.array([[1],
[1]])


X = lambda :A + np.random.randn(2,2)
y = lambda X: X@theta_0 + np.random.randn(2,1)

Ff = lambda X, y, theta: 2*X.T @ (X@theta - y)

stepsTypes = [ConstantStep(0.1), ConstantStep(0.01), ConstantStep(0.001),
DecreasingStep(0.1)]


plt.figure(figsize=(7,7))
plt.xlabel('x')
plt.ylabel('y')
plt.title(r'Trayectoria $\theta_k$')

for step_type in stepsTypes:
thetas = list()
theta_k = np.zeros((2,1))
thetas.append(theta_k)

alpha_const = 0.1

K = 10000
for k in range(K):
X_ = X()
y_ = y(X_)
F = Ff(X_, y_, theta_k)
alpha_k = step_type.step(k+1)
theta_k = theta_k - alpha_k * F
thetas.append(theta_k)

print(theta_k)

thetas_ = np.array(thetas)[:,:,0]

plt.plot(thetas_[:, 0], thetas_[:, 1], label=str(step_type))

plt.scatter(1, 1, c='r')
plt.xlim(-.25,2.25)
plt.ylim(-.25,2.25)
plt.grid(True)

plt.legend()
plt.savefig('figures/ej1.png')
94 changes: 94 additions & 0 deletions ob5/ej2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# -*- coding: utf-8 -*-

import numpy as np
import matplotlib.pyplot as plt

gatos = np.loadtxt('Gatos.asc').astype(np.uint8)
conejos = np.loadtxt('Conejos.asc').astype(np.uint8)

#%%
def vect2img(x):
return np.transpose((x[:-1].reshape(3, 256,256)), (2,1,0))


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

plt.figure()
plt.imshow(np.transpose((conejos[:-1, 10].reshape(3, 256,256)), (2,1,0)))
#%%

class ConstantStep:
def __init__(self, const):
self.const = const
def step(self, *k1, **k2):
return self.const
def __str__(self):
return fr'Paso constante $\alpha_k = {self.const}$'

epsilon = 0.1

def ReLU(x, epsilon):
if x>0:
return x
else:
return epsilon * x

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

# gatos : label=0
# conejos : label=1
gato_label = 0
conejo_label = 1

step = ConstantStep(1e-9)

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

#%%
K = 20
N_EPOCHS = 50
for n in 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)

#%%
# Train
label = lambda x: 'gato' if x<0.5 else 'conejo'

print('GATOS')
for k in range(0,10):
gato = gatos[:, k:k+1]
v = a.T@gato
print(v, label(v))

print('\n\nCONEJOS')
for k in range(0,10):
conejo = conejos[:, k:k+1]
v = a.T@conejo
print(v, label(v))
#%%
# Test
label = lambda x: 'gato' if x<0.5 else 'conejo'

print('GATOS')
for k in range(20,30):
gato = gatos[:, k:k+1]
v = a.T@gato
print(v, label(v))

print('\n\nCONEJOS')
for k in range(20,30):
conejo = conejos[:, k:k+1]
v = a.T@conejo
print(v, label(v))
Binary file added 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.

0 comments on commit c822af3

Please sign in to comment.