当前位置:网站首页>Pytorch学习笔记--常用函数总结2
Pytorch学习笔记--常用函数总结2
2022-07-25 15:28:00 【whut_L】
目录
3--Model.train()函数和Model.eval()函数
1-- torch.nn.Sequential( )函数
作用:通过顺序序列创建神经网络结构
示例:调用Block函数时,将顺序执行括号内的Function。
import torch
x = torch.randn(1, 1, 64, 64)
Block = torch.nn.Sequential(
torch.nn.Conv2d(in_channels = 1, out_channels = 32, kernel_size = 3, stride = 1),
torch.nn.ReLU(),
torch.nn.Conv2d(in_channels = 32, out_channels = 32, kernel_size = 3, stride = 1),
torch.nn.ReLU()
)
result = Block(x)
print(result)上述代码等同于:
import torch
Conv1 = torch.nn.Conv2d(in_channels = 1, out_channels = 32, kernel_size = 3, stride = 1)
Conv2 = torch.nn.Conv2d(in_channels = 32, out_channels = 32, kernel_size = 3, stride = 1),
Relu = torch.nn.ReLU()
x = torch.randn(1, 1, 64, 64)
print(x)
x = Conv1(x)
x = Relu(x)
x = Conv2(x)
result = Relu(x)
print(result)############################
2--torch.flatten()函数
作用:按维度进行拼接;torch.flatten(input, start_dim = 0, end_dim = -1);input为输入tensor数据,start_dim为拼接的起始维度,end_dim为拼接的终止维度。
示例:
import torch
x = torch.randn(2, 3, 3)
print(x)
result1 = torch.flatten(x, start_dim = 0, end_dim = 2)
print(result1)
result2 = torch.flatten(x, start_dim = 0, end_dim = 1)
print(result2)
result3 = torch.flatten(x, start_dim = 1, end_dim = 2)
print(result3)结果:
tensor([[[ 0.3546, -0.8551, 2.3490],
[-0.0920, 0.0773, -0.4556],
[-1.6943, 1.4517, -0.0767]],
[[-0.6950, 0.4382, -1.2691],
[-0.0252, -0.4980, -0.5994],
[-0.2581, -0.2544, -0.6787]]]) #X
tensor([ 0.3546, -0.8551, 2.3490, -0.0920, 0.0773, -0.4556, -1.6943, 1.4517,
-0.0767, -0.6950, 0.4382, -1.2691, -0.0252, -0.4980, -0.5994, -0.2581,
-0.2544, -0.6787]) #result1
tensor([[ 0.3546, -0.8551, 2.3490],
[-0.0920, 0.0773, -0.4556],
[-1.6943, 1.4517, -0.0767],
[-0.6950, 0.4382, -1.2691],
[-0.0252, -0.4980, -0.5994],
[-0.2581, -0.2544, -0.6787]]) #result2
tensor([[ 0.3546, -0.8551, 2.3490, -0.0920, 0.0773, -0.4556, -1.6943, 1.4517,
-0.0767],
[-0.6950, 0.4382, -1.2691, -0.0252, -0.4980, -0.5994, -0.2581, -0.2544,
-0.6787]]) #result3############################
3--Model.train()函数和Model.eval()函数
Model.train()函数用于模型的训练状态,将模型置于训练模式;
Model.eval()函数用于模型的测试状态,将模型置于测试模式;
模型处于训练和测试这两种不同的模式,有一些函数所起的作用是不同的,如:Batch Normalization 和 Dropout(训练模式时将启用,测试模式时则禁用);
如果模型中有BN层(Batch Normalization)和Dropout,需要在训练时添加model.train()。model.train()是保证BN层能够用到每一批数据的均值和方差。对于Dropout,model.train()是随机取一部分网络连接来训练更新参数。
如果模型中有BN层(Batch Normalization)和Dropout,在测试时添加model.eval()。model.eval()是保证BN层能够用全部训练数据的均值和方差,即测试过程中要保证BN层的均值和方差不变。对于Dropout,model.eval()是利用到了所有网络连接,即不进行随机舍弃神经元。
训练完train样本后,生成的模型model要用来测试样本。在model(test)之前,需要加上model.eval(),否则的话,有输入数据,即使不训练,它也会改变权值。这是model中含有BN层和Dropout所带来的的性质。
参考链接
边栏推荐
猜你喜欢

Spark submission parameters -- use of files

MySQL installation and configuration super detailed tutorial and simple database and table building method

为什么PrepareStatement性能更好更安全?

ML - 语音 - 语音处理介绍

小波变换--dwt2 与wavedec2

Image cropper example

ML - 图像 - 深度学习和卷积神经网络

Remember that spark foreachpartition once led to oom

Idea remotely submits spark tasks to the yarn cluster

Geogle Colab笔记1--运行Geogle云端硬盘上的.py文件
随机推荐
Xcode added mobileprovision certificate file error: Xcode encoded an error
二进制补码
Remember that spark foreachpartition once led to oom
Image cropper example
Pytorch学习笔记-Advanced_CNN(Using Inception_Module)实现Mnist数据集分类-(注释及结果)
Spark SQL common time functions
Take you to create your first C program (recommended Collection)
Implementation of asynchronous FIFO
理解“平均负载”
C#精挑整理知识要点10 泛型(建议收藏)
window系统黑窗口redis报错20Creating Server TCP listening socket *:6379: listen: Unknown error19-07-28
ML - natural language processing - Basics
《图书馆管理系统——“借书还书”模块》项目研发阶段性总结
In depth: micro and macro tasks
Instance tunnel use
ML - Speech - advanced speech model
Reflection - Notes
Spark judges that DF is empty
Run redis on docker to start in the form of configuration file, and the connection client reports an error: server closed the connection
UIDocumentInteractionController UIDocumentPickerViewController