PyTorch 的基本使用

1. 环境搭建

安装 python:

  1. Windows https://blog.csdn.net/YKenan/article/details/88135978
  2. Centos https://blog.csdn.net/YKenan/article/details/97121698

PyTorch官网: https://pytorch.org/

在这里插入图片描述

pip3 install torch==1.8.1+cpu torchvision==0.9.1+cpu torchaudio===0.8.1 -f https://download.pytorch.org/whl/torch_stable.html

在这里插入图片描述

检查是否安装成功

import torch
torch.__version__

在这里插入图片描述

在 PyCharm 中添加 torch 和普通添加 numpy 等一样

在这里插入图片描述
在这里插入图片描述

2. 基础

2.1 Tensors 的含义

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

上面是官方所解释的意思,可以得出,Tensors 是 PyTorch 运算的基础。

2.2 Tensors 和 Numpy 中的数组的关系

官方也指出,Tensors 和 Numpy 中 ndarrays 非常相似,了解 Numpy 中 ndarrays 可以直接上手 Tensors。
而且 Tensors 和 Numpy 中的数组具有底层内存共享,意味着不需要进行复制直接就可以相互转化。

import torch
import numpy as np
# Tensor
data = [[1, 2], [3, 4]]
x_data = torch.tensor(data)
print(f"Tensor: \n {x_data} \n")

# numpy 数组直接转化为 Tensor
np_array = np.array(data)
x_np = torch.from_numpy(np_array)
print(f"Tensor from numpy: \n {x_np} \n")

在这里插入图片描述

一个新的 Tensors 可以从另一个 Tensors 获取,其中新的 Tensors 与原来的 Tensors 中的参数不会改变,除非被明确重写。
明确重写,这里可以理解为 Tensors 被改变了。

# retainsthe properties of x_data
x_ones = torch.ones_like(x_data)
print(f"Ones Tensor: \n {x_ones} \n")

# overrides the datatype of x_data
x_rand = torch.rand_like(x_data, dtype=torch.float)
print(f"Random Tensor: \n {x_rand} \n")

在这里插入图片描述

2.3 Tensors 带有随机和常量值以及其属性

Tensors 带有随机和常量值

# 带有随机和不变的值
shape = (2, 3)
# 随机值
rand_tensor = torch.rand(shape)
# 全是 1
ones_tensor = torch.ones(shape)
# 全是 0
zeros_tensor = torch.zeros(shape)
# 对角矩阵
eye_tensor = torch.eye(2, 3)

print(f"Random Tensor: \n {rand_tensor} \n")
print(f"Ones Tensor: \n {ones_tensor} \n")
print(f"Zeros Tensor: \n {zeros_tensor} \n")
print(f"Eye Tensor: \n {eye_tensor} \n")

在这里插入图片描述

Tensors 的属性

# 属性
tensor = torch.rand(3, 4)

print(f"Size of tensor: {tensor.size()}")
print(f"Shape of tensor: {tensor.shape}")
print(f"Datatype of tensor: {tensor.dtype}")
print(f"Device tensor is stored on: {tensor.device} \n")

在这里插入图片描述

2.4 Tensors 操作

Tensors 操作有百余种,例如算术、线性代数、矩阵操作(转置、索引、切片)、采样等。 每个操作都可以在 GPU 上运行,其效率高于 CPU。

# We move our tensor to the GPU if available
print(f"Datatype of tensor: {torch.cuda.is_available()} \n")
if torch.cuda.is_available():
	tensor = tensor.to('cuda')

在这里插入图片描述

2.4.1 Tensors 矩阵操作

Tensors 的索引和切片,与 Numpy 简直一模一样。

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

在这里插入图片描述

Tensors 链接

torch.cat() 方法中 dim=1 按行拼接
torch.cat() 方法中 dim=0 按列拼接

# Joiningtensors torch.cat()
horizontal = torch.cat([tensor, tensor, tensor], dim=1)
print(horizontal)
vertical = torch.cat([tensor, tensor, tensor], dim=0)
print(vertical)

在这里插入图片描述

torch.stack(tensors, dim=0, *, out=None) → Tensor 所有的 tensors 具有相同的函数。

# Joining tensors torch.stack() 0 <= dim <= len()
tensor = torch.tensor([[1, 2, 3], [3, 4, 5], [5, 6, 7]])
print(tensor.shape)
output = torch.stack([tensor, tensor], dim=0)
print(output, output.shape)
output = torch.stack([tensor, tensor], dim=1)
print(output, output.shape)
output = torch.stack([tensor, tensor], dim=2)
print(output, output.shape)

在这里插入图片描述

2.4.2 Tensors 算术操作
# 输出
def println(*key):
    for k in key:
        print(k)

乘法操作-矩阵乘法

tensor = torch.ones(3, 3)
# 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)
println(y1, y2, y3)
torch.matmul(tensor, tensor.T, out=y3)
println(y1, y2, y3)

乘法操作-元素相乘

# This computes the element-wise product. z1, z2 will have the same value
tensor = torch.tensor([[1, 2, 3], [3, 4, 5], [5, 6, 7]])
z1 = tensor * tensor
z2 = tensor.mul(tensor)
z3 = torch.rand_like(tensor, dtype=torch.float)
println(z1, z2, z3)
torch.mul(tensor, tensor, out=z3)
println(z1, z2, z3)

在这里插入图片描述

Tensors 总和计算

# 总和
agg = tensor.sum()
print(agg, type(agg))
# Tentor 转化为数值型
agg_item = agg.item()
print(agg_item, type(agg_item))

在这里插入图片描述

Tensors 中每个元素加上同一个数值,该操作属于 In-place 操作。

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

在这里插入图片描述

2.5 Tensors 与 NumPy array 的转化

下面的例子就可以看出内存是共享的。

Tensor 转 NumPy array

# Tensor to NumPy array
t = torch.ones(5)
print(f"t: {t}")
n = t.numpy()
print(f"n: {n}")
# A change in the tensor reflects in the NumPy array.
t.add_(1)
print(f"t: {t}")
print(f"n: {n}")

在这里插入图片描述

NumPy array 转 Tensor

# NumPy array to Tensor
n = np.ones(5)
t = torch.from_numpy(n)
print(f"t: {t}")
print(f"n: {n}")

# Changes in the NumPy array reflects in the tensor.
np.add(n, 1, out=n)
print(f"t: {t}")
print(f"n: {n}")

在这里插入图片描述

  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值