In [2]:
    
%matplotlib inline
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
import scipy as sc
import pandas as pd
# import seaborn as sns
# sns.set(color_codes=True)
# plt.figure(figsize=(5,5))
# df = pd.read_csv(u'data/wind_tribune.csv')
# sns.jointplot(x='wind_speed', y='production', data=df);
# plt.show()
    
In [4]:
    
df_iris = pd.read_csv(u'notes/data/iris.txt',sep=' ')
    
In [10]:
    
df_iris
    
    Out[10]:
In [16]:
    
target = np.array(df_iris['c'])
features = np.array(df_iris[['sl','sw','pl','pw']])
    
In [17]:
    
features
    
    Out[17]:
In [18]:
    
w = np.array([1,1,1,1])
def f(x, w):
    return x.dot(w)
def E(y, f_est):
    return np.sum((y-f_est)**2)
    
In [19]:
    
f_est = f(features, w)
print(E(target, f_est))
    
    
In [27]:
    
w = np.array([1,1,1,1])
eta = 0.0001
for epoch in range(10000):
    f_est = f(features, w)
    e = target - f_est
    dE = -features.T.dot(e)
    
    w = w - eta*dE
    if epoch%100==0:
        print(E(target, f_est))
        print(w)
    
    
In [29]:
    
plt.plot(features.dot(w), target, 'o')
    
    Out[29]:
    
In [107]:
    
def f(x):
    return x**2 - 2*x
def df(x):
    return 2*x - 2
x = 0
eta = 0.1
for i in range(200):
    x = x - eta*df(x)
    
    print(x, df(x))
    
    
In [108]:
    
def f(x):
    return 3*x[0]**2 + 2*x[1]**2 - 2*x[0]*x[1] + x[0] - x[1]
def df(x):
    return np.array([6*x[0]  - 2*x[1] + 1 ,  4*x[1] - 2*x[0]  - 1])
x = np.array([0, 0])
eta = 0.1
for i in range(200):
    x = x - eta*df(x)
    
    print(x, df(x))
    
    
In [12]:
    
x = np.array([2,-1,5,-3,1])
y =  np.array([1, 0, 1, 0, 1])
w = 0.1
E = np.sum(-y*x*w + np.log(1+np.exp(x*w)))
    
    
In [15]:
    
%matplotlib inline
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
import scipy as sc
import pandas as pd
df_iris = pd.read_csv(u'/Users/cemgil/src/ipynb/notes/data/iris.txt',sep=' ')
    
In [24]:
    
y = np.array(df_iris['c'])
y[y>1] = 0
X = np.array(df_iris[['sl','sw','pl','pw']])
    
In [27]:
    
w = np.ones(4)
E = 0
for i in range(len(y)):
    f = X[i,:].dot(w)
    E += -y[i]*f + np.log(1+np.exp(f))
    
In [39]:
    
%matplotlib inline
import matplotlib.pylab as plt
import numpy as np
import torch
import torch.autograd
from torch.autograd import Variable
y = torch.Tensor(np.array(df_iris['c'])).double()
y[y>1] = 0
X = Variable(torch.Tensor(np.array(df_iris[['sl','sw','pl','pw']])).double(), requires_grad=False)
w = Variable(20*torch.randn(4).double(), requires_grad=True)
# learning rate
eta = 0.0005
for epoch in range(1000):
    for i in range(X.shape[0]):
        f = torch.matmul(X[i,:], w)
        E = -y[i]*f + torch.log(1+torch.exp(f))
        
        # Compute the gradients by automated differentiation
        E.backward()
    
    #print(E.data)
    
    # For each adjustable parameter 
    # Move along the negative gradient direction
    w.data.add_(-eta * w.grad.data)
    
    
    # Reset the gradients, as otherwise they are accumulated in param.grad
    w.grad.zero_()
    
ws = w.data
print(ws)
    
    
In [ ]:
    
x = [4.9, 3.7, 1.4, 0.3 ]
    
In [42]:
    
X.shape[0]
    
    Out[42]:
In [138]:
    
