本文整理汇总了Python中theano.tensor.batched_dot函数的典型用法代码示例。如果您正苦于以下问题:Python batched_dot函数的具体用法?Python batched_dot怎么用?Python batched_dot使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了batched_dot函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: one_step
def one_step(self, l, images):
'''
l = [n_examples, 5]
image = [n_examples, height, width]
'''
tol = 1e-4
g_x = self.B * (l[:, 0] + 1) / 2.
g_y = self.A * (l[:, 1] + 1) / 2.
delta = (max(self.A, self.B) - 1) / (self.N - 1) * T.exp(l[:, 2])
sigma = T.exp(l[:, 3])
mu_x = g_x.dimshuffle([0, 'x']) +\
(self.mu_ind - self.N / 2. + 0.5) * delta.dimshuffle([0, 'x'])
mu_y = g_y.dimshuffle([0, 'x']) +\
(self.mu_ind - self.N / 2. + 0.5) * delta.dimshuffle([0, 'x'])
F_x = T.exp(-((self.B_ind - mu_x.dimshuffle([0, 1, 'x']))**2) / (
2 * (sigma.dimshuffle([0, 'x', 'x'])))**2)
F_x = F_x / (F_x.sum(axis=-1).dimshuffle(0, 1, 'x') + tol)
# Compute Y filter banks##
F_y = T.exp(-((self.A_ind - mu_y.dimshuffle([0, 1, 'x']))**2) / (
2 * (sigma.dimshuffle([0, 'x', 'x'])))**2)
F_y = F_y / (F_y.sum(axis=-1).dimshuffle(0, 1, 'x') + tol)
read = T.batched_dot(T.batched_dot(F_y, images), F_x.dimshuffle([0, 2, 1]))
return read, g_x, g_y, delta, sigma
示例2: energy
def energy(self):
rho_x = rho(self.x)
rho_h = rho(self.h)
squared_norm = ( T.batched_dot(self.x,self.x) + T.batched_dot(self.h,self.h) ) / 2
uni_terms = - T.dot(rho_x, self.bx) - T.dot(rho_h, self.bh)
bi_terms = - T.batched_dot( T.dot(rho_x, self.W1), rho_h )
return squared_norm + uni_terms + bi_terms
示例3: get_output_for
def get_output_for(self, inputs, **kwargs):
# seq_input: (batch_size, seq_size, n_hidden_con)
# seq_mask: (batch_size, seq_size)
# condition: (batch_size, n_hidden_con)
seq_input, seq_mask, condition = inputs
if self.gate_covariance:
update = T.nnet.sigmoid(
T.sum(seq_input * self.w_gate, axis=-1, keepdims=True) +
self.b_gate)
seq_input *= update
length_seq = seq_input.shape[1]
if self.covariance_decay:
decay = T.arange(1, length_seq+1)
decay = (self.covariance_decay +
(length_seq-decay) * (1 - self.covariance_decay))
decay = T.sqrt(decay)
decay = decay.dimshuffle('x', 0, 'x')
seq_input *= decay
seq_input *= T.shape_padright(seq_mask)
# (batch_size, n_hidden_question, n_hidden_question)
covariance = T.batched_dot(seq_input.dimshuffle(0, 2, 1), seq_input)
# (batch_size, n_hidden_question), equivalent to the following line:
# att = T.sum(covariance * condition.dimshuffle((0, 'x', 1)), axis=2)
att = 1000 * T.batched_dot(covariance, condition.dimshuffle((0, 1)))
if not self.covariance_decay:
att /= T.sum(seq_mask, axis=1, keepdims=True)
# norm2_att = T.sum(att * condition, axis=1, keepdims=True)
# att = 1000 * att / norm2_att
return att
示例4: compute_psi2
def compute_psi2(lls, lsf, z, input_means, input_vars):
ls = T.exp(lls)
sf = T.exp(lsf)
b = ls / casting(2.0)
term_1 = T.prod(T.sqrt(b / (b + input_vars)), 1)
scale = T.sqrt(4 * (2 * b[ None, : ] + 0 * input_vars))
scaled_z = z[ None, : , : ] / scale[ : , None , : ]
scaled_z_minus_m = scaled_z
r2b = T.sum(scaled_z_minus_m**2, 2)[ :, None, : ] + T.sum(scaled_z_minus_m**2, 2)[ :, : , None ] - \
2 * T.batched_dot(scaled_z_minus_m, np.transpose(scaled_z_minus_m, [ 0, 2, 1 ]))
term_2 = T.exp(-r2b)
scale = T.sqrt(4 * (2 * b[ None, : ] + 2 * input_vars))
scaled_z = z[ None, : , : ] / scale[ : , None , : ]
scaled_m = input_means / scale
scaled_m = T.tile(scaled_m[ : , None, : ], [ 1, z.shape[ 0 ], 1])
scaled_z_minus_m = scaled_z - scaled_m
r2b = T.sum(scaled_z_minus_m**2, 2)[ :, None, : ] + T.sum(scaled_z_minus_m**2, 2)[ :, : , None ] + \
2 * T.batched_dot(scaled_z_minus_m, np.transpose(scaled_z_minus_m, [ 0, 2, 1 ]))
term_3 = T.exp(-r2b)
psi2_computed = sf**casting(2.0) * term_1[ :, None, None ] * term_2 * term_3
return T.transpose(psi2_computed, [ 1, 2, 0 ])
示例5: propup_given_h_lag
def propup_given_h_lag(self, vt, h_lag, hbias):
if h_lag == self.h0:
x = T.batched_dot(vt, self.W) + T.addbroadcast(
T.dot(h_lag, self.Wt) + hbias, 0, 1)
else:
x = T.batched_dot(vt, self.W) + hbias + T.dot(h_lag, self.Wt)
return [x, T.nnet.sigmoid(x)]
示例6: read
def read(self, images, center_y, center_x, delta, sigma):
"""
Parameters
----------
images : T.matrix (shape: batch_size x img_size)
Batch of images. Internally it will be reshaped to be a
(batch_size, img_height, img_width)-shaped stack of images.
center_y : T.vector (shape: batch_size)
center_x : T.vector (shape: batch_size)
delta : T.vector (shape: batch_size)
sigma : T.vector (shape: batch_size)
Returns
-------
window : T.matrix (shape: batch_size x N**2)
"""
N = self.N
batch_size = images.shape[0]
# Reshape input into proper 2d images
I = images.reshape( (batch_size, self.img_height, self.img_width) )
# Get separable filterbank
FY, FX = self.filterbank_matrices(center_y, center_x, delta, sigma)
# apply to the batch of images
W = T.batched_dot(T.batched_dot(FY, I), FX.transpose([0,2,1]))
return W.reshape((batch_size, N*N))
示例7: fwd
def fwd(self, x, V, A, L):
"""
x : signal
V : eigenvectors
A : area
L : eigenvalues
"""
V = V[:,:self.K]
L = L[:self.K]
L = L.dimshuffle('x','x',0)
rho = T.sqrt(T.sum(A))
# Q x 1 x K, a window for each input function
ghat = self.activation_interp(
T.batched_dot(T.tile(L, [self.nin,1,1]), self.Winterp))
# Q x K x N
V_ = T.tile(V.dimshuffle('x',1,0), [self.nin, 1, 1])
# Q x K x N
tmp = (ghat * V).dimshuffle(0,2,1)
# Q x N x N
transl = rho * T.batched_dot(V_.dimshuffle(0,2,1), tmp)
transl = A.dimshuffle('x',0,'x') * transl
# Q x K x N
tmp = (V.dimshuffle(0,'x',1) * x.dimshuffle(0,1,'x')).dimshuffle(1,2,0)
# Q x K x N
desc = rho * T.batched_dot(tmp, transl)
desc = T.abs_(desc)
desc = desc.dimshuffle(2,0,'x',1) # BC01 format : N x Q x 1 x K
return self.activation(theano.tensor.nnet.conv.conv2d(desc, self.W).flatten(2) + self.b)
示例8: h_given_h_lag_vt
def h_given_h_lag_vt(self, vt, h_lag, hbias):
if h_lag == self.h0:
x = T.batched_dot(vt, self.W) + T.addbroadcast(
T.dot(h_lag, self.Wt) + hbias.dimshuffle('x', 0), 0, 1)
else:
x = T.batched_dot(vt, self.W) + \
T.dot(h_lag, self.Wt) + hbias.dimshuffle('x', 0)
return [x, T.nnet.sigmoid(x)]
示例9: defmodel
def defmodel(self):
lhs = T.ivector("lhs")
rhs, nrhs = T.ivectors("rhs","nrhs")
lhsemb = self.entembs[lhs, :]
rhsemb = self.W[rhs, :]
nrhsemb = self.W[nrhs, :]
pdot = T.batched_dot(lhsemb, rhsemb)
ndot = T.batched_dot(lhsemb, nrhsemb)
return pdot, ndot, [lhs, rhs, nrhs]
示例10: energy_function
def energy_function():
squared_norm = (
T.batched_dot(self.x, self.x) + T.batched_dot(self.h, self.h) + T.batched_dot(self.y, self.y)
) / 2.0
uni_terms = -T.dot(self.rho_x, self.bx) - T.dot(self.rho_h, self.bh) - T.dot(self.rho_y, self.by)
bi_terms = -T.batched_dot(T.dot(self.rho_x, self.W1), self.rho_h) - T.batched_dot(
T.dot(self.rho_h, self.W2), self.rho_y
)
return squared_norm + uni_terms + bi_terms
示例11: step
def step(self, x, states):
h_tild_tm1 = states[0]
B_U = states[1]
B_W = states[2]
if self.consume_less == 'cpu':
x_i = x[:, :self.output_dim]
x_f = x[:, self.output_dim: 2 * self.output_dim]
x_c = x[:, 2 * self.output_dim: 3 * self.output_dim]
x_o = x[:, 3 * self.output_dim: 4 * self.output_dim]
x_new = x[:, 4 * self.output_dim:]
else:
x_i = K.dot(x * B_W[0], self.W_i) + self.b_i
x_f = K.dot(x * B_W[1], self.W_f) + self.b_f
x_c = K.dot(x * B_W[2], self.W_c) + self.b_c
x_o = K.dot(x * B_W[3], self.W_o) + self.b_o
x_new = x
# self.C_tape -> BT, t-1, k
# self.H_tape -> BT, t-1, k
# x -> BT, k
# h_tild_tm1 -> BT, k
if self.H_tape is None:
self.H_tape = K.zeros_like(h_tild_tm1).dimshuffle((0,'x',1))
self.C_tape = K.zeros_like(h_tild_tm1).dimshuffle((0,'x',1))
# s_t -> BT, t-1, 1
t = K.shape(self.C_tape)[1]
sum1 = K.dot(self.H_tape, self.W_h)
sum2 = K.dot(K.repeat_elements(x_new.dimshuffle((0,'x',1)),t, axis=1), self.W_x)
sum3 = K.dot(K.repeat_elements(h_tild_tm1.dimshuffle((0,'x',1)),t, axis=1), self.W_h_tilde)
tanhed_sum = K.tanh(sum1 + sum2 + sum3)
a_t = K.dot(tanhed_sum, self.v)[:,:,0]
s_t = K.softmax(a_t)
h_tilde_t = T.batched_dot(self.H_tape.dimshuffle((0,2,1)), s_t.dimshuffle((0,1,'x')))[:,:,0]
c_tilde_t = T.batched_dot(self.C_tape.dimshuffle((0,2,1)), s_t.dimshuffle((0,1,'x')))[:,:,0]
i = self.inner_activation(x_i + K.dot(h_tilde_t * B_U[0], self.U_i))
f = self.inner_activation(x_f + K.dot(h_tilde_t * B_U[1], self.U_f))
c_t = f * c_tilde_t + i * self.activation(x_c + K.dot(h_tilde_t * B_U[2], self.U_c))
o = self.inner_activation(x_o + K.dot(h_tilde_t * B_U[3], self.U_o))
h_t = o * self.activation(c_t)
# Add to Tape
self.C_tape = K.concatenate([self.C_tape, c_t.dimshuffle((0,'x',1))], axis=1)
self.H_tape = K.concatenate([self.H_tape, h_t.dimshuffle((0,'x',1))], axis=1)
return h_t, [h_tilde_t]
示例12: batched_cos_sim
def batched_cos_sim(s):
""" from (x,y,z)-shaped pair, produce (x,y)-shaped pair that replaces the z-vector pairs by their cosine similarities """
import theano
import theano.tensor as T
return theano.scan(
fn=lambda xm, ym: T.batched_dot(xm, ym) / T.sqrt(T.batched_dot(xm, xm) * T.batched_dot(ym, ym)),
outputs_info=None,
sequences=s,
non_sequences=None,
)[0]
示例13: free_energy_given_hid_lag
def free_energy_given_hid_lag(self, vt, h_lag, hbias, vbias):
if h_lag == self.h0:
wx_b = T.batched_dot(vt, self.W) +\
T.addbroadcast(T.dot(h_lag, self.Wt) + hbias, 0, 1)
vbias_term = T.batched_dot(vt, vbias)
hidden_term = T.sum(T.log(1 + T.exp(wx_b)), axis=2)
else:
wx_b = T.batched_dot(vt, self.W) + T.dot(h_lag, self.Wt) + \
hbias.dimshuffle('x', 0)
vbias_term = T.batched_dot(vt, vbias)
hidden_term = T.sum(T.log(1 + T.exp(wx_b)), axis=2)
return -hidden_term - vbias_term
示例14: MemLayer
def MemLayer(incomings, params, linear=0):
'''
incomings = (u, u_shape, A, A_shape, C, C_shape)
'''
((u, u_shape), (A, A_shape), (C, C_shape)) = incomings
p = T.switch(linear, T.batched_dot(A, u), nnet.softmax(T.batched_dot(A, u)))
p_shape = A_shape[:2]
# C.shape = (batch_size, num_sen, embed_size), u.shape = (batch_size, embed_size)
# p.shape = (batch_size, num_sen, 1)
#return (p, u_shape)
O = (C * p[:, :, None]).sum(axis = 1)
return ((O, u_shape), (p, p_shape))
示例15: write
def write(self, windows, center_y, center_x, delta, sigma):
N = self.N
batch_size = windows.shape[0]
# Reshape input into proper 2d windows
W = windows.reshape( (batch_size, N, N) )
# Get separable filterbank
FY, FX = self.filterbank_matrices(center_y, center_x, delta, sigma)
# apply...
I = T.batched_dot(T.batched_dot(FY.transpose([0,2,1]), W), FX)
return I.reshape( (batch_size, self.img_height*self.img_width) )