PyTorch的数据类型与创建

PyTorch的数据类型与创建

数据对比:
pythonPyTorch
IntIntTensor of size()
FloatFloatTensor of size()
Int arrayIntTensor of size[d1, d2,…]
FloatFloatTensor of size [d1, d2,…]
string没有对string的支持,可以One-hot/Embedding的编码表示
数据类型
Data typedtypeCPU tensorGPU tensor
32-bit floating pointtorch.float32 or torch.floattorch.FloatTensortorch.cuda.FloatTensor
64-bit floating pointtorch.float64 or torch.dobletorch.DoubleTensortorch.cuda.DoubleTensor
16-bit floating pointtorch.float16 or torch.halftorch.HalfTensortorch.cuda.HalfTensor
8-bit integer (unsigned)torch.unit8torch.ByteTensortorch.cuda.ByteTensor
8-bit integer (signed)torch.int8torch.CharTensortorch.cuda.CharTensor
16-bit integer (signed)torch.shorttorch.ShortTensortorch.cuda.ShortTensor
32-bit integer (signed)torch.inttorch.IntTensortorch.cuda.IntTensor
64-bit integer (signed)torch.longtorch.LongTensortorch.cuda.LongTensor
类型检查:
In [1]: a = torch.randn(2,3)
In [2]: a.type()
out[3]: 'torch.FloatTensor'
In [4]: type(a)
out[5]: torch.Tensor
In [6]: isinstance(a, torch.FloatTensor)
out[7]: True

isinstance():检查数据是否为某种类型

标量:torch.tensor()

Pytorch0.3之前的版本dim=0会返回1tensor

dim为0,loss

dim为1,Bias; Linear Input

dim为2,Linear Input batch

dim为3,RNN Input Batch

dim为4,图片:数量,通道,高,宽。适合卷积神经网络

数组导入
a = np.array([2,3,3])
torch.from_numpy(a)
列表导入
torch.tensor([2.,3.2])
torch.FlostTensor([2.,3.2])


tensor:大写接收维度,小写接收数据。多用小写的,直接用现成的data。创建后shape不变


torch.empty()
torch.IntTensor()

rand:随机0-1之间的数据
rand_like(shape):生成指定shape的tensor
randint(mix_num,max_num,shape):指定最小值,最大值,shape,随机生成tensor.(图中第15行为:torch.randint(1,10,(3,3)) )
01
正态分布:randn
02
full:用于全部赋值为同一个数
03
生成递增或递减的等差数列,arrange(a,b,s),a表示起始值,b表示上限但不包括在数列中,s表示递增或递减的步长
04
生成等分的数列,linspacee(a,b,steps=num),a表示起始值,b表示上限但不包括在数列中,steps=num表示等分的个数
05
生成全为1,全为0和对角线的tensor,eye只能接收2个以下的参数
06
randperm:随机打散;random.shuffle:随机数种子
07

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值