%matplotlib inline
import matplotlib.pylab as plt
import numpy as np
import torch
import torch.autograd
from torch.autograd import Variable
x = Variable(20*torch.randn(1).double(), requires_grad=True)
# learning rate
eta = 0.0005
for epoch in range(1000):
    ## Compute the forward pass
    #f = torch.matmul(A, w)
    #f = 3*x[0]**2 + 2*x[1]**2 - 2*x[0]*x[1] + x[0] - x[1]
    #f = x**2 - 2*x
    f = torch.sin(x)*x**2
    
    # Compute the gradients by automated differentiation
    f.backward()
    
    # For each adjustable parameter 
    # Move along the negative gradient direction
    x.data.add_(-eta * x.grad.data)
    
    
    # Reset the gradients, as otherwise they are accumulated in param.grad
    x.grad.zero_()
    
print(epoch,':',f.data[0])
xs = x.data
fs = f.data[0]
print(x.data)
    
    
In [139]:
    
x = np.linspace(-30,30,100)
plt.plot(x, np.sin(x)*x**2)
plt.plot(xs[0], fs, 'ro')
plt.show()
    
    
In [140]:
    
%matplotlib inline
import matplotlib.pylab as plt
import numpy as np
import torch
import torch.autograd
from torch.autograd import Variable
y = [1,2,3,-1]
x = [-2,-1,0, 1]
w = Variable(20*torch.randn(4).double(), requires_grad=True)
# learning rate
eta = 0.0005
for epoch in range(1000):
    ## Compute the forward pass
    #f = torch.matmul(A, w)
    #f = 3*x[0]**2 + 2*x[1]**2 - 2*x[0]*x[1] + x[0] - x[1]
    #f = x**2 - 2*x
    #f = torch.sin(x)*x**2
    for i in range(len(x)):
        f = w[0]*torch.sin(w[1]*x[i]) + w[2]*torch.cos(w[3]*x[i])
        E = (y[i]-f)**2
        
        # Compute the gradients by automated differentiation
        E.backward()
    
    # For each adjustable parameter 
    # Move along the negative gradient direction
    w.data.add_(-eta * w.grad.data)
    
    
    # Reset the gradients, as otherwise they are accumulated in param.grad
    w.grad.zero_()
    
ws = w.data
print(ws)
    
    
Stochastic Gradient Descent
In [ ]:
    
%matplotlib inline
import matplotlib.pylab as plt
import numpy as np
import torch
import torch.autograd
from torch.autograd import Variable
y = [1,2,3,-1]
x = [-2,-1,0, 1]
w = Variable(20*torch.randn(3).double(), requires_grad=True)
# learning rate
eta = 0.00005
for epoch in range(1000):
    ## Compute the forward pass
    #f = torch.matmul(A, w)
    #f = 3*x[0]**2 + 2*x[1]**2 - 2*x[0]*x[1] + x[0] - x[1]
    #f = x**2 - 2*x
    #f = torch.sin(x)*x**2
    for i in range(len(x)):
        f = w[0] + w[1]*x[i] + w[2]*x[i]**2 
        E = (y[i]-f)**2
        
        # Compute the gradients by automated differentiation
        E.backward()
    
        # For each adjustable parameter 
        # Move along the negative gradient direction
        w.data.add_(-eta * w.grad.data)
    
        # Reset the gradients, as otherwise they are accumulated in param.grad
        w.grad.zero_()
    
ws = w.data
print(ws)
    
In [8]:
    
%matplotlib inline
import matplotlib.pylab as plt
import numpy as np
import torch
import torch.autograd
from torch.autograd import Variable
y = [32,29,49,13, 51, 28, 35]
w = Variable(200*torch.randn(1).double(), requires_grad=True)
# learning rate
eta = 0.005
for epoch in range(1000):
    for i in range(len(y)):
        f = w[0] 
        E = (y[i]-f)**2
        
        # Compute the gradients by automated differentiation
        E.backward()
    
        # For each adjustable parameter 
        # Move along the negative gradient direction
        w.data.add_(-eta * w.grad.data)
    
        # Reset the gradients, as otherwise they are accumulated in param.grad
        w.grad.zero_()
    
    print(float(w.data))
    
ws = w.data
print(ws)
    
    
In [11]:
    
