def output_func(self, input):
# P(Y|X) = softmax(W.X + b)
q, a, feats = input[0], input[1], input[2]
dot = T.batched_dot(q, T.dot(a, self.W))
feats_dot = T.dot(feats, self.W_feats)
self.p_y_given_x = T.nnet.softmax(dot + feats_dot + self.b.dimshuffle('x', 0))
self.prob = self.p_y_given_x[:,-1]
self.y_pred = T.argmax(self.p_y_given_x, axis=1)
return self.y_pred
python类batched_dot()的实例源码
def output_func(self, input):
# P(Y|X) = softmax(W.X + b)
q, a, feats = input[0], input[1], input[2]
# dot = T.batched_dot(q, T.batched_dot(a, self.W))
dot = T.batched_dot(q, T.dot(a, self.W.T))
out = T.concatenate([dot.dimshuffle(0, 'x'), q, a, feats], axis=1)
return out
def output_func(self, input):
# P(Y|X) = softmax(W.X + b)
q, a, feats = input[0], input[1], input[2]
# dot = T.batched_dot(q, T.batched_dot(a, self.W))
dot = T.batched_dot(q, T.dot(a, self.W.T))
out = T.concatenate([dot.dimshuffle(0, 'x'), feats], axis=1)
# out = feats
return out
def output_func(self, input):
# P(Y|X) = softmax(W.X + b)
q, a = input[0], input[1]
# dot = T.batched_dot(q, T.batched_dot(a, self.W))
dot = T.batched_dot(q, T.dot(a, self.W.T))
out = T.concatenate([dot.dimshuffle(0, 'x'), q, a], axis=1)
return out
def output_func(self, input):
# P(Y|X) = softmax(W.X + b)
q, a = input[0], input[1]
# dot = T.batched_dot(q, T.batched_dot(a, self.W))
qdot = T.dot(q, self.Wq)
adot = T.dot(a, self.Wa)
dot = T.batched_dot(qdot, adot)
out = T.concatenate([dot.dimshuffle(0, 'x'), q, a], axis=1)
return out
def output_func(self, input):
# P(Y|X) = softmax(W.X + b)
q, a = input[0], input[1]
# dot = T.batched_dot(q, T.batched_dot(a, self.W))
out = T.batched_dot(q, T.dot(a, self.W.T)).dimshuffle(0, 'x')
return out
def output_func(self, input):
# P(Y|X) = softmax(W.X + b)
q, a, feats = input[0], input[1], input[2]
dot = T.batched_dot(q, T.dot(a, self.W))
feats_dot = T.dot(feats, self.W_feats)
self.p_y_given_x = T.nnet.softmax(dot + feats_dot + T.dot(q, self.W_q) + T.dot(a, self.W_a) + self.b.dimshuffle('x', 0))
self.prob = self.p_y_given_x[:,-1]
self.y_pred = T.argmax(self.p_y_given_x, axis=1)
return self.y_pred
def LayerNormalization(x, gamma, mask, estimated_mean=0.0, estimated_var=1.0):
assert x.ndim == 3 or x.ndim == 2
if x.ndim == 3:
x_mean = T.mean(x, axis=2).dimshuffle(0, 1, 'x')
x_var = T.var(x, axis=2).dimshuffle(0, 1, 'x')
return gamma*((x - x_mean) / T.sqrt(x_var+1e-7)), x_mean[0, 0], x_var[0, 0]
elif x.ndim == 2:
x_mean = T.mean(x, axis=1).dimshuffle(0, 'x')
x_var = T.var(x, axis=1).dimshuffle(0, 'x')
return gamma*((x - x_mean) / T.sqrt(x_var+1e-7)), x_mean[0], x_var[0]
# Does theano.batched_dot. If last_axis is on it will loop over the last axis, otherwise it will loop over the first axis.
def BatchedDot(x, y, last_axis=False):
if last_axis==False:
return T.batched_dot(x, y)
elif last_axis:
if x.ndim == 2:
shuffled_x = x.dimshuffle(1,0)
elif x.ndim == 3:
shuffled_x = x.dimshuffle(2,0,1)
elif x.ndim == 4:
shuffled_x = x.dimshuffle(3,0,1,2)
else:
raise ValueError('BatchedDot inputs must have between 2-4 dimensions, but x has ' + str(x.ndim) + ' dimensions')
if y.ndim == 2:
shuffled_y = y.dimshuffle(1,0)
elif y.ndim == 3:
shuffled_y = y.dimshuffle(2,0,1)
elif y.ndim == 4:
shuffled_y = y.dimshuffle(3,0,1,2)
else:
raise ValueError('BatchedDot inputs must have between 2-4 dimensions, but y has ' + str(y.ndim) + ' dimensions')
dot = T.batched_dot(shuffled_x, shuffled_y)
if dot.ndim == 2:
return dot.dimshuffle(1,0)
elif dot.ndim == 3:
return dot.dimshuffle(1,2,0)
elif dot.ndim == 4:
return dot.dimshuffle(1,2,3,0)
def batched_gram5d(self, fmap):
# (layer, batch, featuremaps, height*width)
fmap=fmap.flatten(ndim=4)
# (layer*batch, featuremaps, height*width)
fmap2=fmap.reshape((-1, fmap.shape[-2], fmap.shape[-1]))
# The T.prod term can't be taken outside as a T.mean in style_loss(), since the width and height of the image might vary
return T.batched_dot(fmap2, fmap2.dimshuffle(0,2,1)).reshape(fmap.shape)/T.prod(fmap.shape[-2:])
def batched_gram(self, fmap):
# (batch, featuremaps, height*width)
fmap=fmap.flatten(ndim=3)
# The T.prod term can't be taken outside as a T.mean in style_loss(), since the width and height of the image might vary
if self.net_type == 0:
return T.batched_dot(fmap, fmap.dimshuffle(0,2,1))/T.prod(fmap.shape[-2:])
elif self.net_type == 1:
return T.batched_dot(fmap, fmap.dimshuffle(0,2,1))/T.prod(fmap.shape[-1])
def output_func(self, input):
# P(Y|X) = softmax(W.X + b)
q, a = input[0], input[1]
dot = T.batched_dot(q, T.dot(a, self.W))
self.p_y_given_x = T.nnet.softmax(dot + self.b.dimshuffle('x', 0))
self.prob = self.p_y_given_x[:, -1]
self.y_pred = T.argmax(self.p_y_given_x, axis=1)
return self.y_pred
def output_func(self, input):
# P(Y|X) = softmax(W.X + b)
q, a, feats = input[0], input[1], input[2]
dot = T.batched_dot(q, T.dot(a, self.W))
feats_dot = T.dot(feats, self.W_feats)
l = self.lamda.dimshuffle('x', 0)
self.p_y_given_x = T.nnet.softmax(l * dot + (1 - l) * feats_dot + self.b.dimshuffle('x', 0))
self.prob = self.p_y_given_x[:, -1]
self.y_pred = T.argmax(self.p_y_given_x, axis=1)
return self.y_pred
def output_func(self, input):
# P(Y|X) = softmax(W.X + b)
q, a = input[0], input[1]
# dot = T.batched_dot(q, T.dot(a, self.W.T))
dot = T.batched_dot(q, T.dot(a, self.W))
self.p_y_given_x = T.nnet.softmax(dot + self.b.dimshuffle('x', 0))
self.prob = self.p_y_given_x[:, -1]
self.y_pred = T.argmax(self.p_y_given_x, axis=1)
return self.y_pred
def output_func(self, input):
# P(Y|X) = softmax(W.X + b)
q, a, feats = input[0], input[1], input[2]
dot = T.batched_dot(q, T.dot(a, self.W))
feats_dot = T.dot(feats, self.W_feats)
self.p_y_given_x = T.nnet.softmax(dot + feats_dot + self.b.dimshuffle('x', 0))
self.prob = self.p_y_given_x[:, -1]
self.y_pred = T.argmax(self.p_y_given_x, axis=1)
return self.y_pred
def output_func(self, input):
# P(Y|X) = softmax(W.X + b)
q, a, feats = input[0], input[1], input[2]
# dot = T.batched_dot(q, T.batched_dot(a, self.W))
dot = T.batched_dot(q, T.dot(a, self.W.T))
out = T.concatenate([dot.dimshuffle(0, 'x'), q, a, feats], axis=1)
return out
def output_func(self, input):
# P(Y|X) = softmax(W.X + b)
q = input[0]
all_list = [q]
for i in xrange(self.position):
dot = T.batched_dot(q, T.dot(input[i + 1], self.W[i].T))
all_list.append(dot.dimshuffle(0, 'x'))
all_list.append(input[i + 1])
# dot = T.batched_dot(q, T.batched_dot(a, self.W))
#dot = T.batched_dot(q, T.dot(a, self.W.T))
#out = T.concatenate([dot.dimshuffle(0, 'x'), q, a], axis=1)
out = T.concatenate(all_list, axis=1)
return out
def output_func(self, input):
# P(Y|X) = softmax(W.X + b)
q, a, feats = input[0], input[1], input[2]
# dot = T.batched_dot(q, T.batched_dot(a, self.W))
dot = T.batched_dot(q, T.dot(a, self.W.T))
out = T.concatenate([dot.dimshuffle(0, 'x'), feats], axis=1)
# out = feats
return out
def output_func(self, input):
# P(Y|X) = softmax(W.X + b)
q, a = input[0], input[1]
# dot = T.batched_dot(q, T.batched_dot(a, self.W))
dot = T.batched_dot(q, T.dot(a, self.W.T))
out = T.concatenate([dot.dimshuffle(0, 'x'), q, a], axis=1)
return out
def output_func(self, input):
# P(Y|X) = softmax(W.X + b)
q, a = input[0], input[1]
# dot = T.batched_dot(q, T.batched_dot(a, self.W))
qdot = T.dot(q, self.Wq)
adot = T.dot(a, self.Wa)
dot = T.batched_dot(qdot, adot)
out = T.concatenate([dot.dimshuffle(0, 'x'), q, a], axis=1)
return out