其他分享
首页 > 其他分享> > lua+torch equivalents of numpy functions

lua+torch equivalents of numpy functions

作者:互联网

文章目录

torch equivalents of numpy functions

lua+torch的社区资源太少了,搬运自torch的官网,希望对大家有所帮助

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

NumpyTorch

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.cliptorch.clamp
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

标签:dim,functions,torch,lua,np,equivalents,indices,size,NumpyTorchnp
来源: https://blog.csdn.net/bj_zhb/article/details/122029171