Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- import random
- from matplotlib import pyplot as plt
- def main():
- vectors, answers = loadData('hw3_dataset.txt')
- row, col = vectors.shape
- weights = np.random.rand(1, col+1)
- epoch = 0
- while(True):
- # print(epoch)
- # print(weights)
- predicts = classification(vectors, weights)
- # print(predicts == answers)
- if not np.all(predicts == answers):
- # print(predicts)
- # print(answers)
- weights = update(predicts, answers, weights, vectors)
- epoch += 1
- else:
- epoch += 1
- break
- # input()
- # print(epoch)
- # print(weights)
- return epoch
- def classification(vectors, weights):
- row, col = vectors.shape
- ones = np.ones((row, 1), dtype=np.int64)
- vectors = np.append(vectors, ones, axis=-1)
- weights = np.tile(weights, (row, 1))
- weighted = np.multiply(vectors, weights)
- output = np.array([w.sum() for w in weighted])
- # print(output)
- return output > 0
- def update(predicts, labels, weights, vectors):
- row, col = vectors.shape
- ones = np.ones((row, 1), dtype=np.int64)
- vectors = np.append(vectors, ones, axis=-1)
- predicts = np.where(predicts == True, 1, 0)
- labels = np.where(labels == True, 1, 0)
- # print('loss = %d' % np.abs(predicts - labels).sum())
- for i in range(row):
- # print(learning_rate * (labels[i] - predicts[i]) * weights)
- weights += learning_rate * (labels[i] - predicts[i]) * vectors[i]
- return weights
- def loadData(filename):
- data = np.genfromtxt(filename, dtype='str')
- vectors = np.array([list(bstring) for bstring in data[:, 1]]).astype(np.int64)
- answers = np.array([vector.sum() >= 3 for vector in vectors])
- return vectors, answers
- if __name__ == '__main__':
- base = 0.1
- epochs = []
- learning_rates = []
- for i in range(20):
- learning_rate = base * (i + 1)
- epochnum = main()
- learning_rates.append(learning_rate)
- epochs.append(epochnum)
- # print('learning_rate = %.3f, epoch = %d' %(learning_rate, epochnum))
- # print(epochs)
- plt.plot(learning_rates, epochs)
- plt.xlabel('learning rate')
- plt.ylabel('epoch')
- plt.savefig('hw3_1_%.3f.jpg' % base)
- # learning_rate = 0.001
- # print(main())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement