- 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])