1.向量是一阶张量,矩阵是二阶张量。
张量算法:按元素相加,A+B;按元素相乘,Hadamard积 A*B。
#创建两个4*4的张量
import torch
A = torch.arange(16).reshape(4,4)
print("A=",A)
B = A
print("B=",B)
out:
A= tensor([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15]])
B= tensor([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15]])
#按元素相加,A+B;
A+B
out:
tensor([[ 0, 2, 4, 6],
[ 8, 10, 12, 14],
[16, 18, 20, 22],
[24, 26, 28, 30]])
#按元素相乘,Hadamard积 A*B
A*B
out:
tensor([[ 0, 1, 4, 9],
[ 16, 25, 36, 49],
[ 64, 81, 100, 121],
[144, 169, 196, 225]])
#降维:调⽤求和函数sum()会沿所有的轴降低张量的维度,使它变为⼀个标量
A.sum(0)
out:tensor([24, 28, 32, 36])
#在0维的元素个数
A.shape[0]
out:
4
2.维度含义
创建一个列表,并转换成2*2*4的张量,三个维度分别为C*H*W(channels*height*weight)
#C*H*W channels*height*weight
C = [[[0,1,2,3],[4,5,6,7]],[[0,1,2,3],[4,5,6,7]]]
C = torch.tensor(C)#列表转张量类型
print("C=",C)
out:
C= tensor([[[0, 1, 2, 3],
[4, 5, 6, 7]],
[[0, 1, 2, 3],
[4, 5, 6, 7]]])
3.按不同纬度求和
#按深度(通道)求和:H*W 2*4
C.sum(axis=0)
out:
tensor([[ 0, 2, 4, 6],
[ 8, 10, 12, 14]])
#按高度(列)求和:C*W 2*4
C.sum(axis=1)
out:
tensor([[ 4, 6, 8, 10],
[ 4, 6, 8, 10]])
#按宽度(行)求和:C*H 2*2
C.sum(axis=2)
out:
tensor([[ 6, 22],
[ 6, 22]])
#⾮降维求和(保持维度不变):xTy或<x,y>
D=C.sum(axis=1, keepdims=True)
print("D=",D)
D.shape
out:
D= tensor([[[ 4, 6, 8, 10]],
[[ 4, 6, 8, 10]]])
torch.Size([2, 1, 4])
4.reshape_view_flatten_transpose张量维度转换
import torch
x=torch.arange(0,16)
print(x)
x.shape
type(x)
out:
tensor([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])
torch.Tensor
4.1view()
y=x.view(4,4)
print(y)
y.shape
type(y)
y=torch.transpose(y,0,1)
print(y)
out:
tensor([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15]])
tensor([[ 0, 4, 8, 12],
[ 1, 5, 9, 13],
[ 2, 6, 10, 14],
[ 3, 7, 11, 15]])
z=x.view(-1,8)
print(z)
z.shape
type(z)
print(x)
out:
tensor([[ 0, 1, 2, 3, 4, 5, 6, 7],
[ 8, 9, 10, 11, 12, 13, 14, 15]])
tensor([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])
4.2reshape()
w=x.reshape(-1,4*4)
print(w)
w.shape
out:
tensor([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]])
torch.Size([1, 16])
4.3flatten()
v=x.flatten()
print(v)
v.shape
out:
tensor([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])
torch.Size([16])
4.4transpose()
z=torch.transpose(z, 0, 1)
z
out:
tensor([[ 0, 8],
[ 1, 9],
[ 2, 10],
[ 3, 11],
[ 4, 12],
[ 5, 13],
[ 6, 14],
[ 7, 15]])
5.自注意力中点积相似度的运算示范
import torch
q = torch.arange(24).reshape(2, 2, 2, 3)#batch,heards,len_q,d_model
k = torch.arange(24).reshape(2, 2, 2, 3)#batch,heards,len_k,d_model
print(q)
print(k)
out:
tensor([[[[ 0, 1, 2],
[ 3, 4, 5]],
[[ 6, 7, 8],
[ 9, 10, 11]]],
[[[12, 13, 14],
[15, 16, 17]],
[[18, 19, 20],
[21, 22, 23]]]])
tensor([[[[ 0, 1, 2],
[ 3, 4, 5]],
[[ 6, 7, 8],
[ 9, 10, 11]]],
[[[12, 13, 14],
[15, 16, 17]],
[[18, 19, 20],
[21, 22, 23]]]])
score = torch.matmul(q,k.transpose(-1,-2))#最后两位做矩阵乘法
print(score)
print(score.shape)
out:
tensor([[[[ 5, 14],
[ 14, 50]],
[[ 149, 212],
[ 212, 302]]],
[[[ 509, 626],
[ 626, 770]],
[[1085, 1256],
[1256, 1454]]]])
torch.Size([2, 2, 2, 2])
python的学习还是要多以练习为主,想要练习python的同学,推荐可以去看,他们现在的IT题库内容很丰富,属于国内做的很好的了,而且是课程+刷题+面经+求职+讨论区分享,一站式求职学习网站,最最最重要的里面的资源全部免费。
他们这个python的练习题,知识点编排详细,题目安排合理,题目表述以指导的形式进行。整个题单覆盖了Python入门的全部知识点以及全部语法,通过知识点分类逐层递进,从Hello World开始到最后的实践任务,都会非常详细地指导你应该使用什么函数,应该怎么输入输出。
牛客网(牛客网 - 找工作神器|笔试题库|面试经验|实习招聘内推,求职就业一站解决_牛客网)还提供题解专区和讨论区会有大神提供题解思路,对新手玩家及其友好,有不清楚的语法,不理解的地方,看看别人的思路,别人的代码,也许就能豁然开朗。
快点击下方链接学起来吧!