One issue is that you were passing ids instead of tensors to the optimizer ({'params': encode_params}), so this would raise an error eventually but not the one you mention. Other than that, it’s not obvious what’s the problem in your original code, so please provide the entire stack trace (in both cases) to make it easier for us.