-
Notifications
You must be signed in to change notification settings - Fork 3
/
layers.py
318 lines (257 loc) · 12.4 KB
/
layers.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
__author__ = 'madhumathi'
from deepLearningLibrary.activations import *
from deepLearningLibrary.costs import *
from deepLearningLibrary.connections import *
from abc import ABCMeta, abstractmethod
import numpy as np
import theano
from theano.sandbox.rng_mrg import MRG_RandomStreams as RandomStreams
class Layer(object):
'''
Abstract class for layers. Not to be instantiated
'''
def __init__(self, shape, passFunction, aggregate_method=None, dropout=None,lossFunction=None,ifOutput = False):
'''
:param name: Name for the layer
:param shape: Shape of the current Layer(num of neurons spatially arranged)
:param inConnections: list of incoming connection objects
:param outConnections: list of outgoing connection objects
:param aggregate_method: function to specify method of concatenating
:return:
'''
self.name = None
self.shape = shape
self.inConnections = []
self.outConnections = []
self.recurrentInConnections = []
self.recurrentOutConnections = []
self.numOfNeurons = 1
self.ifOutput = ifOutput
for i in self.shape:
self.numOfNeurons *= i
self.aggregate_method = aggregate_method
self.output = None
self.lossFunction = self.getLossFunction(lossFunction)
self.passFunction = self.getPassFunction(passFunction)
self.dropout = dropout
def setName(self,name):
self.name = name
def addIncomingConnection(self,connection):
'''
:param connection: new incoming connection to be added
:return: None
'''
if isinstance(connection,RecurrentConnection):
self.recurrentInConnections.append(connection)
else:
self.inConnections.append(connection)
def addOutgoingConnection(self, connection):
'''
:param connection: new outgoing connection to be added
:return: None
'''
if isinstance(connection,RecurrentConnection):
self.recurrentOutConnections.append(connection)
else:
self.outConnections.append(connection)
def setNetwork(self,networkName):
self.network = networkName
def initializeInputOutput(self,mini_batch_size):
self.input = T.zeros(shape=(mini_batch_size, self.numOfNeurons))
self.output = T.zeros(shape=(mini_batch_size, self.numOfNeurons))
self.hiddenState = T.zeros(shape=(mini_batch_size, self.numOfNeurons))
def aggregateInput(self):
### Handles for for multiple outputs
if self.aggregate_method == 'sum':
for connection in self.inConnections:
'''
if len(connection.fromLayer.shape) != len(connection.toLayer.shape):
raise(SizeMismatch(len(connection.fromLayer.shape), len(connection.toLayer.shape),
"Input and Output Layer dimensions not same."))
else:
for i in range(0, len(connection.fromLayer.shape)):
if connection.fromLayer.shape[i] != connection.toLayer.shape[i]:
raise(SizeMismatch(len(connection.fromLayer.shape[i]), len(connection.toLayer.shape[i]),
"Input and Output Layer dimensions not same."))
'''
self.input = self.input + connection.output
elif self.aggregate_method == 'concat':
start_index = 0
inConnectionSizeSum = 0
for connection in self.inConnections:
inConnectionSizeSum += connection.targetNeurons
if self.numOfNeurons != inConnectionSizeSum:
raise(SizeMismatch(self.numOfNeurons, inConnectionSizeSum,
"Input and Output Layer dimensions not same."))
for connection in self.inConnections:
self.input = T.set_subtensor(self.input[:, start_index:start_index+connection.targetNeurons], connection.output)
start_index += connection.targetNeurons
elif self.aggregate_method is None:
if self.inConnections[0].targetNeurons != self.numOfNeurons:
raise(SizeMismatch(self.numOfNeurons, self.inConnections[0].targetNeurons,
"Input and Output Layer dimensions not same."))
self.input += self.inConnections[0].output
else:
raise(AggregateMethodNotDefined(self.aggregate_method))
def computeShapes(self,minibatchSize):
self.shape_minibatch_flattened = (minibatchSize,self.numOfNeurons)
self.shape_with_minibatch = list(self.shape)
self.shape_with_minibatch.insert(0,minibatchSize)
self.shape_with_minibatch = tuple(self.shape_with_minibatch)
def addDropout(self):
if self.dropout is not None:
if self.dropout < 0. or self.dropout >= 1:
raise(DropoutPercentInvalid(self.dropout))
rng = RandomStreams()
retain_prob = 1. - self.dropout
random_tensor = rng.binomial(self.numOfNeurons, p=retain_prob, dtype=self.output.dtype)
random_tensor = T.patternbroadcast(random_tensor, [dim == 1 for dim in self.shape_minibatch_flattened])
self.output *= random_tensor
self.output /= retain_prob
def run(self,minibatchSize):
'''
Compute the output of each of the incoming connections and then aggregate all such connection output
to form the input for this layer, followed by an activation function on this total input
:return:
'''
### compute shape Tuples(Hack :( )
self.computeShapes(minibatchSize)
self.input = T.zeros(shape=(minibatchSize,self.numOfNeurons))
### compute connection outputs (currently flattened outputs)
for connection in self.inConnections:
connection.feedForward(minibatchSize)
# Compute aggregate input from previously calculated connection outputs
'''
ADD MORE AGGREGATION FUNCTIONS
'''
self.aggregateInput()
for recurrentConnection in self.recurrentInConnections:
self.input += recurrentConnection.recurrentHiddenOutput
'''
if len(self.recurrentInConnections) > 0:
# self.hiddenState = self.passFunction(self.input)
aggregatedRecurrentInput = T.zeros((minibatchSize,self.numOfNeurons))
for recurrentConnection in self.recurrentInConnections:
recurrentConnection.fromLayer.hiddenState = self.passFunction(self.input)
# self.output += recurrentConnection.getUpdatedHiddenOutput(recurrentConnection.fromLayer.hiddenState)
aggregatedRecurrentInput += recurrentConnection.getUpdatedHiddenOutput(recurrentConnection.fromLayer.hiddenState)
self.output = self.passFunction(aggregatedRecurrentInput)
else:
self.output = self.passFunction(self.input)
'''
self.output = self.passFunction(self.input)
# Add dropout
self.addDropout()
self.y_out = T.argmax(self.output, axis=1)
def cost(self, y, size):
"Return the log-likelihood cost."
self.output = self.output.reshape((size, self.numOfNeurons))
# return -T.mean(T.log(self.output)[T.arange(size), y])
return self.lossFunction(self.output,y)
# return -T.mean(y * T.log(self.output) + (1-y) * T.log(1 - self.output))
def accuracy(self, y):
"Return the accuracy for the mini-batch."
print self.y_out.shape
return T.mean(T.eq(y, self.y_out))
def getPassFunction(self,passFunction):
# passFunction is a string input
activationFunction = None
if passFunction == "sigmoid":
activationFunction = sigmoid
elif passFunction == "tanh":
activationFunction = tanh
elif passFunction == "relu":
activationFunction = relu
elif passFunction == "softmax":
activationFunction = softmax
elif passFunction == "passthrough":
activationFunction = passthrough
else:
raise(ActivationFunctionNotImplemented(passFunction))
'''
TODO : ADD MORE PASS FUNCTIONS
'''
return activationFunction
def getLossFunction(self,lossFunction):
loss = None
if lossFunction == "meanSquared":
loss = meanSquare
elif lossFunction == "meanSquaredLog":
loss = meanSquareLog
elif lossFunction == "meanAbsolute":
loss = meanAbsolute
elif lossFunction == "crossEntropy":
loss = crossEntropy
elif lossFunction == "negativeLogLikelihood":
loss = negativeLogLikelihood
elif lossFunction == "klDivergence":
loss = kullbackLeiblerDivergence
elif lossFunction == "poisson":
loss = poisson
elif lossFunction == "cosineProximity":
loss = cosine_proximity
else:
# pass
if self.ifOutput:
raise(LossFunctionNotImplemented(lossFunction))
else:
pass
'''
TODO : ADD MORE LOSS FUNCTIONS
'''
return loss
#### Implement the below methods ####
# def __new__(cls, *args):
# # if cls is Layer:
# # raise NaadiAbstractClassInstantiationError('Connectivity')
# # return object.__new__(cls, *args)
# pass
#
# def allow(self, source, dest):
# # Given source and destination neurons, return True/False to indicate if a connection exists between them
# # raise NaadiNotImplementedError(inspect.currentframe().f_code.co_name, str(type(self)))
# pass
class InputLayer(Layer):
def __init__(self, inputShape,passFunction="passthrough",aggregate_method=None, lossFunction=None, ifOutput=False):
super(InputLayer,self).__init__(inputShape,passFunction,aggregate_method,lossFunction)
def firstLayerRun(self, input, minibatchSize):
### compute shape Tuples(Hack :( )
self.computeShapes(minibatchSize)
input = input.reshape(self.shape_minibatch_flattened)
self.output = self.passFunction(input)
class ActivationLayer(Layer):
def __init__(self, inputShape, passFunction,aggregate_method=None, lossFunction=None, ifOutput=False, dropout=None):
super(ActivationLayer,self).__init__(inputShape,passFunction,aggregate_method,lossFunction=lossFunction,ifOutput=ifOutput)
# self.ifOutput = ifOutput # this is a boolean
class MemoryLayer(Layer):
# check this!!!
def __init__(self, inputShape,passFunction,
aggregate_method=None, lossFunction=None, ifOutput=False, dropout=None):
super(MemoryLayer,self).__init__(inputShape,passFunction,aggregate_method,lossFunction=lossFunction,ifOutput=ifOutput)
# self.ifOutput = ifOutput # this is a boolean
def run(self,minibatchSize):
self.computeShapes(minibatchSize)
self.input = T.zeros(shape=(minibatchSize,self.numOfNeurons))
# compute connection outputs (currently flattened outputs)
for connection in self.inConnections:
connection.feedForward(minibatchSize)
# Compute aggregate input from previously calculated connection outputs
self.aggregateInput()
for recurrentConnection in self.recurrentInConnections:
self.input += recurrentConnection.recurrentHiddenOutput
''' May be buggy : not sure if self.output will be reinitialized to 0 in each run. Ideally it should not'''
'''
if len(self.recurrentInConnections) > 0:
# self.hiddenState = self.passFunction(self.input)
aggregatedRecurrentInput = T.zeros((minibatchSize,self.numOfNeurons))
for recurrentConnection in self.recurrentInConnections:
recurrentConnection.fromLayer.hiddenState = self.passFunction(self.input)
# self.output += recurrentConnection.getUpdatedHiddenOutput(recurrentConnection.fromLayer.hiddenState)
aggregatedRecurrentInput += recurrentConnection.getUpdatedHiddenOutput(recurrentConnection.fromLayer.hiddenState)
self.output += self.passFunction(aggregatedRecurrentInput)
else:
self.output += self.passFunction(self.input)
'''
self.output += self.passFunction(self.input)
self.addDropout()
self.y_out = T.argmax(self.output, axis=1)