Error Message (Error Codes below):
RuntimeError: Expected object of scalar type Float but got scalar type Double for argument #2 ‘mat1’ in call to _th_addmm
for epoch in range(num_epochs):
# Convert numpy arrays to torch tensors
inputs = torch.from_numpy(x_train)
targets = torch.from_numpy(y_train)
# Forward pass
outputs = model(inputs)
loss = criterion(outputs, targets)
# Backward and optimize
optimizer.zero_grad()
loss.backward()
optimizer.step()
print("Epoch [{}/{}] Loss: {:.4f}".format(epoch+1, num_epochs, loss.item()))
Solution:
Method 1. Add
inputs = inputs.float()
targets = targets.float()
model = model.float()
Complete code
for epoch in range(num_epochs):
# Convert numpy arrays to torch tensors
inputs = torch.from_numpy(x_train)
targets = torch.from_numpy(y_train)
inputs = inputs.float()
targets = inputs.float()
model = model.float()
# Forward pass
outputs = model(inputs)
loss = criterion(outputs, targets)
# Backward and optimize
optimizer.zero_grad()
loss.backward()
optimizer.step()
print("Epoch [{}/{}] Loss: {:.4f}".format(epoch+1, num_epochs, loss.item()))
Method 2. Add
inputs = inputs.double()
targets = inputs.double()
model = model.double()
Complete code
for epoch in range(num_epochs):
# Convert numpy arrays to torch tensors
inputs = torch.from_numpy(x_train)
targets = torch.from_numpy(y_train)
inputs = inputs.double()
targets = inputs.double()
model = model.double()
# Forward pass
outputs = model(inputs)
loss = criterion(outputs, targets)
# Backward and optimize
optimizer.zero_grad()
loss.backward()
optimizer.step()
print("Epoch [{}/{}] Loss: {:.4f}".format(epoch+1, num_epochs, loss.item()))
Read More:
- RuntimeError: expected scalar type Double but found Float
- Solve the runtimeerror in RNN: expected scalar type long but found float error
- Successfully solved the problem of “runtimee” in RESNET dataset classification rror:expected scalar type Long but found Float”
- RuntimeError: Found dtype Double but expected Float”
- [solved] error: valueerror: expected 2D array, got scalar array instead
- RuntimeError: ‘lengths’ argument should be a 1D CPU int64 tensor, but got 1D cuda:0 Long tensor
- After introducing sass into Vue project, start to report error typeerror [err]_ INVALID_ ARG_ Type]: the “path” argument must be of type string
- RuntimeError: Input type (torch.FloatTensor) and weight type (torch.cuda.FloatTensor) should be the
- Can’t multiply sequence by non int of type ‘float’
- Type error: cannot unpack non Iterable non type object appears when starting Bert server in Ubuntu system
- Type error: sequence item 0: expected STR instance, int found
- Python common error: if using all scalar values, you must pass an index (four solutions)
- TypeError: Required argument ‘mat‘ (pos 2) not found
- RuntimeError: reciprocal is not implemented for type torch.cuda.LongTensor
- Python error type error: ‘range’ object does not support item assignment, solution
- Module build failed: TypeError [ERR_INVALID_ARG_TYPE]: The “path“ argument must be of type string.
- Index error: invalid index to scalar variable
- Es error mapper of different type, current_ Type “text” solution
- Unity Error:InvalidCastException: Cannot cast from source type to destination type.
- Type error: object (…) is not a function