PyTorch---(六)基本数据类型

b 站(14,15):https://www.bilibili.com/video/av49008640/?p=14

1 数据类型

int   
float  
int array  
float array
string 

int tensor
float tensor
IntTensor of size
FloatTensor of size
--

 

dimension维度

2 PyTorch 是面向数据计算的加速库,没有完备的语言库,没有对应的string的对应表示方法

pytorch不支持string,
用编码方式代替表示:
one-hot [0,1,0,0,..]

3 pytorch数据类型

左是CPU上面的, 右是GPU,加了cuda

常用:

IntTensor

FloatTensor

ByteTensor  用于检验对应位置是否相等

4类型检查

1 随机初始化一个 二维tensor
torch.randn(2,3)

2 查看类型

3 python自带的方法

4 参数的合法化检验


 

上述,代码实现

import torch
import numpy as np
a = torch.randn(2, 3)  # 随机初始化一个 二维tensor torch.randn(2,3)
print('1 pytorch的方法,查看类型:', a.type())
print('2 python自带方法,查看类型:',type(a))
print('3 参数的合法化检验:', isinstance(a, torch.FloatTensor))

 

 

5 因为一开始data 是部署在cpu
所以检验结果是False

6 搬运到cuda

7 再检验结果是True


 

上述,代码实现

isinstance(data, torch.cuda.DoubleTensor)
# False

data=data.cuda()

isinstance(data, torch.cuda.DoubleTensor)
# True

 

5 dimension为0的 标量

下面是标量,0维度,

1 使用场景

用于loss

torch.tensor(1.)
# 输出 tensor(1.)

torch.tensor(1.3)
# 输出 tensor(1.300)

这里的随机产生的是正态分布的,均值0 方差1

b = torch.tensor(2.2)
print('\n4:', b.shape)
# 输出 torch.Size([])

print('5:', len(b.shape))  # .shape是一个成员
# 输出 0

print('6:', b.dim())
# 输出 0

print('7:', b.size())  # .size是一个成员函数,所以用.size()调用
# 输出 torch.Size([])

6 dimension为1的 vector 向量,pytorch中统一称为tensor 张量

 

 

# tensor接受的是具体的数据
c = torch.tensor([1.1])
print('\n8:', c)
# 输出 tensor([1.1000])   1维

c = torch.tensor([1.1, 2.2])
print('9:', c)
# 输出 tensor([1.1000, 2.2000])   2维


# FloatTensor接受的是数据的shape
c = torch.FloatTensor(1)  # dimension为1 size为1
print('10', c)
# 输出 tensor([3.2239e-25])随机生成的

c = torch.FloatTensor(2)  # dimension为2 size为2
# 输出 tensor([3.2239e-25, 4.5915e-41])随机初始化两个数据
print('11', c)

data = np.ones(2)
print('12:', data)
# 输出 array([1., 1.])  生成长度为2的vector


c = torch.from_numpy(data)
print('13:', c)
# 输出 tensor([1., 1.], dtype=torch.float64) 变成了floattensor类型


 

1 使用场景

bias : 常用1维 的

2 使用场景

input :  Linear input 

28*28的图片打平成784  ,此时是一维的

知识扩展: 

pytorch0.3时候,  loss = [0.3]  是一维的,这里没有0维概念

pytorch1.0, 现在有标量了,维度为0

dim =0   loss = 0.3

3
对于[2, 2]

dim维度是  2

shape/size指的是具体形状  [2, 2]

tensor指的是里面具体的内容 
[1, 2
 3, 4]

a.shape

a = torch.ones(2)
print('a.shape', a.shape)

7 dimension为2的 vector 向量

# a = torch.FloatTensor(2,3)等同
a = torch.randn(2,3)
print('a', a)  # a tensor([[ 0.2111,  0.8824, -0.6216],
                           # [-1.0907, -0.3094,  0.3236]])
        

print('a.shape', a.shape)  # a.shape torch.Size([2, 3])


print('a.size(0)', a.size(0))  # a.size(0) 2

print('a.size(1)', a.size(1))  # a.shape[1] 3

print('a.shape[1]', a.shape[1])  # 等同于a.size(1)  a.shape[1] 3

输出

 

dim 2 的向量

1 使用场景

常用于

linear input batch

4张照片,每个图784信息

8 dimension为3的 vector 向量

a = torch.rand(1, 2, 3)
print('\na', a)  #    tensor([[[0.1648, 0.6084, 0.6563],
                       #       [0.6411, 0.4967, 0.2643]]])

print('a.shape', a.shape)  #  torch.Size([1, 2, 3])

print('a[0]', a[0])  # tensor([[0.1648, 0.6084, 0.6563],
                       #       [0.6411, 0.4967, 0.2643]]) 

print(list(a.shape))  # 把torch.Siaze([1, 2, 3])数据类型转换为列表
                      # [1, 2, 3]

dim3:

1 使用场景RNN

10word,100特征(来表示单词的意思)

10word,20句话, 100特征

适合NLP自然语言处理,文字处理

9 dimension为4的 vector 向量

1 使用场景

适合卷积神经网络,适合图片处理

(2, 3, 28, 28)

(b, c, h, w)

2张照片 batch

3通道 channel

28*28像素mnist  height weight

 

10 Mixed

number of elment

2*3*28*28=4708

a = torch.rand(2, 3, 28, 28)
print(a.shape)  # torch.Size([2, 3, 28, 28])

print(a.numel())  # 4704

print(a.dim())  # 4
# 等同于
print(len(a.shape))  # 4

a = torch.tensor(1)  # tensor接受的是具体的数据1 
print(a.dim())  # 0

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

计算机视觉-Archer

图像分割没有团队的同学可加群

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值