目录
一、张量和numpy 转换
二、张量运算
三、张量的索引
四、张量的计算函数
五、张量 形状改变
六、张量的拼接
一、张量和numpy 转换
关键:
1.t0.numpy().copy() 不共享内存
2.ndarray -> 共享内存
3.张量 -> 标量 (只支持一个元素)
""" 张量 -> ndarray 深copy 不共享内存 """ t0 = torch.randint(low=0,high=10,size=(2,3)) print(t0) # 深copy 不共享内存 arr0 = t0.numpy().copy() print(arr0) """ ndarray -> 张量 共享内存 """ arr1 = np.array([[1,2,3],[4,5,6],[7,8,9]]) t1 = torch.from_numpy(arr1) t1[0][0] = -1 print(t1) print(arr1) """ 张量 -> 标量 只支持一个元素 """ t2 = torch.tensor(10) print(t2.item())二、张量运算
关键点:
1.前提条件 形状相同
add、sub、mul、div、neg(取反 = 相当于 * -1)
add_、sub_、mul_、div_、neg_ (带_会改变自身)
2.矩阵点乘元素1:1对应 形状不变
用*或者mul函数
3.矩阵相乘
(前提) 前一个矩阵的列 和 后一个矩阵的行 相同
用@或者matmul函数
t0 = torch.tensor([10,20,30]) t1 = torch.tensor([11,21,31]) """ 前提条件 形状相同 add、sub、mul、div、neg(取反 = 相当于 * -1) add_、sub_、mul_、div_、neg_ (带_ 会改变自身) """ print('乘法: -> ') print(t0.mul_(2)) print('加法: -> ') print(t0 + t1) """ 矩阵 点乘 元素1:1对应 形状不变 * 或者 mul 函数 """ t00 = torch.tensor([[1,2,3],[4,5,6]]) t01 = torch.tensor([[10,20,30],[40,50,60]]) print('矩阵 点乘: * ->') t_new = t00 * t01 print(t_new) """ 矩阵 相乘 (前提) 前一个矩阵的列 和 后一个矩阵的行 相同 @ 或者 matmul 函数 """ t000 = torch.tensor([[1,2,3],[4,5,6],[7,8,9]]) # (2,3) 3行 t001 = torch.tensor([[10,100],[20,200],[30,300]]) # (3,2) 1列 print('矩阵 相乘: @ -> ') # [[1 * 10 + 2 * 20 + 3 * 30],[1 * 100 + 2 * 200 + 3 * 300]] = [[140],[1400], # [[4 * 10 + 5 * 20 + 6 * 30],[4 * 100 + 5 * 200 + 6 * 300]] = [320],[3200], # [[7 * 10 + 8 * 20 + 9 * 30],[7 * 300 + 8 * 200 + 9 * 300]] = [500],[5000]] t_new1 = t000@t001 # t_new1 = t000.matmul(t001) print(t_new1)三、张量的索引
t0 = torch.randint(0, 10, (4,5)) print(t0) r1 = t0[:-1] #相当于取 (3,3) 里的 (2,3) print(r1) r2 = t0[:,-1] #相当于取 (4,5) 里面的 最后一列 # r2 = t0[:,2] #相当于取 (4,5) 里面的 取第三列 print(r2) r3 = t0[1:3,:2] #相当于取 第2 第3行 的前两列 print(r3) r4 = t0[1::2,:2] #从第2行开始,间隔1行 的所有行的前两列 print(r4) r5 = t0[[2,3],[3,4]] #取 【(3,4) , (4,5)】的值 print(r5) r6 = t0[[[2],[3]],[3,4]] #取 [[(3,4) (3,5)],[(4,4),(4,5)]]的值 print(r6) r7 = t0[:,t0[1]>5] #取 t0[1]这行 大于5这个值的 对应列 print(r7)四、张量的计算函数
函数:
均值 mean 函数
累加 sum
最小值 min
最大值 max
pow 次幂 pow(2)
根号 sqrt
e的x次方 exp
对数 log
等
必须是浮点型,这里只举一个例子。
t0 = torch.randint(low=0, high=10, size=(3,4),dtype=torch.float) print(t0) print(t0.mean()) print(t0.mean(dim=0)) #第0维 求平均 3个元素的 print(t0.mean(dim=1)) #第1维 求平均 4个元素的五、张量 形状改变
关键:
1. reshape 修改形状
(-1,1) 第一个参数代表行数自动算,第二个参数,分几列
2.升维度 unsqueeze 默认dim=0. unsqueeze(dim=?).
3.降维度 squeeze 降维 只能降维度为1的 ,dim=-1 时,降最里面的
4.换维度 transpose(dim= ?,dim=?) 两个维度兑换
5.同时换 permute (dims=(1,2,0)) 这里1,2,0 是维度的索引
import torch t0 = torch.tensor([[1, 2, 3,4], [5, 6, 7, 8]]) # size (2,4) -> (8,1) 总个数要相同 """ tensor([[1], [2], [3], [4], [5], [6], [7], [8]]) """ t1 = t0.reshape(8,1) print(t1) """ tensor([[1, 2], [3, 4], [5, 6], [7, 8]]) 参1: 行数自动算 参2: 2列 """ t2 = t0.reshape(-1,2) print(t2) """ t0: [[1, 2, 3,4], [5, 6, 7, 8]] (2,4) 升维度 unsqueeze """ """ t0: [[1, 2, 3,4], [5, 6, 7, 8]] t30 = [[[1, 2, 3,4], [5, 6, 7, 8]]] """ t30 = t0.unsqueeze(dim=0) #(2,4) -> (1,2,4) """ t0: [[1, 2, 3,4], [5, 6, 7, 8]] t31 = [[[1, 2, 3,4]], [[5, 6, 7, 8]]] """ t31 = t0.unsqueeze(dim=1) #(2,4) -> (2,1,4) """ t0: [[1, 2, 3,4], [5, 6, 7, 8]] t32 = [[[1], [2], [3],[4]], [5], [6], [7], [8]]] """ t32 = t0.unsqueeze(dim=2) #(2,4) -> (2,4,1) print(t30) print('=' * 50) print(t31) print('=' * 50) print(t32) """ 降维度 squeeze 只能降维度为1的 (1,2,4) -> (2,4) """ t4 = torch.tensor( [[[1, 2, 3,4], [5, 6, 7, 8]]]) print(t4.shape) t40 = t4.squeeze() print(t40) print(t40.shape) """ torch.Size([2, 1, 3, 1, 4, 1]) torch.Size([2, 3, 4]) """ t5 = torch.randint(0,10,size=(2,1,3,1,4,1)) print(t5.shape) t50 = t5.squeeze() print(t50.shape) """ torch.Size([2, 1, 3, 1, 4, 1]) torch.Size([2, 1, 3, 1, 4]) 降最里面的 """ t51 = t5.squeeze(dim=-1) print(t51.shape) """ 换维度 # 需求1:(3,4,5) -> (3,5,4) t1 = t0.transpose(dim0=2,dim1=1) # 需求2:(3,4,5) -> (4,5,3) t2 = t0.transpose(dim0=0,dim1=1).transpose(dim0=1,dim1=2) # permute:同一时刻可以交换任意个位置的形状 # 需求:(3,4,5) -> (4,5,3) # 索引:(0,1,2) -> (1,2,0) t1 = t0.permute(dims=(1,2,0)) """六、张量的拼接
关键:
1. cat
dim=0 拼行 ,列数要相同
dim=1 拼列, 行数要相同
拼接的那个维度,元素个数要一致!!!
2.stack
两个完全相同形状,同时拼接后结果会进行升维
""" cat/stack 拼接 cat 拼接的那个维度 元素个数要一致 比如 (2,3) (2,4) 不能行拼接,只能列拼接 (2,3) (3,3) 只能行拼接,不能列 """ """ t0 [[1,2,3], t1 [[11,12,13], [4,5,6]] [14,15,16]] tensor([[ 1, 2, 3], tensor([[ 1, 2, 3, 11, 12, 13], [ 4, 5, 6], [ 4, 5, 6, 14, 15, 16]]) [11, 12, 13], [14, 15, 16]]) torch.Size([4, 3]) torch.Size([2, 6]) """ t0 = torch.tensor([[1,2,3],[4,5,6]]) t1 = torch.tensor([[11,12,13],[14,15,16]]) cat_t1 = torch.cat([t0,t1],dim=0) # 二维张量的情况下,dim=0按行进行拼接 print(cat_t1) print(cat_t1.shape) cat_t2 = torch.cat([t0,t1],dim=1) # 二维张量的情况下,dim=0按行进列拼接 print(cat_t2) print(cat_t2.shape) """ stack 两个完全相同形状,同时拼接后结果会进行升维 t0 [[1,2,3], t1 [[11,12,13], [4,5,6]] [14,15,16]] (2,3) (2,3) -> (2,2,3) dim=0 行拼升维 -> (2,2,3) tensor([[[ 1, 2, 3], [ 4, 5, 6]], [[11, 12, 13], [14, 15, 16]]]) -> (2,2,3) dim=1 列拼升维 -> (2,2,3) tensor([[[ 1, 2, 3], [11, 12, 13]], [[ 4, 5, 6], [14, 15, 16]]]) -> (2,2,3) dim=2 维度2 -> (2,3,2) tensor([[[ 1, 11], [ 2, 12], [ 3, 13]], [[ 4, 14], [ 5, 15], [ 6, 16]]]) """ stack_t1 = torch.stack([t0,t1]) print(stack_t1) print(stack_t1.shape)