Pytorch里.t()的作用

举报
悲恋花丶无心之人 发表于 2021/02/02 22:24:09 2021/02/02
【摘要】 目录 一、函数解释 二、用法示例 一、函数解释 在torch/_C/_VariableFunctions.py的有该定义,意义就是将Tensor进行转置 def t(self, input): # real signature unknown; restored from __doc__ """ t(input) -> Tensor Expects :attr...

目录

一、函数解释

二、用法示例


一、函数解释

torch/_C/_VariableFunctions.py的有该定义,意义就是将Tensor进行转置


  
  1. def t(self, input): # real signature unknown; restored from __doc__
  2. """
  3. t(input) -> Tensor
  4. Expects :attr:`input` to be a matrix (2-D tensor) and transposes dimensions 0
  5. and 1.
  6. Can be seen as a short-hand function for :meth:`transpose(input, 0, 1)`
  7. Args:
  8. input (Tensor): the input tensor
  9. Example::
  10. >>> x = torch.randn(2, 3)
  11. >>> x
  12. tensor([[ 0.4875, 0.9158, -0.5872],
  13. [ 0.3938, -0.6929, 0.6932]])
  14. >>> torch.t(x)
  15. tensor([[ 0.4875, 0.3938],
  16. [ 0.9158, -0.6929],
  17. [-0.5872, 0.6932]])
  18. """
  19. pass

二、用法示例

1.示例代码如下


  
  1. import torch
  2. rectangle_height = 3
  3. rectangle_width = 3
  4. inputs = torch.randn(rectangle_height, rectangle_width)
  5. for i in range(rectangle_height):
  6. for j in range(rectangle_width):
  7. inputs[i] = i * torch.ones(rectangle_width)
  8. print(inputs)
  9. inputs2 = inputs.t()
  10. print(inputs2)
  11. inputs = inputs + inputs2
  12. print(inputs)

2.运行结果,其中矩阵加上其转置矩阵得到了一个对角矩阵~


  
  1. tensor([[0., 0., 0.],
  2. [1., 1., 1.],
  3. [2., 2., 2.]])
  4. tensor([[0., 1., 2.],
  5. [0., 1., 2.],
  6. [0., 1., 2.]])
  7. tensor([[0., 1., 2.],
  8. [1., 2., 3.],
  9. [2., 3., 4.]])

文章来源: nickhuang1996.blog.csdn.net,作者:悲恋花丶无心之人,版权归原作者所有,如需转载,请联系作者。

原文链接:nickhuang1996.blog.csdn.net/article/details/90635095

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。