forked from jramapuram/LSTM_Anomaly_Detector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
convolutional.py
79 lines (63 loc) · 2.71 KB
/
convolutional.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
__author__ = 'jramapuram'
from theano.tensor.signal import downsample
from keras.utils.theano_utils import shared_zeros
from keras.layers.core import Layer
import theano
import theano.tensor as T
import keras.initializations as initializations
import keras.activations as activations
class Convolution1D(Layer):
def __init__(self, nb_filter, stack_size, filter_length,
init='glorot_uniform', activation='linear', weights=None,
image_shape=None, border_mode='valid', subsample_length=1):
super(Convolution1D, self).__init__()
nb_row = 1
nb_col = filter_length
subsample = (1,subsample_length)
self.init = initializations.get(init)
self.activation = activations.get(activation)
self.subsample = subsample
self.border_mode = border_mode
self.image_shape = image_shape
self.nb_filter = nb_filter
self.stack_size = stack_size
self.input = T.tensor4()
self.W_shape = (nb_filter, stack_size, nb_row, nb_col)
self.W = self.init(self.W_shape)
self.b = shared_zeros((nb_filter,))
self.params = [self.W, self.b]
if weights is not None:
self.set_weights(weights)
def get_output(self, train):
X = self.get_input(train)
conv_out = theano.tensor.nnet.conv.conv2d(X, self.W
, border_mode=self.border_mode
, subsample=self.subsample
, image_shape=self.image_shape)
output = self.activation(conv_out + self.b.dimshuffle('x', 0, 'x', 'x'))
return output
def get_config(self):
return {"name":self.__class__.__name__,
"nb_filter":self.nb_filter,
"stack_size":self.stack_size,
"init":self.init.__name__,
"activation":self.activation.__name__,
"image_shape":self.image_shape,
"border_mode":self.border_mode,
"subsample":self.subsample}
class MaxPooling1D(Layer):
def __init__(self, pool_length=2, ignore_border=True):
super(MaxPooling1D,self).__init__()
poolsize = (1, pool_length)
self.input = T.tensor4()
self.poolsize = poolsize
self.ignore_border = ignore_border
self.params = []
def get_output(self, train):
X = self.get_input(train)
output = downsample.max_pool_2d(X, self.poolsize, ignore_border=self.ignore_border)
return output
def get_config(self):
return {"name":self.__class__.__name__,
"poolsize":self.poolsize,
"ignore_border":self.ignore_border}