1.矩阵的转置

方法:t()
a=torch.randint(1,10,[2,3])
print(a,'\n')
print(a.t())

输出结果

tensor([[2, 8, 2],
[9, 2, 4]]) tensor([[2, 9],
[8, 2],
[2, 4]])
transpose(维度下标1,维度下标2):任意两个维度之间的转换
a=torch.randint(1,10,[2,3,4,5])
print(a.shape)
a1=a.transpose(1,3)
print(a1.shape)

输出结果

torch.Size([2, 3, 4, 5])
torch.Size([2, 5, 4, 3])
permute(维度的下标):所有维度之间的任意转换
a=torch.randint(1,10,[2,3,4,5])
print(a.shape)
a1=a.permute(2,3,1,0)
print(a1.shape)

输出结果

torch.Size([2, 3, 4, 5])
torch.Size([4, 5, 3, 2])

2.矩阵的四则运算

矩阵的加法:2行3列矩阵+2行3列矩阵:
a=torch.randint(1,10,[2,3])
b=torch.randint(1,10,[2,3])
print(a,'\n')
print(b,'\n')
print(a+b,'\n')

输出结果

tensor([[4, 1, 8],
[6, 7, 4]]) tensor([[9, 7, 1],
[5, 1, 6]]) tensor([[13, 8, 9],
[11, 8, 10]])
2行3列矩阵+1行3列矩阵:会先将第二个矩阵复制一行,然后再相加
a=torch.randint(1,10,[2,3])
b=torch.randint(1,10,[1,3])
print(a,'\n')
print(b,'\n')
print(a+b,'\n')

输出结果

tensor([[9, 2, 3],
[2, 7, 9]]) tensor([[7, 8, 2]]) tensor([[16, 10, 5],
[ 9, 15, 11]])
2行1列矩阵+1行3列矩阵:会先将第一个矩阵复制成三列,然后将第二个矩阵复制成两行,再进行相加
a=torch.randint(1,10,[2,1])
b=torch.randint(1,10,[1,3])
print(a,'\n')
print(b,'\n')
print(a+b,'\n')

输出结果

tensor([[3],
[2]]) tensor([[4, 2, 5]]) tensor([[7, 5, 8],
[6, 4, 7]])
cat(所要相加的矩阵,维度):两个矩阵的某个维度相加

除了相加的维度之外,其余的维度的值必须相同

a=torch.randint(1,10,[2,3])
b=torch.randint(1,10,[1,3])
print(a.shape)
print(b.shape)
print(torch.cat([a,b],dim=0).shape,'\n')

输出结果

torch.Size([2, 3])
torch.Size([1, 3])
torch.Size([3, 3])
stack():会在所相加维度之前加一个2维的维度,用于两个tensor相加,但不想合并。
a=torch.randint(1,10,[1,3])
b=torch.randint(1,10,[1,3])
print(a.shape)
print(b.shape)
print(torch.stack([a,b],dim=0).shape)
print(torch.stack([a,b],dim=1).shape)

输出结果

torch.Size([1, 3])
torch.Size([1, 3])
torch.Size([2, 1, 3])
torch.Size([1, 2, 3])
矩阵的外积
a=torch.tensor([[1,2],[3,4]])
b=torch.tensor([[1,2],[3,4]])
print(a)
print(b)
print(a*b)

输出结果

tensor([[1, 2],
[3, 4]])
tensor([[1, 2],
[3, 4]])
tensor([[ 1, 4],
[ 9, 16]])
matmul(矩阵a,矩阵b): 计算矩阵的内积(推荐)
@:计算矩阵的内积
a=torch.tensor([[1,2],[3,4]])
b=torch.tensor([[1,2],[3,4]])
print(a)
print(b)
print(torch.matmul(a,b)) #推荐使用此方法
print(a@b) # 不推荐

输出结果

tensor([[1, 2],
[3, 4]])
tensor([[1, 2],
[3, 4]])
tensor([[ 7, 10],
[15, 22]])
tensor([[ 7, 10],
[15, 22]])

