torch-tensor创建

系列教程

torch.int/long/float/double

方式-1
import torch

a = torch.IntTensor([1,2,3,4,5])
print(a.dtype)

a = torch.LongTensor([1,2,3])
print(a.dtype)

a = torch.FloatTensor([1,2,3])
print(a,a.dtype)

a = torch.DoubleTensor([1,2,3])
print(a.dtype)

运行结果如下

torch.int32
torch.int64
tensor([1., 2., 3.]) torch.float32
torch.float64
方式-2
import torch

a = torch.tensor([1,2,3],dtype=torch.int32)
print(a)

a = torch.tensor([1,2,3],dtype=torch.int64)
print(a)

a = torch.tensor([1,2,3],dtype=torch.float32)
print(a)

a = torch.tensor([1,2,3],dtype=torch.float64)
print(a)

a = torch.tensor([1,2,3],dtype=torch.float16)
print(a)

运行结果如下

tensor([1, 2, 3], dtype=torch.int32)
tensor([1, 2, 3])
tensor([1., 2., 3.])
tensor([1., 2., 3.], dtype=torch.float64)
tensor([1., 2., 3.], dtype=torch.float16)

int/float互转

import torch

a = torch.tensor([1,2,3],dtype=torch.int32)
print(a)

b = a.type(torch.float16)
print(b)

c = torch.tensor([0.563,4.78,9.15])
print(c)

d = c.type(torch.int64)
print(d)

运行结果如下

tensor([1, 2, 3], dtype=torch.int32)
tensor([1., 2., 3.], dtype=torch.float16)
tensor([0.5630, 4.7800, 9.1500])
tensor([0, 4, 9])

list/numpy互转

import numpy as np

a = [[1,2,3],[4,5.01,6]]
print(a)

np_a = np.array(a)
print(np_a)

new_a = np_a.tolist()
print(new_a)

运行结果如下

[[1, 2, 3], [4, 5.01, 6]]
[[1.   2.   3.  ]
 [4.   5.01 6.  ]]
[[1.0, 2.0, 3.0], [4.0, 5.01, 6.0]]

list/torch互转

import torch
import numpy as np

a = [[1,2,3],[4,5.01,6]]
print(a)

torch_a = torch.tensor(a)
print(torch_a)

new_a = torch_a.tolist()
print(new_a)

运行结果如下

[[1, 2, 3], [4, 5.01, 6]]
tensor([[1.0000, 2.0000, 3.0000],
        [4.0000, 5.0100, 6.0000]])
[[1.0, 2.0, 3.0], [4.0, 5.010000228881836, 6.0]]

numpy/torch互转

import torch
import numpy as np

np_a = np.array([[1.1,2,2.5],[2.9,109,800]])
print(np_a,np_a.dtype)

torch_a = torch.from_numpy(np_a)
print(torch_a)

new_a = torch_a.numpy()
print(new_a,new_a.dtype,new_a.shape)

运行结果如下

[[  1.1   2.    2.5]
 [  2.9 109.  800. ]] float64
tensor([[  1.1000,   2.0000,   2.5000],
        [  2.9000, 109.0000, 800.0000]], dtype=torch.float64)
[[  1.1   2.    2.5]
 [  2.9 109.  800. ]] float64 (2, 3)

特定维度

torch.ones
import torch

a = torch.ones([2,3])
print(a)

a = torch.ones((1,4))
print(a)

a = torch.ones((1,2,3))
print(a)

a = torch.ones((1,2,2,3))
print(a)

运行结果如下

tensor([[1., 1., 1.],
        [1., 1., 1.]])
tensor([[1., 1., 1., 1.]])
tensor([[[1., 1., 1.],
         [1., 1., 1.]]])
tensor([[[[1., 1., 1.],
          [1., 1., 1.]],

         [[1., 1., 1.],
          [1., 1., 1.]]]])
torch.zeros
import torch

a = torch.zeros((2,3))
print(a)

a = torch.zeros((2,3),dtype=torch.int64)
print(a)

运行结果如下

tensor([[0., 0., 0.],
        [0., 0., 0.]])
tensor([[0, 0, 0],
        [0, 0, 0]])
torch.ones_like/zeros_like
import torch

a = torch.tensor([[1,2,3],[4,5,6]])
print(a.shape)

b = torch.ones_like(a)
print(b)

c = torch.zeros_like(a)
print(c)

运行结果如下

torch.Size([2, 3])
tensor([[1, 1, 1],
        [1, 1, 1]])
tensor([[0, 0, 0],
        [0, 0, 0]])
torch.full/full_like
import torch

a = torch.full((2,3), 0.95)
print(a)

b = torch.full_like(a,2.6)
print(b)

运行结果如下

tensor([[0.9500, 0.9500, 0.9500],
        [0.9500, 0.9500, 0.9500]])
tensor([[2.6000, 2.6000, 2.6000],
        [2.6000, 2.6000, 2.6000]])
torch.empty
import torch

for _ in range(2):
    a = torch.empty((2,2))
    print(a)

运行结果如下

tensor([[0., 0.],
        [0., 0.]])
tensor([[7.0065e-45, 0.0000e+00],
        [0.0000e+00, 0.0000e+00]])
torch.eye
import torch

a = torch.eye(1)
print(a)

a = torch.eye(2)
print(a)

a = torch.eye(3)
print(a)

运行结果如下

tensor([[1.]])
tensor([[1., 0.],
        [0., 1.]])
tensor([[1., 0., 0.],
        [0., 1., 0.],
        [0., 0., 1.]])

序列

torch.arange
import torch

a = torch.arange(start=0,end=10,step=1)
print(a)

a = torch.arange(start=0,end=10,step=2)
print(a)

a = torch.arange(start=0,end=10,step=5)
print(a)

a = torch.arange(start=0,end=10.0,step=5)
print(a)

运行结果如下

tensor([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
tensor([0, 2, 4, 6, 8])
tensor([0, 5])
tensor([0., 5.])
torch.linspace
import torch

a = torch.linspace(start=1,end=10,steps=1)
print(a)

a = torch.linspace(start=1,end=10,steps=2)
print(a)

a = torch.linspace(start=1,end=10,steps=3)
print(a)

运行结果如下

tensor([1.])
tensor([ 1., 10.])
tensor([ 1.0000,  5.5000, 10.0000])
torch.randperm
import torch

a = torch.randperm(3)
print(a)

a = torch.randperm(7)
print(a)

a = torch.randperm(10)
print(a)

运行结果如下

tensor([0, 2, 1])
tensor([0, 2, 3, 5, 1, 4, 6])
tensor([1, 6, 8, 5, 0, 9, 7, 4, 2, 3])

GitHub-Torch系列教程​​​​​​​

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值