初始化与形状
import torch
x = torch.arange(12)
print(x)
print(x.shape)
print(x.numel())
形状变化
X = x.reshape(3, 4)
print(X)
初始化为0或1
zeros_4 = torch.zeros((4, 4))
ones_3 = torch.ones((3, 3))
print(zeros_4)
print(ones_3)
数值初始化
y = torch.tensor([[2,1,4,3], [1,2,3,4], [4,3,2,1]])
print(y)
基本算数运算
x = torch.tensor([1.0, 2, 4, 8])
y = torch.tensor([2, 2, 2, 2])
数据堆叠
x = torch.arange(12, dtype=torch.float32).reshape((3,4))
y = torch.tensor([[2,1,4,3], [1,2,3,4], [4,3,2,1]])
print(x)
print(y)
numpy <==> tensor
x = torch.arange(12)
A = x.numpy()
B = torch.tensor(A)
print(type(A), A)
print( type(B), B)
python <==> tensor(单个数值)
a = torch.tensor([3.5])
print(a)
print(a.item())
print(float(a))
print(int(a))