【学习系列2】Pytorch

目录

Pytorch使用

1.张量Tensor

2.Pytorch中创建张量

3. Pytorch中tensor的常用方法

4. tensor的数据类型

5. tensor的其他操作


Pytorch使用

1.张量Tensor

 张量是一个统称,其中包含很多类型:
        1.0阶张量: 标量、常数,0-D Tensor
        2.1阶张量:向量,1-D Tensor
        3.2阶张量:矩阵,2-D Tensor
        4.3阶张量
        5. ..
        6.N阶张量

2.Pytorch中创建张量

1.使用python中的列表或者序列创建tensor

a = torch.Tensor([[1, 2, 3], [4, 5, 6]])
print(a)
'''
tensor([[1., 2., 3.],
        [4., 5., 6.]])
'''

2.使用numpy中的数组创建tensor

array = np.arange(12).reshape(3, 4)
print(array)
'''
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]
'''

3.使用torch的api创建tensor
        1.torch.empty(3,4)创建3行4列的空的tensor,会用无用数据进行填充

        2.torch.ones([3,4]) 创建3行4列的全为1的tensor

        3.torch.zeros([3,4])创建3行4列的全为0的tensor

        4.torth.rand([3,4]) 创建3行4列的随机值的tensor,随机值的区间是 [0,1)

rand = torch.rand(2, 3)
print(rand)
'''
tensor([[0.0311, 0.8508, 0.8894],
        [0.5359, 0.6662, 0.5128]])
'''

        5.torch,randint(1ow=0,high=10,size=[3,4])创建3行4列的随机整数的tensor,随机值的区间是[low,high)

randint = torch.randint(3, 10, (2, 2))
print(randint)
'''
tensor([[3, 3],
        [5, 6]])
'''

        6.torch.randn([3,4])创动3行4列的随机数的tensor,随机值的分布式均值为0,方差为1

3. Pytorch中tensor的常用方法

1.获取tensor中的数据(当tensor中只有一个元素可用):tensor .item()

t1 = torch.Tensor([[10]])
print(t1)
print(t1.item())
'''
10
'''

2.转化为numpy数组

t2 = t1.numpy()
print(t2)
'''
[[10.]]
'''

3.获取形状: tensor.size()

t3 = torch.rand(3, 4)
print(t3.size())
'''
torch.Size([3, 4])
'''

4.形状改变: tensor.view((3,4))。类似numpy中的reshape,是一种浅拷贝,仅仅是形状发生改变

t4 = t3.view(2, 6)
print(t3)
print(t4)
'''
tensor([[0.5064, 0.2271, 0.7398, 0.1612],
        [0.9324, 0.7501, 0.8088, 0.4055],
        [0.7744, 0.1162, 0.8764, 0.2311]])
tensor([[0.5064, 0.2271, 0.7398, 0.1612, 0.9324, 0.7501],
        [0.8088, 0.4055, 0.7744, 0.1162, 0.8764, 0.2311]])
'''

5.获取维(阶)数: tensor.dim()

print(t4.dim())
'''
2
'''

6.获取最大值: tensor.max()

print(t4)
print(t4.max())
'''
tensor([[0.6645, 0.9753, 0.1691, 0.9781, 0.9510, 0.5923],
        [0.7698, 0.4558, 0.5393, 0.7076, 0.2258, 0.8842]])
tensor(0.9781)
'''

7.转置: tensor.t() (在numpy中为 T)

print(t4)
print(t4.t())
'''
tensor([[0.8161, 0.3213, 0.7988, 0.9154, 0.0466, 0.0426],
        [0.3853, 0.7590, 0.3691, 0.5878, 0.1044, 0.6844]])
tensor([[0.8161, 0.3853],
        [0.3213, 0.7590],
        [0.7988, 0.3691],
        [0.9154, 0.5878],
        [0.0466, 0.1044],
        [0.0426, 0.6844]])
'''
t4 = torch.rand(2, 3, 4)
print(t4)
print(t4.transpose(0, 1))
print(t4.permute(1, 0, 2))
'''
tensor([[[0.5269, 0.0171, 0.0110, 0.3139],
         [0.9625, 0.0424, 0.1559, 0.9512],
         [0.2839, 0.9033, 0.7233, 0.7983]],

        [[0.4119, 0.1300, 0.7744, 0.7593],
         [0.7209, 0.8706, 0.0809, 0.3257],
         [0.7994, 0.2252, 0.3721, 0.4984]]])
tensor([[[0.5269, 0.0171, 0.0110, 0.3139],
         [0.4119, 0.1300, 0.7744, 0.7593]],

        [[0.9625, 0.0424, 0.1559, 0.9512],
         [0.7209, 0.8706, 0.0809, 0.3257]],

        [[0.2839, 0.9033, 0.7233, 0.7983],
         [0.7994, 0.2252, 0.3721, 0.4984]]])
tensor([[[0.5269, 0.0171, 0.0110, 0.3139],
         [0.4119, 0.1300, 0.7744, 0.7593]],

        [[0.9625, 0.0424, 0.1559, 0.9512],
         [0.7209, 0.8706, 0.0809, 0.3257]],

        [[0.2839, 0.9033, 0.7233, 0.7983],
         [0.7994, 0.2252, 0.3721, 0.4984]]])
'''

