У меня есть проблема, похожая на этот пост . Однако противоположное решение не работает в моем случае.
Я проверил dtypes своих входов и выходов, и все они float32
>>>y_train.dtype
torch.float32
>>>y_test.dtype
torch.float32
>>>X_train.dtype
torch.float32
>>>X_test.dtype
torch.float32
Однако я получаю сообщение об ошибке:
RuntimeError: Expected object of scalar type Float but got scalar type Double for argument #4 'mat1'
Вот мой код - Ошибка возникает в grid_NN.fit()
:
def create_NN_model(neurons):
hidden_n = neurons
model_NN = torch.nn.Sequential(
torch.nn.Linear(X.shape[1], hidden_n),
torch.nn.Dropout(0.5),
torch.nn.ReLU(),
torch.nn.Linear(hidden_n, hidden_n),
torch.nn.Dropout(0.5),
torch.nn.ReLU(),
torch.nn.Linear(hidden_n, 1),
)
return model_NN
model_NN = create_NN_model(300)
net = NeuralNetRegressor(model_NN,
max_epochs=100,
lr=0.001,
verbose=1)
steps_NN = [('scaler', StandardScaler()), ('Net', net)]
pipeline_NN = Pipeline(steps_NN)
parameters_NN = {'Net__lr': [0.001,0.005],'Net__max_epochs': list(range(400,500))}
grid_NN = GridSearchCV(pipeline_NN, param_grid= parameters_NN, cv=5, verbose=1, scoring='neg_median_absolute_error')
X_train = torch.from_numpy(np.array(X_train)).float()
X_test = torch.from_numpy(np.array(X_test)).float()
y_train = torch.from_numpy(np.array(y_train).reshape(-1,1)).float()
y_test = torch.from_numpy(np.array(y_test).reshape(-1,1)).float()
grid_NN.fit(X_train, y_train)
print(grid_NN.score(X_test, y_test))
pred = grid_NN.best_estimator_.predict(X_test)
Полная трассировка:
Traceback (most recent call last):
File "C:/Users/Maximilian/maxAnalytics/LisbonHousing/venv/models.py", line 115, in <module>
grid_NN.fit(X_train, y_train)
File "C:\Users\Maximilian\Anaconda3\envs\LisbonHousing\lib\site-packages\sklearn\model_selection\_search.py", line 739, in fit
self.best_estimator_.fit(X, y, **fit_params)
File "C:\Users\Maximilian\Anaconda3\envs\LisbonHousing\lib\site-packages\sklearn\pipeline.py", line 354, in fit
self._final_estimator.fit(Xt, y, **fit_params)
File "C:\Users\Maximilian\Anaconda3\envs\LisbonHousing\lib\site-packages\skorch\regressor.py", line 91, in fit
return super(NeuralNetRegressor, self).fit(X, y, **fit_params)
File "C:\Users\Maximilian\Anaconda3\envs\LisbonHousing\lib\site-packages\skorch\net.py", line 848, in fit
self.partial_fit(X, y, **fit_params)
File "C:\Users\Maximilian\Anaconda3\envs\LisbonHousing\lib\site-packages\skorch\net.py", line 807, in partial_fit
self.fit_loop(X, y, **fit_params)
File "C:\Users\Maximilian\Anaconda3\envs\LisbonHousing\lib\site-packages\skorch\net.py", line 739, in fit_loop
step = self.train_step(Xi, yi, **fit_params)
File "C:\Users\Maximilian\Anaconda3\envs\LisbonHousing\lib\site-packages\skorch\net.py", line 664, in train_step
self.optimizer_.step(step_fn)
File "C:\Users\Maximilian\Anaconda3\envs\LisbonHousing\lib\site-packages\torch\optim\sgd.py", line 80, in step
loss = closure()
File "C:\Users\Maximilian\Anaconda3\envs\LisbonHousing\lib\site-packages\skorch\net.py", line 661, in step_fn
step = self.train_step_single(Xi, yi, **fit_params)
File "C:\Users\Maximilian\Anaconda3\envs\LisbonHousing\lib\site-packages\skorch\net.py", line 603, in train_step_single
y_pred = self.infer(Xi, **fit_params)
File "C:\Users\Maximilian\Anaconda3\envs\LisbonHousing\lib\site-packages\skorch\net.py", line 996, in infer
return self.module_(x, **fit_params)
File "C:\Users\Maximilian\Anaconda3\envs\LisbonHousing\lib\site-packages\torch\nn\modules\module.py", line 493, in __call__
result = self.forward(*input, **kwargs)
File "C:\Users\Maximilian\Anaconda3\envs\LisbonHousing\lib\site-packages\torch\nn\modules\container.py", line 92, in forward
input = module(input)
File "C:\Users\Maximilian\Anaconda3\envs\LisbonHousing\lib\site-packages\torch\nn\modules\module.py", line 493, in __call__
result = self.forward(*input, **kwargs)
File "C:\Users\Maximilian\Anaconda3\envs\LisbonHousing\lib\site-packages\torch\nn\modules\linear.py", line 92, in forward
return F.linear(input, self.weight, self.bias)
File "C:\Users\Maximilian\Anaconda3\envs\LisbonHousing\lib\site-packages\torch\nn\functional.py", line 1406, in linear
ret = torch.addmm(bias, input, weight.t())
RuntimeError: Expected object of scalar type Float but got scalar type Double for argument #4 'mat1'