本文整理汇总了Python中torch.nn.functional.tanh方法的典型用法代码示例。如果您正苦于以下问题:Python functional.tanh方法的具体用法?Python functional.tanh怎么用?Python functional.tanh使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类torch.nn.functional
的用法示例。
在下文中一共展示了functional.tanh方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: node_forward
# 需要导入模块: from torch.nn import functional [as 别名]
# 或者: from torch.nn.functional import tanh [as 别名]
def node_forward(self, inputs, child_c, child_h):
child_h_sum = torch.sum(child_h, dim=0, keepdim=True)
iou = self.ioux(inputs) + self.iouh(child_h_sum)
i, o, u = torch.split(iou, iou.size(1) // 3, dim=1)
i, o, u = F.sigmoid(i), F.sigmoid(o), F.tanh(u)
f = F.sigmoid(
self.fh(child_h) +
self.fx(inputs).repeat(len(child_h), 1)
)
fc = torch.mul(f, child_c)
c = torch.mul(i, u) + torch.sum(fc, dim=0, keepdim=True)
h = torch.mul(o, F.tanh(c))
return c, h
示例2: forward
# 需要导入模块: from torch.nn import functional [as 别名]
# 或者: from torch.nn.functional import tanh [as 别名]
def forward(self, z_enc_out, u_enc_out, u_input_np, m_t_input, degree_input, last_hidden, z_input_np):
sparse_z_input = Variable(self.get_sparse_selective_input(z_input_np), requires_grad=False)
m_embed = self.emb(m_t_input)
z_context = self.attn_z(last_hidden, z_enc_out)
u_context = self.attn_u(last_hidden, u_enc_out)
gru_in = torch.cat([m_embed, u_context, z_context, degree_input.unsqueeze(0)], dim=2)
gru_out, last_hidden = self.gru(gru_in, last_hidden)
gen_score = self.proj(torch.cat([z_context, u_context, gru_out], 2)).squeeze(0)
z_copy_score = F.tanh(self.proj_copy2(z_enc_out.transpose(0, 1)))
z_copy_score = torch.matmul(z_copy_score, gru_out.squeeze(0).unsqueeze(2)).squeeze(2)
z_copy_score = z_copy_score.cpu()
z_copy_score_max = torch.max(z_copy_score, dim=1, keepdim=True)[0]
z_copy_score = torch.exp(z_copy_score - z_copy_score_max) # [B,T]
z_copy_score = torch.log(torch.bmm(z_copy_score.unsqueeze(1), sparse_z_input)).squeeze(
1) + z_copy_score_max # [B,V]
z_copy_score = cuda_(z_copy_score)
scores = F.softmax(torch.cat([gen_score, z_copy_score], dim=1), dim=1)
gen_score, z_copy_score = scores[:, :cfg.vocab_size], \
scores[:, cfg.vocab_size:]
proba = gen_score + z_copy_score[:, :cfg.vocab_size] # [B,V]
proba = torch.cat([proba, z_copy_score[:, cfg.vocab_size:]], 1)
return proba, last_hidden, gru_out
示例3: pointer
# 需要导入模块: from torch.nn import functional [as 别名]
# 或者: from torch.nn.functional import tanh [as 别名]
def pointer(self, x, state, x_mask):
x_ = torch.cat([x, state.unsqueeze(1).repeat(1,x.size(1),1)], 2)
s0 = F.tanh(self.linear(x_))
s = self.weights(s0).view(x.size(0), x.size(1))
s.data.masked_fill_(x_mask.data, -float('inf'))
a = F.softmax(s)
res = a.unsqueeze(1).bmm(x).squeeze(1)
if self.normalize:
if self.training:
# In training we output log-softmax for NLL
scores = F.log_softmax(s)
else:
# ...Otherwise 0-1 probabilities
scores = F.softmax(s)
else:
scores = a.exp()
return res, scores
示例4: calc_score
# 需要导入模块: from torch.nn import functional [as 别名]
# 或者: from torch.nn.functional import tanh [as 别名]
def calc_score(self, att_query, att_keys):
"""
att_query is: b x t_q x n
att_keys is b x t_k x n
return b x t_q x t_k scores
"""
b, t_k, n = list(att_keys.size())
t_q = att_query.size(1)
if self.mode == 'bahdanau':
att_query = att_query.unsqueeze(2).expand(b, t_q, t_k, n)
att_keys = att_keys.unsqueeze(1).expand(b, t_q, t_k, n)
sum_qk = att_query + att_keys
sum_qk = sum_qk.view(b * t_k * t_q, n)
out = self.linear_att(F.tanh(sum_qk)).view(b, t_q, t_k)
elif self.mode == 'dot_prod':
out = torch.bmm(att_query, att_keys.transpose(1, 2))
if hasattr(self, 'scale'):
out = out * self.scale
return out
开发者ID:nadavbh12,项目名称:Character-Level-Language-Modeling-with-Deeper-Self-Attention-pytorch,代码行数:22,代码来源:attention.py
示例5: forward
# 需要导入模块: from torch.nn import functional [as 别名]
# 或者: from torch.nn.functional import tanh [as 别名]
def forward(self, input_val, prev_stack):
batch_size = prev_stack.size(0)
controls = self.stack_controls_layer(input_val.squeeze(0))
controls = F.softmax(controls, dim=1)
controls = controls.view(-1, 3, 1, 1)
stack_input = self.stack_input_layer(input_val)
stack_input = F.tanh(stack_input)
stack_input = stack_input.permute(1, 0, 2)
zeros_at_the_bottom = torch.zeros(batch_size, 1, self.stack_width)
if self.use_cuda:
zeros_at_the_bottom = torch.tensor(zeros_at_the_bottom.cuda(),
requires_grad=True)
else:
zeros_at_the_bottom = torch.tensor(zeros_at_the_bottom,
requires_grad=True)
a_push, a_pop, a_no_op = controls[:, 0], controls[:, 1], controls[:, 2]
stack_down = torch.cat((prev_stack[:, 1:], zeros_at_the_bottom), dim=1)
stack_up = torch.cat((stack_input, prev_stack[:, :-1]), dim=1)
new_stack = a_no_op * prev_stack + a_push * stack_up + \
a_pop * stack_down
return new_stack
示例6: forward
# 需要导入模块: from torch.nn import functional [as 别名]
# 或者: from torch.nn.functional import tanh [as 别名]
def forward(self, h, adj):
bs, n = h.size()[:2] # h is of size bs x n x f_in
h_prime = torch.matmul(h.unsqueeze(1), self.w) # bs x n_head x n x f_out
attn_src = torch.matmul(F.tanh(h_prime), self.a_src) # bs x n_head x n x 1
attn_dst = torch.matmul(F.tanh(h_prime), self.a_dst) # bs x n_head x n x 1
attn = attn_src.expand(-1, -1, -1, n) + attn_dst.expand(-1, -1, -1, n).permute(0, 1, 3, 2) # bs x n_head x n x n
attn = self.leaky_relu(attn)
mask = 1 - adj.unsqueeze(1) # bs x 1 x n x n
attn.data.masked_fill_(mask, float("-inf"))
attn = self.softmax(attn) # bs x n_head x n x n
attn = self.dropout(attn)
output = torch.matmul(attn, h_prime) # bs x n_head x n x f_out
if self.bias is not None:
return output + self.bias
else:
return output
示例7: forward
# 需要导入模块: from torch.nn import functional [as 别名]
# 或者: from torch.nn.functional import tanh [as 别名]
def forward(self, dec_state, enc_states, mask, dag=None):
"""
:param dec_state:
decoder hidden state of size batch_size x dec_dim
:param enc_states:
all encoder hidden states of size batch_size x max_enc_steps x enc_dim
:param flengths:
encoder video frame lengths of size batch_size
"""
dec_contrib = self.decoder_in(dec_state)
batch_size, max_enc_steps, _ = enc_states.size()
enc_contrib = self.encoder_in(enc_states.contiguous().view(-1, self.enc_dim)).contiguous().view(batch_size, max_enc_steps, self.attn_dim)
pre_attn = F.tanh(enc_contrib + dec_contrib.unsqueeze(1).expand_as(enc_contrib))
energy = self.attn_linear(pre_attn.view(-1, self.attn_dim)).view(batch_size, max_enc_steps)
alpha = F.softmax(energy, 1)
# mask alpha and renormalize it
alpha = alpha* mask
alpha = torch.div(alpha, alpha.sum(1).unsqueeze(1).expand_as(alpha))
context_vector = torch.bmm(alpha.unsqueeze(1), enc_states).squeeze(1) # (batch_size, enc_dim)
return context_vector, alpha
示例8: forward
# 需要导入模块: from torch.nn import functional [as 别名]
# 或者: from torch.nn.functional import tanh [as 别名]
def forward(self, input, source_hids, encoder_padding_mask):
# input: bsz x input_embed_dim
# source_hids: srclen x bsz x output_embed_dim
# x: bsz x output_embed_dim
x = self.input_proj(input)
# compute attention
attn_scores = (source_hids * x.unsqueeze(0)).sum(dim=2)
# don't attend over padding
if encoder_padding_mask is not None:
attn_scores = attn_scores.float().masked_fill_(
encoder_padding_mask,
float('-inf')
).type_as(attn_scores) # FP16 support: cast to float and back
attn_scores = F.softmax(attn_scores, dim=0) # srclen x bsz
# sum weighted sources
x = (attn_scores.unsqueeze(2) * source_hids).sum(dim=0)
x = F.tanh(self.output_proj(torch.cat((x, input), dim=1)))
return x, attn_scores
示例9: _transform_leafs
# 需要导入模块: from torch.nn import functional [as 别名]
# 或者: from torch.nn.functional import tanh [as 别名]
def _transform_leafs(self, x, mask):
if self.leaf_transformation == BinaryTreeBasedModule.no_transformation:
pass
elif self.leaf_transformation == BinaryTreeBasedModule.lstm_transformation:
x = self.lstm(x, mask)
elif self.leaf_transformation == BinaryTreeBasedModule.bi_lstm_transformation:
h_f = self.lstm_f(x, mask)
h_b = self.lstm_b(x, mask, backward=True)
x = torch.cat([h_f, h_b], dim=-1)
elif self.leaf_transformation == BinaryTreeBasedModule.conv_transformation:
x = x.permute(0, 2, 1)
x = self.conv1(x)
x = F.relu(x)
x = self.conv2(x)
x = F.tanh(x)
x = x.permute(0, 2, 1)
# tanh is applied to make sure that leafs and other nodes are in the same range
return self.linear(x).tanh().chunk(chunks=2, dim=-1)
示例10: forward
# 需要导入模块: from torch.nn import functional [as 别名]
# 或者: from torch.nn.functional import tanh [as 别名]
def forward(self, x):
if self.activation == 'relu':
if self.batch_norm:
x = F.relu(self.bn1(self.fc1(x)))
x = F.relu(self.bn2(self.fc2(x)))
x = F.relu(self.bn3(self.fc3(x)))
else:
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = F.relu(self.fc3(x))
elif self.activation == 'tanh':
if self.batch_norm:
x = F.tanh(self.bn1(self.fc1(x)))
x = F.tanh(self.bn2(self.fc2(x)))
x = F.tanh(self.bn3(self.fc3(x)))
else:
x = F.tanh(self.fc1(x))
x = F.tanh(self.fc2(x))
x = F.tanh(self.fc3(x))
return self.fc4(x)
示例11: LSTMCell
# 需要导入模块: from torch.nn import functional [as 别名]
# 或者: from torch.nn.functional import tanh [as 别名]
def LSTMCell(input, hidden, w_ih, w_hh, b_ih=None, b_hh=None):
"""
A modified LSTM cell with hard sigmoid activation on the input, forget and output gates.
"""
hx, cx = hidden
gates = F.linear(input, w_ih, b_ih) + F.linear(hx, w_hh, b_hh)
ingate, forgetgate, cellgate, outgate = gates.chunk(4, 1)
ingate = hard_sigmoid(ingate)
forgetgate = hard_sigmoid(forgetgate)
cellgate = F.tanh(cellgate)
outgate = hard_sigmoid(outgate)
cy = (forgetgate * cx) + (ingate * cellgate)
hy = outgate * F.tanh(cy)
return hy, cy
示例12: __init__
# 需要导入模块: from torch.nn import functional [as 别名]
# 或者: from torch.nn.functional import tanh [as 别名]
def __init__(self, dim_in, dim_out, dim_hidden, layers_hidden, act='tanh',
xavier_init=True):
super(CPPN, self).__init__()
self.add_module('fc0', nn.Linear(dim_in, dim_hidden, bias=None))
self.add_module('act0', nn.Tanh())
for i in range(1, layers_hidden):
self.add_module('fc{}'.format(i), nn.Linear(dim_hidden, dim_hidden, bias=True))
if act == 'tanh':
self.add_module('act{}'.format(i), nn.Tanh())
elif act == 'relu':
self.add_module('act{}'.format(i), nn.ReLU())
else:
raise ValueError(f'unknown activation function: {act}')
self.add_module('fc{}'.format(layers_hidden), nn.Linear(dim_hidden, dim_out))
if xavier_init:
self.init_xavier()
示例13: train_layer
# 需要导入模块: from torch.nn import functional [as 别名]
# 或者: from torch.nn.functional import tanh [as 别名]
def train_layer(self, h, t):
""" Defines the forward pass training layers of the algorithm.
Args:
h (Tensor): Head entities ids.
t (Tensor): Tail entity ids of the triple.
"""
mr1h = torch.matmul(h, self.mr1.weight) # h => [m, self.ent_hidden_size], self.mr1 => [self.ent_hidden_size, self.rel_hidden_size]
mr2t = torch.matmul(t, self.mr2.weight) # t => [m, self.ent_hidden_size], self.mr2 => [self.ent_hidden_size, self.rel_hidden_size]
expanded_h = h.unsqueeze(dim=0).repeat(self.rel_hidden_size, 1, 1) # [self.rel_hidden_size, m, self.ent_hidden_size]
expanded_t = t.unsqueeze(dim=-1) # [m, self.ent_hidden_size, 1]
temp = (torch.matmul(expanded_h, self.mr.weight.view(self.rel_hidden_size, self.ent_hidden_size, self.ent_hidden_size))).permute(1, 0, 2) # [m, self.rel_hidden_size, self.ent_hidden_size]
htmrt = torch.squeeze(torch.matmul(temp, expanded_t), dim=-1) # [m, self.rel_hidden_size]
return F.tanh(htmrt + mr1h + mr2t + self.br.weight)
示例14: forward
# 需要导入模块: from torch.nn import functional [as 别名]
# 或者: from torch.nn.functional import tanh [as 别名]
def forward(self, input, class_id):
codes = torch.split(input, 20, 1)
class_emb = self.linear(class_id) # 128
out = self.G_linear(codes[0])
# out = out.view(-1, 1536, 4, 4)
out = out.view(-1, self.first_view, 4, 4)
ids = 1
for i, conv in enumerate(self.conv):
if isinstance(conv, GBlock):
conv_code = codes[ids]
ids = ids+1
condition = torch.cat([conv_code, class_emb], 1)
# print('condition',condition.size()) #torch.Size([4, 148])
out = conv(out, condition)
else:
out = conv(out)
out = self.ScaledCrossReplicaBN(out)
out = F.relu(out)
out = self.colorize(out)
return F.tanh(out)
示例15: forward
# 需要导入模块: from torch.nn import functional [as 别名]
# 或者: from torch.nn.functional import tanh [as 别名]
def forward(self, input, class_id):
out = self.lin_code(input)
out = out.view(-1, 512, 4, 4)
for conv in self.conv:
if isinstance(conv, ConvBlock):
out = conv(out, class_id)
else:
out = conv(out)
out = self.bn(out)
out = F.relu(out)
out = self.colorize(out)
return F.tanh(out)