8.tensor[1,3] 获取tensor中第一行第三列的值

t4 = torch.rand(2, 3, 2)
print(t4)
print(t4[1, 2])
'''
tensor([[[0.0768, 0.1384],
         [0.9043, 0.0226],
         [0.0334, 0.2946]],

        [[0.9075, 0.4634],
         [0.8371, 0.1950],
         [0.3811, 0.9326]]])
tensor([0.3811, 0.9326])
'''

9.tensor[1,2]=100 对tensor中第一行第三列的位置进行赋值100

t4 = torch.rand(2, 3, 2)
print(t4)
print(t4[1, 2])
t4[1, 2] = 100
print(t4)
'''
tensor([[[0.2413, 0.9267],
         [0.7468, 0.1347],
         [0.8241, 0.6128]],

        [[0.1088, 0.0445],
         [0.5081, 0.5531],
         [0.4178, 0.0222]]])
tensor([0.4178, 0.0222])
tensor([[[2.4131e-01, 9.2668e-01],
         [7.4677e-01, 1.3468e-01],
         [8.2412e-01, 6.1279e-01]],

        [[1.0878e-01, 4.4516e-02],
         [5.0811e-01, 5.5308e-01],
         [1.0000e+02, 1.0000e+02]]])
'''

10.tensor的切片

4. tensor的数据类型

tensor中的数据类型非常多,常见类型如下:

上图中的Tensor types  表示这种type的tensor是实例

1. 获取tensor的数据类型:tensor.dtype

print(t4)
print(t4.dtype)
'''
tensor([[[  0.9967,   0.8391],
         [  0.9651,   0.7411],
         [  0.4698,   0.5371]],

        [[  0.9868,   0.1172],
         [  0.3568,   0.8628],
         [100.0000, 100.0000]]])
torch.float32
'''

2. 创建数据的时候指定类型

t5 = torch.ones([2, 3], dtype=torch.float32)
print(t5)
print(t5.dtype)
'''
tensor([[1., 1., 1.],
        [1., 1., 1.]])
torch.float32
'''

3. 类型的修改

t5 = torch.ones([2, 3], dtype=torch.int32)
print(t5.dtype)
t5 = t5.type(torch.float)
print(t5.dtype)
t5 = t5.double()
print(t5.dtype)
'''
torch.int32
torch.float32
torch.float64
'''

5. tensor的其他操作

1. tensor和tensor相加

x = torch.rand(3, 4)
x = x.new_ones((2, 6), dtype=torch.float32)
y = torch.rand(2, 6)
print(x)
print(y)
print(x + y)
'''
tensor([[1., 1., 1., 1., 1., 1.],
        [1., 1., 1., 1., 1., 1.]])
tensor([[0.0763, 0.2841, 0.7078, 0.7183, 0.9554, 0.5018],
        [0.0874, 0.9510, 0.0774, 0.1421, 0.1593, 0.8375]])
tensor([[1.0763, 1.2841, 1.7078, 1.7183, 1.9554, 1.5018],
        [1.0874, 1.9510, 1.0774, 1.1421, 1.1593, 1.8375]])
'''

print(torch.add(x, y))
'''
tensor([[1.0763, 1.2841, 1.7078, 1.7183, 1.9554, 1.5018],
        [1.0874, 1.9510, 1.0774, 1.1421, 1.1593, 1.8375]])
'''
print(x.add(y))
'''
tensor([[1.0763, 1.2841, 1.7078, 1.7183, 1.9554, 1.5018],
        [1.0874, 1.9510, 1.0774, 1.1421, 1.1593, 1.8375]])
'''

2. 原地修改的方法 

print(x.add_(y))  # 带下划线的方法会对x进行就地修改
'''
tensor([[1.0763, 1.2841, 1.7078, 1.7183, 1.9554, 1.5018],
        [1.0874, 1.9510, 1.0774, 1.1421, 1.1593, 1.8375]])
'''
print(x)
'''
tensor([[1.0763, 1.2841, 1.7078, 1.7183, 1.9554, 1.5018],
        [1.0874, 1.9510, 1.0774, 1.1421, 1.1593, 1.8375]])
'''

3. Gpu中的tensor的使用

  • 实例化device: torch.device("cuda:0"iftorch.cuda.is avaiable() else "cpu")
  • tensor.to(device) #把tensor转化为CUDA支持的tensor,或者cpu支持的tensor

猜你喜欢

转载自blog.csdn.net/WakingStone/article/details/129459297