Skip to content

Instantly share code, notes, and snippets.

@simonegiacomelli
Created October 20, 2018 05:55
Show Gist options
  • Save simonegiacomelli/6b3ad1d7db0994fb57187d80f349d01c to your computer and use it in GitHub Desktop.
Save simonegiacomelli/6b3ad1d7db0994fb57187d80f349d01c to your computer and use it in GitHub Desktop.
Machine Learning 2018/19. Assignment 1: Neural Networks - exercise1
import numpy as np
X = np.asarray([
[6, 4, 1, 1],
[3, 1, 7, 1],
[6, 10, 9, 1],
[4, 2, 4, 1],
[1, 5, 10, 1],
[5, 3, 7, 1],
[3, 3, 4, 1],
[10, 3, 3, 1],
[3, 4, 9, 1],
[4, 6, 6, 1],
])
T = np.asarray([1, 1, 1, 1, 1, 1, -1, -1, -1, -1]).T
Y = np.zeros(T.shape)
w = [-0.1, -0.3, 0.2, 2.0]
def forward(x, w):
y = 0.0
for i in range(len(w)):
y += w[i] * x[i]
return y
def forwardall():
for k in range(X.shape[0]):
Y[k] = forward(X[k], w)
lr = 0.02
def gradient():
n = X.shape[0]
for k in range(n):
x = X[k]
for i in range(len(w)):
ewi = (Y[k] - T[k]) / n * x[i]
w[i] -= lr * ewi
forwardall()
gradient()
print("w")
print(np.round(w, 6))
# [-0.1986, -0.38219999999999993, 0.027600000000000017, 1.9735999999999998]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment