forked from carpedm20/NTM-tensorflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ops.py
209 lines (172 loc) · 6.59 KB
/
ops.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
import math
import numpy as np
import tensorflow as tf
from tensorflow.python.ops import array_ops
from tensorflow.python.ops import init_ops
from tensorflow.python.framework import ops
from tensorflow.python.ops import math_ops
from tensorflow.python.ops import variable_scope as vs
from utils import *
def linear(args, output_size, bias, bias_start=0.0, scope=None):
"""Linear map: sum_i(args[i] * W[i]), where W[i] is a variable.
Args:
args: a 2D Tensor or a list of 2D, batch x n, Tensors.
output_size: int, second dimension of W[i].
bias: boolean, whether to add a bias term or not.
bias_start: starting value to initialize the bias; 0 by default.
scope: VariableScope for the created subgraph; defaults to "Linear".
Returns:
A 2D Tensor with shape [batch x output_size] equal to
sum_i(args[i] * W[i]), where W[i]s are newly created matrices.
Raises:
ValueError: if some of the arguments has unspecified or wrong shape.
"""
if not isinstance(args, (list, tuple)):
args = [args]
# Calculate the total size of arguments on dimension 1.
total_arg_size = 0
shapes = []
for a in args:
try:
shapes.append(a.get_shape().as_list())
except Exception as e:
shapes.append(a.shape)
is_vector = False
for idx, shape in enumerate(shapes):
if len(shape) != 2:
is_vector = True
args[idx] = tf.reshape(args[idx], [1, -1])
total_arg_size += shape[0]
else:
total_arg_size += shape[1]
# Now the computation.
with vs.variable_scope(scope or "Linear"):
matrix = vs.get_variable("Matrix", [total_arg_size, output_size])
if len(args) == 1:
res = math_ops.matmul(args[0], matrix)
else:
res = math_ops.matmul(array_ops.concat(1, args), matrix)
if not bias:
return res
bias_term = vs.get_variable(
"Bias", [output_size],
initializer=init_ops.constant_initializer(bias_start))
if is_vector:
return tf.reshape(res + bias_term, [-1])
else:
return res + bias_term
def Linear(input_, output_size, stddev=0.5,
is_range=False, squeeze=False,
name=None, reuse=None):
"""Applies a linear transformation to the incoming data.
Args:
input: a 2-D or 1-D data (`Tensor` or `ndarray`)
output_size: the size of output matrix or vector
"""
with tf.variable_scope("Linear", reuse=reuse):
if type(input_) == np.ndarray:
shape = input_.shape
else:
shape = input_.get_shape().as_list()
is_vector = False
if len(shape) == 1:
is_vector = True
input_ = tf.reshape(input_, [1, -1])
input_size = shape[0]
elif len(shape) == 2:
input_size = shape[1]
else:
raise ValueError("Linear expects shape[1] of inputuments: %s" % str(shape))
w_name = "%s_w" % name if name else None
b_name = "%s_b" % name if name else None
w = tf.get_variable(w_name, [input_size, output_size], tf.float32,
tf.random_normal_initializer(stddev=stddev))
mul = tf.matmul(input_, w)
if is_range:
def identity_initializer(tensor):
def _initializer(shape, dtype=tf.float32, partition_info=None):
return tf.identity(tensor)
return _initializer
range_ = tf.reverse(tf.range(1, output_size+1, 1), [True])
b = tf.get_variable(b_name, [output_size], tf.float32,
identity_initializer(tf.cast(range_, tf.float32)))
else:
b = tf.get_variable(b_name, [output_size], tf.float32,
tf.random_normal_initializer(stddev=stddev))
if squeeze:
output = tf.squeeze(tf.nn.bias_add(mul, b))
else:
output = tf.nn.bias_add(mul, b)
if is_vector:
return tf.reshape(output, [-1])
else:
return output
def smooth_cosine_similarity(m, v):
"""Computes smooth cosine similarity.
Args:
m: a 2-D `Tensor` (matrix)
v: a 1-D `Tensor` (vector)
"""
shape_x = m.get_shape().as_list()
shape_y = v.get_shape().as_list()
if shape_x[1] != shape_y[0]:
raise ValueError("Smooth cosine similarity is expecting same dimemsnion")
m_norm = tf.sqrt(tf.reduce_sum(tf.pow(m, 2),1))
v_norm = tf.sqrt(tf.reduce_sum(tf.pow(v, 2)))
m_dot_v = tf.matmul(m, tf.reshape(v, [-1, 1]))
similarity = tf.div(tf.reshape(m_dot_v, [-1]), m_norm * v_norm + 1e-3)
return similarity
def circular_convolution(v, k):
"""Computes circular convolution.
Args:
v: a 1-D `Tensor` (vector)
k: a 1-D `Tensor` (kernel)
"""
size = int(v.get_shape()[0])
kernel_size = int(k.get_shape()[0])
kernel_shift = int(math.floor(kernel_size/2.0))
def loop(idx):
if idx < 0: return size + idx
if idx >= size : return idx - size
else: return idx
kernels = []
for i in xrange(size):
indices = [loop(i+j) for j in xrange(kernel_shift, -kernel_shift-1, -1)]
v_ = tf.gather(v, indices)
kernels.append(tf.reduce_sum(v_ * k, 0))
# # code with double loop
# for i in xrange(size):
# for j in xrange(kernel_size):
# idx = i + kernel_shift - j + 1
# if idx < 0: idx = idx + size
# if idx >= size: idx = idx - size
# w = tf.gather(v, int(idx)) * tf.gather(kernel, j)
# output = tf.scatter_add(output, [i], tf.reshape(w, [1, -1]))
return tf.dynamic_stitch([i for i in xrange(size)], kernels)
def outer_product(*inputs):
"""Computes outer product.
Args:
inputs: a list of 1-D `Tensor` (vector)
"""
inputs = list(inputs)
order = len(inputs)
for idx, input_ in enumerate(inputs):
if len(input_.get_shape()) == 1:
inputs[idx] = tf.reshape(input_, [-1, 1] if idx % 2 == 0 else [1, -1])
if order == 2:
output = tf.mul(inputs[0], inputs[1])
elif order == 3:
size = []
idx = 1
for i in xrange(order):
size.append(inputs[i].get_shape()[0])
output = tf.zeros(size)
u, v, w = inputs[0], inputs[1], inputs[2]
uv = tf.mul(inputs[0], inputs[1])
for i in xrange(self.size[-1]):
output = tf.scatter_add(output, [0,0,i], uv)
return output
def scalar_mul(x, beta, name=None):
return x * beta
def scalar_div(x, beta, name=None):
return x / beta