-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3LayerNN_BP.py
More file actions
63 lines (48 loc) · 2.19 KB
/
3LayerNN_BP.py
File metadata and controls
63 lines (48 loc) · 2.19 KB
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
# Bowtie problem for a three layer neural network
# written by Tautvydas Lisas
# 01/07/2021
import numpy as np
import pandas as pd
# sigmoid activation function
def sigmoid(x):
return 1.0 / (1.0 + np.exp(-x))
# derivative of sigmoid activation function
def sigmoid_derivative(x):
return (x)*(1-(x))
# initialise neural network class
class NeuralNetwork:
def __init__(self, x, y):
# initialise weights, input, output and target matrix. The matrix size will determine the number of neurons each layer has
self.input = x
print(x.shape[1])
self.weights1 = np.random.rand(x.shape[1],15)
self.weights2 = np.random.rand(15,5)
self.weights3 = np.random.rand(5,1)
self.y = y
self.output = np.zeros(y.shape)
# forward propogation
def feedforward(self):
self.layer1 = sigmoid(np.dot(self.input, self.weights1))
self.layer2 = sigmoid(np.dot(self.layer1, self.weights2))
self.output = sigmoid(np.dot(self.layer2, self.weights3))
# backward propogation
def backprop(self):
d_weights3 = np.dot(self.layer2.T, (2*(self.y - self.output) * sigmoid_derivative(self.output)))
d_weights2 = np.dot(self.layer1.T, (np.dot(2*(self.y - self.output) * sigmoid_derivative(self.output), self.weights3.T) * sigmoid_derivative(self.layer2)))
d_weights1 = np.dot(self.input.T,(np.dot(np.dot(2*(self.y - self.output) * sigmoid_derivative(self.output), self.weights3.T) * \
sigmoid_derivative(self.layer2), self.weights2.T) * sigmoid_derivative(self.layer1)))
# update the weights with the derivative (slope) of loss function
self.weights1 += d_weights1
self.weights2 += d_weights2
self.weights3 += d_weights3
# import Bowtie data
data = pd.read_csv('bow_tie_dataset.csv', header=None)
x = np.array(data)
# targets
y = np.array([[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[1],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0]])
nn = NeuralNetwork(x,y)
# set number of epochs
for i in range(50000):
nn.feedforward()
nn.backprop()
print(np.round(nn.output.T))