Pytorch RuntimeError: expected scalar type Long but found Float

Viewed 1500

When trying to run my classification model with pytorch I get the error:
RuntimeError: expected scalar type Long but found Float
Which is weird since I looked at line where the error is supposed to be x = self.linear1(x) and checked with the debugger the type of x and its of type LongTensor.
I also tried adding .long() to x which doesnt seems to help either.

enter image description here

Here`s the full error message:

Validation sanity check:   0%|          | 0/2 [00:00<?, ?it/s]Traceback (most recent call last):
  File "E:/Programming/workspace_python/LOL-Match-Outcome-Predictor/code/leaguePreditctSimple_train.py", line 287, in <module>
    trainer.fit(net)
  File "E:\Programming\workspace_python\LOL-Match-Outcome-Predictor\venv\lib\site-packages\pytorch_lightning\trainer\trainer.py", line 552, in fit
    self._run(model)
  File "E:\Programming\workspace_python\LOL-Match-Outcome-Predictor\venv\lib\site-packages\pytorch_lightning\trainer\trainer.py", line 917, in _run
    self._dispatch()
  File "E:\Programming\workspace_python\LOL-Match-Outcome-Predictor\venv\lib\site-packages\pytorch_lightning\trainer\trainer.py", line 985, in _dispatch
    self.accelerator.start_training(self)
  File "E:\Programming\workspace_python\LOL-Match-Outcome-Predictor\venv\lib\site-packages\pytorch_lightning\accelerators\accelerator.py", line 92, in start_training
    self.training_type_plugin.start_training(trainer)
  File "E:\Programming\workspace_python\LOL-Match-Outcome-Predictor\venv\lib\site-packages\pytorch_lightning\plugins\training_type\training_type_plugin.py", line 161, in start_training
    self._results = trainer.run_stage()
  File "E:\Programming\workspace_python\LOL-Match-Outcome-Predictor\venv\lib\site-packages\pytorch_lightning\trainer\trainer.py", line 995, in run_stage
    return self._run_train()
  File "E:\Programming\workspace_python\LOL-Match-Outcome-Predictor\venv\lib\site-packages\pytorch_lightning\trainer\trainer.py", line 1030, in _run_train
    self._run_sanity_check(self.lightning_module)
  File "E:\Programming\workspace_python\LOL-Match-Outcome-Predictor\venv\lib\site-packages\pytorch_lightning\trainer\trainer.py", line 1114, in _run_sanity_check
    self._evaluation_loop.run()
  File "E:\Programming\workspace_python\LOL-Match-Outcome-Predictor\venv\lib\site-packages\pytorch_lightning\loops\base.py", line 111, in run
    self.advance(*args, **kwargs)
  File "E:\Programming\workspace_python\LOL-Match-Outcome-Predictor\venv\lib\site-packages\pytorch_lightning\loops\dataloader\evaluation_loop.py", line 111, in advance
    dataloader_iter, self.current_dataloader_idx, dl_max_batches, self.num_dataloaders
  File "E:\Programming\workspace_python\LOL-Match-Outcome-Predictor\venv\lib\site-packages\pytorch_lightning\loops\base.py", line 111, in run
    self.advance(*args, **kwargs)
  File "E:\Programming\workspace_python\LOL-Match-Outcome-Predictor\venv\lib\site-packages\pytorch_lightning\loops\epoch\evaluation_epoch_loop.py", line 110, in advance
    output = self.evaluation_step(batch, batch_idx, dataloader_idx)
  File "E:\Programming\workspace_python\LOL-Match-Outcome-Predictor\venv\lib\site-packages\pytorch_lightning\loops\epoch\evaluation_epoch_loop.py", line 154, in evaluation_step
    output = self.trainer.accelerator.validation_step(step_kwargs)
  File "E:\Programming\workspace_python\LOL-Match-Outcome-Predictor\venv\lib\site-packages\pytorch_lightning\accelerators\accelerator.py", line 211, in validation_step
    return self.training_type_plugin.validation_step(*step_kwargs.values())
  File "E:\Programming\workspace_python\LOL-Match-Outcome-Predictor\venv\lib\site-packages\pytorch_lightning\plugins\training_type\training_type_plugin.py", line 178, in validation_step
    return self.model.validation_step(*args, **kwargs)
  File "E:/Programming/workspace_python/LOL-Match-Outcome-Predictor/code/leaguePreditctSimple_train.py", line 170, in validation_step
    y_hat = self(x)
  File "E:\Programming\workspace_python\LOL-Match-Outcome-Predictor\venv\lib\site-packages\torch\nn\modules\module.py", line 1051, in _call_impl
    return forward_call(*input, **kwargs)
  File "E:/Programming/workspace_python/LOL-Match-Outcome-Predictor/code/leaguePreditctSimple_train.py", line 151, in forward
    x = self.linear1(x)
  File "E:\Programming\workspace_python\LOL-Match-Outcome-Predictor\venv\lib\site-packages\torch\nn\modules\module.py", line 1051, in _call_impl
    return forward_call(*input, **kwargs)
  File "E:\Programming\workspace_python\LOL-Match-Outcome-Predictor\venv\lib\site-packages\torch\nn\modules\container.py", line 139, in forward
    input = module(input)
  File "E:\Programming\workspace_python\LOL-Match-Outcome-Predictor\venv\lib\site-packages\torch\nn\modules\module.py", line 1051, in _call_impl
    return forward_call(*input, **kwargs)
  File "E:\Programming\workspace_python\LOL-Match-Outcome-Predictor\venv\lib\site-packages\torch\nn\modules\linear.py", line 96, in forward
    return F.linear(input, self.weight, self.bias)
  File "E:\Programming\workspace_python\LOL-Match-Outcome-Predictor\venv\lib\site-packages\torch\nn\functional.py", line 1847, in linear
    return torch._C._nn.linear(input, weight, bias)
RuntimeError: expected scalar type Long but found Float

Here`s the network code:

