Pytorch官网教程学习笔记

注:还在学习中,会继续维护

一、入门语法

1.1、创建张量

1.1.1使用python基础数据结构创建张量(torch会自动推测元素的数据类型)

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

1.1.2使用numpy数组创建张量

np_array = np.array(data)
x_np = torch.from_numpy(np_array)

1.1.3使用一个张量的参数来创建一个新的张量

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

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

1.2张量的属性(Attributes)

shape:指定张量的形状

datatype:指定张量的数据类型

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

1.3操作张量(张量的运算)

线性代数

算法

矩阵计算(转置,指标,切片)

抽样

以上所有运算都可以使用.to方法张量从CPU转移到GPU中去运行。但是当数据量庞大时,会耗费大量时间和内存

使用GPU运算之前,需要先使用torch.cuda.is_available方法判断是否支持GPU运算

# We move our tensor to the GPU if available
if torch.cuda.is_available():
    tensor = tensor.to('cuda')

1.3.1张量的切片和下标索引操作

张量具备和numpy一样的张量操作方式,indexing和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)

1.3.2张量的拼接操作

可以使用torch.cat方法来实现沿着指定地维度方向进行拼接。也可以使用torch.stack这种更为精妙地拼接方法。

dim中0代表第一个维度,1代表第2个维度。(这里建议去按照维度来理解拼接,而不是去记横着拼接还是竖着拼接)

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

1.4张量地算法操作

1.4.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)

1.4.2元素乘法

# 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)

1.5单元素张量

单元素张量可以使用.item方法将其转化为python地基础数据类型

agg = tensor.sum()
agg_item = agg.item()
print(agg_item, type(agg_item))

1.6替代性地操作命令

这类命令的计算结果会替代原来的计算对象(操作数),这类命令通常以"_"后缀结尾

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

注:因为当进行一些对衍生数据的计算时,将直接损失历史数据,因此这种替代性的操作命令不建议大家使用,实际场所中使用也很少。

1.7在CPU环境下运行的张量和numpy的数组公用相同的地址空间,改变其中一个会对另一个产生影响

1.7.1将tensor和numpy链接起来

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

改变其中一个的值,会发现两个都发生了变化

t.add_(1)
print(f"t: {t}")
print(f"n: {n}")

1.7.2将numpy和tensor连接起来

n = np.ones(5)
t = torch.from_numpy(n)

改变其中一个的值,会发现两个都发生了变化

np.add(n, 1, out=n)
print(f"t: {t}")
print(f"n: {n}")

二、Datasets和DataLoader

2.1Pytorch提供了两个数据加载的方式,他们分别是torch.utils.data.DataLoader和torch.utils.data.Dataset。这两个方式可以帮助你导入pytorch提供的学习数据,也可以帮助你导入你自己的实操数据。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值