pytorch tutorials——Tensors

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值