PyTorch 中的测试精度非常低

问题描述 投票:0回答:0

我正在尝试在 PyTorch 中构建 CNN 模型。当在 GPU 上运行时,测试准确度变得非常低,而在 CPU 上运行时准确度更高。我不知道为什么会这样。两者的代码逻辑是否相同?我哪里错了?

#----------------------------fnction to train model-------------------------------#
def train(model,loss_fn,optimizer,epochs=10):
    trainAcc=[]
    testAcc=list()
    losses=torch.zeros(epochs)
    testloss=torch.zeros(epochs)
    print('Training Started........')
    for i in range(epochs):
    model.train()
    batchAcc=[]
    batchLoss=[]
    
    for x,y in train_loader:
        x=x.to(device)
        y=y.to(device)
        y=y.float()
        y=y[:,None]
        yHat=model(x)
        loss=lossfn(yHat,y)
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
        #---bring the targets to cpu------#
        yHat=yHat.cpu()
        y=y.cpu()
        #----------predictions----------#
        predictions=yHat>0
        batchAcc.append(100*torch.mean((predictions==y).float()))
        batchLoss.append(loss.item())
    trainAcc.append(np.mean(batchAcc))
    losses[i]=np.mean(batchLoss)
    
    #-------------------------evaluation mode----------------------#
    model.eval()
    X,y=next(iter(test_loader))
    batchT=list()
    X=X.to(device)
    y=y.to(device)
    with torch.no_grad():
        yHat=model(X)
        y=y.float()
        y=y[:,None]
        loss=lossfn(yHat,y)
        #---------convert back to cpu----------#
        yHat=yHat.cpu()
        y=y.cpu()
        predictions=yHat>0
        testloss[i]=loss.item()
        batchT.append(100*torch.mean((predictions==y).float()))
    testAcc.append(np.mean(batchT))
    print(f'Epoch:{i+1} \t loss:{loss.item():.3f} \t train accuracy:{trainAcc[-1]:.3f} \t test accuracy:{testAcc[-1]:.3f}')
    
return trainAcc,testAcc,losses,testloss

训练后的结果

Training Started........
Epoch:1      loss:2.839      train accuracy:89.436   test accuracy:56.250
Epoch:2      loss:2.388      train accuracy:91.794   test accuracy:12.500
Epoch:3      loss:2.614      train accuracy:93.520   test accuracy:12.500
Epoch:4      loss:1.502      train accuracy:94.248   test accuracy:50.000
Epoch:5      loss:1.844      train accuracy:94.632   test accuracy:12.500

训练精度看起来不错,但不知何故测试精度没有多大意义,而且这种情况只在 GPU 上训练时发生。如何解决这个问题?

python tensorflow deep-learning pytorch conv-neural-network
© www.soinside.com 2019 - 2024. All rights reserved.