rasbt / machine-learning-book

Code Repository for Machine Learning with PyTorch and Scikit-Learn
https://sebastianraschka.com/books/#machine-learning-with-pytorch-and-scikit-learn
MIT License
3.66k stars 1.32k forks source link

Error: TypeError: Accuracy.__new__() missing 1 required positional argument: 'task'(Chapter-13) #128

Closed andysingal closed 1 year ago

andysingal commented 1 year ago

Hey Sebastian, While running the code , i am getting the following error: TypeError: Accuracy.new() missing 1 required positional argument: 'task'

from pytorch_lightning.callbacks import ModelCheckpoint

mnistclassifier = MultiLayerPerceptron()

callbacks = [ModelCheckpoint(save_top_k=1, mode='max', monitor="valid_acc")] # save top 1 model

if torch.cuda.is_available(): # if you have GPUs
    trainer = pl.Trainer(max_epochs=10, callbacks=callbacks, gpus=1)
else:
    trainer = pl.Trainer(max_epochs=10, callbacks=callbacks)

trainer.fit(model=mnistclassifier, datamodule=mnist_dm)

code before that is the same as shared in your git repo. Thanks, Ankush Singal

rasbt commented 1 year ago

Hi there! In newer versions of torchmetrics it's now required to specify the task and number of classes. E.g., whereas it was

acc = torchmetrics.Accuracy()

before, it's now

acc = torchmetrics.Accuracy(task="multiclass", num_classes=10)
rasbt commented 1 year ago

Should be fixed now!