Pytorch里.t()的作用

目录

一、函数解释

二、用法示例


一、函数解释

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

    def t(self, input): # real signature unknown; restored from __doc__
        """
        t(input) -> Tensor
        
        Expects :attr:`input` to be a matrix (2-D tensor) and transposes dimensions 0
        and 1.
        
        Can be seen as a short-hand function for :meth:`transpose(input, 0, 1)`
        
        Args:
            input (Tensor): the input tensor
        
        Example::
        
            >>> x = torch.randn(2, 3)
            >>> x
            tensor([[ 0.4875,  0.9158, -0.5872],
                    [ 0.3938, -0.6929,  0.6932]])
            >>> torch.t(x)
            tensor([[ 0.4875,  0.3938],
                    [ 0.9158, -0.6929],
                    [-0.5872,  0.6932]])
        """
        pass

二、用法示例

1.示例代码如下

import torch

rectangle_height = 3
rectangle_width = 3
inputs = torch.randn(rectangle_height, rectangle_width)
for i in range(rectangle_height):
    for j in range(rectangle_width):
        inputs[i] = i * torch.ones(rectangle_width)

print(inputs)
inputs2 = inputs.t()
print(inputs2)
inputs = inputs + inputs2
print(inputs)

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

tensor([[0., 0., 0.],
        [1., 1., 1.],
        [2., 2., 2.]])
tensor([[0., 1., 2.],
        [0., 1., 2.],
        [0., 1., 2.]])
tensor([[0., 1., 2.],
        [1., 2., 3.],
        [2., 3., 4.]])
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

老兵安帕赫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值