def train(opt): device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu') train_dataloader, val_dataloader = create_dataloader(opt) net = Classification() # 定义训练的网络模型 net.to(device) net.train() loss_function = nn.CrossEntropyLoss() # 定义损失函数为交叉熵损失函数 optimizer = optim.Adam(net.parameters(), lr=0.001) # 定义优化器(训练参数,学习率) for epoch in range(opt.num_epochs): # 一个epoch即对整个训练集进行一次训练 running_loss = 0.0 correct = 0 total = 0 time_start = time.perf_counter() for step, data in enumerate(train_dataloader, start=0): # 遍历训练集,step从0开始计算 inputs, labels = data # 获取训练集的图像和标签 inputs, labels = inputs.to(device), labels.to(device) optimizer.zero_grad() # 清除历史梯度 # forward + backward + optimize # outputs = net(inputs.permute(0,1,3,2)) # 正向传播 outputs = net(inputs) # 正向传播 print('outputs.shape', outputs.shape, labels.shape) loss = loss_function(outputs, labels) # 计算损失 loss.backward() # 反向传播 optimizer.step() # 优化器更新参数 predict_y = torch.max(outputs, dim=1)[1] total += labels.size(0) correct += (predict_y == labels).sum().item() running_loss += loss.item() # print statistics # print('train_dataloader length: ', len(train_dataloader)) acc = correct / total print('Train on epoch {}: loss:{}, acc:{}%'.format(epoch + 1, running_loss / total, 100 * correct / total)) # 保存训练得到的参数 if opt.model == 'basic': save_weight_name = os.path.join(opt.save_path, 'Basic_Epoch_{0}_Accuracy_{1:.2f}.pth'.format( epoch + 1, acc)) elif opt.model == 'plus': save_weight_name = os.path.join(opt.save_path, 'Plus_Epoch_{0}_Accuracy_{1:.2f}.pth'.format( epoch + 1, acc)) torch.save(net.state_dict(), save_weight_name) print('Finished Training')
cifar_10_train_dt = MyCustomDataset('data', download=True, transform=ToTensor()) cifar_10_train_l = DataLoader(cifar_10_train_dt, batch_size=batch_size, shuffle=True, drop_last=True, pin_memory=torch.cuda.is_available()) encoder = Encoder().to(device) loss_fn = DeepInfoMaxLoss(1, 0, 1).to(device) classification = Classification().to(device) encoder_optim = Adam(encoder.parameters(), lr=1e-4) loss_optim = Adam(loss_fn.parameters(), lr=1e-4) classification_optim = Adam(classification.parameters(), lr=1e-4) epoch_restart = 0 root = Path(r'models') if epoch_restart > 0 and root is not None: enc_file = root / Path('encoder' + str(epoch_restart) + '.wgt') loss_file = root / Path('loss' + str(epoch_restart) + '.wgt') classification_loss_file = root / Path('classification_loss' + str(epoch_restart) + '.wgt') encoder.load_state_dict(torch.load(str(enc_file))) loss_fn.load_state_dict(torch.load(str(loss_file))) classification.load_state_dict( torch.load(str(classification_loss_file))) for epoch in range(epoch_restart + 1, 501):
config = args.parse_args() config2 = Config() dataset = SentenceClassificationDataset('../data/processing_data', config.strmaxlen) print('unique labels = {}'.format(dataset.get_unique_labels_num())) print('vocab size = {}'.format(dataset.get_vocab_size())) if config.model_name == 'CNN': model = Classification(config.embedding, config.strmaxlen, dataset.get_unique_labels_num(), dataset.get_vocab_size()) criterion = nn.CrossEntropyLoss() optimizer = optim.Adam(model.parameters(), lr=0.01) elif config.model_name == 'RCNN': model = RCNN(config.embedding, config.strmaxlen, dataset.get_unique_labels_num(), dataset.get_vocab_size()) if config.mode == 'train': model = model.cuda() criterion = nn.CrossEntropyLoss() optimizer = optim.Adam(model.parameters(), lr=0.01) elif config.model_name == "DUALRCNN": model = DualRCNN(config.embedding, config.strmaxlen, dataset.get_unique_labels_num(), dataset.get_vocab_size()) if torch.cuda.is_available():