forked from ChrisACas/MLProject-Neural_Tigers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathneuralNetwork.py
37 lines (31 loc) · 1.5 KB
/
neuralNetwork.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
import matplotlib.pyplot as plt
import numpy as np
from MNIST_Dataloader import MNIST_Dataloader
class NeuralNetwok:
def __init__(self, input_size=28*28, output_size=10, h_layers=1, h_neurons_per_layer=128):
self.input_size = input_size
self.output_size = output_size
self.h_layers = h_layers
self.h_neurons_per_layer = h_neurons_per_layer
self.layers = self.init_layers(input_size, h_neurons_per_layer, output_size)
# TODO: implement a programmable amount of hidden layer initialization
def init_layers(self, input_size, h_neurons_per_layer, output_size):
'''
Get layer size info and develop weight array
initialize random weights for each connection to next layer
weight array of output size, in array for every input node
return these weight arrays for each node as layer
'''
layer1 = np.random.uniform(-1.,1.,size=(input_size, h_neurons_per_layer))\
/np.sqrt(input_size * h_neurons_per_layer)
layer2 = np.random.uniform(-1.,1.,size=(h_neurons_per_layer, output_size))\
/np.sqrt(h_neurons_per_layer * output_size)
return [layer1, layer2]
def desired_array_out(self, label):
'''Turn label into desired output array
input label 5
return desire array [0 0 0 0 0 1 0 0 0 0]
'''
desired_array = np.zeros(self.output_size, np.float32)
desired_array[label] = 1
return desired_array