Pytorch-tensor的转置,运算的更多相关文章

  1. Pytorch Tensor 常用操作

    https://pytorch.org/docs/stable/tensors.html dtype: tessor的数据类型,总共有8种数据类型,其中默认的类型是torch.FloatTensor, ...

  2. pytorch 不使用转置卷积来实现上采样

    上采样(upsampling)一般包括2种方式: Resize,如双线性插值直接缩放,类似于图像缩放,概念可见最邻近插值算法和双线性插值算法——图像缩放 Deconvolution,也叫Transpo ...

  3. Pytorch Tensor, Variable, 自动求导

    2018.4.25,Facebook 推出了 PyTorch 0.4.0 版本,在该版本及之后的版本中,torch.autograd.Variable 和 torch.Tensor 同属一类.更确切地 ...

  4. pytorch tensor与numpy转换

    从官网拷贝过来的,就是做个学习记录.版本 0.4 tensor to numpy a = torch.ones(5) print(a) 输出 tensor([1., 1., 1., 1., 1.]) ...

  5. pytorch tensor 维度理解.md

    torch.randn torch.randn(*sizes, out=None) → Tensor(张量) 返回一个张量,包含了从标准正态分布(均值为0,方差为 1)中抽取一组随机数,形状由可变参数 ...

  6. pytorch tensor的索引与切片

    切片方式与numpy是类似. * a[:2, :1, :, :], * 可以用-1索引. * ::2,表示所有数据,间隔为2,即 start:end:step. *  a.index_select(1 ...

  7. Pytorch Tensor 维度的扩充和压缩

    维度扩展 x.unsqueeze(n) 在 n 号位置添加一个维度 例子: import torch x = torch.rand(3,2) x1 = x.unsqueeze(0) # 在第一维的位置 ...

  8. 深度学习框架PyTorch一书的学习-第三章-Tensor和autograd-1-Tensor

    参考https://github.com/chenyuntc/pytorch-book/tree/v1.0 希望大家直接到上面的网址去查看代码,下面是本人的笔记 Tensor Tensor可以是一个数 ...

  9. [Pytorch]Pytorch中tensor常用语法

    原文地址:https://zhuanlan.zhihu.com/p/31494491 上次我总结了在PyTorch中建立随机数Tensor的多种方法的区别. 这次我把常用的Tensor的数学运算总结到 ...

  10. 【小白学PyTorch】10 pytorch常见运算详解

    参考目录: 目录 1 矩阵与标量 2 哈达玛积 3 矩阵乘法 4 幂与开方 5 对数运算 6 近似值运算 7 剪裁运算 这一课主要是讲解PyTorch中的一些运算,加减乘除这些,当然还有矩阵的乘法这些 ...

随机推荐

  1. Codeforces Round 916 (Div. 3)(A~E2)

    A 统计一下每个字母的出现次数然后输出即可 #include <bits/stdc++.h> #define rep(i,a,b) for(register int i = (a); i ...

  2. c 语言默认什么编码

    C语言是没有编码的.它的编码就是平台的默认编码.比方说在windows 上汉字编码用gb2312 或者 说cp936(GBK一般的windows默认代码页,windows分为不同的代码页,可以查看一下 ...

  3. JS(循环)

    一 for循环 在程序中,一组被重复执行的语句被称之为循环体,能否继续重复执行,取决于循环的终止条件.由循环体及循环的终止条件组成的语句,被 称之为循环语句 1 语法结构 for循环主要用于把某些代码 ...

  4. python面向对象编程(封装、隐藏)

    一 封装 1.封装介绍封装是面向对象三大特性最核心的一个特性封装<----->整合2.将封装的属性进行隐藏操作1).如何隐藏:在属性名前加__前缀,就会实现一个对外隐藏属性效果该隐藏需要注 ...

  5. Flutter如何状态管理

    目录介绍 01.什么是状态管理 02.状态管理方案分类 03.状态管理使用场景 04.Widget管理自己的状态 05.Widget管理子Widget状态 06.简单混合管理状态 07.全局状态如何管 ...

  6. 记录--一道字节面试题引出的this指向问题

    这里给大家分享我在网上总结出来的一些知识,希望对大家有所帮助 var length = 10; function fn () { return this.length + 1; } var obj = ...

  7. C++ pieces

    standard lib fmax double fmax (double x, double y); float fmax (float x, float y); long double fmax ...

  8. .NET开源、免费、强大的交互式绘图库

    前言 今天大姚给大家分享一款.NET开源(采用MIT许可证).免费.强大的交互式绘图库,该库能够轻松地实现大型数据集的交互式显示.使用几行代码即可快速创建折线图.柱状图.饼图.散点图等不同类型的图表: ...

  9. 【FastDFS】面试官:如何实现文件的大规模分布式存储?(全程实战)

    写在前面 在<[FastDFS]小伙伴们说在CentOS 8服务器上搭建FastDFS环境总报错?>一文中,详细的介绍了如何在CentOS 8服务器行搭建FastDFS环境.在生产环境中, ...

  10. #根号分治,动态规划#洛谷 5616 [MtOI2019]恶魔之树

    题目传送门 分析 最小公倍数最终一定会被表示成若干个质数指数幂的情况(1的情况就直接乘上二的次幂) 然后每个数的加入相当于对每个质数的指数取最大值,但是如果将每个质数的次数都表示出来状态数很多, 考虑 ...