各种数值数据称为张量
a.常数:scale:0阶张量
b.向量:vector:1阶张量
c.矩阵:matrix:2阶张量
1.pytorch中创建张量
import torch
import numpy as np
#使用python中的列表或序列创建tensor
t1=torch.Tensor([1,2,3])
print(t1)
# #使用numpy中的数组创建tensor
t2=torch.tensor(np.array([[1,2,3],[4,5,6]]))
print(t2)
#使用torch的api创建tensor
t3=torch.empty(3,4)
print(t3)
t4=np.arange(12).reshape(3,4)
print(t4)
#创建整型数组
t5=torch.randint(low=0,high=4,size=[3,4])
print(t5)
2.张量的方法和属性
a,数据中只有一个元素时,取值:tensor.item()
b.转化为numpy数组的时候:tensor.numpy()
c.获取形状:tensor.size()
d.形状的变化:tensor.view([shape])
e.获取阶数:tensor.dim()
f.转置
二维:tensor.t() tensor.transpose(0,1)
高维:tensor.transpose(1,2)/tensor.permute(0,2,1)
g.取值和切片