Python torch.arange() Examples
The following are 30
code examples of torch.arange().
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: utils.py From pruning_yolov3 with GNU General Public License v3.0 | 8 votes |
def plot_wh_methods(): # from utils.utils import *; plot_wh_methods() # Compares the two methods for width-height anchor multiplication # https://github.com/ultralytics/yolov3/issues/168 x = np.arange(-4.0, 4.0, .1) ya = np.exp(x) yb = torch.sigmoid(torch.from_numpy(x)).numpy() * 2 fig = plt.figure(figsize=(6, 3), dpi=150) plt.plot(x, ya, '.-', label='yolo method') plt.plot(x, yb ** 2, '.-', label='^2 power method') plt.plot(x, yb ** 2.5, '.-', label='^2.5 power method') plt.xlim(left=-4, right=4) plt.ylim(bottom=0, top=6) plt.xlabel('input') plt.ylabel('output') plt.legend() fig.tight_layout() fig.savefig('comparison.png', dpi=200)
Example #2
Source File: kaldi.py From audio with BSD 2-Clause "Simplified" License | 6 votes |
def _feature_window_function(window_type: str, window_size: int, blackman_coeff: float, device: torch.device, dtype: int, ) -> Tensor: r"""Returns a window function with the given type and size """ if window_type == HANNING: return torch.hann_window(window_size, periodic=False, device=device, dtype=dtype) elif window_type == HAMMING: return torch.hamming_window(window_size, periodic=False, alpha=0.54, beta=0.46, device=device, dtype=dtype) elif window_type == POVEY: # like hanning but goes to zero at edges return torch.hann_window(window_size, periodic=False, device=device, dtype=dtype).pow(0.85) elif window_type == RECTANGULAR: return torch.ones(window_size, device=device, dtype=dtype) elif window_type == BLACKMAN: a = 2 * math.pi / (window_size - 1) window_function = torch.arange(window_size, device=device, dtype=dtype) # can't use torch.blackman_window as they use different coefficients return (blackman_coeff - 0.5 * torch.cos(a * window_function) + (0.5 - blackman_coeff) * torch.cos(2 * a * window_function)).to(device=device, dtype=dtype) else: raise Exception('Invalid window type ' + window_type)
Example #3
Source File: utils.py From ICDAR-2019-SROIE with MIT License | 6 votes |
def decimate(tensor, m): """ Decimate a tensor by a factor 'm', i.e. downsample by keeping every 'm'th value. This is used when we convert FC layers to equivalent Convolutional layers, BUT of a smaller size. :param tensor: tensor to be decimated :param m: list of decimation factors for each dimension of the tensor; None if not to be decimated along a dimension :return: decimated tensor """ assert tensor.dim() == len(m) for d in range(tensor.dim()): if m[d] is not None: tensor = tensor.index_select(dim=d, index=torch.arange(start=0, end=tensor.size(d), step=m[d]).long()) return tensor
Example #4
Source File: graphML.py From graph-neural-networks with GNU General Public License v3.0 | 6 votes |
def forward(self, x): # x is of shape: batchSize x dimInFeatures x numberNodesIn B = x.shape[0] F = x.shape[1] Nin = x.shape[2] # And now we add the zero padding if Nin < self.N: x = torch.cat((x, torch.zeros(B, F, self.N-Nin)\ .type(x.dtype).to(x.device) ), dim = 2) # Compute the filter output u = jARMA(self.inverseWeight, self.directWeight, self.filterWeight, self.S, x, b = self.bias, tMax = self.tMax) # So far, u is of shape batchSize x dimOutFeatures x numberNodes # And we want to return a tensor of shape # batchSize x dimOutFeatures x numberNodesIn # since the nodes between numberNodesIn and numberNodes are not required if Nin < self.N: u = torch.index_select(u, 2, torch.arange(Nin).to(u.device)) return u
Example #5
Source File: graphML.py From graph-neural-networks with GNU General Public License v3.0 | 6 votes |
def forward(self, x): # x is of shape: batchSize x dimInFeatures x numberNodesIn B = x.shape[0] F = x.shape[1] Nin = x.shape[2] # If we have less filter coefficients than the required ones, we need # to use the copying scheme if self.M == self.N: self.h = self.weight else: self.h = torch.index_select(self.weight, 4, self.copyNodes) # And now we add the zero padding if Nin < self.N: zeroPad = torch.zeros(B, F, self.N-Nin).type(x.dtype).to(x.device) x = torch.cat((x, zeroPad), dim = 2) # Compute the filter output u = NVGF(self.h, self.S, x, self.bias) # So far, u is of shape batchSize x dimOutFeatures x numberNodes # And we want to return a tensor of shape # batchSize x dimOutFeatures x numberNodesIn # since the nodes between numberNodesIn and numberNodes are not required if Nin < self.N: u = torch.index_select(u, 2, torch.arange(Nin).to(u.device)) return u
Example #6
Source File: graphML.py From graph-neural-networks with GNU General Public License v3.0 | 6 votes |
def forward(self, x): # x is of shape: batchSize x dimInFeatures x numberNodesIn B = x.shape[0] F = x.shape[1] Nin = x.shape[2] # And now we add the zero padding if Nin < self.N: x = torch.cat((x, torch.zeros(B, F, self.N-Nin)\ .type(x.dtype).to(x.device) ), dim = 2) # Compute the filter output u = LSIGF(self.weight, self.S, x, self.bias) # So far, u is of shape batchSize x dimOutFeatures x numberNodes # And we want to return a tensor of shape # batchSize x dimOutFeatures x numberNodesIn # since the nodes between numberNodesIn and numberNodes are not required if Nin < self.N: u = torch.index_select(u, 2, torch.arange(Nin).to(u.device)) return u
Example #7
Source File: trainer.py From treelstm.pytorch with MIT License | 6 votes |
def test(self, dataset): self.model.eval() with torch.no_grad(): total_loss = 0.0 predictions = torch.zeros(len(dataset), dtype=torch.float, device='cpu') indices = torch.arange(1, dataset.num_classes + 1, dtype=torch.float, device='cpu') for idx in tqdm(range(len(dataset)), desc='Testing epoch ' + str(self.epoch) + ''): ltree, linput, rtree, rinput, label = dataset[idx] target = utils.map_label_to_target(label, dataset.num_classes) linput, rinput = linput.to(self.device), rinput.to(self.device) target = target.to(self.device) output = self.model(ltree, linput, rtree, rinput) loss = self.criterion(output, target) total_loss += loss.item() output = output.squeeze().to('cpu') predictions[idx] = torch.dot(indices, torch.exp(output)) return total_loss / len(dataset), predictions
Example #8
Source File: losses.py From ACAN with MIT License | 6 votes |
def get_entropy(self, pred, label): n, c, h, w = pred.size() label = label.unsqueeze(3).long() pred = F.softmax(pred, 1).permute(0, 2, 3, 1) one_hot_label = ((torch.arange(c)).cuda() == label).float() if self.eps == 0: prior = 0 else: if self.priorType == 'gaussian': tensor = (torch.arange(c).cuda() - label).float() prior = NormalDist(tensor, c / 10) elif self.priorType == 'uniform': prior = 1 / (c-1) smoothed_label = (1 - self.eps) * one_hot_label + self.eps * prior * (1 - one_hot_label) entropy = smoothed_label * safe_log(pred) + (1 - smoothed_label) * safe_log(1 - pred) return -entropy
Example #9
Source File: stage.py From TVQAplus with MIT License | 6 votes |
def get_ts_loss(self, temporal_scores, ts_labels, answer_indices): """ Args: temporal_scores: (N, 5, Li, 2) ts_labels: dict(st=(N, ), ed=(N, )) answer_indices: (N, ) Returns: """ bsz = len(answer_indices) # compute loss ca_temporal_scores_st_ed = \ temporal_scores[torch.arange(bsz, dtype=torch.long), answer_indices] # (N, Li, 2) loss_st = self.temporal_criterion(ca_temporal_scores_st_ed[:, :, 0], ts_labels["st"]) loss_ed = self.temporal_criterion(ca_temporal_scores_st_ed[:, :, 1], ts_labels["ed"]) return (loss_st + loss_ed) / 2.
Example #10
Source File: ner_model.py From Doc2EDAG with MIT License | 6 votes |
def forward(self, batch_token_ids): batch_size, sent_len = batch_token_ids.size() device = batch_token_ids.device batch_pos_ids = torch.arange( sent_len, dtype=torch.long, device=device, requires_grad=False ) batch_pos_ids = batch_pos_ids.unsqueeze(0).expand_as(batch_token_ids) batch_token_emb = self.token_embedding(batch_token_ids) batch_pos_emb = self.pos_embedding(batch_pos_ids) batch_token_emb = batch_token_emb + batch_pos_emb batch_token_out = self.layer_norm(batch_token_emb) batch_token_out = self.dropout(batch_token_out) return batch_token_out
Example #11
Source File: dee_model.py From Doc2EDAG with MIT License | 6 votes |
def forward(self, batch_elem_emb, sent_pos_ids=None): if sent_pos_ids is None: num_elem = batch_elem_emb.size(-2) sent_pos_ids = torch.arange( num_elem, dtype=torch.long, device=batch_elem_emb.device, requires_grad=False ) elif not isinstance(sent_pos_ids, torch.Tensor): sent_pos_ids = torch.tensor( sent_pos_ids, dtype=torch.long, device=batch_elem_emb.device, requires_grad=False ) batch_pos_emb = self.embedding(sent_pos_ids) out = batch_elem_emb + batch_pos_emb out = self.dropout(self.layer_norm(out)) return out
Example #12
Source File: distributed_sampler.py From mmdetection with Apache License 2.0 | 6 votes |
def __iter__(self): # deterministically shuffle based on epoch if self.shuffle: g = torch.Generator() g.manual_seed(self.epoch) indices = torch.randperm(len(self.dataset), generator=g).tolist() else: indices = torch.arange(len(self.dataset)).tolist() # add extra samples to make it evenly divisible indices += indices[:(self.total_size - len(indices))] assert len(indices) == self.total_size # subsample indices = indices[self.rank:self.total_size:self.num_replicas] assert len(indices) == self.num_samples return iter(indices)
Example #13
Source File: test_compliance_kaldi.py From audio with BSD 2-Clause "Simplified" License | 6 votes |
def _create_data_set(self): # used to generate the dataset to test on. this is not used in testing (offline procedure) test_filepath = common_utils.get_asset_path('kaldi_file.wav') sr = 16000 x = torch.arange(0, 20).float() # between [-6,6] y = torch.cos(2 * math.pi * x) + 3 * torch.sin(math.pi * x) + 2 * torch.cos(x) # between [-2^30, 2^30] y = (y / 6 * (1 << 30)).long() # clear the last 16 bits because they aren't used anyways y = ((y >> 16) << 16).float() torchaudio.save(test_filepath, y, sr) sound, sample_rate = torchaudio.load(test_filepath, normalization=False) print(y >> 16) self.assertTrue(sample_rate == sr) torch.testing.assert_allclose(y, sound)
Example #14
Source File: test_compliance_kaldi.py From audio with BSD 2-Clause "Simplified" License | 6 votes |
def _test_get_strided_helper(self, num_samples, window_size, window_shift, snip_edges): waveform = torch.arange(num_samples).float() output = kaldi._get_strided(waveform, window_size, window_shift, snip_edges) # from NumFrames in feature-window.cc n = window_size if snip_edges: m = 0 if num_samples < window_size else 1 + (num_samples - window_size) // window_shift else: m = (num_samples + (window_shift // 2)) // window_shift self.assertTrue(output.dim() == 2) self.assertTrue(output.shape[0] == m and output.shape[1] == n) window = torch.empty((m, window_size)) for r in range(m): extract_window(window, waveform, r, window_size, window_shift, snip_edges) torch.testing.assert_allclose(window, output)
Example #15
Source File: mask_point_head.py From mmdetection with Apache License 2.0 | 6 votes |
def _get_uncertainty(self, mask_pred, labels): """Estimate uncertainty based on pred logits. We estimate uncertainty as L1 distance between 0.0 and the logits prediction in 'mask_pred' for the foreground class in `classes`. Args: mask_pred (Tensor): mask predication logits, shape (num_rois, num_classes, mask_height, mask_width). labels (list[Tensor]): Either predicted or ground truth label for each predicted mask, of length num_rois. Returns: scores (Tensor): Uncertainty scores with the most uncertain locations having the highest uncertainty score, shape (num_rois, 1, mask_height, mask_width) """ if mask_pred.shape[1] == 1: gt_class_logits = mask_pred.clone() else: inds = torch.arange(mask_pred.shape[0], device=mask_pred.device) gt_class_logits = mask_pred[inds, labels].unsqueeze(1) return -torch.abs(gt_class_logits)
Example #16
Source File: functional_cpu_test.py From audio with BSD 2-Clause "Simplified" License | 6 votes |
def _test_istft_of_sine(self, amplitude, L, n): # stft of amplitude*sin(2*pi/L*n*x) with the hop length and window size equaling L x = torch.arange(2 * L + 1, dtype=torch.get_default_dtype()) sound = amplitude * torch.sin(2 * math.pi / L * x * n) # stft = torch.stft(sound, L, hop_length=L, win_length=L, # window=torch.ones(L), center=False, normalized=False) stft = torch.zeros((L // 2 + 1, 2, 2)) stft_largest_val = (amplitude * L) / 2.0 if n < stft.size(0): stft[n, :, 1] = -stft_largest_val if 0 <= L - n < stft.size(0): # symmetric about L // 2 stft[L - n, :, 1] = stft_largest_val estimate = torchaudio.functional.istft(stft, L, hop_length=L, win_length=L, window=torch.ones(L), center=False, normalized=False) # There is a larger error due to the scaling of amplitude _compare_estimate(sound, estimate, atol=1e-3)
Example #17
Source File: CBP.py From fast-MPN-COV with MIT License | 6 votes |
def __init__(self, thresh=1e-8, projDim=8192, input_dim=512): super(CBP, self).__init__() self.thresh = thresh self.projDim = projDim self.input_dim = input_dim self.output_dim = projDim torch.manual_seed(1) self.h_ = [ torch.randint(0, self.output_dim, (self.input_dim,),dtype=torch.long), torch.randint(0, self.output_dim, (self.input_dim,),dtype=torch.long) ] self.weights_ = [ (2 * torch.randint(0, 2, (self.input_dim,)) - 1).float(), (2 * torch.randint(0, 2, (self.input_dim,)) - 1).float() ] indices1 = torch.cat((torch.arange(input_dim, dtype=torch.long).reshape(1, -1), self.h_[0].reshape(1, -1)), dim=0) indices2 = torch.cat((torch.arange(input_dim, dtype=torch.long).reshape(1, -1), self.h_[1].reshape(1, -1)), dim=0) self.sparseM = [ torch.sparse.FloatTensor(indices1, self.weights_[0], torch.Size([self.input_dim, self.output_dim])).to_dense(), torch.sparse.FloatTensor(indices2, self.weights_[1], torch.Size([self.input_dim, self.output_dim])).to_dense(), ]
Example #18
Source File: resampling.py From pyfilter with MIT License | 6 votes |
def _matrix(weights: torch.Tensor, u: torch.Tensor): """ Performs systematic resampling of a 2D array of log weights along the second axis. independent of the others. :param weights: The weights to use for resampling :return: Resampled indices """ n = weights.shape[1] index_range = torch.arange(n, dtype=u.dtype, device=weights.device).unsqueeze(0) probs = (index_range + u) / n cumsum = weights.cumsum(-1) cumsum[..., -1] = 1. return torch.searchsorted(cumsum, probs)
Example #19
Source File: auto_crop.py From L3C-PyTorch with GNU General Public License v3.0 | 5 votes |
def _get_crop_idx_mapping(side): """Helper method to get the order of crops. :param side: how many crops live on each side. Example. Say you have an image that gets devided into 16 crops, i.e., the image gets cut into 16 parts: [[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11], [12, 13, 14, 15]], However, due to our recursive cropping code, this results in crops that are ordered like this: index of crop: 0 1 2 3 4 ... corresponds to part in image: 0, 1, 4, 5, 2, 3, 6, 7, 8, 9, 12, 13, 10, 11, 14, 15 This method returns the inverse, going from the index of the crop back to the index in the image. """ a = torch.arange(side * side).reshape(1, 1, side, side) a = torch.cat((a, a, a), dim=1) # Create mapping # Index of crop in original image -> index of crop in the order it was extracted, # E.g. 2 -> 4 means it's the 2nd crop, but in the image, it's at position 4 (see above). crops = {i: crop[0, 0, ...].flatten().item() for i, crop in enumerate(iter_crops(a, 1))} return crops
Example #20
Source File: resampling.py From pyfilter with MIT License | 5 votes |
def _vector(weights: torch.Tensor, u: torch.Tensor): """ Performs systematic resampling of a 1D array log weights. :param weights: The weights to use for resampling :return: Resampled indices """ n = weights.shape[0] probs = (torch.arange(n, dtype=u.dtype, device=weights.device) + u) / n cumsum = weights.cumsum(0) cumsum[..., -1] = 1. return torch.searchsorted(cumsum, probs)
Example #21
Source File: kde.py From pyfilter with MIT License | 5 votes |
def sample(self, inds=None): inds = inds if inds is not None else torch.arange(self._means.shape[0], device=self._means.device) return _jitter(self._means[inds], self._bw_fac * self._cov.sqrt())
Example #22
Source File: Transformer.py From ConvLab with MIT License | 5 votes |
def __init__(self, d_model, max_len=512): super(PositionalEmbedding, self).__init__() # Compute the positional encodings once in log space. pe = torch.zeros(max_len, d_model).float() pe.require_grad = False position = torch.arange(0, max_len).float().unsqueeze(1) div_term = (torch.arange(0, d_model, 2).float() * -(math.log(10000.0) / d_model)).exp() pe[:, 0::2] = torch.sin(position * div_term) pe[:, 1::2] = torch.cos(position * div_term) pe = pe.unsqueeze(0) self.register_buffer('pe', pe)
Example #23
Source File: csd.py From CSD-SSD with MIT License | 5 votes |
def flip(x, dim): dim = x.dim() + dim if dim < 0 else dim return x[tuple(slice(None, None) if i != dim else torch.arange(x.size(i)-1, -1, -1).long() for i in range(x.dim()))]
Example #24
Source File: torchac.py From L3C-PyTorch with GNU General Public License v3.0 | 5 votes |
def _renorm_cast_cdf_(cdf, precision): Lp = cdf.shape[-1] finals = 1 # NHW1 # RENORMALIZATION_FACTOR in cuda f = torch.tensor(2, dtype=torch.float32, device=cdf.device).pow_(precision) cdf = cdf.mul((f - (Lp - 1)) / finals) # TODO cdf = cdf.round() cdf = cdf.to(dtype=torch.int16, non_blocking=True) r = torch.arange(Lp, dtype=torch.int16, device=cdf.device) cdf.add_(r) return cdf
Example #25
Source File: kaldi.py From audio with BSD 2-Clause "Simplified" License | 5 votes |
def _get_lifter_coeffs(num_ceps: int, cepstral_lifter: float) -> Tensor: # returns size (num_ceps) # Compute liftering coefficients (scaling on cepstral coeffs) # coeffs are numbered slightly differently from HTK: the zeroth index is C0, which is not affected. i = torch.arange(num_ceps) return 1.0 + 0.5 * cepstral_lifter * torch.sin(math.pi * i / cepstral_lifter)
Example #26
Source File: functional.py From audio with BSD 2-Clause "Simplified" License | 5 votes |
def create_dct( n_mfcc: int, n_mels: int, norm: Optional[str] ) -> Tensor: r"""Create a DCT transformation matrix with shape (``n_mels``, ``n_mfcc``), normalized depending on norm. Args: n_mfcc (int): Number of mfc coefficients to retain n_mels (int): Number of mel filterbanks norm (str or None): Norm to use (either 'ortho' or None) Returns: Tensor: The transformation matrix, to be right-multiplied to row-wise data of size (``n_mels``, ``n_mfcc``). """ # http://en.wikipedia.org/wiki/Discrete_cosine_transform#DCT-II n = torch.arange(float(n_mels)) k = torch.arange(float(n_mfcc)).unsqueeze(1) dct = torch.cos(math.pi / float(n_mels) * (n + 0.5) * k) # size (n_mfcc, n_mels) if norm is None: dct *= 2.0 else: assert norm == "ortho" dct[0] *= 1.0 / math.sqrt(2.0) dct *= math.sqrt(2.0 / float(n_mels)) return dct.t()
Example #27
Source File: functional.py From audio with BSD 2-Clause "Simplified" License | 5 votes |
def mask_along_axis_iid( specgrams: Tensor, mask_param: int, mask_value: float, axis: int ) -> Tensor: r""" Apply a mask along ``axis``. Mask will be applied from indices ``[v_0, v_0 + v)``, where ``v`` is sampled from ``uniform(0, mask_param)``, and ``v_0`` from ``uniform(0, max_v - v)``. Args: specgrams (Tensor): Real spectrograms (batch, channel, freq, time) mask_param (int): Number of columns to be masked will be uniformly sampled from [0, mask_param] mask_value (float): Value to assign to the masked columns axis (int): Axis to apply masking on (2 -> frequency, 3 -> time) Returns: Tensor: Masked spectrograms of dimensions (batch, channel, freq, time) """ if axis != 2 and axis != 3: raise ValueError('Only Frequency and Time masking are supported') device = specgrams.device dtype = specgrams.dtype value = torch.rand(specgrams.shape[:2], device=device, dtype=dtype) * mask_param min_value = torch.rand(specgrams.shape[:2], device=device, dtype=dtype) * (specgrams.size(axis) - value) # Create broadcastable mask mask_start = min_value[..., None, None] mask_end = (min_value + value)[..., None, None] mask = torch.arange(0, specgrams.size(axis), device=device, dtype=dtype) # Per batch example masking specgrams = specgrams.transpose(axis, -1) specgrams.masked_fill_((mask >= mask_start) & (mask < mask_end), mask_value) specgrams = specgrams.transpose(axis, -1) return specgrams
Example #28
Source File: test_compliance_kaldi.py From audio with BSD 2-Clause "Simplified" License | 5 votes |
def _test_resample_waveform_accuracy(self, up_scale_factor=None, down_scale_factor=None, atol=1e-1, rtol=1e-4): # resample the signal and compare it to the ground truth n_to_trim = 20 sample_rate = 1000 new_sample_rate = sample_rate if up_scale_factor is not None: new_sample_rate *= up_scale_factor if down_scale_factor is not None: new_sample_rate //= down_scale_factor duration = 5 # seconds original_timestamps = torch.arange(0, duration, 1.0 / sample_rate) sound = 123 * torch.cos(2 * math.pi * 3 * original_timestamps).unsqueeze(0) estimate = kaldi.resample_waveform(sound, sample_rate, new_sample_rate).squeeze() new_timestamps = torch.arange(0, duration, 1.0 / new_sample_rate)[:estimate.size(0)] ground_truth = 123 * torch.cos(2 * math.pi * 3 * new_timestamps) # trim the first/last n samples as these points have boundary effects ground_truth = ground_truth[..., n_to_trim:-n_to_trim] estimate = estimate[..., n_to_trim:-n_to_trim] torch.testing.assert_allclose(estimate, ground_truth, atol=atol, rtol=rtol)
Example #29
Source File: test_io.py From audio with BSD 2-Clause "Simplified" License | 5 votes |
def _test_1_save_sine(self): # save created file sinewave_filepath = os.path.join(self.test_dirpath, "assets", "sinewave.wav") sr = 16000 freq = 440 volume = 0.3 y = (torch.cos( 2 * math.pi * torch.arange(0, 4 * sr).float() * freq / sr)) y.unsqueeze_(0) # y is between -1 and 1, so must scale y = (y * volume * (2**31)).long() torchaudio.save(sinewave_filepath, y, sr) self.assertTrue(os.path.isfile(sinewave_filepath)) # test precision new_precision = 32 new_filepath = os.path.join(self.test_dirpath, "test.wav") si, ei = torchaudio.info(sinewave_filepath) torchaudio.save(new_filepath, y, sr, new_precision) si32, ei32 = torchaudio.info(new_filepath) self.assertEqual(si.precision, 16) self.assertEqual(si32.precision, new_precision) os.unlink(new_filepath)
Example #30
Source File: position_encoding.py From TVQAplus with MIT License | 5 votes |
def __init__(self, n_filters=128, max_len=500): """ :param n_filters: same with input hidden size :param max_len: maximum sequence length """ super(PositionEncoding, self).__init__() # Compute the positional encodings once in log space. pe = torch.zeros(max_len, n_filters) # (L, D) position = torch.arange(0, max_len).float().unsqueeze(1) div_term = torch.exp(torch.arange(0, n_filters, 2).float() * - (math.log(10000.0) / n_filters)) pe[:, 0::2] = torch.sin(position * div_term) pe[:, 1::2] = torch.cos(position * div_term) self.register_buffer('pe', pe) # buffer is a tensor, not a variable, (L, D)