Python torch.tanh() Examples
The following are 30
code examples of torch.tanh().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
torch
, or try the search function
.
Example #1
Source File: graphML.py From graph-neural-networks with GNU General Public License v3.0 | 6 votes |
def __init__(self, F, H, K, nonlinearity = torch.tanh, E = 1, bias = True): # Initialize parent: super().__init__() # Store the values (using the notation in the paper): self.F = F # Input Features self.H = H # Hidden Features self.K = K # Filter taps self.E = E # Number of edge features self.S = None self.bias = bias # Boolean self.sigma = nonlinearity # torch.nn.functional # Create parameters: self.aWeights = nn.parameter.Parameter(torch.Tensor(H, E, K, F)) self.bWeights = nn.parameter.Parameter(torch.Tensor(H, E, K, H)) if self.bias: self.xBias = nn.parameter.Parameter(torch.Tensor(H, 1)) self.zBias = nn.parameter.Parameter(torch.Tensor(H, 1)) else: self.register_parameter('xBias', None) self.register_parameter('zBias', None) # Initialize parameters self.reset_parameters()
Example #2
Source File: set2set.py From LanczosNetwork with MIT License | 6 votes |
def forward(self, input_set): """ Args: input_set: shape N X D Returns: output_vec: shape 1 X 2D """ num_element = input_set.shape[0] element_dim = input_set.shape[1] assert element_dim == self.element_dim hidden = torch.zeros(1, 2 * self.element_dim).to(input_set.device) memory = torch.zeros(1, self.element_dim).to(input_set.device) for tt in range(self.num_step_encoder): hidden, memory = self.LSTM(hidden, memory) energy = torch.tanh(torch.mm(hidden, self.W_1) + input_set).mm(self.W_2) att_weight = F.softmax(energy, dim=0) read = (input_set * att_weight).sum(dim=0, keepdim=True) hidden = torch.cat([hidden, read], dim=1) return hidden
Example #3
Source File: set2set.py From LanczosNetwork with MIT License | 6 votes |
def forward(self, hidden, memory): """ Args: hidden: shape N X 2D memory: shape N X D Returns: hidden: shape N X D memory: shape N X D """ ft = self.forget_gate(hidden) it = self.input_gate(hidden) ot = self.output_gate(hidden) ct = self.memory_gate(hidden) memory = ft * memory + it * ct hidden = ot * torch.tanh(memory) return hidden, memory
Example #4
Source File: encoder.py From pytorch_sac_ae with MIT License | 6 votes |
def forward(self, obs, detach=False): h = self.forward_conv(obs) if detach: h = h.detach() h_fc = self.fc(h) self.outputs['fc'] = h_fc h_norm = self.ln(h_fc) self.outputs['ln'] = h_norm out = torch.tanh(h_norm) self.outputs['tanh'] = out return out
Example #5
Source File: rnn.py From prediction-flow with MIT License | 6 votes |
def forward(self, input, hx, att_score): """ References ---------- https://github.com/pytorch/pytorch/blob/v0.4.1/torch/nn/_functions/rnn.py#L49 """ gi = F.linear(input, self.weight_ih, self.bias_ih) gh = F.linear(hx, self.weight_hh, self.bias_hh) i_r, i_z, i_n = gi.chunk(3, 1) h_r, h_z, h_n = gh.chunk(3, 1) resetgate = torch.sigmoid(i_r + h_r) # updategate = torch.sigmoid(i_z + h_z) newgate = torch.tanh(i_n + resetgate * h_n) # hy = newgate + updategate * (hx - newgate) att_score = att_score.view(-1, 1) hy = (1. - att_score) * hx + att_score * newgate return hy
Example #6
Source File: rnn.py From prediction-flow with MIT License | 6 votes |
def forward(self, input, hx, att_score): """ References ---------- https://github.com/pytorch/pytorch/blob/v0.4.1/torch/nn/_functions/rnn.py#L49 """ gi = F.linear(input, self.weight_ih, self.bias_ih) gh = F.linear(hx, self.weight_hh, self.bias_hh) i_r, i_z, i_n = gi.chunk(3, 1) h_r, h_z, h_n = gh.chunk(3, 1) resetgate = torch.sigmoid(i_r + h_r) updategate = torch.sigmoid(i_z + h_z) newgate = torch.tanh(i_n + resetgate * h_n) updategate = att_score.view(-1, 1) * updategate hy = newgate + updategate * (hx - newgate) return hy
Example #7
Source File: models.py From IGMC with MIT License | 6 votes |
def forward(self, data): x, edge_index, batch = data.x, data.edge_index, data.batch if self.adj_dropout > 0: edge_index, edge_type = dropout_adj( edge_index, edge_type, p=self.adj_dropout, force_undirected=self.force_undirected, num_nodes=len(x), training=self.training ) concat_states = [] for conv in self.convs: x = torch.tanh(conv(x, edge_index)) concat_states.append(x) concat_states = torch.cat(concat_states, 1) x = global_add_pool(concat_states, batch) x = F.relu(self.lin1(x)) x = F.dropout(x, p=0.5, training=self.training) x = self.lin2(x) if self.regression: return x[:, 0] else: return F.log_softmax(x, dim=-1)
Example #8
Source File: sgcn.py From SGCN with GNU General Public License v3.0 | 6 votes |
def forward(self, positive_edges, negative_edges, target): """ Model forward propagation pass. Can fit deep and single layer SGCN models. :param positive_edges: Positive edges. :param negative_edges: Negative edges. :param target: Target vectors. :return loss: Loss value. :return self.z: Hidden vertex representations. """ self.h_pos, self.h_neg = [], [] self.h_pos.append(torch.tanh(self.positive_base_aggregator(self.X, positive_edges))) self.h_neg.append(torch.tanh(self.negative_base_aggregator(self.X, negative_edges))) for i in range(1, self.layers): self.h_pos.append(torch.tanh(self.positive_aggregators[i-1](self.h_pos[i-1], self.h_neg[i-1], positive_edges, negative_edges))) self.h_neg.append(torch.tanh(self.negative_aggregators[i-1](self.h_neg[i-1], self.h_pos[i-1], positive_edges, negative_edges))) self.z = torch.cat((self.h_pos[-1], self.h_neg[-1]), 1) loss = self.calculate_loss_function(self.z, positive_edges, negative_edges, target) return loss, self.z
Example #9
Source File: modules.py From BAMnet with Apache License 2.0 | 6 votes |
def forward(self, query_embed, in_memory_embed, atten_mask=None): if self.atten_type == 'simple': # simple attention attention = torch.bmm(in_memory_embed, query_embed.unsqueeze(2)).squeeze(2) elif self.atten_type == 'mul': # multiplicative attention attention = torch.bmm(in_memory_embed, torch.mm(query_embed, self.W).unsqueeze(2)).squeeze(2) elif self.atten_type == 'add': # additive attention attention = torch.tanh(torch.mm(in_memory_embed.view(-1, in_memory_embed.size(-1)), self.W2)\ .view(in_memory_embed.size(0), -1, self.W2.size(-1)) \ + torch.mm(query_embed, self.W).unsqueeze(1)) attention = torch.mm(attention.view(-1, attention.size(-1)), self.W3).view(attention.size(0), -1) else: raise RuntimeError('Unknown atten_type: {}'.format(self.atten_type)) if atten_mask is not None: # Exclude masked elements from the softmax attention = atten_mask * attention - (1 - atten_mask) * INF return attention
Example #10
Source File: module.py From End-to-end-ASR-Pytorch with MIT License | 6 votes |
def forward(self, q, k, v): bs_nh, ts, _ = k.shape bs = bs_nh//self.num_head # Uniformly init prev_att if self.prev_att is None: self.prev_att = torch.zeros((bs, self.num_head, ts)).to(k.device) for idx, sl in enumerate(self.k_len): self.prev_att[idx, :, :sl] = 1.0/sl # Calculate location context loc_context = torch.tanh(self.loc_proj(self.loc_conv( self.prev_att).transpose(1, 2))) # BxNxT->BxTxD loc_context = loc_context.unsqueeze(1).repeat( 1, self.num_head, 1, 1).view(-1, ts, self.dim) # BxNxTxD -> BNxTxD q = q.unsqueeze(1) # BNx1xD # Compute energy and context energy = self.gen_energy(torch.tanh( k+q+loc_context)).squeeze(2) # BNxTxD -> BNxT output, attn = self._attend(energy, v) attn = attn.view(bs, self.num_head, ts) # BNxT -> BxNxT self.prev_att = attn return output, attn
Example #11
Source File: DGCNN.py From gnn-comparison with GNU General Public License v3.0 | 6 votes |
def forward(self, data): # Implement Equation 4.2 of the paper i.e. concat all layers' graph representations and apply linear model # note: this can be decomposed in one smaller linear model per layer x, edge_index, batch = data.x, data.edge_index, data.batch hidden_repres = [] for conv in self.convs: x = torch.tanh(conv(x, edge_index)) hidden_repres.append(x) # apply sortpool x_to_sortpool = torch.cat(hidden_repres, dim=1) x_1d = global_sort_pool(x_to_sortpool, batch, self.k) # in the code the authors sort the last channel only # apply 1D convolutional layers x_1d = torch.unsqueeze(x_1d, dim=1) conv1d_res = F.relu(self.conv1d_params1(x_1d)) conv1d_res = self.maxpool1d(conv1d_res) conv1d_res = F.relu(self.conv1d_params2(conv1d_res)) conv1d_res = conv1d_res.reshape(conv1d_res.shape[0], -1) # apply dense layer out_dense = self.dense_layer(conv1d_res) return out_dense
Example #12
Source File: gcn_encoder.py From OpenChem with MIT License | 6 votes |
def forward(self, inp): x = inp[0] adj = inp[1] for i in range(self.n_layers): x = self.graph_convolutions[i](x, adj) x = torch.tanh(x) n = adj.size(1) d = x.size()[-1] adj_new = adj.unsqueeze(3) adj_new = adj_new.expand(-1, n, n, d) x_new = x.repeat(1, n, 1).view(-1, n, n, d) res = x_new*adj_new x = res.max(dim=2)[0] x = torch.tanh(self.dense(x)) x = torch.tanh(x.sum(dim=1)) return x
Example #13
Source File: seal_link_pred.py From pytorch_geometric with MIT License | 6 votes |
def forward(self, x, edge_index, batch): xs = [x] for conv in self.convs: xs += [torch.tanh(conv(xs[-1], edge_index))] x = torch.cat(xs[1:], dim=-1) # Global pooling. x = global_sort_pool(x, batch, self.k) x = x.unsqueeze(1) # [num_graphs, 1, k * hidden] x = F.relu(self.conv1(x)) x = self.maxpool1d(x) x = F.relu(self.conv2(x)) x = x.view(x.size(0), -1) # [num_graphs, dense_dim] # MLP. x = F.relu(self.lin1(x)) x = F.dropout(x, p=0.5, training=self.training) x = self.lin2(x) return x
Example #14
Source File: BiLSTM.py From pytorch_NER_BiLSTM_CNN_CRF with Apache License 2.0 | 6 votes |
def forward(self, word, sentence_length): """ :param word: :param sentence_length: :param desorted_indices: :return: """ word, sentence_length, desorted_indices = prepare_pack_padded_sequence(word, sentence_length, device=self.device) x = self.embed(word) # (N,W,D) x = self.dropout_embed(x) packed_embed = pack_padded_sequence(x, sentence_length, batch_first=True) x, _ = self.bilstm(packed_embed) x, _ = pad_packed_sequence(x, batch_first=True) x = x[desorted_indices] x = self.dropout(x) x = torch.tanh(x) logit = self.linear(x) return logit
Example #15
Source File: BiLSTM_CNN.py From pytorch_NER_BiLSTM_CNN_CRF with Apache License 2.0 | 6 votes |
def forward(self, word, char, sentence_length): """ :param char: :param word: :param sentence_length: :return: """ char_conv = self._char_forward(char) char_conv = self.dropout(char_conv) word = self.embed(word) # (N,W,D) x = torch.cat((word, char_conv), -1) x = self.dropout_embed(x) x, _ = self.bilstm(x) x = self.dropout(x) x = torch.tanh(x) logit = self.linear(x) return logit
Example #16
Source File: test_manifold_basic.py From geoopt with Apache License 2.0 | 6 votes |
def poincare_case(): torch.manual_seed(42) shape = manifold_shapes[geoopt.manifolds.PoincareBall] ex = torch.randn(*shape, dtype=torch.float64) / 3 ev = torch.randn(*shape, dtype=torch.float64) / 3 x = torch.tanh(torch.norm(ex)) * ex / torch.norm(ex) ex = x.clone() v = ev.clone() manifold = geoopt.PoincareBall().to(dtype=torch.float64) x = geoopt.ManifoldTensor(x, manifold=manifold) case = UnaryCase(shape, x, ex, v, ev, manifold) yield case manifold = geoopt.PoincareBallExact().to(dtype=torch.float64) x = geoopt.ManifoldTensor(x, manifold=manifold) case = UnaryCase(shape, x, ex, v, ev, manifold) yield case
Example #17
Source File: common.py From decaNLP with BSD 3-Clause "New" or "Revised" License | 6 votes |
def forward(self, context, question, context_padding, question_padding): context_padding = torch.cat([context.new_zeros((context.size(0), 1), dtype=torch.long)==1, context_padding], 1) question_padding = torch.cat([question.new_zeros((question.size(0), 1), dtype=torch.long)==1, question_padding], 1) context_sentinel = self.embed_sentinel(context.new_zeros((context.size(0), 1), dtype=torch.long)) context = torch.cat([context_sentinel, self.dropout(context)], 1) # batch_size x (context_length + 1) x features question_sentinel = self.embed_sentinel(question.new_ones((question.size(0), 1), dtype=torch.long)) question = torch.cat([question_sentinel, question], 1) # batch_size x (question_length + 1) x features question = torch.tanh(self.proj(question)) # batch_size x (question_length + 1) x features affinity = context.bmm(question.transpose(1,2)) # batch_size x (context_length + 1) x (question_length + 1) attn_over_context = self.normalize(affinity, context_padding) # batch_size x (context_length + 1) x 1 attn_over_question = self.normalize(affinity.transpose(1,2), question_padding) # batch_size x (question_length + 1) x 1 sum_of_context = self.attn(attn_over_context, context) # batch_size x (question_length + 1) x features sum_of_question = self.attn(attn_over_question, question) # batch_size x (context_length + 1) x features coattn_context = self.attn(attn_over_question, sum_of_context) # batch_size x (context_length + 1) x features coattn_question = self.attn(attn_over_context, sum_of_question) # batch_size x (question_length + 1) x features return torch.cat([coattn_context, sum_of_question], 2)[:, 1:], torch.cat([coattn_question, sum_of_context], 2)[:, 1:]
Example #18
Source File: modules.py From waveglow with Apache License 2.0 | 6 votes |
def forward(self, sample, local_condition): """ Args: sample: Shape: [batch_size, channels, time]. local_condition: Shape: [batch_size, channels, time]. """ sample_filter_gate = self.conv_filter_gate(sample) if self.local_condition_channels is not None: lc_filter_gate = self.conv_lc_filter_gate(local_condition) sample_filter_gate += lc_filter_gate sample_filter, sample_gate = torch.split( sample_filter_gate, self.dilation_channels, 1) gated_sample_batch = torch.tanh(sample_filter) * torch.sigmoid(sample_gate) # The 1x1 conv to produce the residual output transformed = self.conv_dense(gated_sample_batch) residual_output = transformed + sample # The 1x1 conv to produce the skip output skip_output = self.conv_skip(gated_sample_batch) return residual_output, skip_output
Example #19
Source File: nonlinearities.py From nsf with MIT License | 6 votes |
def forward(self, inputs, context=None): mask_right = (inputs > self.cut_point) mask_left = (inputs < -self.cut_point) mask_middle = ~(mask_right | mask_left) outputs = torch.zeros_like(inputs) outputs[mask_middle] = torch.tanh(inputs[mask_middle]) outputs[mask_right] = self.alpha * torch.log(self.beta * inputs[mask_right]) outputs[mask_left] = self.alpha * -torch.log(-self.beta * inputs[mask_left]) logabsdet = torch.zeros_like(inputs) logabsdet[mask_middle] = torch.log(1 - outputs[mask_middle] ** 2) logabsdet[mask_right] = torch.log(self.alpha / inputs[mask_right]) logabsdet[mask_left] = torch.log(-self.alpha / inputs[mask_left]) logabsdet = utils.sum_except_batch(logabsdet, num_batch_dims=1) return outputs, logabsdet
Example #20
Source File: edge_pool.py From pytorch_geometric with MIT License | 5 votes |
def compute_edge_score_tanh(raw_edge_score, edge_index, num_nodes): return torch.tanh(raw_edge_score)
Example #21
Source File: SAC.py From Deep-reinforcement-learning-with-pytorch with MIT License | 5 votes |
def select_action(self, state): state = torch.FloatTensor(state).to(device) mu, log_sigma = self.policy_net(state) sigma = torch.exp(log_sigma) dist = Normal(mu, sigma) z = dist.sample() action = torch.tanh(z).detach().cpu().numpy() return action.item() # return a scalar, float32
Example #22
Source File: SAC.py From Deep-reinforcement-learning-with-pytorch with MIT License | 5 votes |
def get_action_log_prob(self, state): batch_mu, batch_log_sigma = self.policy_net(state) batch_sigma = torch.exp(batch_log_sigma) dist = Normal(batch_mu, batch_sigma) z = dist.sample() action = torch.tanh(z) log_prob = dist.log_prob(z) - torch.log(1 - action.pow(2) + min_Val) return action, log_prob, z, batch_mu, batch_log_sigma
Example #23
Source File: DDPG.py From Deep-reinforcement-learning-with-pytorch with MIT License | 5 votes |
def forward(self, x): x = F.relu(self.l1(x)) x = F.relu(self.l2(x)) x = self.max_action * torch.tanh(self.l3(x)) return x
Example #24
Source File: SAC_dual_Q_net.py From Deep-reinforcement-learning-with-pytorch with MIT License | 5 votes |
def select_action(self, state): state = torch.FloatTensor(state).to(device) mu, log_sigma = self.policy_net(state) sigma = torch.exp(log_sigma) dist = Normal(mu, sigma) z = dist.sample() action = torch.tanh(z).detach().cpu().numpy() return action.item() # return a scalar, float32
Example #25
Source File: TD3.py From Deep-reinforcement-learning-with-pytorch with MIT License | 5 votes |
def forward(self, state): a = F.relu(self.fc1(state)) a = F.relu(self.fc2(a)) a = torch.tanh(self.fc3(a)) * self.max_action return a
Example #26
Source File: fc.py From speaksee with BSD 3-Clause "New" or "Revised" License | 5 votes |
def forward(self, xt, state): all_input_sums = self.i2h(xt) + self.h2h(state[0]) sigmoid_chunk = torch.sigmoid(all_input_sums[:, :3 * self.hidden_size]) it, ft, ot = sigmoid_chunk.split(self.hidden_size, 1) maxout = torch.max(all_input_sums[:, 3 * self.hidden_size:4 * self.hidden_size], all_input_sums[:, 4 * self.hidden_size:]) ct = it * maxout + ft * state[1] ht = ot * torch.tanh(ct) ht = self.dropout(ht) output = ht state = (ht, ct) return output, state
Example #27
Source File: absa_layer.py From BERT-E2E-ABSA with Apache License 2.0 | 5 votes |
def forward(self, x): """ :param x: input tensor, shape: (batch_size, seq_len, input_size) :return: """ def recurrence(xt, htm1): """ :param xt: current input :param htm1: previous hidden state :return: """ gates_rz = torch.sigmoid(self.LNx1(self.Wxrz(xt)) + self.LNh1(self.Whrz(htm1))) rt, zt = gates_rz.chunk(2, 1) nt = torch.tanh(self.LNx2(self.Wxn(xt))+rt*self.LNh2(self.Whn(htm1))) ht = (1.0-zt) * nt + zt * htm1 return ht steps = range(x.size(1)) bs = x.size(0) hidden = self.init_hidden(bs) # shape: (seq_len, bsz, input_size) input = x.transpose(0, 1) output = [] for t in steps: hidden = recurrence(input[t], hidden) output.append(hidden) # shape: (bsz, seq_len, input_size) output = torch.stack(output, 0).transpose(0, 1) if self.bidirectional: output_b = [] hidden_b = self.init_hidden(bs) for t in steps[::-1]: hidden_b = recurrence(input[t], hidden_b) output_b.append(hidden_b) output_b = output_b[::-1] output_b = torch.stack(output_b, 0).transpose(0, 1) output = torch.cat([output, output_b], dim=-1) return output, None
Example #28
Source File: modeling.py From BERT-for-Chinese-Question-Answering with Apache License 2.0 | 5 votes |
def gelu(x): """Implementation of the gelu activation function. For information: OpenAI GPT's gelu is slightly different (and gives slightly different results): 0.5 * x * (1 + torch.tanh(math.sqrt(2 / math.pi) * (x + 0.044715 * torch.pow(x, 3)))) """ return x * 0.5 * (1.0 + torch.erf(x / math.sqrt(2.0)))
Example #29
Source File: common.py From decaNLP with BSD 3-Clause "New" or "Revised" License | 5 votes |
def forward(self, input, context): if not self.dot: targetT = self.linear_in(input).unsqueeze(2) # batch x dim x 1 else: targetT = input.unsqueeze(2) context_scores = torch.bmm(context, targetT).squeeze(2) context_scores.masked_fill_(self.context_mask, -float('inf')) context_attention = F.softmax(context_scores, dim=-1) + EPSILON context_alignment = torch.bmm(context_attention.unsqueeze(1), context).squeeze(1) combined_representation = torch.cat([input, context_alignment], 1) output = self.tanh(self.linear_out(combined_representation)) return output, context_attention, context_alignment
Example #30
Source File: common.py From decaNLP with BSD 3-Clause "New" or "Revised" License | 5 votes |
def __init__(self, dim, dot=False): super().__init__() self.linear_in = nn.Linear(dim, dim, bias=False) self.linear_out = nn.Linear(2 * dim, dim, bias=False) self.tanh = nn.Tanh() self.mask = None self.dot = dot