torch.nn.Module.train(mode=True)

参考链接: torch.nn.Module.train(mode=True)

在这里插入图片描述

原文及翻译:

train(mode=True)
方法: train(mode=True)
    Sets the module in training mode.
    将当前模块设置成训练模式.
    This has any effect only on certain modules. See documentations of
    particular modules for details of their behaviors in 
    training/evaluation mode, if they are affected, 
    e.g. Dropout, BatchNorm, etc.
	这个方法只对某些特定的模块有实际效果.读者可以在相关文档上查看这些特
    定的模块(受该函数影响的模块,比如Dropout和BatchNorm模块等等)在训练/评估
    模式下的不同行为方式的详细信息.
    
    Parameters  参数
        mode (bool) – whether to set training mode (True) or
        evaluation mode (False). Default: True.
		mode (布尔类型) – 是否将模块设置成训练模式(True)或者评估
		模式(False). 默认值是True.
    
    Returns  函数返回
        self  自身self
    Return type  返回类型
        Module  模块Module类型

代码实验展示:

import torch 
import torch.nn as nn
torch.manual_seed(seed=20200910)
class Model(torch.nn.Module):
    def __init__(self):
        super(Model,self).__init__()
        self.conv1=torch.nn.Sequential(  # 输入torch.Size([64, 1, 28, 28])
                torch.nn.Conv2d(1,64,kernel_size=3,stride=1,padding=1),
                torch.nn.ReLU(),  # 输出torch.Size([64, 64, 28, 28])
                torch.nn.Conv2d(64,128,kernel_size=3,stride=1,padding=1),  # 输出torch.Size([64, 128, 28, 28])
                torch.nn.ReLU(),
                torch.nn.MaxPool2d(stride=2,kernel_size=2)  # 输出torch.Size([64, 128, 14, 14])
        )

        self.dense=torch.nn.Sequential(  # 输入torch.Size([64, 14*14*128])
                    torch.nn.Linear(14*14*128,1024),  # 输出torch.Size([64, 1024])
                    torch.nn.ReLU(),
                    torch.nn.Dropout(p=0.5),
                    torch.nn.Linear(1024,10)  # 输出torch.Size([64, 10])        
        )
        self.layer4cxq1 = torch.nn.Conv2d(2,33,4,4)
        self.layer4cxq2 = torch.nn.ReLU()
        self.layer4cxq3 = torch.nn.MaxPool2d(stride=2,kernel_size=2)
        self.layer4cxq4 = torch.nn.Linear(14*14*128,1024)
        self.layer4cxq5 = torch.nn.Dropout(p=0.8)
        self.attribute4cxq = nn.Parameter(torch.tensor(20200910.0))
        self.attribute4lzq = nn.Parameter(torch.tensor([2.0,3.0,4.0,5.0]))    
        self.attribute4hh = nn.Parameter(torch.randn(3,4,5,6))
        self.attribute4wyf = nn.Parameter(torch.randn(7,8,9,10))

    def forward(self,x):  # torch.Size([64, 1, 28, 28])
        x = self.conv1(x)  # 输出torch.Size([64, 128, 14, 14])
        x = x.view(-1,14*14*128)  # torch.Size([64, 14*14*128])
        x = self.dense(x)  # 输出torch.Size([64, 10])
        return x

print('cuda(GPU)是否可用:',torch.cuda.is_available())
print('torch的版本:',torch.__version__)

model = Model() #.cuda()

print('调用方法eval()之后'.center(100,'-'))
model_1 = model.eval()
print(id(model))
print(id(model_1))
print(id(model)==id(model_1))


print('调用方法train()之后'.center(100,'-'))
model_2 = model.train()
print(id(model))
print(id(model_2))
print(id(model)==id(model_2))


print('调用方法train(mode=True)之后'.center(100,'-'))
model_3 = model.train(mode=True)
print(id(model))
print(id(model_3))
print(id(model)==id(model_3))


print('调用方法train(mode=False)之后'.center(100,'-'))
model_4 = model.train(mode=False)
print(id(model))
print(id(model_4))
print(id(model)==id(model_4))

