pytorch学习(1)
pytorch的基本数据类型
- 在torch中默认的数据类型是32位浮点型(torch.FloatTensor)
- 可以通过torch.set_default_tensor_type()函数设置默认的数据类型,但该函数只支持设置浮点型数据类型
Data type | dtype | CPU tensor | GPU tensor |
---|---|---|---|
32-bit floating | torch.float | torch.FloatTensor | torch.cuda.FloatTensor |
6-bit floating | torch.double | torch.DoubleTensor | torch.cuda.DoubleTensor |
16-bit floating | torch.half | torch.HalfTensor | torch.cuda.HalfTensor |
8-bit integer(unsigned) | torch.uint8 | torch.ByteTensor | torch.cuda.ByteTensor |
8-bit integer(signed) | torch.int8 | torch.CharTensor | torch.cuda.CharTensor |
16-bit integer(signed) | torch.short | torch.ShortTensor | torch.cuda.ShortTensor |
32-bit integer(signed) | torch.int | torch.IntTensor | torch.cuda.IntTensor |
64-bit integer(signed) | torch.long | torch.LongTensor | torch.cuda.LongTensor |
字符串的表达
- one-hot编码(独热编码)
- word embedding(词嵌入)
- word2vec
- GloVe
标量
- loss 一般是一个标量,标量维度等于0
import torch
a = torch.tensor(1)
print(a)
print(type(a))
print(a.dim()) #检验维度的方法
print(len(a.shape)) #检验维度的方法,与a.dim()返回值相同
-----------------------------------------------------------------------
tensor(1)
<class 'torch.Tensor'>
0
0
张量
- 查看设备 a.device
- 判断 isinstance(a, torch.LongTensor) ---输出值为True或False
- 查看形状(即具体维度分量)a.shape / a.size()
import torch
a = torch.tensor([1,2,3,4])
print(a.device)
print(isinstance(a, torch.LongTensor))
print(a.shape)
print(a.size())
-----------------------------------------------------------------------
cpu
True
torch.Size([4]) #表示该张量为1维,且该tensor中含有四个元素
torch.Size([4])
维度dim=1(bias,linear layer input 线性层的输入)
# dim=1 ---linear layer input 线性层的输入
import torch
b = torch.tensor([1,2,3,4])
print(b)
print(b.dim())
print(b.type())
print(b.device)
print(b.shape)
print(b.item) #得到tensor中的元素值
-------------------------------------------------------
tensor([1, 2, 3, 4])
1
torch.LongTensor
cpu
torch.Size([4])
<built-in method item of Tensor object at 0x000001C08A635958>
维度dim=2(batch,linear layer input)
import torch
c = torch.tensor([[1,2,3],[5,6,7]])
print(c)
print(c.dim())
print(c.type())
print(c.device)
print(c.shape)
-------------------------------------------------------
tensor([[1, 2, 3],
[5, 6, 7]])
2
torch.LongTensor
cpu
torch.Size([2, 3])
维度dim=3(RNN循环神经网络)
import torch
d = torch.tensor([[[1,2,3,4],[5,6,7,8]]])
print(d)
print(d.device)
print(d.type())
print(d.dim())
print(d.shape)
print(torch.numel(d)) #---输出元素的个数
tensor([[[1, 2, 3, 4],
[5, 6, 7, 8]]])
cpu
torch.LongTensor
3
torch.Size([1, 2, 4])
8
维度dim=4(CNN卷积神经网络)
import torch
dim_4 = torch.rand(2,3,32,32)
#创建一个矩阵,数值随机[照片数量,rgb色彩通道,高度,宽度]
print(dim_4.shape)
print(dim_4)
print(dim_4.type)
print(dim_4.dim())
-------------------------------------------------------
torch.Size([2, 3, 32, 32])
tensor([[[[0.3390, 0.9483, 0.3298, ..., 0.2760, 0.0703, 0.2972],
[0.0949, 0.2986, 0.4318, ..., 0.4174, 0.1951, 0.7157],
[0.0490, 0.5559, 0.3220, ..., 0.2061, 0.0510, 0.5373],
...,
[0.9616, 0.8165, 0.3552, ..., 0.5538, 0.0972, 0.3558],
[0.4873, 0.4493, 0.1939, ..., 0.0287, 0.2172, 0.3970],
[0.4623, 0.2889, 0.6900, ..., 0.0053, 0.6636, 0.3639]],
[[0.0261, 0.7910, 0.5141, ..., 0.9742, 0.4137, 0.4942],
[0.7926, 0.2910, 0.4155, ..., 0.4437, 0.2966, 0.1524],
[0.8576, 0.6514, 0.8738, ..., 0.4547, 0.8521, 0.9133],
...,
[0.5866, 0.4359, 0.5897, ..., 0.3321, 0.3467, 0.3109],
[0.7515, 0.2906, 0.7983, ..., 0.8138, 0.8543, 0.5278],
[0.2317, 0.1437, 0.8601, ..., 0.1081, 0.5985, 0.0319]],
[[0.2833, 0.2059, 0.4972, ..., 0.1107, 0.9525, 0.2098],
[0.7568, 0.1247, 0.5259, ..., 0.6636, 0.7032, 0.7777],
[0.0343, 0.3005, 0.4679, ..., 0.5751, 0.2757, 0.2981],
...,
[0.2303, 0.0075, 0.9801, ..., 0.5632, 0.2719, 0.0312],
[0.6431, 0.7712, 0.5269, ..., 0.6739, 0.9665, 0.5807],
[0.8637, 0.4354, 0.9796, ..., 0.8433, 0.5195, 0.5800]]],
[[[0.0621, 0.3406, 0.6101, ..., 0.0182, 0.5445, 0.0415],
[0.6412, 0.3514, 0.3366, ..., 0.9296, 0.7228, 0.5305],
[0.1690, 0.4457, 0.0372, ..., 0.7834, 0.7996, 0.4179],
...,
[0.6941, 0.3181, 0.0958, ..., 0.7913, 0.2281, 0.8063],
[0.9883, 0.6680, 0.3952, ..., 0.0072, 0.7170, 0.2350],
[0.2541, 0.4535, 0.8134, ..., 0.5470, 0.8399, 0.5755]],
[[0.3427, 0.4750, 0.8580, ..., 0.6513, 0.9364, 0.6572],
[0.5132, 0.9791, 0.9604, ..., 0.3622, 0.1767, 0.1125],
[0.8013, 0.9486, 0.3601, ..., 0.6069, 0.1177, 0.7456],
...,
[0.1332, 0.0858, 0.5337, ..., 0.9435, 0.5755, 0.4948],
[0.5014, 0.5651, 0.2074, ..., 0.1604, 0.7589, 0.7703],
[0.0965, 0.0679, 0.3821, ..., 0.4512, 0.5636, 0.9658]],
[[0.1001, 0.0370, 0.7520, ..., 0.5650, 0.4478, 0.4201],
[0.7473, 0.3291, 0.1565, ..., 0.7339, 0.1427, 0.2472],
[0.2715, 0.5282, 0.5252, ..., 0.3999, 0.0868, 0.5859],
...,
[0.1139, 0.2224, 0.6831, ..., 0.6763, 0.3141, 0.5551],
[0.4020, 0.5816, 0.2453, ..., 0.3932, 0.1766, 0.9528],
[0.9874, 0.4264, 0.3816, ..., 0.8513, 0.7539, 0.4453]]]])
<built-in method type of Tensor object at 0x000001C08A6509F8>
4
标签:dim,tensor,...,torch,数据类型,---,print,bit
From: https://www.cnblogs.com/311dih/p/16583846.html