一、Tensors

原文链接

一、[Tensor]

张量是一种特殊的数据结构,与数组和矩阵非常相似。在PyTorch中,我们使用张量对模型的输入和输出以及模型的参数进行编码。

张量与NumPy的ndarray相似,除了张量可以在GPU或其他专用硬件上运行以加速计算。如果您熟悉ndarrays,则可以轻松使用Tensor API。如果没有,请遵循此快速API演练。

二、[Tensor Initialization]

import torch
import numpy as np

(1)、直接从数据创建

data = [[1, 2],[3, 4]]
x_data = torch.tensor(data)

在这里插入图片描述

(2)、从nmupy的array转化

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)、具有随机或者恒定值

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}")

在这里插入图片描述
shape为(2,3)表示两行三列
shape为(1,2,3)表示两行三列的数组两个
shape为(1,2,3,4)表示三行四列的数组作为一个元素在1行2列的数组中

tips

x_rand2 = np.random.randn(2,2) #生成2*2的nparray数据是-1,1的小数

x_rand2 = np.random.rand(2,2) #生成2*2的nparray数据是0,1的小数

三、[Tensor Attributes]

Tensor属性描述了它们的形状,数据类型以及存储它们的设备。

tensor = torch.rand(3,4)
print(tensor)
print(f"Shape of tensor: {tensor.shape}")
print(f"Datatype of tensor: {tensor.dtype}")
print(f"Device tensor is stored on: {tensor.device}")
# We move our tensor to the GPU if available
if torch.cuda.is_available():
  tensor = tensor.to('cuda')
  print(f"Device tensor is stored on: {tensor.device}")

在这里插入图片描述

四、[Tensor Operations]

这里全面介绍了100多个张量运算,包括转置,索引,切片,数学运算,线性代数,随机采样等 。

(1)、Standard numpy-like indexing and slicing:

tensor = torch.ones(4, 4)
tensor[:,1] = 0
print(tensor)

在这里插入图片描述

numpy

arr = np.ones((4,4))
arr[:,1] = 0
print(arr)

在这里插入图片描述
[0:2,1:3]表示选择二维数组[0,2)行,[1,3)列
[0:2,:]表示选择二维数组[0,2)行,所有列

arr = np.arange(36)
arr = arr.reshape(6,6)
print(arr)
print(arr[0:2,1:3])
print(arr[:,3])
print(arr[3,:])//第“3”行,所有列
print(arr[:,:])//所有行,所有列

在这里插入图片描述

(2)、Joining tensors:

您可以用来torch.cat将给定维度上的一系列张量连接起来。另请参阅torch.stack,这是另一个 tensor joining op that is subtly different from torch.cat.

tensor = torch.ones(4, 4)
tensor[:,1] = 0
print(tensor)

tensor[:,1] = 0
t1 = torch.cat([tensor, tensor, tensor], dim=1)
print(t1)

在这里插入图片描述

(3)、Multiplying tensors:

tensor = torch.ones(3, 3)
tensor[:,1] = 2
print(tensor) #matrix
print(tensor.matmul(tensor))#a.matmul(b)
#print(torch.matmul(tensor,tensor)) """此句与上句都是矩阵相乘"""
print((tensor.matmul(tensor)).t())#转置
#print(torch.t(tensor.matmul(tensor))) """此句与上句都是矩阵转置"""
print((tensor.matmul(tensor)).T)

在这里插入图片描述

pytorch中.t()与.T都表示转置,那么区别是什么呢?
.t() 是 .transpose函数的简写版本, 只能对二维tensor进行处理
.T是.permute函数的简化版本, 可以对多维tensor进行处理

(4)、In-place operations:

就地操作 具有_后缀的操作是就地操作。例如:x.copy_(y),x.t_(),将改变x。

print(tensor, "\n")
tensor.add_(5)
print(tensor)

在这里插入图片描述

5、[Bridge with NumPy]

(1)、Tensor to NumPy array:

t = torch.ones(5)
print(f"t: {t}")
n = t.numpy()
print(f"n: {n}")
print(type(t))
print(type(n))

在这里插入图片描述

(2)、NumPy array to Tensor

n = np.ones(5)
t = torch.from_numpy(n)
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值