Python torch.nn.functional.linear() Examples
The following are 30
code examples of torch.nn.functional.linear().
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.nn.functional
, or try the search function
.
Example #1
Source File: stylegan2.py From StyleGAN2_PyTorch with MIT License | 6 votes |
def forward(self, x): # Pass Add bias. x += self.bias # Evaluate activation function. if self.act == "linear": pass elif self.act == 'lrelu': x = F.leaky_relu(x, self.alpha, inplace=True) x = x * np.sqrt(2) # original repo def_gain=np.sqrt(2). # Scale by gain. if self.gain != 1: x = x * self.gain return x
Example #2
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 #3
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 #4
Source File: layers.py From variance-networks with Apache License 2.0 | 6 votes |
def forward(self, x): if self.zero_mean: lrt_mean = 0.0 else: lrt_mean = F.linear(x, self.W) if self.bias is not None: lrt_mean = lrt_mean + self.bias sigma2 = Variable.exp(self.log_alpha) * self.W * self.W if self.permute_sigma: sigma2 = sigma2.view(-1)[torch.randperm(self.in_features * self.out_features).cuda()].view(self.out_features, self.in_features) lrt_std = Variable.sqrt(1e-16 + F.linear(x * x, sigma2)) if self.training: eps = Variable(lrt_std.data.new(lrt_std.size()).normal_()) else: eps = 0.0 return lrt_mean + lrt_std * eps
Example #5
Source File: vae.py From scVI with MIT License | 6 votes |
def get_loadings(self) -> np.ndarray: """Extract per-gene weights (for each Z, shape is genes by dim(Z)) in the linear decoder.""" # This is BW, where B is diag(b) batch norm, W is weight matrix if self.use_batch_norm is True: w = self.decoder.factor_regressor.fc_layers[0][0].weight bn = self.decoder.factor_regressor.fc_layers[0][1] sigma = torch.sqrt(bn.running_var + bn.eps) gamma = bn.weight b = gamma / sigma bI = torch.diag(b) loadings = torch.matmul(bI, w) else: loadings = self.decoder.factor_regressor.fc_layers[0][0].weight loadings = loadings.detach().cpu().numpy() if self.n_batch > 1: loadings = loadings[:, : -self.n_batch] return loadings
Example #6
Source File: identifier.py From kaggle-humpback with BSD 2-Clause "Simplified" License | 6 votes |
def forward(self, inputs, labels): cos_th = F.linear(inputs, F.normalize(self.weight)) cos_th = cos_th.clamp(-1, 1) sin_th = torch.sqrt(1.0 - torch.pow(cos_th, 2)) cos_th_m = cos_th * self.cos_m - sin_th * self.sin_m cos_th_m = torch.where(cos_th > self.th, cos_th_m, cos_th - self.mm) cond_v = cos_th - self.th cond = cond_v <= 0 cos_th_m[cond] = (cos_th - self.mm)[cond] if labels.dim() == 1: labels = labels.unsqueeze(-1) onehot = torch.zeros(cos_th.size()).cuda() onehot.scatter_(1, labels, 1) outputs = onehot * cos_th_m + (1.0 - onehot) * cos_th outputs = outputs * self.s return outputs
Example #7
Source File: stylegan2.py From StyleGAN2_PyTorch with MIT License | 6 votes |
def forward(self, x): if self.bias is not None and self.mode != 'modulate': out = F.linear(x, self.weight * self.w_lrmul, self.bias * self.b_lrmul) elif self.bias is not None and self.mode == 'modulate': # original # out = F.linear(x, self.weight * self.w_lrmul, self.bias * self.b_lrmul) + 1 # re-implement out = F.linear(x, self.weight * self.w_lrmul, self.bias * self.b_lrmul) else: out = F.linear(x, self.weight * self.w_lrmul) if self.act == 'lrelu': out = F.leaky_relu(out, 0.2, inplace=True) out = out * np.sqrt(2) # original repo def_gain=np.sqrt(2). return out elif self.act == 'linear': return out return out
Example #8
Source File: linear.py From nsf with MIT License | 6 votes |
def inverse_no_cache(self, inputs): """Cost: output = O(D^3 + D^2N) logabsdet = O(D^3) where: D = num of features N = num of inputs """ batch_size = inputs.shape[0] outputs = inputs - self.bias outputs, lu = torch.gesv(outputs.t(), self._weight) # Linear-system solver. outputs = outputs.t() # The linear-system solver returns the LU decomposition of the weights, which we # can use to obtain the log absolute determinant directly. logabsdet = -torch.sum(torch.log(torch.abs(torch.diag(lu)))) logabsdet = logabsdet * torch.ones(batch_size) return outputs, logabsdet
Example #9
Source File: lstm_hard_sigmoid.py From SemEval2019Task3 with MIT License | 6 votes |
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
Example #10
Source File: CustomLayers.py From BMSG-GAN with MIT License | 6 votes |
def forward(self, x): """ forward pass of the FinalBlock :param x: input :return: y => output """ # minibatch_std_dev layer y = self.batch_discriminator(x) # define the computations y = self.lrelu(self.conv_1(y)) y = self.lrelu(self.conv_2(y)) # fully connected layer y = self.conv_3(y) # This layer has linear activation # flatten the output raw discriminator scores return y.view(-1)
Example #11
Source File: CustomLayers.py From BMSG-GAN with MIT License | 6 votes |
def forward(self, x): """ forward pass of the layer :param x: input :return: y => output """ from torch.nn.functional import linear return linear(x, self.weight * self.scale, self.bias if self.use_bias else None) # ----------------------------------------------------------------------------------- # Pixelwise feature vector normalization. # reference: # https://github.com/tkarras/progressive_growing_of_gans/blob/master/networks.py#L120 # -----------------------------------------------------------------------------------
Example #12
Source File: mnist_with_visdom.py From tnt with BSD 3-Clause "New" or "Revised" License | 5 votes |
def f(params, inputs, mode): o = inputs.view(inputs.size(0), 1, 28, 28) o = F.conv2d(o, params['conv0.weight'], params['conv0.bias'], stride=2) o = F.relu(o) o = F.conv2d(o, params['conv1.weight'], params['conv1.bias'], stride=2) o = F.relu(o) o = o.view(o.size(0), -1) o = F.linear(o, params['linear2.weight'], params['linear2.bias']) o = F.relu(o) o = F.linear(o, params['linear3.weight'], params['linear3.bias']) return o
Example #13
Source File: model.py From fairseq with MIT License | 5 votes |
def forward(self, features, masked_tokens=None, **kwargs): # Only project the masked tokens while training, # saves both memory and computation if masked_tokens is not None: features = features[masked_tokens, :] x = self.dense(features) x = self.activation_fn(x) x = self.layer_norm(x) # project back to size of vocabulary with bias x = F.linear(x, self.weight) + self.bias return x
Example #14
Source File: SpectralNormLayer.py From cortex with BSD 3-Clause "New" or "Revised" License | 5 votes |
def forward(self, input): w_sn, self.u = sn_weight( self.weight, self.u, self.height, self.n_power_iterations) return F.linear(input, w_sn, self.bias)
Example #15
Source File: mnist.py From tnt with BSD 3-Clause "New" or "Revised" License | 5 votes |
def f(params, inputs, mode): o = inputs.view(inputs.size(0), 1, 28, 28) o = F.conv2d(o, params['conv0.weight'], params['conv0.bias'], stride=2) o = F.relu(o) o = F.conv2d(o, params['conv1.weight'], params['conv1.bias'], stride=2) o = F.relu(o) o = o.view(o.size(0), -1) o = F.linear(o, params['linear2.weight'], params['linear2.bias']) o = F.relu(o) o = F.linear(o, params['linear3.weight'], params['linear3.bias']) return o
Example #16
Source File: transformer.py From fairseq with MIT License | 5 votes |
def output_layer(self, features, **kwargs): """Project features to the vocabulary size.""" features = copy_to_model_parallel_region(features) # project back to size of vocabulary if self.share_input_output_embed: x = F.linear(features, self.embed_tokens.weight) else: x = F.linear(features, self.embed_out) if getattr(self.args, 'criterion') != 'vocab_parallel_cross_entropy': x = gather_from_model_parallel_region(x).contiguous() return x
Example #17
Source File: levenshtein_transformer.py From fairseq with MIT License | 5 votes |
def forward_word_del(self, normalize, encoder_out, prev_output_tokens, **unused): features, extra = self.extract_features( prev_output_tokens, encoder_out=encoder_out, early_exit=self.early_exit[0], layers=self.layers_del, **unused ) decoder_out = F.linear(features, self.embed_word_del.weight) if normalize: return F.log_softmax(decoder_out, -1), extra['attn'] return decoder_out, extra['attn']
Example #18
Source File: weight_norm.py From ITDD with MIT License | 5 votes |
def forward(self, x, init=False): if init is True: # out_features * in_features self.V.data.copy_(torch.randn(self.V.data.size()).type_as( self.V.data) * 0.05) # norm is out_features * 1 v_norm = self.V.data / \ self.V.data.norm(2, 1).expand_as(self.V.data) # batch_size * out_features x_init = F.linear(x, v_norm).data # out_features m_init, v_init = x_init.mean(0).squeeze( 0), x_init.var(0).squeeze(0) # out_features scale_init = self.init_scale / \ torch.sqrt(v_init + 1e-10) self.g.data.copy_(scale_init) self.b.data.copy_(-m_init * scale_init) x_init = scale_init.view(1, -1).expand_as(x_init) \ * (x_init - m_init.view(1, -1).expand_as(x_init)) self.V_avg.copy_(self.V.data) self.g_avg.copy_(self.g.data) self.b_avg.copy_(self.b.data) return x_init else: v, g, b = get_vars_maybe_avg(self, ['V', 'g', 'b'], self.training, polyak_decay=self.polyak_decay) # batch_size * out_features x = F.linear(x, v) scalar = g / torch.norm(v, 2, 1).squeeze(1) x = scalar.view(1, -1).expand_as(x) * x + \ b.view(1, -1).expand_as(x) return x
Example #19
Source File: mnist_with_meterlogger.py From tnt with BSD 3-Clause "New" or "Revised" License | 5 votes |
def f(params, inputs, mode): o = inputs.view(inputs.size(0), 1, 28, 28) o = F.conv2d(o, params['conv0.weight'], params['conv0.bias'], stride=2) o = F.relu(o) o = F.conv2d(o, params['conv1.weight'], params['conv1.bias'], stride=2) o = F.relu(o) o = o.view(o.size(0), -1) o = F.linear(o, params['linear2.weight'], params['linear2.bias']) o = F.relu(o) o = F.linear(o, params['linear3.weight'], params['linear3.bias']) return o
Example #20
Source File: levenshtein_transformer.py From fairseq with MIT License | 5 votes |
def forward_mask_ins(self, normalize, encoder_out, prev_output_tokens, **unused): features, extra = self.extract_features( prev_output_tokens, encoder_out=encoder_out, early_exit=self.early_exit[1], layers=self.layers_msk, **unused ) features_cat = torch.cat([features[:, :-1, :], features[:, 1:, :]], 2) decoder_out = F.linear(features_cat, self.embed_mask_ins.weight) if normalize: return F.log_softmax(decoder_out, -1), extra['attn'] return decoder_out, extra['attn']
Example #21
Source File: nonautoregressive_transformer.py From fairseq with MIT License | 5 votes |
def forward_length(self, normalize, encoder_out): enc_feats = encoder_out.encoder_out # T x B x C src_masks = encoder_out.encoder_padding_mask # B x T or None enc_feats = _mean_pooling(enc_feats, src_masks) if self.sg_length_pred: enc_feats = enc_feats.detach() length_out = F.linear(enc_feats, self.embed_length.weight) return F.log_softmax(length_out, -1) if normalize else length_out
Example #22
Source File: stylegan2.py From StyleGAN2_PyTorch with MIT License | 5 votes |
def forward(self, x): if self.bias is not None: out = F.conv2d(x, self.weight * self.w_lrmul, self.bias * self.b_lrmul, padding=self.kernel_size // 2) else: out = F.conv2d(x, self.weight * self.w_lrmul, padding=self.kernel_size // 2) if self.act == 'lrelu': out = F.leaky_relu(out, 0.2, inplace=True) out = out * np.sqrt(2) # original repo def_gain=np.sqrt(2). return out elif self.act == 'linear': return out
Example #23
Source File: qlinear.py From fairseq with MIT License | 5 votes |
def forward(self, input): # train with QuantNoise and evaluate the fully quantized network p = self.p if self.training else 1 # update parameters every 100 iterations if self.counter % self.update_step == 0: self.scale = None self.zero_point = None self.counter += 1 # quantize weight weight_quantized, self.scale, self.zero_point = emulate_int( self.weight.detach(), bits=self.bits, method=self.method, scale=self.scale, zero_point=self.zero_point, ) # mask to apply noise mask = torch.zeros_like(self.weight) mask.bernoulli_(1 - p) noise = (weight_quantized - self.weight).masked_fill(mask.bool(), 0) # using straight-through estimator (STE) clamp_low = - self.scale * self.zero_point clamp_high = self.scale * (2 ** self.bits - 1 - self.zero_point) weight = torch.clamp(self.weight, clamp_low.item(), clamp_high.item()) + noise.detach() # return output output = F.linear(input, weight, self.bias) return output
Example #24
Source File: qlinear.py From fairseq with MIT License | 5 votes |
def forward(self, x): return F.linear( x, self.weight, self.bias, )
Example #25
Source File: linearized_convolution.py From fairseq with MIT License | 5 votes |
def forward(self, input, incremental_state=None): """ Args: incremental_state: Used to buffer signal; if not None, then input is expected to contain a single frame. If the input order changes between time steps, call reorder_incremental_state. Input: Time x Batch x Channel during training Batch x Time x Channel during inference """ if incremental_state is None: output = super().forward(input) if self.kernel_size[0] > 1 and self.padding[0] > 0: # remove future timesteps added by padding output = output[:-self.padding[0], :, :] return output # reshape weight weight = self._get_linearized_weight() kw = self.kernel_size[0] bsz = input.size(0) # input: bsz x len x dim if kw > 1: input = input.data input_buffer = self._get_input_buffer(incremental_state) if input_buffer is None: input_buffer = input.new(bsz, kw, input.size(2)).zero_() self._set_input_buffer(incremental_state, input_buffer) else: # shift buffer input_buffer[:, :-1, :] = input_buffer[:, 1:, :].clone() # append next input input_buffer[:, -1, :] = input[:, -1, :] input = input_buffer with torch.no_grad(): output = F.linear(input.view(bsz, -1), weight, self.bias) return output.view(bsz, 1, -1)
Example #26
Source File: adaptive_softmax.py From fairseq with MIT License | 5 votes |
def forward(self, input): return F.linear(input, self.weight.t() if self.transpose else self.weight)
Example #27
Source File: sequence.py From DeepCTR-Torch with Apache License 2.0 | 5 votes |
def forward(self, input, hx, att_score): 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) reset_gate = torch.sigmoid(i_r + h_r) update_gate = torch.sigmoid(i_z + h_z) new_state = torch.tanh(i_n + reset_gate * h_n) att_score = att_score.view(-1, 1) update_gate = att_score * update_gate hy = (1. - update_gate) * hx + update_gate * new_state return hy
Example #28
Source File: sequence.py From DeepCTR-Torch with Apache License 2.0 | 5 votes |
def forward(self, input, hx, att_score): 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) reset_gate = torch.sigmoid(i_r + h_r) # update_gate = torch.sigmoid(i_z + h_z) new_state = torch.tanh(i_n + reset_gate * h_n) att_score = att_score.view(-1, 1) hy = (1. - att_score) * hx + att_score * new_state return hy
Example #29
Source File: drop_connect.py From MNIST-baselines with MIT License | 5 votes |
def forward(self, x): weight = self.weight_dropout(self.weight) bias = self.bias_dropout(self.bias) if self.bias is not None else None return F.linear(x, weight, bias)
Example #30
Source File: flows.py From pytorch-flows with MIT License | 5 votes |
def forward(self, inputs, cond_inputs=None): output = F.linear(inputs, self.linear.weight * self.mask, self.linear.bias) if cond_inputs is not None: output += self.cond_linear(cond_inputs) return output