forked from jys5609/DSTC5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
220 lines (187 loc) · 6.42 KB
/
utils.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
'''
Created on Jul 5, 2016
@author: kekim
'''
from __future__ import absolute_import
from __future__ import division
import numpy as np
import copy
import inspect
import types as python_types
import marshal
import sys
import warnings
from keras.layers import Embedding, Layer
from keras import backend as K
class ZonzEmbedding(Embedding):
def call(self, x, mask=None):
if 0. < self.dropout < 1.:
retain_p = 1. - self.dropout
B = K.random_binomial((self.input_dim,), p=retain_p) * (1. / retain_p)
B = K.expand_dims(B)
W = K.in_train_phase(self.W * B, self.W)
else:
W = self.W
M = K.concatenate([K.zeros((1,)), K.ones((self.input_dim - 1,))], axis=0)
M = K.expand_dims(M)
out = K.gather(W * M, x)
return out
class MaskEatingLambda(Layer):
'''Used for evaluating an arbitrary Theano / TensorFlow expression
on the output of the previous layer.
# Examples
```python
# add a x -> x^2 layer
model.add(Lambda(lambda x: x ** 2))
```
```python
# add a layer that returns the concatenation
# of the positive part of the input and
# the opposite of the negative part
def antirectifier(x):
x -= K.mean(x, axis=1, keepdims=True)
x = K.l2_normalize(x, axis=1)
pos = K.relu(x)
neg = K.relu(-x)
return K.concatenate([pos, neg], axis=1)
def antirectifier_output_shape(input_shape):
shape = list(input_shape)
assert len(shape) == 2 # only valid for 2D tensors
shape[-1] *= 2
return tuple(shape)
model.add(Lambda(antirectifier, output_shape=antirectifier_output_shape))
```
# Arguments
function: The function to be evaluated.
Takes one argument: the output of previous layer
output_shape: Expected output shape from function.
Could be a tuple or a function of the shape of the input
arguments: optional dictionary of keyword arguments to be passed
to the function.
# Input shape
Arbitrary. Use the keyword argument input_shape
(tuple of integers, does not include the samples axis)
when using this layer as the first layer in a model.
# Output shape
Specified by `output_shape` argument.
'''
def __init__(self, function, output_shape=None, arguments={}, **kwargs):
self.function = function
self.arguments = arguments
self.supports_masking = True
if output_shape is None:
self._output_shape = None
elif type(output_shape) in {tuple, list}:
self._output_shape = tuple(output_shape)
else:
if not hasattr(output_shape, '__call__'):
raise Exception('In Lambda, `output_shape` '
'must be a list, a tuple, or a function.')
self._output_shape = output_shape
super(MaskEatingLambda, self).__init__(**kwargs)
def get_output_shape_for(self, input_shape):
if self._output_shape is None:
# if TensorFlow, we can infer the output shape directly:
if K._BACKEND == 'tensorflow':
if type(input_shape) is list:
xs = [K.placeholder(shape=shape) for shape in input_shape]
x = self.call(xs)
else:
x = K.placeholder(shape=input_shape)
x = self.call(x)
if type(x) is list:
return [K.int_shape(x_elem) for x_elem in x]
else:
return K.int_shape(x)
# otherwise, we default to the input shape
return input_shape
elif type(self._output_shape) in {tuple, list}:
nb_samples = input_shape[0] if input_shape else None
return (nb_samples,) + tuple(self._output_shape)
else:
shape = self._output_shape(input_shape)
if type(shape) not in {list, tuple}:
raise Exception('output_shape function must return a tuple')
return tuple(shape)
def call(self, x, mask=None):
arguments = self.arguments
arg_spec = inspect.getargspec(self.function)
if 'mask' in arg_spec.args:
arguments['mask'] = mask
return self.function(x, **arguments)
def get_config(self):
py3 = sys.version_info[0] == 3
if isinstance(self.function, python_types.LambdaType):
if py3:
function = marshal.dumps(self.function.__code__).decode('raw_unicode_escape')
else:
function = marshal.dumps(self.function.func_code).decode('raw_unicode_escape')
function_type = 'lambda'
else:
function = self.function.__name__
function_type = 'function'
if isinstance(self._output_shape, python_types.LambdaType):
if py3:
output_shape = marshal.dumps(self._output_shape.__code__)
else:
output_shape = marshal.dumps(self._output_shape.func_code)
output_shape_type = 'lambda'
elif callable(self._output_shape):
output_shape = self._output_shape.__name__
output_shape_type = 'function'
else:
output_shape = self._output_shape
output_shape_type = 'raw'
config = {'function': function,
'function_type': function_type,
'output_shape': output_shape,
'output_shape_type': output_shape_type,
'arguments': self.arguments}
base_config = super(MaskEatingLambda, self).get_config()
return dict(list(base_config.items()) + list(config.items()))
def compute_mask(self, x, input_mask=None):
return None
@classmethod
def from_config(cls, config):
function_type = config.pop('function_type')
if function_type == 'function':
function = globals()[config['function']]
elif function_type == 'lambda':
function = marshal.loads(config['function'].encode('raw_unicode_escape'))
function = python_types.FunctionType(function, globals())
else:
raise Exception('Unknown function type: ' + function_type)
output_shape_type = config.pop('output_shape_type')
if output_shape_type == 'function':
output_shape = globals()[config['output_shape']]
elif output_shape_type == 'lambda':
output_shape = marshal.loads(config['output_shape'])
output_shape = python_types.FunctionType(output_shape, globals())
else:
output_shape = config['output_shape']
config['function'] = function
config['output_shape'] = output_shape
return cls(**config)
# to be used in the above lambda layer
def lambda_mask_sum(x, mask=None):
return K.batch_dot(x, mask, axes=1)
def lambda_mask_mean(x, mask=None):
return K.batch_dot(x, mask, axes=1) / K.sum(x, axis=1)
def lambda_mask_reverse(x, mask=None):
return x[:, ::-1, :] * K.expand_dims(mask[::-1])
def lambda_mask_zero(x, mask=None):
return x * K.expand_dims(mask)
# [attention, input]
def lambda_attended(x, mask=None):
x0 = K.expand_dims(x[0], dim=-1)
x1 = K.expand_dims(x[1], dim=2)
return x0 * x1
def time_softmax(x):
ndim = K.ndim(x)
if ndim == 3:
e = K.exp(x - K.max(x, axis=1, keepdims=True))
s = K.sum(e, axis=1, keepdims=True)
return e / s
else:
raise Exception('Cannot apply time_softmax to a tensor that is no 3D. '+
'Here, ndim= '+str(ndim))