<jupyter_start><jupyter_text>Emsemble方式训练十个网络。共同决策，判断预测最多的那个作为结果。<jupyter_code>for i in range(10):
    train_loader = torch.utils.data.DataLoader(dataset=train_dataset,
                                           batch_size=batch_size, shuffle=True)
    test_loader = torch.utils.data.DataLoader(dataset=test_dataset,
                                           batch_size=batch_size, shuffle=False)
    
    model = Net().to(device)
    optimizer = optim.Adam(model.parameters(), lr=0.003)
    criterion = nn.CrossEntropyLoss().to(device)
    exp_lr_scheduler = lr_scheduler.StepLR(optimizer, step_size=7, gamma=0.1)

    n_epochs = 20

    for epoch in range(n_epochs):
        train(epoch)
        evaluate(train_loader)
    
    # 仅保存和加载模型的参数（推荐这个方式）
    torch.save(model.state_dict(), 'pytorchNetEnsemble{}.ckpt'.format(i))
    # resnet.load_state_dict(torch.load('params.ckpt'))
def predicitonWithModel(data_loader, predictModel):
    predictModel.eval()
    test_pred = torch.LongTensor()
    
    for i, data in enumerate(data_loader):
        data =data.to(device)
            
        output = predictModel(data)
        
        pred = output.cpu().data.max(1, keepdim=True)[1]
        test_pred = torch.cat((test_pred, pred), dim=0)
        
    return test_pred
# 建立模型l列表
Nets = []
for i in range(10):
    model = Net().to(device)
    model.load_state_dict(torch.load('pytorchNetEnsemble{}.ckpt'.format(i)))
    Nets.append(model)
results = None
for i in range(10):
    result =  predicitonWithModel(test_loader, Nets[i])
    if results is None:
        results = result.numpy()
    else:
        results = np.hstack((results,result.numpy()))
print(results.shape)
print(results[0])
print(results[5])
test_pred = predicitonWithModel(test_loader,Nets[0])
out_df = pd.DataFrame(np.c_[np.arange(1, len(test_dataset)+1)[:,None], test_pred.numpy()], 
                      columns=['ImageId', 'Label'])
out_df.to_csv('submissionnew1.csv', index=False)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13141993957703926, 'fraction_numerical': 0.012084592145015106, 'mean_word_length': 3.200845665961945, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '485994', 'license': 'MIT', 'n_tokens_mistral': 718, 'n_tokens_neox': 724, 'n_words': 100, 'path': 'dl1/.ipynb_checkpoints/CNN_Digit-checkpoint.ipynb', 'repo_name': 'veraposeidon/Fast.ai_Tips', 'size': '1986'}