Python ValueError:y和y_pred必须具有相同的形状(批次大小、数量类别等)和数量类别>;1.

Python ValueError:y和y_pred必须具有相同的形状(批次大小、数量类别等)和数量类别>;1.,python,deep-learning,pytorch,conv-neural-network,pytorch-ignite,Python,Deep Learning,Pytorch,Conv Neural Network,Pytorch Ignite,这是我的代码,我使用pytorch ignite。样本标签的形状为(batch_size,),我的网络输出为y_pred(batch_size,10),10是我的类的编号。我使用准则=F.交叉熵作为损失函数。一个历元结束后,我有一个错误,如: Epoch [1/50]: 100%|██████████| 63/63 [00:55<00:00, 1.12it/s] Current run is terminating due to exception: y and y_pred must

这是我的代码,我使用pytorch ignite。样本标签的形状为(batch_size,),我的网络输出为y_pred(batch_size,10),10是我的类的编号。我使用
准则=F.交叉熵
作为损失函数。一个历元结束后,我有一个错误,如:

Epoch [1/50]: 100%|██████████| 63/63 [00:55<00:00,  1.12it/s]
Current run is terminating due to exception: y and y_pred must have same shape of (batch_size, num_categories, ...) and num_categories > 1.
Engine run is terminating due to exception: y and y_pred must have same shape of (batch_size, num_categories, ...) and num_categories > 1.
Engine run is terminating due to exception: y and y_pred must have same shape of (batch_size, num_categories, ...) and num_categories > 1.
Traceback (most recent call last):
  File "/home/omid/anaconda3/envs/pytorch/lib/python3.8/site-packages/IPython/core/interactiveshell.py", line 3418, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-2-b8f3a45f8e35>", line 1, in <module>
    runfile('/home/omid/OMID/projects/python/mldl/NeuralMusicClassification/tools/train_net.py', wdir='/home/omid/OMID/projects/python/mldl/NeuralMusicClassification/tools')
  File "/home/omid/OMID/program/pycharm-professional-2020.2.4/pycharm-2020.2.4/plugins/python/helpers/pydev/_pydev_bundle/pydev_umd.py", line 197, in runfile
    pydev_imports.execfile(filename, global_vars, local_vars)  # execute the script
  File "/home/omid/OMID/program/pycharm-professional-2020.2.4/pycharm-2020.2.4/plugins/python/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "/home/omid/OMID/projects/python/mldl/NeuralMusicClassification/tools/train_net.py", line 73, in <module>
    main()
  File "/home/omid/OMID/projects/python/mldl/NeuralMusicClassification/tools/train_net.py", line 69, in main
    train(cfg)
  File "/home/omid/OMID/projects/python/mldl/NeuralMusicClassification/tools/train_net.py", line 48, in train
    do_train(
  File "/home/omid/OMID/projects/python/mldl/NeuralMusicClassification/engine/trainer.py", line 113, in do_train
    trainer.run(train_loader, max_epochs=epochs)
  File "/home/omid/anaconda3/envs/pytorch/lib/python3.8/site-packages/ignite/engine/engine.py", line 702, in run
    return self._internal_run()
  File "/home/omid/anaconda3/envs/pytorch/lib/python3.8/site-packages/ignite/engine/engine.py", line 775, in _internal_run
    self._handle_exception(e)
  File "/home/omid/anaconda3/envs/pytorch/lib/python3.8/site-packages/ignite/engine/engine.py", line 469, in _handle_exception
    raise e
  File "/home/omid/anaconda3/envs/pytorch/lib/python3.8/site-packages/ignite/engine/engine.py", line 752, in _internal_run
    self._fire_event(Events.EPOCH_COMPLETED)
  File "/home/omid/anaconda3/envs/pytorch/lib/python3.8/site-packages/ignite/engine/engine.py", line 424, in _fire_event
    func(*first, *(event_args + others), **kwargs)
  File "/home/omid/OMID/projects/python/mldl/NeuralMusicClassification/engine/trainer.py", line 73, in log_training_results
    evaluator.run(train_loader)
  File "/home/omid/anaconda3/envs/pytorch/lib/python3.8/site-packages/ignite/engine/engine.py", line 702, in run
    return self._internal_run()
  File "/home/omid/anaconda3/envs/pytorch/lib/python3.8/site-packages/ignite/engine/engine.py", line 775, in _internal_run
    self._handle_exception(e)
  File "/home/omid/anaconda3/envs/pytorch/lib/python3.8/site-packages/ignite/engine/engine.py", line 469, in _handle_exception
    raise e
  File "/home/omid/anaconda3/envs/pytorch/lib/python3.8/site-packages/ignite/engine/engine.py", line 745, in _internal_run
    time_taken = self._run_once_on_dataset()
  File "/home/omid/anaconda3/envs/pytorch/lib/python3.8/site-packages/ignite/engine/engine.py", line 850, in _run_once_on_dataset
    self._handle_exception(e)
  File "/home/omid/anaconda3/envs/pytorch/lib/python3.8/site-packages/ignite/engine/engine.py", line 469, in _handle_exception
    raise e
  File "/home/omid/anaconda3/envs/pytorch/lib/python3.8/site-packages/ignite/engine/engine.py", line 834, in _run_once_on_dataset
    self._fire_event(Events.ITERATION_COMPLETED)
  File "/home/omid/anaconda3/envs/pytorch/lib/python3.8/site-packages/ignite/engine/engine.py", line 424, in _fire_event
    func(*first, *(event_args + others), **kwargs)
  File "/home/omid/anaconda3/envs/pytorch/lib/python3.8/site-packages/torch/autograd/grad_mode.py", line 27, in decorate_context
    return func(*args, **kwargs)
  File "/home/omid/anaconda3/envs/pytorch/lib/python3.8/site-packages/ignite/metrics/metric.py", line 296, in iteration_completed
    self.update(output)
  File "/home/omid/anaconda3/envs/pytorch/lib/python3.8/site-packages/ignite/metrics/metric.py", line 575, in wrapper
    func(self, *args, **kwargs)
  File "/home/omid/anaconda3/envs/pytorch/lib/python3.8/site-packages/ignite/metrics/accuracy.py", line 149, in update
    self._check_shape(output)
  File "/home/omid/anaconda3/envs/pytorch/lib/python3.8/site-packages/ignite/metrics/accuracy.py", line 47, in _check_shape
    raise ValueError(
ValueError: y and y_pred must have same shape of (batch_size, num_categories, ...) and num_categories > 1.



可能您必须在类维度的预测上使用
torch.argmax
。但是,由于您并不是在代码中亲自进行评估,因此最好检查PyTorch Ignite的文档和/或GitHub问题以找到解决方案。@akshayk07如果我使用argmax,我将在损失函数中出错是的,我明白您的意思。argmax必须在计算精度之前进行,而不是在计算损耗时进行。因此,它必须由Ignite在内部处理。这就是为什么我建议查看Ignite文档(我不熟悉Ignite)。