x = np.linspace(-30,30,100)
plt.plot(x, np.log(1/(np.exp(-x)+1)))
plt.show()
    
    
In [7]:
    
np.mean(y)
    
    Out[7]:
In [43]:
    
f_est = f(features, w)
c = np.round(f_est)
conf_mat = np.zeros((3,3))
for i in range(len(c)):
    conf_mat[target[i]-1, int(c[i])-1] += 1
    
In [44]:
    
conf_mat
    
    Out[44]:
In [46]:
    
acc = np.sum(np.diag(conf_mat))/np.sum(conf_mat)
print(acc)
    
    
Mnist example
In [152]:
    
from matplotlib.pylab import plt
from sklearn.datasets import fetch_mldata
mnist = fetch_mldata('MNIST original')
    
In [153]:
    
mnist
    
    Out[153]:
In [161]:
    
idx = 50000
x = mnist['data'][idx]
#x[x>0] = 1
plt.imshow(x.reshape(28,28), cmap='gray_r')
plt.show()
    
    
In [41]:
    
features = mnist['data'][0:-1:50].copy()
target = mnist['target'][0:-1:50].copy()
#target[target>0] = 1
    
In [39]:
    
N = features.shape[1]
M= features.shape[0]
    
In [97]:
    
w = np.ones(N)
eta = 0.0001
for epoch in range(500):
    f_est = f(features, w)
    e = (target - f_est)/(M*N)
    dE = -features.T.dot(e)
    
    w = w - eta*dE
    if epoch%10==0:
        print(E(target, f_est))
        #print(w)
    
    
In [98]:
    
f_est = f(features, w)
c = np.round(f_est)
conf_mat = np.zeros((2,2))
for i in range(len(c)):
    ii = min(int(c[i]), 1)
    ii = max(0, ii)
    conf_mat[int(target[i])-1, ii] += 1
    
In [99]:
    
plt.imshow(conf_mat)
plt.show()
    
    
In [101]:
    
conf_mat
    
    Out[101]:
In [100]:
    
plt.plot(features.dot(w), target, 'o')
    
    Out[100]:
    
In [33]:
    
%matplotlib inline
import matplotlib.pylab as plt
import numpy as np
import torch
import torch.autograd
from torch.autograd import Variable
def sigmoid(x):
    return 1./(1+np.exp(-x))
sizes = [1,40,1]
x = 3
W1 = np.random.randn(sizes[1], sizes[0])
b1 = np.random.randn(sizes[1],1)
W2 = np.random.randn(sizes[2], sizes[1])
b2 = np.random.randn(sizes[2],1)
def nnet(x, W1, b1, W2, b2):
    y1 = W1.dot(x) + b1
    f1 = sigmoid(y1)
    y2 = W2.dot(f1) + b2
    f2 = sigmoid(y2)
    return f2
nnet(-3.1, W1, b1, W2, b2)
X = np.linspace(-50,50,100)
F = np.zeros_like(X)
for i in range(len(X)):
    F[i] = nnet(X[i], W1, b1, W2, b2)
plt.plot(X, F)
plt.show()
    
    
In [43]:
    
plt.plot(target)
    
    Out[43]:
    
In [135]:
    
sz = (3,5)
th = np.random.randn(*sz)
c = np.random.choice(range(sz[1]),size=sz[0])
    
In [136]:
    
inp = Variable(torch.FloatTensor(th), requires_grad=True)
target = Variable(torch.LongTensor(c), requires_grad=False)
    
In [151]:
    
#cross_entropy = torch.nn.functional.cross_entropy
CE_Loss = torch.nn.CrossEntropyLoss(reduce=False)
E = CE_Loss(inp, target)
print(E)
#E.backward()
    
    
In [148]:
    
from functools import reduce 
for i,j in enumerate(c):
    res = -th[i,j] + reduce(np.logaddexp, th[i,:])
    print(res)
    
    
In [145]:
    
    
    Out[145]:
In [129]:
    
torch.nn.BCELoss
    
    Out[129]:
In [113]:
    
p = [0.2,0.3,0.5]
torch.multinomial(torch.Tensor(p), 100, replacement=True)
    
    Out[113]: