简体   繁体   English

AttributeError: 'numpy.float64' object 没有属性 'cpu'

[英]AttributeError: 'numpy.float64' object has no attribute 'cpu'

I am trying to run BERT and train a model using pytorch. I am not sure why I am getting this error after finishing the first Epoch.我正在尝试运行 BERT 并使用 pytorch 训练 model。我不确定为什么在完成第一个纪元后出现此错误。 I am using this code link我正在使用此代码链接

history = defaultdict(list)
best_accuracy = 0

for epoch in range(EPOCHS):
    
    # Show details 
    print(f"Epoch {epoch + 1}/{EPOCHS}")
    print("-" * 10)
    
    train_acc, train_loss = train_epoch(
        model,
        train_data_loader,
        loss_fn,
        optimizer,
        device,
        scheduler,
        len(df_train)
    )
    
    print(f"Train loss {train_loss} accuracy {train_acc}")
    
    # Get model performance (accuracy and loss)
    val_acc, val_loss = eval_model(
        model,
        val_data_loader,
        loss_fn,
        device,
        len(df_val)
    )
    
    print(f"Val   loss {val_loss} accuracy {val_acc}")
    print()
    
    history['train_acc'].append(train_acc.cpu())
    history['train_loss'].append(train_loss.cpu())
    history['val_acc'].append(val_acc.cpu())
    history['val_loss'].append(val_loss.cpu())
    
    # If we beat prev performance
    if val_acc > best_accuracy:
        torch.save(model.state_dict(), 'best_model_state.bin')
        best_accuracy = val_acc

Here is the output and the error message Image这是 output 和错误消息图像

It is a first time for me to work with pytorch. Any ideas how to fix the error>这是我第一次使用 pytorch。任何解决错误的想法>

I checked kaggle link and I see that there is no cpu() reference as you have posted in your code.我检查了 kaggle 链接,发现没有您在代码中发布的cpu()参考。 It should simply be:它应该只是:

history['train_acc'].append(train_acc)
history['train_loss'].append(train_loss)
history['val_acc'].append(val_acc)
history['val_loss'].append(val_loss)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM