torch for numpy users

torch equivalents of numpy functions

Types

NumpyTorch
np.ndarraytorch.Tensor
np.float32torch.FloatTensor
np.float64torch.DoubleTensor
np.int8torch.CharTensor
np.uint8torch.ByteTensor
np.int16torch.ShortTensor
np.int32torch.IntTensor
np.int64torch.LongTensor

Constructors

Ones and zeros
NumpyTorch
np.empty([2,2])torch.Tensor(2,2)
np.empty_like(x)x.new(x:size())
np.eyetorch.eye
np.identitytorch.eye
np.onestorch.ones
np.ones_liketorch.ones(x:size())
np.zerostorch.zeros
np.zeros_liketorch.zeros(x:size())
From existing data
NumpyTorch
np.array([ [1,2],[3,4] ])torch.Tensor({{1,2},{3,4}})
np.ascontiguousarray(x)x:contiguous()
np.copy(x)x:clone()
np.fromfile(file)torch.Tensor(torch.Storage(file))
np.frombuffer???
np.fromfunction???
np.fromiter???
np.fromstring???
np.loadtxt???
np.concatenatetorch.cat
np.multiplytorch.cmul
Numerical Ranges
NumpyTorch
np.arange(10)torch.range(0,9)
np.arange(2, 3, 0.1)torch.linspace(2, 2.9, 10)
np.linspace(1, 4, 6)torch.linspace(1, 4, 6)
np.logspacetorch.logspace
Building Matrices
NumpyTorch
np.diagtorch.diag
np.triltorch.tril
np.triutorch.triu
Attributes
NumpyTorch
x.shapex:size()
x.stridesx:stride()
x.ndimx:dim()
x.datax:data()
x.sizex:nElement()
x.size == y.sizex:isSameSizeAs(y)
x.dtypex:type()
Indexing
Shape Manipulation
NumpyTorch
x.reshapex:reshape
x.resizex:resize
?x:resizeAs
x.transposex:transpose()
x.flattenx:view(x:nElement())
x.squeezex:squeeze
Item selection and manipulation
NumpyTorch
np.take(a, indices)a[indices]
x[:,0]x[{{},1}]
np.put????
x.repeatx:repeatTensor
x.fillx:fill
np.choose???
np.sortsorted, indices = torch.sort(x, [dim])
np.argsortsorted, indices = torch.sort(x, [dim])
np.nonzerotorch.find(x:gt(0), 1) (torchx)
Calculation
NumpyTorch
ndarray.minmins, indices = torch.min(x, [dim])
ndarray.argminmins, indices = torch.min(x, [dim])
ndarray.maxmaxs, indices = torch.max(x, [dim])
ndarray.argmaxmaxs, indices = torch.max(x, [dim])
ndarray.clip 
ndarray.round 
ndarray.tracetorch.trace
ndarray.sumtorch.sum
ndarray.cumsumtorch.cumsum
ndarray.meantorch.mean
ndarray.stdtorch.std
ndarray.prodtorch.prod
ndarray.dottorch.mm
ndarray.cumprodtorch.cumprod
ndarray.all???
ndarray.any???
Arithmetic and comparison operations
NumpyTorch
ndarray.lttorch.lt
ndarray.letorch.le
ndarray.gttorch.gt
ndarray.getorch.ge
ndarray.eqtorch.eq
ndarray.netorch.ne
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值