本文整理汇总了Python中chainer.functions.matmul方法的典型用法代码示例。如果您正苦于以下问题:Python functions.matmul方法的具体用法?Python functions.matmul怎么用?Python functions.matmul使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类chainer.functions
的用法示例。
在下文中一共展示了functions.matmul方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __call__
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import matmul [as 别名]
def __call__(self, state):
h = state
for layer in self.hidden_layers:
h = F.relu(layer(h))
v = self.v(h)
mu = self.mu(h)
if self.scale_mu:
mu = scale_by_tanh(mu, high=self.action_space.high,
low=self.action_space.low)
mat_diag = F.exp(self.mat_diag(h))
if hasattr(self, 'mat_non_diag'):
mat_non_diag = self.mat_non_diag(h)
tril = lower_triangular_matrix(mat_diag, mat_non_diag)
mat = F.matmul(tril, tril, transb=True)
else:
mat = F.expand_dims(mat_diag ** 2, axis=2)
return QuadraticActionValue(
mu, mat, v, min_action=self.action_space.low,
max_action=self.action_space.high)
示例2: compute_weighted_value_loss
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import matmul [as 别名]
def compute_weighted_value_loss(eltwise_loss, weights,
batch_accumulator='mean'):
"""Compute a loss for value prediction problem.
Args:
eltwise_loss (Variable): Element-wise loss per example
weights (ndarray): Weights for y, t.
batch_accumulator (str): 'mean' will divide loss by batchsize
Returns:
(Variable) scalar loss
"""
batch_size = eltwise_loss.shape[0]
assert batch_accumulator in ('mean', 'sum')
assert eltwise_loss.ndim == 3
# eltwise_loss is (batchsize, n , n') array of losses
# weights is an array of shape (batch_size)
# apply weights per example in batch
loss_sum = F.matmul(F.sum(F.mean(eltwise_loss, axis=2), axis=1), weights)
if batch_accumulator == 'mean':
loss = loss_sum / batch_size
elif batch_accumulator == 'sum':
loss = loss_sum
return loss
示例3: compute_weighted_value_loss
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import matmul [as 别名]
def compute_weighted_value_loss(eltwise_loss, batch_size, weights,
batch_accumulator='mean'):
"""Compute a loss for value prediction problem.
Args:
eltwise_loss (Variable): Element-wise loss per example per atom
weights (ndarray): Weights for y, t.
batch_accumulator (str): 'mean' will divide loss by batchsize
Returns:
(Variable) scalar loss
"""
assert batch_accumulator in ('mean', 'sum')
# eltwise_loss is (batchsize, n_atoms) array of losses
# weights is an array of shape (batch_size)
# sum loss across atoms and then apply weight per example in batch
loss_sum = F.matmul(F.sum(eltwise_loss, axis=1), weights)
if batch_accumulator == 'mean':
loss = loss_sum / batch_size
elif batch_accumulator == 'sum':
loss = loss_sum
return loss
示例4: dc_loss
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import matmul [as 别名]
def dc_loss(embedding, label):
"""
Deep clustering loss function.
Args:
embedding: (T,D)-shaped activation values
label: (T,C)-shaped labels
return:
(1,)-shaped squared flobenius norm of the difference
between embedding and label affinity matrices
"""
xp = cuda.get_array_module(label)
b = xp.zeros((label.shape[0], 2**label.shape[1]))
b[np.arange(label.shape[0]),
[int(''.join(str(x) for x in t), base=2) for t in label.data]] = 1
label_f = chainer.Variable(b.astype(np.float32))
loss = F.sum(F.square(F.matmul(embedding, embedding, True, False))) \
+ F.sum(F.square(F.matmul(label_f, label_f, True, False))) \
- 2 * F.sum(F.square(F.matmul(embedding, label_f, True, False)))
return loss
示例5: __call__
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import matmul [as 别名]
def __call__(self, doc_ids, update_only_docs=False):
""" Given an array of document integer indices, returns a vector
for each document. The vector is composed of topic weights projected
onto topic vectors.
Args:
doc_ids : chainer.Variable
One-dimensional batch vectors of IDs
Returns:
doc_vector : chainer.Variable
Batch of two-dimensional embeddings for every document.
"""
# (batchsize, ) --> (batchsize, multinomial)
proportions = self.proportions(doc_ids, softmax=True)
# (batchsize, n_factors) * (n_factors, n_dim) --> (batchsize, n_dim)
factors = F.dropout(self.factors(), ratio=self.dropout_ratio)
if update_only_docs:
factors.unchain_backward()
w_sum = F.matmul(proportions, factors)
return w_sum
示例6: propup
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import matmul [as 别名]
def propup(self, vis):
"""
This function propagates the visible units activation upwards to the hidden units
Eq.(7)
:param vis: Variable Matrix(batch_size, in_channels, image_height, image_width)
- given v_sample
:return: Variable Matrix(batch_size, out_channels, image_height_out, image_width_out)
- probability for each hidden units to be h_i=1
"""
# conv.W: Matrix(out_channels, in_channels, filter height=ksize, filter width=ksize)
# conv.b: Vec (out_channels, )
if self.real == 0:
pre_sigmoid_activation = self.conv(vis)
else:
pre_sigmoid_activation = self.conv(vis / self.std_ch)
# F.matmul(vis, self.conv.W, transb=True) + F.broadcast_to(self.conv.b, (vis.data.shape[0], self.n_hidden))
return F.sigmoid(pre_sigmoid_activation)
示例7: propdown
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import matmul [as 别名]
def propdown(self, hid):
""" This function propagates the hidden units activation downwords to the visible units
:param hid: Variable Matrix(batch_size, out_channels, image_height_out, image_width_out) - given h_sample
:return: Variable Matrix(batch_size, in_channels, image_height, image_width) - probability for each visible units to be v_j = 1
"""
batch_size = hid.data.shape[0]
if self.real == 0:
W_flipped = F.swapaxes(CF.flip(self.conv.W, axes=(2, 3)), axis1=0, axis2=1)
pre_sigmoid_activation = F.convolution_2d(hid, W_flipped, self.conv.a, pad=self.ksize-1)
# F.matmul(hid, self.l.W) + F.broadcast_to(self.l.a, (batch_size, self.n_visible))
v_mean = F.sigmoid(pre_sigmoid_activation)
#print('W info ', self.conv.W.data.shape, 'W_flipped info ', W_flipped.data.shape)
#print('W info ', self.conv.W.data[3, 0, 2, 3], 'W_flipped info ', W_flipped.data[0, 3, 8, 7])
#print('W info ', self.conv.W.data[3, 0, 8, 7], 'W_flipped info ', W_flipped.data[0, 3, 2, 3])
#print('W info ', self.conv.W.data[19, 0, 4, 0], 'W_flipped info ', W_flipped.data[0, 19, 6, 10])
#print('pre_sigmoidactivation', F.sum(pre_sigmoid_activation).data)
#print('v_mean', v_mean.data.shape)
#print('v_mean sum', F.sum(v_mean).data)
#print('hid', hid.data.shape)
else:
# TODO: check
W_flipped = F.swapaxes(CF.flip(self.conv.W, axes=(2, 3)), axis1=0, axis2=1)
v_mean = F.convolution_2d(hid, W_flipped, self.conv.a, pad=self.ksize-1)
return v_mean
示例8: reconstruct
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import matmul [as 别名]
def reconstruct(self, v):
"""
:param v: Variable Matrix(batch_size, in_channels, image_height, image_width)
:return: reconstructed_v, Variable Matrix(batch_size, in_channels, image_height, image_width)
"""
batch_size = v.data.shape[0]
xp = cuda.get_array_module(v.data)
if self.real == 0:
h = F.sigmoid(self.conv(v))
else:
std_ch = xp.reshape(self.std, (1, self.in_channels, 1, 1))
h = F.sigmoid(self.conv(v / std_ch))
# F.sigmoid(F.matmul(v, self.l.W, transb=True) + F.broadcast_to(self.l.b, (batch_size, self.n_hidden)))
W_flipped = F.swapaxes(CF.flip(self.conv.W, axes=(2, 3)), axis1=0, axis2=1)
reconstructed_v = F.sigmoid(F.convolution_2d(h, W_flipped, self.conv.a, pad=self.ksize-1))
# = F.sigmoid(F.matmul(h, self.l.W) + F.broadcast_to(self.l.a, (batch_size, self.n_visible)))
return reconstructed_v
示例9: __call__
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import matmul [as 别名]
def __call__(self, h):
# type: (chainer.Variable) -> chainer.Variable
xp = cuda.get_array_module(h)
mb, node, ch = h.shape # type: int, int, int
if self.q_star is None:
self.q_star = [
xp.zeros((1, self.in_channels * 2)).astype('f')
for _ in range(mb)
]
self.hx, self.cx, q = self.lstm_layer(self.hx, self.cx, self.q_star)
# self.hx: (mb, mb, ch)
# self.cx: (mb, mb, ch)
# q: List[(1, ch) * mb]
q = functions.stack(q) # q: (mb, 1, ch)
q_ = functions.transpose(q, axes=(0, 2, 1)) # q_: (mb, ch, 1)
e = functions.matmul(h, q_) # e: (mb, node, 1)
a = functions.softmax(e) # a: (mb, node, 1)
a = functions.broadcast_to(a, h.shape) # a: (mb, node, ch)
r = functions.sum((a * h), axis=1, keepdims=True) # r: (mb, 1, ch)
q_star_ = functions.concat((q, r), axis=2) # q_star_: (mb, 1, ch*2)
self.q_star = functions.separate(q_star_)
return functions.reshape(q_star_, (mb, ch * 2))
示例10: evaluate_actions
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import matmul [as 别名]
def evaluate_actions(self, actions):
u_minus_mu = actions - self.mu
a = - 0.5 * \
F.matmul(F.matmul(
u_minus_mu[:, None, :], self.mat),
u_minus_mu[:, :, None])[:, 0, 0]
return a + F.reshape(self.v, (self.batch_size,))
示例11: batch_matmul
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import matmul [as 别名]
def batch_matmul(a, b, transa=False, transb=False):
return F.matmul(a[:, :, None], b, transa=transa, transb=transb)
示例12: compute_ctxt_demux
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import matmul [as 别名]
def compute_ctxt_demux(self, fb_concat, mask):
mb_size, nb_elems, Hi = fb_concat.data.shape
assert Hi == self.Hi
assert mb_size == 1
assert len(mask) == 0
precomputed_al_factor = F.reshape(self.al_lin_h(
F.reshape(fb_concat, (mb_size * nb_elems, self.Hi))), (mb_size, nb_elems, self.Ha))
# concatenated_mask = F.concat([F.reshape(mask_elem, (mb_size, 1)) for mask_elem in mask], 1)
def compute_ctxt(previous_state, prev_word_embedding=None):
current_mb_size = previous_state.data.shape[0]
al_factor = F.broadcast_to(precomputed_al_factor, (current_mb_size, nb_elems, self.Ha))
# used_fb_concat = F.broadcast_to(fb_concat, (current_mb_size, nb_elems, Hi))
# used_concatenated_mask = F.broadcast_to(concatenated_mask, (current_mb_size, nb_elems))
state_al_factor = self.al_lin_s(previous_state)
#As suggested by Isao Goto
if prev_word_embedding is not None:
state_al_factor = state_al_factor + self.al_lin_y(prev_word_embedding)
state_al_factor_bc = F.broadcast_to(F.reshape(state_al_factor, (current_mb_size, 1, self.Ha)), (current_mb_size, nb_elems, self.Ha))
a_coeffs = F.reshape(self.al_lin_o(F.reshape(F.tanh(state_al_factor_bc + al_factor),
(current_mb_size * nb_elems, self.Ha))), (current_mb_size, nb_elems))
# with cuda.get_device_from_array(used_concatenated_mask.data):
# a_coeffs = a_coeffs - 10000 * (1-used_concatenated_mask.data)
attn = F.softmax(a_coeffs)
# ci = F.reshape(F.batch_matmul(attn, used_fb_concat, transa = True), (current_mb_size, self.Hi))
ci = F.reshape(F.matmul(attn, F.reshape(fb_concat, (nb_elems, Hi))), (current_mb_size, self.Hi))
return ci, attn
return compute_ctxt
示例13: __call__
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import matmul [as 别名]
def __call__(self, prev_states, inpt, is_soft_inpt=False):
if is_soft_inpt:
prev_y = F.matmul(inpt, self.decoder_chain.emb.W)
else:
prev_y = self.decoder_chain.emb(inpt)
new_states, logits, attn = self.advance_one_step(prev_states, prev_y)
return new_states, logits, attn
示例14: forward
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import matmul [as 别名]
def forward(self, enc_hs, dec_z, att_prev):
c, w = self.l(enc_hs, dec_z, att_prev)
return F.matmul(c, w)
示例15: forward
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import matmul [as 别名]
def forward(self, x, y):
return F.matmul(x, y)
# ======================================