-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlayers.py
executable file
·53 lines (40 loc) · 1.3 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
# -*- coding: utf-8 -*-
"""
Created on Tue Jan 20 12:46:06 2015
@author: bordingj
"""
import theano
import theano.tensor as T
import numpy as np
from theano.tensor.nnet import conv
from theano.tensor.signal import downsample
def relu(x):
return (x > 0)*x
class HiddenLayer(object):
def __init__(self, Z, W, b, activationFunction=relu):
#self.input = input
self.W = W
self.b = b
self.output = (
T.dot(self.input, self.W) + self.b if activationFunction is None
else activationFunction(T.dot(Z, self.W) + self.b)
)
class SoftMaxLayer(object):
def __init__(self, Z, W, b):
#self.input = input
self.W = W
self.b = b
self.p_y_given_x = T.nnet.softmax(T.dot(Z, self.W) + self.b)
self.y_pred = T.argmax(self.p_y_given_x, axis=1)
class LeNetConvPoolLayer(object):
def __init__(self, Z, W, b, poolsize, activationFunction):
self.W = W
self.b = b
conv_output = conv.conv2d(input=Z, filters=self.W)
pooled_out = T.signal.downsample.max_pool_2d(
input=conv_output,
ds=poolsize,
ignore_border=True)
self.output = activationFunction(
pooled_out + self.b.dimshuffle('x', 0, 'x', 'x')
)