TENSORS
tensors(张量),可以认为是与numpy中array相类似的数据类型。在pytorch中,我们使用tensors来为模型的变量、输入输出编码。
tensors与Numpy’s ndarrays很相似,除了tensors可以在GPU或者其他硬件加速器上运行。tensors可以和ndarray进行相互转化,详情见https://pytorch.org/tutorials/beginner/blitz/tensor_tutorial.html#bridge-to-np-label
。
Initializing a Tensor
tensor can be initializing in many ways.
Directly from data
data = [[1,2],[3,4]]
x_data = torch.tensor(data)
From a NumPy array:torch.from_numpy
Tensors can be created from NumPy arrays (and vice versa)
np_array = np.array(data)
x_np = torch.from_numpy(np_array)
From another tensor:
x_ones = torch.ones_like(x_data)
print(f"Ones Tnesor:\n {x_ones}\n")
x_rand = torch.rand_like(x_data,dtype=torch.float)
print(f"Random Tensor: \n {x_rand} \n")
Attributes of a Tensor
Tensor attributes describe their shape, datatype, and the device on which they are stored.
tensor = torch.rand(3,4)
print(f"Shape of tnesor:{tensor.shape}")
print(f"Datatype of tensor:{tensor.dtype}")
print(f"Device tensor is sorted on:{tensor.device}")
Operations onTensor
move our tensor to the GPU if available
if torch.uda.is_available():
tensor = tensor.to('cuda')
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
print(tensor)
torch.cat: Joining tensors
# dim=1:横向的。dim=0:纵向的
ta = torch.cat([tensor,tensor,tensor],dim=1)
Arithmetic operations
# 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)
item():Single-element tensors
If you have a one-element tensor, for example by aggregating all values of a tensor into one value, you can convert it to a Python numerical value using item():
agg = tensor.sum()
agg_item = agg.item()
print(agg_item, type(agg_item))
_: in-place operation
Operations that store the result into the operand are called in-place
print(tensor, "\n")
tensor.add_(5)
print(tensor)
Bridge with Numpy
Tensors on the CPU and NumPy arrays can share their underlying memory locations, and changing one will change the other.