栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

pytorch tutorials——Tensors

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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.

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/689501.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号