-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
gated_autoencoder.py
174 lines (150 loc) · 5.66 KB
/
gated_autoencoder.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
"""
Definitions of the cost for the gated-autoencoder.
"""
from pylearn2.costs.cost import Cost, DefaultDataSpecsMixin
from pylearn2.space import VectorSpace
class SymmetricCost(DefaultDataSpecsMixin, Cost):
"""
Summary (Class representing the symmetric cost).
Subclasses can define the type of data they will use.
Mean reconstruction error is used for real valued data
and cross-Entropy loss is used for binary.
See Also
--------
"Gradient-based learning of higher-order image features"
"""
@staticmethod
def cost(x, y, rx, ry):
"""
Symmetric reconstruction cost.
Parameters
----------
x : tensor_like
Theano symbolic representing the first input minibatch.
Assumed to be 2-tensors, with the first dimension
indexing training examples and the second indexing
data dimensions.
y : tensor_like
Theano symbolic representing the seconde input minibatch.
Assumed to be 2-tensors, with the first dimension
indexing training examples and the second indexing
data dimensions.
rx : tensor_like
Reconstruction of the first minibatch by the model.
ry: tensor_like
Reconstruction of the second minibatch by the model.
Returns
-------
Cost: theano_like expression
Representation of the cost
"""
raise NotImplementedError
def expr(self, model, data, *args, **kwargs):
"""
Returns a theano expression for the cost function.
Returns a symbolic expression for a cost function applied to the
minibatch of data.
Optionally, may return None. This represents that the cost function
is intractable but may be optimized via the get_gradients method.
Parameters
----------
model : a pylearn2 Model instance
data : a batch in cost.get_data_specs() form
kwargs : dict
Optional extra arguments. Not used by the base class.
"""
self.get_data_specs(model)[0].validate(data)
x, y = data
input_space = model.get_input_space()
if not isinstance(input_space.components[0], VectorSpace):
conv = input_space.components[0]
vec = VectorSpace(conv.get_total_dimension())
x = conv.format_as(x, vec)
if not isinstance(input_space.components[1], VectorSpace):
conv = input_space.components[1]
vec = VectorSpace(conv.get_total_dimension())
y = conv.format_as(y, vec)
rx, ry = model.reconstructXY((x, y))
return self.cost(x, y, rx, ry)
class SymmetricMSRE(SymmetricCost):
"""
Summary (Symmetric cost for real valued data).
See Also
--------
"Gradient-based learning of higher-order image features"
"""
@staticmethod
def cost(x, y, rx, ry):
"""
Summary (Definition of the cost).
Mean squared reconstruction error.
Parameters
----------
x : tensor_like
Theano symbolic representing the first input minibatch.
Assumed to be 2-tensors, with the first dimension
indexing training examples and the second indexing
data dimensions.
y : tensor_like
Theano symbolic representing the seconde input minibatch.
Assumed to be 2-tensors, with the first dimension
indexing training examples and the second indexing
data dimensions.
rx : tensor_like
Reconstruction of the first minibatch by the model.
ry: tensor_like
Reconstruction of the second minibatch by the model.
Returns
-------
Cost: theano_like expression
Representation of the cost
Notes
-----
Symmetric reconstruction cost as defined by Memisevic in:
"Gradient-based learning of higher-order image features".
This function only works with real valued data.
"""
return (
((0.5*((x - rx)**2)) + (0.5*((y - ry)**2)))).sum(axis=1).mean()
class NormalizedSymmetricMSRE(SymmetricCost):
"""
Summary (Normalized Symmetric cost for real valued data).
Notes
-----
Value used to observe the percentage of reconstruction.
"""
@staticmethod
def cost(x, y, rx, ry):
"""
Summary (Definition of the cost).
Normalized Mean squared reconstruction error. Values
between 0 and 1.
Parameters
----------
x : tensor_like
Theano symbolic representing the first input minibatch.
Assumed to be 2-tensors, with the first dimension
indexing training examples and the second indexing
data dimensions.
y : tensor_like
Theano symbolic representing the seconde input minibatch.
Assumed to be 2-tensors, with the first dimension
indexing training examples and the second indexing
data dimensions.
rx : tensor_like
Reconstruction of the first minibatch by the model.
ry: tensor_like
Reconstruction of the second minibatch by the model.
Returns
-------
Cost: theano_like expression
Representation of the cost
Notes
-----
Do not use this function to train, only to monitor the
average percentage of reconstruction achieved when training on
real valued data.
"""
num = (((0.5*((x - rx)**2)) + (0.5*((y - ry)**2)))).sum(axis=1).mean()
den = ((0.5*(x.norm(2, 1)**2)) + (0.5*(y.norm(2, 1)**2))).mean()
return num/den