PyTorch的基本数据类型张量

深度学习框架

1.基本数据类型

Data type

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-9xrs77H6-1630237149210)(attachment:image.png)]

Type check

import torch
a=torch.randn(2,3)
print(a)
tensor([[-2.0609,  0.7442, -1.7929],
        [ 0.0355, -1.6334,  0.0457]])
# 返回一个符合均值为0,方差为1的正态分布(标准正态分布)中填充随机数的张量
a=torch.randn(4)
print(a)
b=torch.randn(2, 3)
print(b)
tensor([ 1.0229, -0.0424, -0.5356, -0.5094])
tensor([[ 1.8531,  0.8852, -0.8800],
        [ 0.0269,  1.7656,  0.6066]])

Type check

a=torch.randn(2,3)
print(a.type())
print(isinstance(a,torch.FloatTensor)) #检验是不是浮点型tensor|
print(isinstance(a,torch.cuda.DoubleTensor)) #检验a是不是双精度在显卡运算的tebsor
torch.FloatTensor
True
False

进行转换

a=a.cuda()
print(isinstance(a,torch.cuda.DoubleTensor)) #检验a是不是双精度在显卡运算的tebsor
False

维度为零的张量

a=torch.tensor(1.) #直接给出一个不加中括号的数
print(a)
tensor(1.)
print(a.shape)
torch.Size([])
print(len(a.shape))
0
print(a.size())
torch.Size([])

一维张量

一维张量,通俗的理解,就是带一个中括号。

a=torch.tensor([2])
print(a)
tensor([2])
print(a.type())
torch.LongTensor
print(a.shape)
torch.Size([1])
b=torch.tensor([1,2,3,4,5,6])
print(b.size())
print(b.shape)
torch.Size([6])
torch.Size([6])

PS:在一维张量中.shape,.size(),方法返回的都是一维张量的长度

二维张量

a=torch.randn(2,3)  #生成向量时不需要加中括号(错误)a=torch.randn([2,3]) 
print(a)
print(a.shape)      #返回值是[行,列]
print(a.size(0))    #返回行信息
print(a.size(1)) #返回列信息

tensor([[-0.0777,  0.7428,  0.3114],
        [ 1.9080,  0.2888,  0.7801]])
torch.Size([2, 3])
2
3

三维张量

a=torch.randn(2,3,4)
print(a)
print(a.shape)
print(a.size(0))
print(a.size(1))
print(a.size(2))
print(a[0])       # 取出第一页
print(a[0,1])       # 取出第一页第一行
tensor([[[ 0.7987,  1.1427, -1.8349, -1.3582],
         [ 0.0296, -0.6334, -1.7922,  0.1883],
         [ 0.0606, -2.0127, -1.6093, -1.4228]],

        [[-1.2726, -1.7377, -0.7456, -1.4920],
         [ 0.0278,  0.7293, -1.7079, -0.1733],
         [-0.3545, -1.2592,  1.3214, -0.2987]]])
torch.Size([2, 3, 4])
2
3
4
tensor([[ 0.7987,  1.1427, -1.8349, -1.3582],
        [ 0.0296, -0.6334, -1.7922,  0.1883],
        [ 0.0606, -2.0127, -1.6093, -1.4228]])
tensor([ 0.0296, -0.6334, -1.7922,  0.1883])

四维张量

a=torch.rand(3,2,2,4)
print(a)
print(a.shape)
print(a.size(0))
print(a.size(1))
print(a.size(2))
print(a.size(3))
print(a[0])       # 取出第一个
print(a[0,1])     # 取出第一个图片的第一个通道
tensor([[[[0.3432, 0.6277, 0.4887, 0.6735],
          [0.0855, 0.5497, 0.2276, 0.3353]],

         [[0.4916, 0.9801, 0.8916, 0.0391],
          [0.7921, 0.3823, 0.7602, 0.6241]]],


        [[[0.1360, 0.7132, 0.6252, 0.2536],
          [0.2848, 0.1987, 0.9907, 0.7225]],

         [[0.9511, 0.4350, 0.2993, 0.0594],
          [0.3978, 0.6176, 0.3206, 0.4434]]],


        [[[0.9927, 0.8022, 0.7844, 0.2987],
          [0.1238, 0.9070, 0.9900, 0.9117]],

         [[0.9263, 0.2527, 0.6157, 0.7297],
          [0.4173, 0.0396, 0.1232, 0.2115]]]])
torch.Size([3, 2, 2, 4])
3
2
2
4
tensor([[[0.3432, 0.6277, 0.4887, 0.6735],
         [0.0855, 0.5497, 0.2276, 0.3353]],

        [[0.4916, 0.9801, 0.8916, 0.0391],
         [0.7921, 0.3823, 0.7602, 0.6241]]])
tensor([[0.4916, 0.9801, 0.8916, 0.0391],
        [0.7921, 0.3823, 0.7602, 0.6241]])
print(a.numel())     # 48=3*2*3*4计算总个数(元素)

48

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值