控制台输出结果展示:

Windows PowerShell
版权所有 (C) Microsoft Corporation。保留所有权利。

尝试新的跨平台 PowerShell https://aka.ms/pscore6

加载个人及系统配置文件用了 929 毫秒。
(base) PS C:\Users\chenxuqi\Desktop\News4cxq\test4cxq> conda activate ssd4pytorch1_2_0
(ssd4pytorch1_2_0) PS C:\Users\chenxuqi\Desktop\News4cxq\test4cxq>  & 'D:\Anaconda3\envs\ssd4pytorch1_2_0\python.exe' 'c:\Users\chenxuqi\.vscode\extensions\ms-python.python-2020.12.424452561\pythonFiles\lib\python\debugpy\launcher' '59128' '--' 'c:\Users\chenxuqi\Desktop\News4cxq\test4cxq\test2.py'
cuda(GPU)是否可用: True
torch的版本: 1.2.0+cu92
--------------------------------------------调用方法eval()之后--------------------------------------------
2397611164168
2397611164168
True
-------------------------------------------调用方法train()之后--------------------------------------------
2397611164168
2397611164168
True
---------------------------------------调用方法train(mode=True)之后---------------------------------------
2397611164168
2397611164168
True
--------------------------------------调用方法train(mode=False)之后---------------------------------------
2397611164168
2397611164168
True
(ssd4pytorch1_2_0) PS C:\Users\chenxuqi\Desktop\News4cxq\test4cxq>
  • 8
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
LDAM损失函数pytorch代码如下:class LDAMLoss(nn.Module): def init(self, cls_num_list, max_m=0.5, weight=None, s=30): super(LDAMLoss, self).init() m_list = 1.0 / np.sqrt(np.sqrt(cls_num_list)) m_list = m_list * (max_m / np.max(m_list)) m_list = torch.cuda.FloatTensor(m_list) self.m_list = m_list assert s > 0 self.s = s if weight is not None: weight = torch.FloatTensor(weight).cuda() self.weight = weight self.cls_num_list = cls_num_list def forward(self, x, target): index = torch.zeros_like(x, dtype=torch.uint8) index_float = index.type(torch.cuda.FloatTensor) batch_m = torch.matmul(self.m_list[None, :], index_float.transpose(1,0)) # 0,1 batch_m = batch_m.view((16, 1)) # size=(batch_size, 1) (-1,1) x_m = x - batch_m output = torch.where(index, x_m, x) if self.weight is not None: output = output * self.weight[None, :] target = torch.flatten(target) # 将 target 转换成 1D Tensor logit = output * self.s return F.cross_entropy(logit, target, weight=self.weight) 模型部分参数如下:# 设置全局参数 model_lr = 1e-5 BATCH_SIZE = 16 EPOCHS = 50 DEVICE = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu') use_amp = True use_dp = True classes = 7 resume = None CLIP_GRAD = 5.0 Best_ACC = 0 #记录最高得分 use_ema=True model_ema_decay=0.9998 start_epoch=1 seed=1 seed_everything(seed) # 数据增强 mixup mixup_fn = Mixup( mixup_alpha=0.8, cutmix_alpha=1.0, cutmix_minmax=None, prob=0.1, switch_prob=0.5, mode='batch', label_smoothing=0.1, num_classes=classes) # 读取数据集 dataset_train = datasets.ImageFolder('/home/adminis/hpy/ConvNextV2_Demo/RAF-DB/RAF/train', transform=transform) dataset_test = datasets.ImageFolder("/home/adminis/hpy/ConvNextV2_Demo/RAF-DB/RAF/valid", transform=transform_test)# 导入数据 train_loader = torch.utils.data.DataLoader(dataset_train, batch_size=BATCH_SIZE, shuffle=True,drop_last=True) test_loader = torch.utils.data.DataLoader(dataset_test, batch_size=BATCH_SIZE, shuffle=False) 帮我用pytorch实现模型在模型训练中使用LDAM损失函数
05-30
好的,以下是使用LDAM损失函数训练模型的Pytorch代码: ```python import torch import torch.nn as nn import torch.nn.functional as F import numpy as np from torchvision import datasets, transforms from ldam_loss import LDAMLoss # 定义模型 class MyModel(nn.Module): def __init__(self, num_classes): super(MyModel, self).__init__() self.num_classes = num_classes self.features = nn.Sequential( nn.Conv2d(3, 32, kernel_size=3, stride=1, padding=1), nn.BatchNorm2d(32), nn.ReLU(inplace=True), nn.MaxPool2d(kernel_size=2, stride=2), nn.Conv2d(32, 64, kernel_size=3, stride=1, padding=1), nn.BatchNorm2d(64), nn.ReLU(inplace=True), nn.MaxPool2d(kernel_size=2, stride=2), nn.Conv2d(64, 128, kernel_size=3, stride=1, padding=1), nn.BatchNorm2d(128), nn.ReLU(inplace=True), nn.MaxPool2d(kernel_size=2, stride=2), ) self.classifier = nn.Sequential( nn.Linear(128 * 4 * 4, 256), nn.ReLU(inplace=True), nn.Linear(256, num_classes), ) def forward(self, x): x = self.features(x) x = x.view(x.size(0), -1) x = self.classifier(x) return x # 设置超参数 model_lr = 1e-4 BATCH_SIZE = 16 EPOCHS = 50 DEVICE = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu') use_amp = True use_dp = True classes = 7 resume = None CLIP_GRAD = 5.0 Best_ACC = 0 use_ema = True model_ema_decay = 0.9998 start_epoch = 1 seed = 1 # 设置随机种子 def seed_everything(seed): torch.manual_seed(seed) torch.cuda.manual_seed_all(seed) np.random.seed(seed) seed_everything(seed) # 定义数据增强 transform = transforms.Compose([ transforms.Resize(224), transforms.RandomHorizontalFlip(), transforms.RandomRotation(10), transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) ]) transform_test = transforms.Compose([ transforms.Resize(224), transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) ]) # 定义数据集 dataset_train = datasets.ImageFolder('/home/adminis/hpy/ConvNextV2_Demo/RAF-DB/RAF/train', transform=transform) dataset_test = datasets.ImageFolder("/home/adminis/hpy/ConvNextV2_Demo/RAF-DB/RAF/valid", transform=transform_test) # 定义数据加载器 train_loader = torch.utils.data.DataLoader(dataset_train, batch_size=BATCH_SIZE, shuffle=True, drop_last=True) test_loader = torch.utils.data.DataLoader(dataset_test, batch_size=BATCH_SIZE, shuffle=False) # 定义模型及优化器 model = MyModel(num_classes=classes).to(DEVICE) optimizer = torch.optim.Adam(model.parameters(), lr=model_lr) # 使用LDAM损失函数 cls_num_list = [dataset_train.targets.count(i) for i in range(classes)] criterion = LDAMLoss(cls_num_list=cls_num_list, max_m=0.5, weight=None, s=30) # 训练模型 for epoch in range(start_epoch, EPOCHS+1): model.train() for i, (data, target) in enumerate(train_loader): data, target = data.to(DEVICE), target.to(DEVICE) mixup_data, mixup_target = mixup_fn(data, target) # 数据增强 optimizer.zero_grad() output = model(mixup_data) loss = criterion(output, mixup_target) if use_dp: loss.backward() torch.nn.utils.clip_grad_norm_(model.parameters(), CLIP_GRAD) else: with amp.scale_loss(loss, optimizer) as scaled_loss: scaled_loss.backward() torch.nn.utils.clip_grad_norm_(amp.master_params(optimizer), CLIP_GRAD) optimizer.step() if use_ema: ema_model = ModelEMA(model, decay=model_ema_decay) ema_model.update(model) else: ema_model = None test_acc = test(model, test_loader, DEVICE) if test_acc > Best_ACC: Best_ACC = test_acc save_checkpoint({ 'epoch': epoch, 'state_dict': model.state_dict(), 'optimizer': optimizer.state_dict(), 'Best_ACC': Best_ACC, }, is_best=True) ```
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值