Pytorch笔记

这篇PyTorch笔记涵盖了Tensor的基本操作,包括与array的转换、数据类型查看与转换、维度调整,以及PIL和OpenCV的RGB处理。还介绍了图像预处理、数据加载器和损失函数nn.BCEloss的使用。此外,探讨了可视化工具Visdom和tensorboardX,以及如何保存IPython环境数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

tensor与array转换

查看 Tensor 数据类型

Tensor数据类型转换

扩维(unsqueeze)和降维(squeeze)

更改数据维度

PIL和OpneCV的RGB排列

ToTensor

torch.view()

参数 inplace

判断一个对象是否可以迭代

ipython 常用指令

模块函数

Visdom 可视化

tensorboardX 可视化

保存 ipython 环境数据

提取迭代器中的某些元素(将其转化为list)

pytorch图像预处理

张量乘法


PyTorch中文文档:https://www.pytorchtutorial.com/docs/

PyTorch官方手册:https://pytorch.org/docs/stable/index.html

PyTorch 教程目录:https://www.pytorchtutorial.com/pytorch-tutorials-list/#PyTorch

tensor与array转换

>>> a = torch.Tensor([4,5,6])  # 默认 a.dtype 为 int64
>>> b = np.array([1,2,3]       # 默认 b.dtype 为 int32

>>> c = torch.Tensor(b)        # array --> tensor(float32)
>>> c = torch.from_numpy(b)    # array --> tensor(int32) 保持原数据类型

>>> d = np.array(a)            # tensor --> array(int64)
>>> d = a.numpy()              # tensor --> array(int64)

查看 Tensor 数据类型

pytorch所有数据类型:https://pytorch.org/docs/stable/tensors.html

>>> test = torch.Tensor([1.2, 3.4])
>>> test.dtype            # 查看数据类型
torch.float32
>>> test.type()           # 查看数据类型及平台
'torch.FloatTensor'               # CPU数据
'torch.cuda.FloatTensor'          # GPU数据

Tensor数据类型转换

>>> test = torch.Tensor([1.2, 3.4])
>>> test.dtype                    # 查看数据类型
torch.float32
>>> test.to(torch.uint8)          # 转换数据类型位8位整型
>>> test.dtype
torch.uint8
>>> test.to(torch.ByteTensor)     # Error,不能使用CPU或GPU类型来转换

扩维(unsqueeze)和降维(squeeze)

unsqueeze:参数是几,就在该维度上增加 / 减小 一个维度

>>> a = torch.ones(3)    # a.dtype == float32
>>> a.shape              # 输出 [3]
torch.Size([3])

>>> a.unsqueeze(0).shape    
torch.Size([1, 3])   

>>> a.unsqueeze(1).shape
torch.Size([3, 1])

squeeze:参数是几,且参数维度如果是1,则压缩掉,否则不变

>>> a = torch.ones(3, 1)    # [3, 1]
>>> a.squeeze(0).shape      # 不变
torch.Size([3, 1])
>>> a.squeeze(1).shape      # [3]
torch.Size([3])
>>> b = torch.ones(1,3,1)
>>> b.squeeze().shape       # [3],不带参数则会将所有为1的维度全部压缩掉
torch.Size([3])

更改数据维度

举例:将形状为(64, 28, 28)的 data 更改为 (64, 1, 28, 28)

>>> data = torch.zeros([64, 28, 28])    # shape = (64, 28, 28)

1、如上,扩维(unsqueeze)和降维(squeeze)

>>> data.unsqueeze(1).shape    # 在维度1上扩维
torch.Size([64, 1, 28, 28])

2、view(*shape) -> Tensor

>>> data.view(64, 1, 28, 28).shape
torch.Size([64, 1, 28, 28])
>>> data.view(data.shape[0], 1, data.shape[1], data.shape[2]).shape    # 一个道理
torch.Size([64, 1, 28, 28])

3、reshape(*shape) -> Tensor

用法同上

4、直接操作(使用 None,代表新增一个维度)

>>> data[:,None,:,:].shape        # 维度1上增加扩维
torch.Size([64, 1, 28, 28])

 

 

PIL和OpneCV的RGB排列

PIL图像在转换为numpy.ndarray后,格式为(H, W, C),像素顺序为RGB
OpenCV在cv2.imread()后数据类型为numpy.ndarray,格式为(H, W, C),像素顺序为BGR

ToTensor

先将图像由 (H, W, C) 转置为(C, H, W)格式,再转为float后每个像素除以255,将像素范围转换至 [0.0, 1.0]。

from PIL import Image
from torchvision.transforms import ToTensor, ToPILImage

col = Image.open('color.img')    # 彩色图像
col.size()    # (224, 224, 3)
to_tensor = ToTensor() # img -> tensor
t = to_tensor(col)    # to_tensor 接收 array 数组
t.size()    # (3, 224, 224)
gray = Image.open('gray.img')    # 灰度图像
gray.size()    # (224, 224)      # Image读取灰度图为二维
to_tensor(a).size()    # (1, 224, 224),也会添加一个维度

torch.view()

相当于 np.reshape()

参数 inplace

当 inplace = True,则操作是在原对象上完成,返回处理后的原对象。

而 inplace = False,则将在原对象上处理的结果保存到另一个对象上(会消耗多余的内存),返回另一个对象。

判断一个对象是否可以迭代

>>> from collections import Iterable
>>> isinstance([1, 2, 3], Iterable)         # 列表可以迭代
True

ipython 常用指令

shell 快捷键

  • Ctrl-C   中止当前正在执行的代码
  • Ctrl-A   将光标移动到行首
  • Ctrl-E   将光标移动到行尾
  • Ctrl-U  清除该行
  • Ctrl-L   清屏

% —— 魔法指令(实际使用时,指令前面不需加 %)

In [1]: pwd                        # 查看当前路径
In [2]: ls                         # 查看当前路径下的文件
In [3]: cd                         # 切换工作目录
In [4]: run 路径/test.py            # 运行python脚本
In [5]: who 或 %who_ls 或 %whos     # 查看环境中的变量信息
In [6]: xdel (或 del , 其属于python的关键字) # 删除环境中的变量
In [7]: hist                       # 查看输入的历史记录

【注】:切换盘符时,必须使用双引号:

>>> cd "D:\\work"
D:\work

!—— 系统指令,切换到 cmd 模式

In [1]: !pip search torch    # 切换到 cmd 模式

ipython中运行python文件

In [1]: !python "E:\code\example.py"

模块函数

scatter_(dim, index, src) ——> Tensor

功能:表示将src中的数据根据index的索引值按照dim的方向填充到objdect中

eg:E:\J---Paper\Paper Code\luo_[XifengGuo]_CapsNet-Pytorch-master\capsulenet.py    
line 77
--------------------
class CapsuleNet(nn.Module):
    ...
    y = Variable(torch.zeros(length.size()).scatter_(1, index.view(-1, 1), 1.))
    ...

torch.clamp(input, min, max, out=None) -> Tensor

功能:限定输入变量(可以为向量)的区间

y_i \left\{\begin{matrix} min,\ \ if\ x_i<min \ \ \ \ \ \ \ \ \ \ \ \ \\ x_i,\ \ \ \ \ if\ min\leq x_i\leq max \ \ \\ max,\ \ if\ x_i>max \ \ \ \ \ \ \ \ \ \ \ \ \end{matrix}\right.

np.concatenate((a1, a2, ...), axis=0, out=None)

功能:数据拼接

eg:E:\J---Paper\Paper Code\luo_[XifengGuo]_CapsNet-Pytorch-master\capsulenet.py    
line 133
--------------------
def show_reconstruction(model, test_loader, n_images, args):
    ...
    img = combine_images(np.transpose(data, [0, 2, 3, 1]))
    ...

torch.Tensor.max(dim=None, keepdim=False) -> Tensor or (Tensor, Tensor)

功能:求张量在维度dim上的最大值,返回最大值和最大值的索引

eg:E:\J---Paper\Paper Code\luo_[XifengGuo]_CapsNet-Pytorch-master\capsulenet.py    
line 73
--------------------
class CapsuleNet(nn.Module):
    ...
    index = length.max(dim=1)[1]
    ...

 

Visdom 可视化

详细使用方法:https://www.pytorchtutorial.com/tag/visdom/

1、启动 visdom

E:\python> python -m visdom.server    # 前台执行
E:\python> nohup python -m visdom.server &    # 后台执行

2、打开浏览器,输入启动后分配的地址,一般为:http://localhost:8097

3、运行脚本执行

tensorboardX 可视化

详细过程请参考github项目链接:https://github.com/lanpa/tensorboardX

1、首先需要安装 tensorflow、tensorboard、tensorboardX

>>> pip3 install tensorflow    # 一般安装完 tensorflow 后会默认安装后两个
>>> pip3 install tensorboard
>>> pip3 install tensorboardX

2、运行相应的代码,生成数据文件,待下一步可视化运行

以官网 demo.py 为例,其存放在如下路径:https://github.com/lanpa/tensorboardX/tree/master/examples

运行该脚本,则会生成 runs 文件,文件路径及名称在脚本代码中设定

3、使用 tensorboard 运行

E:\Python37\Scripts 是我 tensorboard 所在的路径(默认在python安装文件夹中的Scripts中),C:\Users\Tom\Desktop\runs 是我上部脚本运行生成文件 runs 的路径

E:\Python37\Scripts> tensorboard --logdir C:\Users\Tom\Desktop\runs

运行完后,终端会输出:

Serving TensorBoard on localhost; to expose to the network, use a proxy or pass --bind_all
TensorBoard 2.0.0 at http://localhost:6006/ (Press CTRL+C to quit)

4、复制输出中的链接:http://localhost:6006/ 在浏览器中运行即可

保存 ipython 环境数据

1、安装 save_ipython_variables

pip install save_ipython_variables

2、因为该模块中使用了python2的语法,直接导入会发生错误,所以需修改源码

错误如下:

进入该包的路径:你的python安装路径\Python37\Lib\site-packages\save_ipython_variables

将55行的

print 'Loaded the following variables:', names_loaded_successfully

修改为:

print('Loaded the following variables:', names_loaded_successfully)

提取迭代器中的某些元素(将其转化为list)

以读取MNIST数据集为例:

torch.utils.data.DataLoader(dataset, ...)

功能:装载数据集

【注】:第一个参数为数据集,数据集可通过 torchvision 包导入已有的数据集,也可以自己构造数据集。

构造自己的数据集:https://blog.youkuaiyun.com/Teeyohuang/article/details/79587125

torchvision.dataset.MNIST()

功能:导入MNIST数据集

# 此处仅读取MNIST的测试集
>>> test_loader = torch.utils.data.DataLoader(datasets.MNIST(dataset_path, train=False, \
    download=False,transform=transforms.ToTensor()),batch_size=batch_size, shuffle=True)
>>> for x, y in test_loader:
...     print(x.size())    # x 表示数据 (batch_size, C, W, H)
...     print(y.size())    # y 表示标签 (batch_size)

# 可以将 test_loader 转为列表
>>> list(test_loader)        # MNIST划分为许多batch_size
>>> list(test_loader)[0]     # 第一个batch
>>> list(test_loader)[0][0]  # 第一个batch的数据
>>> list(test_loader)[0][1]  # 第一个batch的标签

model.named_parameters(prefix='', recurse=True)

功能:迭代打印每一层的名称和权重参数

>>> for name, param in model.named_parameters():
...     print(name)
...     print(param.size())
...
conv1.weight
torch.Size([256, 1, 9, 9])
conv1.bias
torch.Size([256])
primarycaps.conv2d.weight
torch.Size([256, 256, 9, 9])
primarycaps.conv2d.bias
torch.Size([256])
digitcaps.weight
torch.Size([10, 1152, 16, 8])

model.parameters(recurse=True)

功能:与model.named_parameters类似,只是仅有权重参数,没有名称

>>> for name, param in model.parameters():
...     print(param.size())
...
torch.Size([256, 1, 9, 9])
torch.Size([256])
torch.Size([256, 256, 9, 9])
torch.Size([256])
torch.Size([10, 1152, 16, 8])

提取其中的元素:

>>> list(model.parameters())
[数据]
>>> len(list(model.parameters()))
5
>>> list(model.parameters())[0].size()
torch.Size([256, 1, 9, 9])
>>> list(model.parameters())[1].size()
torch.Size([256])

pytorch图像预处理

torchvision.transforms是pytorch中的图像预处理包,一般用Compose把多个步骤整合到一起:

transforms.Compose(
    [ transforms.CenterCrop(10),
      transforms.ToTensor() ]
)

参考:Pytorch:transforms的二十二个方法

张量乘法

torch.matmul(input, other, out=None) -> Tensor

功能:张量乘法,对于多维张量相乘:(1)看最后两个维度是否满足矩阵乘法定义: (a,\ b) \times (b,\ c)=(a,\ c),(2)前面的维度必须符合广播要求(对应的不同维度要么一方为空,要么一方为1)

广播机制:https://www.runoob.com/numpy/numpy-broadcast.html

【注】:多维的张量乘法的本质仍然进行的是矩阵乘法(在最后两个维度),而前面的维度则通过广播来进行匹配。

>>> a = torch.randn([3, 4])              # 
>>> b = torch.randn([6, 5, 8, 4, 2])     # 6, 5, 8   可以广播
>>> torch.matmul(a, b).shape
torch.Size([6, 5, 8, 3, 2])

>>> a = torch.randn([7, 1, 3, 4])        #    7, 1    可以广播
>>> b = torch.randn([6, 1, 8, 4, 2])     # 6, 1, 8
>>> torch.matmul(a, b).shape
torch.Size([6, 7, 8, 3, 2])

>>> a = torch.randn([7, 3, 4])           #       7    不能广播
>>> b = torch.randn([6, 1, 8, 4, 2])     # 6, 1, 8
>>> torch.matmul(a, b).shape
Error!

torch.dot(input, tensor) -> Tensor

功能:计算一维张量的点积

>>> torch.dot(torch.tensor([1, 2, 3]), torch.tensor([4, 5, 6]))
tensor(32)    # 1*4 + 2*5 + 3*6 = 32

*

功能:点乘(张量对应位置相乘)

>>> torch.tensor([2, 3]) * torch.tensor([2, 1])
tensor([4, 3]) 
>>> torch.tensor([[2, 3]]) * torch.tensor([[2], [1]])    # dim=(1, 2) dim=(2, 1) 此处两者皆会广播为(2, 2)
tensor([[4, 6],
        [2, 3]])
>>> torch.tensor([[1, 2],[3, 4]]) * torch.tensor([[5, 6],[7, 8]])
tensor([[ 5, 12],
        [21, 32]])

nn.ModuleList 与 nn.Sequential 的区别

        nn.ModuleList 的 forward函数需要自己实现,故可以自己定义网络的执行顺序(甚至重复执行),而 nn.Sequential 则会根据定义的网络自动生成 forward 函数,顺序执行。

PyTorch 中的 ModuleList 和 Sequential: 区别和使用场景:https://blog.youkuaiyun.com/byron123456sfsfsfa/article/details/89930990

官方文档:https://pytorch.org/docs/stable/nn.html#sequential

损失函数 nn.BCEloss()

参数:https://www.jianshu.com/p/ac3bec3dde3e

参考:https://blog.youkuaiyun.com/geter_CS/article/details/84747670

 

 

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值