PyTorch未实现转发时出错

egmofgnx  于 2023-01-05  发布在  其他
关注(0)|答案(1)|浏览(167)

我在用pytorch做一个数字分类的代码

epochess =[]
  train_losses = []
  test_losses = []
  acc_training =[]
  acc_testing = []
  for epoch in range (epochs):
    train_acc, train_epoch_loss = train_CNN(model,loss_function, optimizer, train_load, device)
    print('epoch',epoch ,'training loss',train_epoch_loss)
    train_losses.append(train_epoch_loss)
    print('epoch',epoch,'training accuracy',train_acc)
    acc_training.append(train_acc)
  
    test_acc, test_epoch_loss = validate_CNN(model, loss_function, test_load, device)
    print('epoch',epoch,'testing loss',test_epoch_loss)
    test_losses.append(test_epoch_loss)
    print('epoch',epoch,'testing accuracy',test_acc)
    acc_testing.append(test_acc)

    epochess.append(epoch)

我得到了一个错误,我是遵循正确的道路就像它说在youtube这里是以下错误

---------------------------------------------------------------------------
NotImplementedError                       Traceback (most recent call last)
<ipython-input-17-0bcb51ebbc3d> in <module>
      5 acc_testing = []
      6 for epoch in range (epochs):
----> 7   train_acc, train_epoch_loss = train_CNN(model,loss_function, optimizer, train_load, device)
      8   print('epoch',epoch ,'training loss',train_epoch_loss)
      9   train_losses.append(train_epoch_loss)

2 frames
/usr/local/lib/python3.8/dist-packages/torch/nn/modules/module.py in _forward_unimplemented(self, *input)
    242         registered hooks while the latter silently ignores them.
    243     """
--> 244     raise NotImplementedError(f"Module [{type(self).__name__}] is missing the required \"forward\" function")
    245 
    246 

NotImplementedError: Module [CNN] is missing the required "forward" function
9lowa7mx

9lowa7mx1#

您是如何实现您的model的?您是使用PyTorch的内置模型还是创建了一个自定义模型?
如果您创建了一个自定义模型,请确保使用了PyTorch中的正确组件(例如torch.nn.Lineartorch.nn.Conv2dhttps://pytorch.org/tutorials/beginner/introyt/modelsyt_tutorial.html),否则PyTorch可能会抱怨缺少某些函数,就像您的情况一样。

相关问题