torch.t()

官网:https://pytorch.org/docs/stable/torch.html#torch.t

torch.t(input) → Tensor

torch.t()是一个类似于求矩阵的转置的函数,但是它要求输入的tensor结构维度<=2D。 

当input维度为0D或者1D时,不做改变输出本身(torch version=1.0.1中会报错),维度为2D时,输出维度的转置。

例子:

>>> x = torch.randn(()) ##0D
>>> x
tensor(0.1995)
>>> torch.t(x)
tensor(0.1995)
>>> x = torch.randn(3) ## 1D
>>> x
tensor([ 2.4320, -0.4608,  0.7702])
>>> torch.t(x)  ### torch version =1.0.1 报错
tensor([.2.4320,.-0.4608,..0.7702])
>>> x = torch.randn(2, 3)  ##2D size(2,3)
>>> x
tensor([[ 0.4875,  0.9158, -0.5872],
        [ 0.3938, -0.6929,  0.6932]])
>>> torch.t(x)  ## size(3,2)
tensor([[ 0.4875,  0.3938],
        [ 0.9158, -0.6929],
        [-0.5872,  0.6932]])

注:上面是官方给出的例子,在例子中可以看下1D是可以输出的,但是在torch版本为1.0.1时,不会输出,会报错“t() expects a 2D tensor, but self is 1D”

import torch
x = torch.randn(3)
print(x.size())
print(torch.t(x).size())

output:
torch.Size([3])

->4 print(torch.t(x).size())
RuntimeError: t() expects a 2D tensor, but self is 1D

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Foneone

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

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

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

打赏作者

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

抵扣说明:

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

余额充值