-
Notifications
You must be signed in to change notification settings - Fork 0
/
neuralnetwork.py
187 lines (139 loc) · 5.46 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
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import tensorflow as tf
import numpy as np
class NeuralNetwork :
def __init__(self, system) :
self.system = system
self.network = None
self.hiddenActivation = tf.nn.sigmoid
self.lastActivation = tf.nn.sigmoid
self.summary = self.system.variableSummaries
def initializeBias(self, shape, layer) :
namee = 'b%d' % (layer)
#with tf.name_scope("Biases") :
bias = tf.Variable(tf.zeros(shape), name=namee)
#self.summary(name, bias)
return bias
def initializeWeight(self, shape, layer) :
return tf.Variable( tf.random_normal(shape, stddev=1.0) )
nIn = shape[0]
nOut = shape[1]
limit = 4*np.sqrt(6.0 / (nIn + nOut))
lowerLimit = -limit
upperLimit = limit
namee = 'w%d' % (layer)
print name
#with tf.name_scope("Weights") :
weight = tf.Variable(tf.random_uniform(shape, lowerLimit, upperLimit), name=namee);
#self.summary(name, weight)
return weight
#return tf.Variable(tf.random_normal(shape, stddev=0.1),
# name=name+'%d' % (layer))
def constructNetwork(self,
inputs,
nNodes,
nLayers,
outputs,
networkType=None) :
self.networkType = networkType
self.nLayers = nLayers
self.nNodes = nNodes
self.inputs = inputs
self.outputs = outputs
self.x = tf.placeholder('float', [inputs, None], name='x')
self.y = tf.placeholder('float', [outputs, None], name='y')
self.parseTypeString(networkType)
self.network = lambda inputData : self.fullNetwork(
inputData,
inputs = self.inputs,
nLayers = self.nLayers,
nNodes = self.nNodes,
outputs = self.outputs,
hiddenActivation = self.hiddenActivation,
lastActivation = self.lastActivation)
def parseTypeString(self, networkType) :
self.networkType = networkType
if networkType == 'relu-sigmoid' :
self.hiddenActivation = tf.nn.relu
self.lastActivation = tf.nn.sigmoid
elif networkType == 'sigmoid' :
self.hiddenActivation = tf.nn.sigmoid
self.lastActivation = tf.nn.sigmoid
elif networkType == 'relu' :
self.hiddenActivation = tf.nn.relu
self.lastActivation = tf.nn.relu
#print self.hiddenActivation
#print self.lastActivation
def __call__(self, inputData) :
return self.network(inputData)
def layer(self,
y_,
layerNumber,
activation=None,
inputLayer=False,
outputLayer=False) :
iSize = self.nNodes if (not inputLayer) else self.inputs
jSize = self.nNodes if (not outputLayer) else self.outputs
self.w.append(self.initializeWeight([iSize, jSize], layerNumber))
self.b.append(self.initializeBias ([jSize], layerNumber))
y_ = tf.add(tf.matmul(y_, self.w[-1]), self.b[-1])
return y_ if (activation == None) else activation(y_)
def fullNetwork(self,
# inputData,
y_,
inputs,
nLayers,
nNodes,
outputs,
hiddenActivation,
lastActivation) :
"""
w, b = [], []
w.append(self.initializeWeights([inputs, nNodes], 0, 'w'))
b.append(self.initializeWeights([inputs], 0, 'b'))
y_ = self.hiddenActivation(tf.add(tf.matmul(inputData, w[0]), b[0]))
for layer in range(1, nLayers-1) :
w.append(self.initializeWeights([nNodes, nNodes], layer, 'w'))
b.append(self.initializeWeights([nNodes], layer, 'b'))
y_ = self.hiddenActivation(tf.add(tf.matmul(y_, w[layer]), b[layer]))
w.append(self.initializeWeights([nNodes, nNodes], nLayers-1, 'w'))
b.append(self.initializeWeights([nNodes], nLayers-1, 'b'))
y_ = self.lastActivation(tf.add(tf.matmul(y_, w[nLayers-1]), b[nLayers-1]))
w.append(self.initializeWeights([nNodes, outputs], nLayers, 'w'))
b.append(self.initializeWeights([outputs], nLayers, 'b'))
return tf.add(tf.matmul(y_, w[nLayers]), b[nLayers])
"""
self.w, self.b = [], []
self.inputs = inputs
self.nNodes = nNodes
self.hiddenActivation = hiddenActivation
self.lastActivation = lastActivation
"""
y_ = self.layer(y_, 0, activation=self.hiddenActivation, inputLayer=True)
y_ = self.layer(y_, 1, activation=self.hiddenActivation)
#y_ = self.layer(y_, 2, activation=self.hiddenActivation)
y_ = self.layer(y_, 2, activation=None, outputLayer=True)
"""
y_ = self.layer(y_, 0, activation=self.hiddenActivation, inputLayer=True)
for i in xrange(1, nLayers) :
y_ = self.layer(y_, i, activation=self.hiddenActivation)
#y_ = self.layer(y_, nLayers, activation=self.lastActivation)
y_ = self.layer(y_, nLayers+1, activation=None, outputLayer=True)
return y_
#self.w.append(self.initializeWeights([inputs, nNodes], 0, 'w'))
#self.b.append(self.initializeWeights([nNodes], 0, 'b'))
#y_ = self.hiddenActivation(tf.add(tf.matmul(inputData, self.w[0]), self.b[0]))
#for layer in range(1, nLayers) :
"""
iLimit = inputs
for layer in range(0, nLayers) :
self.w.append(self.initializeWeights([iLimit, nNodes], layer, 'w'))
self.b.append(self.initializeWeights([nNodes], layer, 'b'))
y_ = self.hiddenActivation(tf.add(tf.matmul(y_, self.w[layer]), self.b[layer]))
iLimit = nNodes
self.w.append(self.initializeWeights([nNodes, nNodes], nLayers, 'w'))
self.b.append(self.initializeWeights([nNodes ], nLayers, 'b'))
y_ = self.lastActivation(tf.add(tf.matmul(y_, self.w[nLayers]), self.b[nLayers]))
self.w.append(self.initializeWeights([nNodes, outputs], nLayers+1, 'w'))
self.b.append(self.initializeWeights([outputs], nLayers+1, 'b'))
return tf.add(tf.matmul(y_, self.w[nLayers+1]), self.b[nLayers+1])
"""