Skorch GridSearchCV: FitFailedWarning: Не удалось подобрать оценщик. Оценка для этих параметров в этом разделе поезд-тест будет равна nan. - PullRequest
2 голосов
/ 29 мая 2020

У меня есть данные x размера (n_samples, time_steps, n_features) для функций и (n_samples, 1, n_labels) для меток y.

Из этого я создаю наборы данных для обучения, разработки и тестирования pytorch.

Я хочу использовать GridSearchCV для поиска по сетке по гиперпараметрам. Вот что я написал:

'Define the network'
sampling_interval = 0.1
net = ConvNet(time_window, ny)
net.float()

'Split test training set'
# trainin test. In this case we take some experiements as test and some as trainint
train_set_split = 0.9
dev_set_split = 0.05
test_set_split = 0.05

# Creating data indices for training and validation splits:
dataset_size = x.shape[0]
indices = list(range(dataset_size))
np.random.shuffle(indices)
split1 = int(np.floor(train_set_split * dataset_size))
split2 = int(np.floor(dev_set_split * dataset_size))
split3 = int(np.floor(test_set_split * dataset_size))

train_indices, dev_indices, test_indices = indices[:split1], indices[split1:split1 + split2], indices[split1 + split2:]

'Create a dataset'
train_dataset = MyDataset(x[train_indices, :, :], y[train_indices], net.device, net.dtype)
dev_dataset = MyDataset(x[dev_indices, :, :], y[dev_indices], net.device, net.dtype)
test_dataset = MyDataset(x[test_indices, :, :], y[test_indices], net.device, net.dtype)


'Define the optimizer'
optimizer = torch.optim.Adam(net.parameters(), lr=learning_rate)

'Define the loss function'
loss_func = torch.nn.MSELoss()

net_regr = NeuralNetRegressor(
    module=ConvNet,
    module__ny=ny,
    module__time_window=time_window,
    max_epochs=100,
    lr=0.1,
    train_split=predefined_split(dev_dataset),
    criterion=mean_squared_error,
    batch_size=batch_size,
)


params = {
    'lr': [0.01, 0.05, 0.1],
    'max_epochs': [100, 200, 300],
}

X_sl = SliceDataset(train_dataset, idx=0)  # idx=0 is the default
y_sl = SliceDataset(train_dataset, idx=1)

gs = GridSearchCV(net_regr, params, refit=False, verbose=4)

gs.fit(X_sl, y_sl)
print(gs.best_score_, gs.best_params_)

Но я получаю эту ошибку

 FitFailedWarning: Estimator fit failed. The score on this train-test partition for these parameters will be set to nan. Details: 
ValueError: The target data shouldn't be 1-dimensional but instead have 2 dimensions, with the second dimension having the same size as the number of regression targets (usually 1). Please reshape your target data to be 2-dimensional (e.g. y = y.reshape(-1, 1).

но форма цели двумерная

>>> y_sl[0].shape

torch.Size([1, 4])

где 4 - это количество целей (n_labels).

Так что я не понимаю, откуда взялась эта ошибка

...