-
Notifications
You must be signed in to change notification settings - Fork 0
/
my_own_nn.py
99 lines (80 loc) · 2.98 KB
/
my_own_nn.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# -*- coding: utf-8 -*-
"""
Created on Sun Jan 13 10:15:58 2019
@author: bimta
"""
from sklearn.datasets import make_regression
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
from mpl_toolkits.mplot3d import Axes3D
# Create regression data
X, y, coef = make_regression(n_samples=10, n_features=1, noise=10, coef = True)
#Include bias
intercept = np.random.randint(-50, 50)
y += intercept
y = y.reshape(len(y),1)
#Adaption of James Loy's code for a single Node
class NeuralNetwork:
def __init__(self, x, y, l=0.1):
self.input = x
self.weight = np.random.rand()
self.bias = np.random.rand()
self.y = y
self.output = np.zeros(y.shape)
self.learning_rate = l
def feedforward(self):
#Equal to predicting
self.output = np.dot(self.input.T, self.weight) + self.bias
# Loss function
self.J = (np.sum((self.y - self.output.T) ** 2)) / len(self.input)
def backprop(self):
# Then we want the partial derivative regarding weight and bias of this loss
weight_deriv = (np.sum(-2 * self.input * (self.y - self.output.T))) / len(self.input)
bias_deriv = (np.sum(-2 * (self.y - self.output.T))) / len(self.input)
# update the weights with the derivative (slope) of the loss function
self.weight -= weight_deriv * self.learning_rate
self.bias -= bias_deriv * self.learning_rate
def train(self, n):
self.t_weights = []
self.t_biases = []
self.t_losses = []
for i in range(n):
nn.feedforward()
self.t_weights.append(nn.weight)
self.t_biases.append(nn.bias)
self.t_losses.append(nn.J)
#print(f'Weight: {nn.weight} Bias: {nn.bias} Cost: {nn.J}')
nn.backprop()
self.t_weights.append(nn.weight)
self.t_biases.append(nn.bias)
self.t_losses.append(nn.J)
nn = NeuralNetwork(X,y)
nn.train(200)
# Plot predicitons
fig = plt.figure()
y_pred = nn.output.T
plt.scatter(X,y)
plt.scatter(X,y_pred)
plt.show()
###############################################################################
# Plot Loss surface:
def loss_plot(nn):
weights = np.linspace(coef - 1.5 * coef, coef + 1.5 * coef, 100)
biases = np.linspace(intercept - 1.5 * intercept, intercept + 1.5 * intercept , 100)
ww, bb = np.meshgrid(weights, biases)
#Evaluate function at each point
losses = []
for bias in biases:
for weight in weights:
pred = np.dot(X.T, weight) + bias
loss = (np.sum((y - pred.T) ** 2)) / len(X)
losses.append(loss)
losses = np.array(losses).reshape(100, 100)
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.plot_surface(ww, bb, losses, linewidth=0, antialiased=False, cmap = cm.coolwarm, alpha = 0.5)
ax.scatter(nn.t_weights, nn.t_biases, nn.t_losses, c = 'r')
plt.show()
return
loss_plot(nn)