深度学习框架Pytorch

基本数据类型

数据类型
pythonPyTorch
IntIntTensor of size()
floatFloatTensor of size()
Int arrayIntTensor of size [d1, d2…]
Float arrayFloatTensor of size [d1, d2,…]
string
CPU tensorGPU tensor
torch. FloatTensortorch. cuda . FloatTensor
torch . DoubleTensortorch. cuda . DoubleTensor
torch. HalfTensortorch. cuda . HalfTensor
torch. ByteTensortorch . cuda. ByteTensor
torch. CharTensortorch. cuda. ChaITensor
torch. ShortTensortorch . cuda . ShortTensor
torch. IntTensortorch. cuda. IntTensor
torch. LongTensortorch. cuda. LongTensor
类型检验
>>>a = torch.randn(2,3)
>>>a.type()
'torch.FloatTensor'
>>>type(a)
<class 'torch.Tensor'>
>>>isinstance(a,torch.FloatTensor)
True
>>>a = a.cuda()
>>>isinstance(a,torch.cuda.FloatTensor)
True
Dimension 0
>>>a = torch.tensor(3.4)
>>>a.shape
torch.Size([])
>>>a.size()
torch.Size([])
>>>len(a.shape) #判断张量维度
0
Dimension 1
>>>torch.tensor([1.3])
tensor([1.3000])
>>>torch.FloatTensor(1)
tensor([2.])
>>>torch.FloatTensor(2)
tensor([2.0000, 2.1750])
>>>import numpy as np
>>>data = np.ones(2)
>>>torch.from_numpy(data)
tensor([1., 1.], dtype=torch.float64)

Dimension 2+
>>>torch.randn(2,3)
tensor([[ 0.9089,  0.1607, -0.0087],
        [ 0.6166,  0.0503,  0.8573]])
>>>torch.randn(1,2,3)
tensor([[[ 0.4751,  0.0645, -1.1749],
         [-0.9142,  0.6620, -0.6083]]])
>>>torch.randn(1,2,3,4)
tensor([[[[ 1.6369,  1.1565, -1.3589,  0.2490],
          [-1.2817, -0.0118, -0.7216, -0.8389],
          [ 0.5563, -1.1745,  0.5631, -0.9539]],
         [[ 1.0863, -1.3547,  1.6453, -0.5046],
          [-0.3041,  0.7166,  1.3162,  1.4097],
          [-0.5711, -0.4033,  1.1747,  0.8522]]]])

Mixed
>>>a = torch.randn(2,3,28,28)
>>>a.numel() #求元素个数
4704
>>>a.dim()
4

创建张量

从Numpy中导入
>>>a = np.array([2,3,4])
>>>torch.from_numpy(a)
tensor([2, 3, 4], dtype=torch.int32)
从List中导入
>>>lis = [2.,3.2]
>>>torch.FloatTensor(lis)
tensor([2.0000, 3.2000])
随机数
>>>torch.rand(3,3)
tensor([[0.1348, 0.5997, 0.1680],
        [0.6550, 0.6626, 0.4131],
        [0.8854, 0.3542, 0.4673]])
>>>a = torch.randn(3,3) #正态分布
>>>torch.randn_like(a)
tensor([[-1.0529, -0.0905,  0.4771],
        [-1.1433,  0.8187,  1.3829],
        [ 0.0504, -2.0612,  0.4886]])
>>>torch.randint(1,10,[3,3])
tensor([[3, 4, 2],
        [7, 7, 2],
        [7, 1, 4]])
>>>torch.linspace(0,10,steps=4) #等分切割
tensor([ 0.0000,  3.3333,  6.6667, 10.0000])
>>>torch.logspace(0,-1,steps=10) #对数切割
tensor([1.0000, 0.7743, 0.5995, 0.4642, 0.3594, 0.2783, 0.2154, 0.1668, 0.1292,
        0.1000])
>>>torch.ones(3,3)
tensor([[1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.]])
>>>torch.zeros(3,3)
tensor([[0., 0., 0.],
        [0., 0., 0.],
        [0., 0., 0.]])
>>>torch.eye(3,3)
tensor([[1., 0., 0.],
        [0., 1., 0.],
        [0., 0., 1.]])
#随机打散
>>>a = torch.randn(2,3)
>>>b = torch.randn(3,2)
>>>idx = torch.randperm(2)
>>>idx
tensor([1, 0])
>>>a[idx]
tensor([[ 0.7910,  1.5621,  0.1681],
        [-0.1567,  1.5512, -0.2470]])
>>>b[idx]
tensor([[ 0.3930, -0.0285],
        [ 0.5353,  1.6618]])

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值