-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconvTheano.py
53 lines (47 loc) · 1.79 KB
/
convTheano.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
import theano.tensor as T
from theano.tensor.nnet import conv
import numpy as np
from math import sqrt
import theano
def convTheano(Z,Y,flip=False,border='valid'):
"""
Take Z = array(batch_size, n_z * n_z) input imageand Y = array(nb_filters, n_y * n_y)
convolution filter and do convolution operation. The result should
be n_z - n_y + 1; For eg, the input layer consists of an Nv*Nv
array of binary units. Each group of hidens is associated with a
Nw * Nw, where Nw = Nv - Nh + 1; Also Nh = Nv - Nw + 1.
"""
labelZ = False
if (np.array(Z.shape).shape[0] != 2):
Z = Z.reshape(1,Z.shape[0])
labelZ = True
labelY = False
if(np.array(Y.shape).shape[0] != 2):
Y = Y.reshape(1,Y.shape[0])
labelY = True
windowSize = int(sqrt(Y.shape[1]))
visualSize = int(sqrt(Z.shape[1]))
if(border=='valid'):
convedSize = visualSize - windowSize + 1
else:
convedSize = visualSize + windowSize - 1
batch_size = Z.shape[0]
nb_filters = Y.shape[0]
x = T.tensor4(name = 'x',dtype = 'float32')
y = T.tensor4(name = 'y',dtype = 'float32')
output = conv.conv2d(x,y,border_mode = border)
f = theano.function([x,y],output)
Z = Z.reshape(batch_size,1,visualSize,visualSize)
Y = Y.reshape(nb_filters,1,windowSize,windowSize)
Z = np.float32(Z)
Y = np.float32(Y)
if(flip):
Y = np.array([np.flipud(np.fliplr(Y[i,0,:,:])) for i in range(nb_filters)]).reshape(nb_filters,1,windowSize,windowSize)
result = f(Z,Y)
result = np.array(result).reshape(batch_size,nb_filters,convedSize * convedSize)
if(labelZ):
return result.reshape(nb_filters,convedSize * convedSize)
elif(labelY):
return result.reshape(batch_size,convedSize * convedSize)
else:
return result