A tensor is both a container for numbers as well as a set of rules that define transformations between tensors that produce new tensors[1].
张量既是数字的容器,也是一组规则,这些规则定义了产生新张量的张量之间的转换。
其实张量可以相似地理解为多维数组。
import torch
print(torch.rand(2, 3))
import torch
x = torch.tensor([[0,0,1],[1,1,1],[0,0,0]])
print(x)
import torch
x = torch.tensor([[0,0,1],[1,1,1],[0,0,0]])
x[0][0] = 5
print(x)
import torch
print(torch.zeros(3, 4))
import torch
print(torch.ones(2, 2))
import torch
a = torch.tensor([[1,1,1],[0,0,0]])
b = torch.tensor([[1,2,3],[4,5,6]])
print(a + b)
print(a - b)
print(a * b)
print(a / b)
import torch
x = torch.rand(1)
y = torch.rand(1,1,1)
print(x)
print(x.item())
print(y)
print(y.item())
import torch
x = torch.rand(2)
print(x.device)
输出:
import torch
x = torch.rand(2)
y = x.to('cuda')
print(y.device)
输出:
import torch
x = torch.rand(2,3)
print(x)
print(x.max())
print(x.max().item())
print()
print(x.min())
import torch
x = torch.rand(2,3)
print(x.type())
输出:
import torch
x = torch.tensor([[1,2.2,3],[4,5,0.3]])
y = x.to(dtype=torch.int)
print(y)
print(y.type())
import torch
x = torch.tensor([[1,2,3],[4,5,6]])
print(x.shape)
输出:
import torch
x = torch.tensor([[1,2,3],[4,5,6]])
y = x.reshape(6)
print(y)
print(y.shape)
import torch
x = torch.tensor([[1,2,3],[4,5,6]])
y = x.permute(1,0)
print(y)
print(y.shape)
import torch
x = torch.tensor([[1,2,3],[4,5,6]])
print(x * 2)
《Programming PyTorch for Deep Learning》