1 导入pytorch包
import torch
2 创建随机张量,并查看张量的数据类型
float_32_tensor = torch.rand(3, 4)
float_32_tensor
tensor([[0.0453, 0.6924, 0.8790, 0.8977],
[0.2177, 0.5010, 0.2681, 0.0013],
[0.9107, 0.3847, 0.3516, 0.8254]])
tensor.dtype 来查看张量的数据类型
float_32_tensor.dtype
torch.float32
张量数据有三个比较关键的点
1 张量类型:torch.dtype
2 张量的形状:torch.shape 或者 torch.size()这里需要注意, 这两个方法,前者是属性,后者是函数
3 张量所处的设备:torch.device
将float32的数据类型转换为float16,使用 tensor.type(torch.float16)
float_16_tensor = float_32_tensor.type(torch.float16)
float_16_tensor.dtype
torch.float16
float_16_tensor.device
float_16_tensor.device
在张量上面的基本操作,加减乘除
首先创建一个tensor,使用之前学习的arange()方法
tensor = torch.arange(1, 11)
tensor
tensor([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
加, 每一项都加了10
tensor + 10
tensor([11, 12, 13, 14, 15, 16, 17, 18, 19, 20])
加, 使用add方法,建议还是前面的方法,更方便读者进行阅读代码
tensor.add(10)
tensor([11, 12, 13, 14, 15, 16, 17, 18, 19, 20])
减
tensor - 10
tensor([-9, -8, -7, -6, -5, -4, -3, -2, -1, 0])
乘
tensor * 10
tensor([ 10, 20, 30, 40, 50, 60, 70, 80, 90, 100])
乘的 mul() 方法
tensor.mul(10)
tensor([ 10, 20, 30, 40, 50, 60, 70, 80, 90, 100])
除
tensor / 2
tensor([0.5000, 1.0000, 1.5000, 2.0000, 2.5000, 3.0000, 3.5000, 4.0000, 4.5000,
5.0000])
除的 div() 方法
tensor.div(2)
tensor([0.5000, 1.0000, 1.5000, 2.0000, 2.5000, 3.0000, 3.5000, 4.0000, 4.5000,
5.0000])
矩阵的乘法
这里的问题主要是能不能相乘,要注意两个矩阵的内部维度要一致
比如(3,2) @ (2,3)这就是能够相乘的, (3, 2) @ (3, 2)这就是无法相乘的
tensor_A = torch.tensor([[1, 2], [3, 4], [5, 6]])
tensor_B = torch.tensor([[7, 8], [9, 10], [11, 12]])
print(f"tensor_A:\n{tensor_A}")
print(f"tensor_B:\n{tensor_B}")
print(f"tensor_A shape:\n{tensor_A.shape}")
print(f"tensor_B shape:\n{tensor_B.shape}")
tensor_A:
tensor([[1, 2],
[3, 4],
[5, 6]])
tensor_B:
tensor([[ 7, 8],
[ 9, 10],
[11, 12]])
tensor_A shape:
torch.Size([3, 2])
tensor_B shape:
torch.Size([3, 2])
试验, 这样应该是无法相乘的
print(f"tensor_A * tensor_B:\n {tensor_A @ tensor_B}")
我们将tensor_B进行转置
tensor_B.T, tensor_B.T.shape
(tensor([[ 7, 9, 11],
[ 8, 10, 12]]),
torch.Size([2, 3]))
将tensor_B 进行转置就可以相乘了,因为它的内部是一样的,满足矩阵相乘的条件
print(f"tensor_A * tensor_B.T:\n{tensor_A @ tensor_B.T}")
tensor_A * tensor_B.T:
tensor([[ 23, 29, 35],
[ 53, 67, 81],
[ 83, 105, 127]])
矩阵相乘,我这里用的符号是 @ , 还有方法matmul(), mm(), 这个mm()是matmul()的缩写
print(f"tensor_A * tensor_B.T by matmul function:\n{tensor_A.matmul(tensor_B.T)}\n")
print(f"tensor_A * tensor_B.T by mm function:\n{tensor_A.mm(tensor_B.T)}")
tensor_A * tensor_B.T by matmul function:
tensor([[ 23, 29, 35],
[ 53, 67, 81],
[ 83, 105, 127]])
tensor_A * tensor_B.T by mm function:
tensor([[ 23, 29, 35],
[ 53, 67, 81],
[ 83, 105, 127]])
张量的聚合(aggregation)
聚合指的是min, max, mean
将一堆数据变成一个值不就是聚合嘛
tensor
tensor([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
min
tensor.min(), torch.min(tensor)
(tensor(1), tensor(1))
max
torch.max(tensor), tensor.max()
(tensor(10), tensor(10))
mean 平均值
tensor.mean(), torch.mean(tensor)
这里出现问题了,什么意思:不能推断出输出类型。输入类型要么是一个浮点型要么是一个复杂类型。获得:Long
那我们这里受限转换数据类型咯
tensor.type(torch.float32).mean(), torch.mean(tensor.type(torch.float32))
(tensor(5.5000), tensor(5.5000))
这样就成功啦,这里就涉及到我们刚刚提到过的,张量三个方面的第一个方面,数据类型的问题了
好,接下来让我们看看,找到了最大值和最小值,我们如何知道最大值和最小值所在的位置
这里就涉及到了 torch.argmax() 和 torch.argmin() 了
print(f"This is tensor:\n{tensor}")
print(f"This is torch.argmax(tensor)\n{torch.argmax(tensor)}")
print(f"This is torch.argmin(tensor)\n{torch.argmin(tensor)}")
print(f"This is tensor.argmax()\n{tensor.argmax()}")
print(f"This is tensor.argmin()\n{tensor.argmin()}")
This is tensor:
tensor([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
This is torch.argmax(tensor)
9
This is torch.argmin(tensor)
0
This is tensor.argmax()
9
This is tensor.argmin()
0
ok, 今天的学习就到这里啦!nice,今天买了一个帽子和一个超级可爱的小奶龙,好开心呀~
BB,记得找点让自己开心的事情哟~
虽然也花了一点米米,but千金难买我开心,而且这也是我需要的东西呀~