Repository navigation
Expand file tree
/
Copy pathnet.py
More file actions
103 lines (75 loc) · 3.52 KB
/
Copy pathnet.py
File metadata and controls
103 lines (75 loc) · 3.52 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
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
100
101
102
103
# %%
import numpy
from numpy import dot
# needed for the sigmoid activation function
from scipy.special import expit as activation
# neural net calss
class neuralNetwork:
# init
def __init__(self, inputnodes, hiddennodes, outputnodes, learningrate):
self.inodes = inputnodes
self.hnodes = hiddennodes
self.onodes = outputnodes
# link weight matrices
self.weights_input_hidden = numpy.random.normal(0.0, pow(self.inodes, -0.5), (self.hnodes, self.inodes))
self.weights_hidden_output = numpy.random.normal(0.0, pow(self.hnodes, -0.5), (self.onodes, self.hnodes))
# define activation lambda function as a property of the class
self.activation_function = lambda x: activation(x)
# learning rate
self.lr = learningrate
pass
# train
def train(self, inputs_list, targets_list):
inputs = numpy.array(inputs_list, ndmin = 2).T
targets = numpy.array(targets_list, ndmin = 2).T
# create the input to the hidden node
# by multiplying the Input matrix by the Weight matrix
# of the input later to the hidden layer
hidden_inputs = dot(self.weights_input_hidden, inputs)
# signals emerging from hidden layer
hidden_outputs = self.activation_function(hidden_inputs)
# final layer inputs
final_inputs = dot(self.weights_hidden_output, hidden_outputs)
# signals emerging from final layer
final_outputs = self.activation_function(final_inputs)
output_errors = targets - final_outputs
# hidden layer error is the output_errors split by weights, recombined at hidden nodes
hidden_errors = dot(self.weights_hidden_output.T, output_errors)
# update the weights for the links between hidden and output layer
# starting from the last layer going to the first - back propagation
self.weights_hidden_output += self.lr * dot((output_errors * final_outputs * (1.0 - final_outputs)), numpy.transpose(hidden_outputs))
# update weight for links between hidden and input nodes
self.weights_input_hidden += lr * dot((hidden_errors * hidden_outputs * (1.0 - hidden_outputs)), numpy.transpose(inputs))
pass
# query
def query(self, inputs_list):
inputs = numpy.array(inputs_list, ndmin = 2).T
# create the input to the hidden node
# by multiplying the Input matrix by the Weight matrix
# of the input later to the hidden layer
hidden_inputs = dot(self.weights_input_hidden, inputs)
# signals emerging from hidden layer
hidden_outputs = self.activation_function(hidden_inputs)
# final layer inputs
final_inputs = dot(self.weights_hidden_output, hidden_outputs)
# signals emerging from final layer
final_outputs = self.activation_function(final_inputs)
return final_outputs
pass
# %%
# %%
input_nodes = 3
hidden_nodes = 3
output_nodes = 3
learning_rate = 0.5
net = neuralNetwork(input_nodes, hidden_nodes, output_nodes, learning_rate)
# %%
import matplotlib.pyplot
%matplotlib inline
data_file = open('mnist_dataset/mnist_train_100.csv', 'r')
data_list = data_file.readlines()
data_file.close()
all_values = data_list[0].split(',')
image_array = numpy.asfarray(all_values[1:]).reshape((28, 28))
matplotlib.pyplot.imshow(image_array, cmap='Blues', interpolation='None')
# net.query([1.0, 0.5, -0.5])