简体   繁体   中英

How to form a sequence of consecutive numbers in Pytorch?

How to convert the Matlab code

v = [1: n]

to pytorch?

Writing a whole loop for that seems inefficient.

This should be straightforward to find, but I am facing difficulty searching the right keyword to get there.

You can directly use the arange method from Pytorch.

torch_v = torch.arange(1,n)

Reference: https://pytorch.org/docs/master/torch.html?highlight=arange#torch.arange

You form a sequence of consecutive numbers in python

import numpy as np
v= np.arange(1,n)

if you want a torch tensor you can transform the numpy array like this:

torch_v = torch.from_numpy(v)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM