开个新坑。最近在准备面试的时候,突然被一个写神经网络的题给难住了,因此也意识到了我很长一段时间内忘了去巩固pytorch的基本矩阵操作知识,太过于依赖调包+google搜索文档(甚至现在开始依赖chatgpt)。虽然不关注代码细节,关注输入输出,然后cv+拼积木的做法在短期内可以搭建出合格的框架,但是这显然缺少更深入的理解。遂决定,要把pytorch的一些基本概念再复习复习。
Tensor的基本概念:tensor是很像numpy的一种矩阵存储方式,甚至在很多时候和numpy公用一个存储空间。
Tensor的初始化
1、直接从list初始化:
data = [[1, 2],[3, 4]]
x_data = torch.tensor(data)
2、从numpy初始化:
np_array = np.array(data)
x_np = torch.from_numpy(np_array)
3、从另一个tensor初始化:
x_ones = torch.ones_like(x_data) # retains the properties of x_data
print(f"Ones Tensor: \n {x_ones} \n")
x_rand = torch.rand_like(x_data, dtype=torch.float) # overrides the datatype of x_data
print(f"Random Tensor: \n {x_rand} \n")
4、random或固定值初始化:
shape = (2,3,)
rand_tensor = torch.rand(shape)
ones_tensor = torch.ones(shape)
zeros_tensor = torch.zeros(shape)
print(f"Random Tensor: \n {rand_tensor} \n")
print(f"Ones Tensor: \n {ones_tensor} \n")
print(f"Zeros Tensor: \n {zeros_tensor}")
Tensor的固有属性
三个固有属性,shape,dtype,device
tensor = torch.rand(3,4)
print(f"Shape of tensor: {tensor.shape}")
print(f"Datatype of tensor: {tensor.dtype}")
print(f"Device tensor is stored on: {tensor.device}")
Tensor的基本操作
1、默认情况下用cpu运行,需要用gpu的话需要用.to方法
# We move our tensor to the GPU if available
if torch.cuda.is_available():
tensor = tensor.to('cuda')
2、基础操作
#Standard numpy-like indexing and slicing:
tensor = torch.ones(4, 4)
print('First row: ',tensor[0])
print('First column: ', tensor[:, 0])
print('Last column:', tensor[..., -1])
tensor[:,1] = 0
#Joining tensors
t1 = torch.cat([tensor, tensor, tensor], dim=1)
# 叉乘This computes the matrix multiplication between two tensors. y1, y2, y3 will have the same value
y1 = tensor @ tensor.T
y2 = tensor.matmul(tensor.T)
y3 = torch.rand_like(tensor)
torch.matmul(tensor, tensor.T, out=y3)
# 点乘This computes the element-wise product. z1, z2, z3 will have the same value
z1 = tensor * tensor
z2 = tensor.mul(tensor)
z3 = torch.rand_like(tensor)
torch.mul(tensor, tensor, out=z3)
3、单值用.item()提取
agg = tensor.sum()
agg_item = agg.item()
与NumPy的联结
numpy()和.from_numpy()共享存储空间。