Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- def generate(count):
- X = np.random.random_integers(0, high=255, size=(count, 9))
- Y = X.dot(np.array([1, 1, 1, 0, 0, 0, -1, -1, -1]))
- Y[Y > 0] = 1
- Y[Y < 0] = 0
- return X, Y
- def run():
- m = 5000
- c = 9
- epochs = 50000
- # weight vector
- w = np.random.randn(c).reshape(c, 1)
- # training loop
- lr = 1e-3
- print('\n\n{:^8s} | {:^8s} | {:^6s}'.format('epoch', 'loss', 'acc'))
- print('----------------------------')
- for t in range(epochs):
- # get new training data
- X, y = generate(m)
- X = X / 255
- y = y.reshape(m, 1) * 2 - 1
- # model function
- h = X.dot(w)
- # compute loss
- loss = np.square(h - y).mean()
- # compute accuracy
- acc = (np.sign(h) == y).mean()
- if t % 5000 == 0:
- print('{:>8d} | {:>8f} | {:>.4f}'.format(t, loss, acc))
- # no more to do
- if acc >= 1:
- print('\nStopping:\n{:>8d} | {:>8f} | {:>.4f}'.format(t, loss, acc))
- break
- # grad + update
- grad = 2 * (h - y)
- w = w - lr * X.T.dot(grad) / m
- print('\nFinal W = \n\n{}'.format(w))
- if __name__ == "__main__":
- run()
Add Comment
Please, Sign In to add comment