SHARE
TWEET

Untitled

a guest Oct 21st, 2019 90 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import numpy as np
  2. import random
  3. from matplotlib import pyplot as plt
  4.  
  5. def main():
  6.     vectors, answers = loadData('hw3_dataset.txt')
  7.     row, col = vectors.shape
  8.     weights = np.random.rand(1, col+1)
  9.    
  10.     epoch = 0
  11.    
  12.     while(True):
  13.         # print(epoch)
  14.         # print(weights)
  15.         predicts = classification(vectors, weights)
  16.  
  17.         # print(predicts == answers)
  18.         if not np.all(predicts == answers):    
  19.             # print(predicts)
  20.             # print(answers)
  21.             weights = update(predicts, answers, weights, vectors)
  22.             epoch += 1
  23.  
  24.         else:
  25.             epoch += 1
  26.             break
  27.        
  28.         # input()
  29.     # print(epoch)
  30.     # print(weights)
  31.     return epoch
  32.  
  33.  
  34. def classification(vectors, weights):
  35.     row, col = vectors.shape
  36.     ones = np.ones((row, 1), dtype=np.int64)
  37.     vectors = np.append(vectors, ones, axis=-1)
  38.  
  39.     weights = np.tile(weights, (row, 1))
  40.     weighted = np.multiply(vectors, weights)
  41.     output = np.array([w.sum() for w in weighted])
  42.  
  43.     # print(output)
  44.    
  45.     return output > 0
  46.  
  47. def update(predicts, labels, weights, vectors):
  48.     row, col = vectors.shape
  49.     ones = np.ones((row, 1), dtype=np.int64)
  50.     vectors = np.append(vectors, ones, axis=-1)
  51.    
  52.    
  53.     predicts = np.where(predicts == True, 1, 0)
  54.     labels = np.where(labels == True, 1, 0)
  55.  
  56.     # print('loss = %d' % np.abs(predicts - labels).sum())
  57.  
  58.     for i in range(row):
  59.         # print(learning_rate * (labels[i] - predicts[i]) * weights)
  60.         weights += learning_rate * (labels[i] - predicts[i]) * vectors[i]
  61.  
  62.     return weights
  63.  
  64. def loadData(filename):
  65.     data = np.genfromtxt(filename, dtype='str')
  66.     vectors = np.array([list(bstring) for bstring in data[:, 1]]).astype(np.int64)
  67.     answers = np.array([vector.sum() >= 3 for vector in vectors])
  68.  
  69.    
  70.     return vectors, answers
  71.  
  72.  
  73. if __name__ == '__main__':
  74.  
  75.     base = 0.1
  76.     epochs = []
  77.     learning_rates = []
  78.     for i in range(20):
  79.  
  80.         learning_rate = base * (i + 1)
  81.         epochnum = main()
  82.         learning_rates.append(learning_rate)
  83.         epochs.append(epochnum)
  84.         # print('learning_rate = %.3f, epoch = %d' %(learning_rate, epochnum))
  85.     # print(epochs)
  86.  
  87.     plt.plot(learning_rates, epochs)
  88.     plt.xlabel('learning rate')
  89.     plt.ylabel('epoch')
  90.     plt.savefig('hw3_1_%.3f.jpg' % base)
  91.  
  92.  
  93.  
  94.     # learning_rate = 0.001
  95.     # print(main())
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top