Advertisement
Guest User

Untitled

a guest
Apr 24th, 2017
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.06 KB | None | 0 0
  1. import numpy as np
  2. import random
  3. import pandas as pd
  4. import matplotlib.pyplot as plt
  5. import matplotlib
  6. from matplotlib import cm
  7. from mpl_toolkits.mplot3d import Axes3D
  8.  
  9.  
  10. def logistic_z(z):
  11.     return 1.0/(1.0+np.exp(-z))
  12.  
  13.  
  14. def logistic_wx(w,x):
  15.     return logistic_z(np.inner(w,x))
  16.  
  17.  
  18. def classify(w,x):
  19.     x = np.hstack(([1],x))
  20.     return 0 if (logistic_wx(w,x)<0.5) else 1
  21.  
  22.  
  23. # x_train = [number_of_samples,number_of_features] = number_of_samples x \in R^number_of_features
  24. def stochast_train_w(x_train,y_train,learn_rate=0.1,niter=1000):
  25.     x_train = np.hstack((np.array([1]*x_train.shape[0]).reshape(x_train.shape[0],1),x_train))
  26.     dim = x_train.shape[1]
  27.     num_n = x_train.shape[0]
  28.     w = np.random.rand(dim)
  29.     index_lst = []
  30.     for it in xrange(niter):
  31.         if len(index_lst) == 0:
  32.             index_lst = random.sample(xrange(num_n), k=num_n)
  33.         xy_index = index_lst.pop()
  34.         x = x_train[xy_index,:]
  35.         y = y_train[xy_index]
  36.         for i in xrange(dim):
  37.             update_grad = 1 ### something needs to be done here
  38.             w[i] = w[i] + learn_rate ### something needs to be done here
  39.     return w
  40.  
  41.  
  42. def batch_train_w(x_train,y_train,learn_rate=0.1,niter=1000):
  43.     x_train = np.hstack((np.array([1]*x_train.shape[0]).reshape(x_train.shape[0],1),x_train))
  44.     dim = x_train.shape[1]
  45.     num_n = x_train.shape[0]
  46.     w = np.random.rand(dim)
  47.     index_lst = []
  48.     for it in xrange(niter):
  49.         for i in xrange(dim):
  50.             update_grad=0.0
  51.             for n in xrange(num_n):
  52.                 update_grad+=(-logistic_wx(w,x_train[n])+y_train[n])# something needs to be done here
  53.             w[i] = w[i] + learn_rate * update_grad/num_n
  54.     return w
  55.  
  56.  
  57. def train_and_plot(xtrain, ytrain, xtest, ytest, training_method, learn_rate=0.1, niter=10):
  58.     plt.figure()
  59.     #train data
  60.     data = pd.DataFrame(np.hstack((xtrain,ytrain.reshape(xtrain.shape[0],1))),columns=['x','y','lab'])
  61.     ax=data.plot(kind='scatter',x='x',y='y',c='lab',cmap=cm.copper,edgecolors='black')
  62.  
  63.     #train weights
  64.     w=training_method(xtrain,ytrain,learn_rate,niter)
  65.     error=[]
  66.     y_est=[]
  67.     for i in xrange(len(ytest)):
  68.         error.append(np.abs(classify(w,xtest[i])-ytest[i]))
  69.         y_est.append(classify(w,xtest[i]))
  70.     y_est=np.array(y_est)
  71.     data_test = pd.DataFrame(np.hstack((xtest,y_est.reshape(xtest.shape[0],1))),columns=['x','y','lab'])
  72.     data_test.plot(kind='scatter',x='x',y='y',c='lab',ax=ax,cmap=cm.coolwarm,edgecolors='black')
  73.     print "error=",np.mean(error)
  74.     return w
  75.  
  76.  
  77. def l_simple(weights):
  78.     return (logistic_wx(weights, [1,0]) - 1) ** 2 + (logistic_wx(weights, [0,1])) ** 2 + (logistic_wx(weights, [1, 1]) - 1) ** 2
  79.  
  80.  
  81. def i_simple_loss():
  82.     r = 13
  83.     w_grid = np.zeros((r, r, 3))
  84.     for row in range(r):
  85.         for col in range(r):
  86.             x = row - r/2
  87.             y = col - r/2
  88.             w_grid[row][col] = [x, y, l_simple([x, y])]
  89.  
  90.     lowest_point = [0, 0 , float("inf")]
  91.     number_of_lowest_points = 1
  92.     for row in w_grid:
  93.         for col in row:
  94.             if col[2] < lowest_point[2]:
  95.                 lowest_point = col
  96.             elif col[2] == lowest_point[2]:
  97.                 number_of_lowest_points += 1
  98.  
  99.     print "Lowest point: ", lowest_point, "\nNumber of points equally low: ", number_of_lowest_points
  100.  
  101.     fig = plt.figure()
  102.     ax = fig.add_subplot(111, projection='3d')
  103.     Axes3D.set_xlabel(ax, "x")
  104.     Axes3D.set_ylabel(ax, "y")
  105.     Axes3D.set_zlabel(ax, "loss")
  106.     Axes3D.plot_trisurf(ax, w_grid[:,:,0].flatten(), w_grid[:,:,1].flatten(), w_grid[:,:,2].flatten())
  107.     plt.show()
  108.  
  109.  
  110. def der_l_simple(w):
  111.     def expo(w, x):
  112.         return np.exp(-np.inner(w, x))
  113.     w1_p1 = (logistic_wx(w, [1,0]) - 1) * (logistic_wx(w, [1,0])**2) * expo(w, [1,0])
  114.     w2_p1 = (logistic_wx(w, [0,1])) * (logistic_wx(w, [0,1])**2) * expo(w, [0,1])
  115.     shared_p2 = (logistic_wx(w, [1,1]) - 1) * (logistic_wx(w, [1,1])**2) * expo(w, [1,1])
  116.     return [w1_p1 + shared_p2, w2_p1 + shared_p2]
  117.  
  118. def i_gradient_descent(eta, iterations, plot=False):
  119.     weights = [-10, 10]
  120.     t = np.arange(0, iterations+1, 1)
  121.     l = np.zeros(iterations+1)
  122.     l[0] = l_simple(weights)
  123.     for i in range(iterations):
  124.         weights = np.array(weights) - eta * np.array(der_l_simple(weights))
  125.         l[i+1] = l_simple(weights)
  126.     if plot:
  127.         print weights, l_simple(weights)
  128.         plt.plot(t, l)
  129.         plt.xlabel("iteration")
  130.         plt.ylabel("loss (logscale)")
  131.         plt.yscale("log")
  132.         plt.show()
  133.     return l_simple(weights)
  134.  
  135.  
  136. def i_plot_gd():
  137.     iterations = 10000
  138.     doublings = 30
  139.     etas = np.zeros(doublings)
  140.     etas[0] = 0.000001
  141.     for i in range(len(etas) - 1):
  142.         etas[i+1] = etas[i]*2
  143.     l_mins = np.zeros(doublings)
  144.     for i in range(len(l_mins)):
  145.         l_mins[i] = i_gradient_descent(etas[i], iterations)
  146.     plt.plot(etas, l_mins)
  147.     plt.xscale("log")
  148.     plt.xlabel('eta (logscale)')
  149.     plt.ylabel('loss minimum')
  150.     plt.grid(True)
  151.     plt.show()
  152.  
  153.  
  154.  
  155. i_plot_gd()
  156. #i_gradient_descent(0.1, 10000, plot=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement