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. vue和xml复习

    复习 JS知识梳理 JS定义的位置 行内js(事件名="javascript:js代码"),内部js(

  2. TLS原理与实践(四)国密TLS

    主页 个人微信公众号:密码应用技术实战 个人博客园首页:https://www.cnblogs.com/informatics/ 引言 TLS作为保证网络通信安全的关键技术和基石被广泛应用,但目前主流 ...

  3. 遥感图像镶嵌拼接:ENVI的Pixel Based Mosaicking工具操作方法

      本文介绍基于ENVI软件,利用"Pixel Based Mosaicking"工具实现栅格遥感影像镶嵌拼接的方法.   首先需要说明的是,本文需要镶嵌的遥感影像并不含地理参考信 ...

  4. 网络io与select

    我们知道网络IO模型一共有5种,这里我们主要讨论同步IO和select多路复用的情况. 我们先从一个简单的TCP服务器的代码出发,来讨论一下这个是怎么实现的. 一个十分简单的TCP服务器 一个简单的T ...

  5. Some characters cannot be mapped using 'ISO-8859-1'解决方案

    问题: MyEclipse中:Some characters cannot be mapped using 'ISO-8859-1' character encoding   分析:由于默认的字符是I ...

  6. 你是怎么处理vue项目中的错误的?

    这里给大家分享我在网上总结出来的一些知识,希望对大家有所帮助 一.错误类型 任何一个框架,对于错误的处理都是一种必备的能力 在Vue 中,则是定义了一套对应的错误处理规则给到使用者,且在源代码级别,对 ...

  7. 记录--新的HTML标签 :<search>

    这里给大家分享我在网上总结出来的一些知识,希望对大家有所帮助 本文介绍了一种新的HTML元素搜索方法,并提供了一个实用的工具来帮助开发者快速找到所需的元素.这对于那些需要处理大量HTML元素的开发者来 ...

  8. linux上pip install mysqlclient报错

    linux上pip install mysqlclient报错 django连接mysql数据库时 乱糟糟的 一大片红色报错,查了半天资料,失败了无数次,最终终于成功 先用以下代码: sudo apt ...

  9. Python - inspect 模块的简单使用

    Python中的inspect模块解析 Python的inspect模块是一个强大的内省工具,允许开发者检查(inspect)活动对象和源代码.它提供了一系列函数,用于获取信息关于正在运行的程序和调用 ...

  10. Python---flask框架实现免密登录功能

    思路总结: html代码: 1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta c ...