{"id":271,"hash":"5f125460a235b3b49e4948291757035eec085092c6750551b9ccc9a2fc407757","pattern":"RuntimeError: Input type (torch.FloatTensor) and weight type (torch.cuda.FloatTensor) should be the same","full_message":"This:\n\ndevice = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\nmodel.to(device)\n\nfor data in dataloader:\n    inputs, labels = data\n    outputs = model(inputs)\n\nGives the error:\n\nRuntimeError: Input type (torch.FloatTensor) and weight type (torch.cuda.FloatTensor) should be the same","ecosystem":"pypi","package_name":"python-3.x","package_version":null,"solution":"You get this error because your model is on the GPU, but your data is on the CPU. So, you need to send your input tensors to the GPU.\n\ninputs, labels = data                         # this is what you had\ninputs, labels = inputs.cuda(), labels.cuda() # add this line\n\nOr like this, to stay consistent with the rest of your code:\n\ndevice = torch.device(\"cuda:0\" if torch.cuda.is_available() else \"cpu\")\n\ninputs, labels = inputs.to(device), labels.to(device)\n\nThe same error will be raised if your input tensors are on the GPU but your model weights aren't. In this case, you need to send your model weights to the GPU.\n\nmodel = MyModel()\n\nif torch.cuda.is_available():\n    model.cuda()\n\nSee the documentation for cuda(), and its opposite, cpu().","confidence":0.95,"source":"stackoverflow","source_url":"https://stackoverflow.com/questions/59013109/runtimeerror-input-type-torch-floattensor-and-weight-type-torch-cuda-floatte","votes":201,"created_at":"2026-04-19T04:41:44.165867+00:00","updated_at":"2026-04-19T04:51:56.197096+00:00"}