torch.tensor([2])和 torch.tensor([2,3,4]) 与 torch.tensor([[2,3,4]])

  • torch.tensor([2]) 的size: [1]还是 []?
  • torch.tensor([2,3,4]) 的 size : [3]
  • torch.tensor([[2,3,4]]) 的 size:[1,3]

torch.tensor([2,3,4]) 是一维的,如果它执行 torch.mm 时,会报错 it is not a matrix

关于 torch.tensor([2])

import torch
import numpy as np

shape1 = torch.tensor([23]).shape
print(type(shape1))
print(shape1)
npShape1 = np.array(shape1)
print(npShape1)

Vec1 = torch.tensor([6,4,2])
print("Vec1's shape: ",Vec1.shape)
Vec2 = torch.tensor([5,3,1])
print("Vec2's shape: ",Vec2.shape)
Vec3 = torch.matmul(Vec1, Vec2)
print("Vec3: ",Vec3)
print("Vec3's shape: ",Vec3.shape,"\n")
print("Vec3's shape(np.ndarray 类型)",np.array(Vec3.shape))

Mat1 = torch.tensor([[1, 3]])
print("Mat1's shape: ",Mat1.shape)
Mat2 = torch.tensor([[6, 4, 2],
                      [5, 3, 1]])
print("Mat2's shape: ",Mat2.shape)
Mat3 = torch.matmul(Mat1, Mat2)
print("Mat3: ",Mat3)
print("Mat3's shape: ",Mat3.shape,"\n")

输出:

<class 'torch.Size'>
torch.Size([1])
[1]
Vec1's shape:  torch.Size([3])
Vec2's shape:  torch.Size([3])
Vec3:  tensor(44)
Vec3's shape:  torch.Size([]) 

Vec3's shape(np.ndarray 类型) []
Mat1's shape:  torch.Size([1, 2])
Mat2's shape:  torch.Size([2, 3])
Mat3:  tensor([[21, 13,  5]])
Mat3's shape:  torch.Size([1, 3]) 


Process finished with exit code 0

size: [4], [1,4], [4,1]

import torch

T1 = torch.randn(4)
print(T1)
print(T1.shape,"\n")

T2 = torch.randn((1,4))
print(T2)
print(T2.shape,"\n")

T3 = torch.randn((4,1))
print(T3)
print(T3.shape,"\n")

输出:

tensor([-0.1918,  1.4673,  0.3593, -0.0108])
torch.Size([4]) 

tensor([[-0.8551, -0.1023, -1.5520, -1.2220]])
torch.Size([1, 4]) 

tensor([[ 0.7785],
        [-0.5002],
        [ 0.7169],
        [ 1.2581]])
torch.Size([4, 1]) 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

培之

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

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

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

打赏作者

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

抵扣说明:

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

余额充值