class NN(pl.LightningModule):
    def __init__(self, hparams, experiment_name):
        super().__init__()

        # self.hparams = hparams  # Depreceated in newer versions
        self.save_hyperparameters(hparams)

        self.checkpoint_folder = "LeaguePredictCheckpoints/"

        self.experiment_name = experiment_name

        # creating checkpoint folder
        dirpath = Path(self.checkpoint_folder)
        if not dirpath.exists():
            os.makedirs(dirpath, 0o755)

        self.linear1 = nn.Sequential(
            nn.Linear(in_features=1480, out_features=50),  # "*10" because we have 10 players
            nn.PReLU(),
        )
        self.linear2 = nn.Sequential(
            nn.Linear(in_features=50, out_features=20),
            # nn.BatchNorm1d(num_features=20),  # Batchnorm only in hidden layers?
            nn.PReLU()
        )
        self.linear3 = nn.Sequential(
            nn.Linear(in_features=20, out_features=2),
            nn.Sigmoid()
        )

    def forward(self, x):
        x = self.linear1(x)
        x = self.linear2(x)
        x = self.linear3(x)
        return x

    def training_step(self, batch, batch_idx):
        x, y = batch
        y_hat = self(x)
        loss = self.loss(y_hat, y)

        # calculate accuracy
        y_hat = torch.argmax(y_hat, dim=1)
        y = torch.argmax(y, dim=1)
        acc_train = torch.tensor(torch.sum(y == y_hat).item() / (len(y) * 1.0))

        return {"loss": loss, "acc_train": acc_train}

    def validation_step(self, batch, batch_idx):
        x, y = batch
        y_hat = self(x)
        loss = self.loss(y_hat, y)

        # calculate accuracy
        y_hat = torch.argmax(y_hat, dim=1)
        y = torch.argmax(y, dim=1)
        acc_val = torch.tensor(torch.sum(y == y_hat).item() / (len(y) * 1.0))

        return {"val_loss": loss, "acc_val": acc_val}

    def validation_epoch_end(self, outputs):
        # average loss per epoch
        avg_loss = torch.stack([x['val_loss'] for x in outputs]).mean()
        self.log("avg_epoch_val_loss", avg_loss)

        # average accuracy per epoch
        avg_acc = torch.stack([x['acc_val'] for x in outputs]).mean()
        self.log("avg_epoch_val_acc", avg_acc)

        return {'val_loss': avg_loss}

    def training_epoch_end(self, outputs):
        # average loss per epoch
        avg_loss = torch.stack([x['loss'] for x in outputs]).mean()
        self.log("avg_epoch_train_loss", avg_loss)

        # average accuracy per epoch
        avg_acc = torch.stack([x['acc_train'] for x in outputs]).mean()
        self.log("avg_epoch_train_acc", avg_acc)

    def loss(self, input, target):
        return F.binary_cross_entropy(input.float(), target.float())

    def configure_optimizers(self):
        return torch.optim.Adam(self.parameters(), lr=self.hparams.LR)

    def train_dataloader(self):
        train_dataset = LaegueDataset_train("../train_dataset_location.txt", "../championidtable_location.txt", self.hparams.CHUNKSIZE)
        return DataLoader(train_dataset, num_workers=self.hparams.NUMWORK, batch_size=self.hparams.BATCHSIZE)

    def val_dataloader(self):
        val_dataset = LaegueDataset_train("../test_dataset_location.txt", "../championidtable_location.txt", self.hparams.CHUNKSIZE)
        return DataLoader(val_dataset, num_workers=self.hparams.NUMWORK, batch_size=self.hparams.BATCHSIZE)

    def on_epoch_end(self) -> None:
        if self.current_epoch % self.hparams.SAVE_MODEL_EVERY_EPOCH == 0:
            trainer.save_checkpoint(
                self.checkpoint_folder + "/" + self.experiment_name + "_epoch_" + str(self.current_epoch) + ".ckpt")
            comet_logger.experiment.log_asset_folder(self.checkpoint_folder, step=self.current_epoch)

            # Deleting the folder where we saved the model so that we dont upload a thing twice
            dirpath = Path(self.checkpoint_folder)
            if dirpath.exists() and dirpath.is_dir():
                shutil.rmtree(dirpath)

            # creating checkpoint folder
            access_rights = 0o755
            os.makedirs(dirpath, access_rights)

    def on_train_end(self):
        trainer.save_checkpoint(
            self.checkpoint_folder + "/" + self.experiment_name + "_epoch_" + str(self.current_epoch) + ".ckpt")
        comet_logger.experiment.log_asset_folder(self.checkpoint_folder, step=self.current_epoch)
0 Answers
Related