Я искал вопросы об этой ошибке ValueError, но они в основном относятся к неправильным измерениям, которые были определены сразу. Моя проблема в том, что моя модель нейронной сети работает в течение определенного периода времени , прежде чем возникнет эта ошибка. Эта ошибка появилась, так как я изменил модель, чтобы предсказать 14 выходных данных вместо 1. Действительно застрял для идей и был бы признателен за любую помощь.
(Это мой первый вопрос, задаваемый по stackoverflow, поэтому, пожалуйста, дайте мне знать, если что-то не хватает )
Форма ввода и контрольный код nan:
print («Проверьте, есть ли NaN:», np.any (np.isnan ( X)))
print («Проверьте, все ли числа X конечны:», np.all (np.isfinite (X)))
print («Проверьте, все ли числа Y конечны: ", np.all (np.isfinite (Y)))
print (" X_train shape: ", X_train.shape)
print (" Y_train shape: ", Y_train.shape)
Результаты:
Проверьте, есть ли значения NaN: False
Проверьте, все ли числа X конечны: True
Проверьте, все ли числа Y конечны: True
Форма X_train: (30446, 451)
Форма Y_train: (30446, 14)
Код нейронной сети:
def custom_loss(y_true, y_pred):
mse = K.mean(K.square(y_true - y_pred), axis = -1)
return mse
def baseline_model():
model = Sequential()
model.add(Dense(100, input_dim=451, kernel_initializer='normal', activation='relu'))
model.add(Dense(34, activation='relu'))
model.add(Dense(14, kernel_initializer='normal'))
model.compile(loss=custom_loss, optimizer='adam')
return model
estimator = MultiOutputRegressor(KerasRegressor(build_fn=baseline_model, epochs=30, batch_size=32))
kfold = KFold(n_splits=20)
results = cross_val_score(estimator, X_train, Y_train, cv=kfold)
**
* 104 9 * Сообщение об ошибке [ПОСЛЕ МОДЕЛИ РАБОТАЕТ НА ПРИМЕР 10 МИНУТ)
....
Эпоха 29/30
28923/28923 [ ==============================] - 1с 31us / шаг - потеря: 0,0051
Эпоха 30/30
28923/28923 [====================================] - 1с 30 мс / шаг - потеря: 0,0045
**
ValueError Traceback (most recent call last)
<ipython-input-285-f63a094e64d0> in <module>
25 estimator = MultiOutputRegressor(KerasRegressor(build_fn=baseline_model, epochs=30,
batch_size=32))
26 kfold = KFold(n_splits=20)
27 results = cross_val_score(estimator, X_train, Y_train, cv=kfold)
28 print("Baseline: %.2f (%.2f) MSE" % (results.mean(), results.std()))
29 print("Run time (mins): ", (time.time() - start_time)/60)
C:\ProgramData\Anaconda3\lib\site-packages\sklearn\model_selection\_validation.py in
cross_val_score(estimator, X, y, groups, scoring, cv, n_jobs, verbose, fit_params, pre_dispatch,
error_score)
389 fit_params=fit_params,
390 pre_dispatch=pre_dispatch,
391 error_score=error_score)
392 return cv_results['test_score']
393
C:\ProgramData\Anaconda3\lib\site-packages\sklearn\model_selection\_validation.py in
cross_validate(estimator, X, y, groups, scoring, cv, n_jobs, verbose, fit_params, pre_dispatch,
return_train_score, return_estimator, error_score)
230 return_times=True, return_estimator=return_estimator,
231 error_score=error_score)
232 for train, test in cv.split(X, y, groups))
233
234 zipped_scores = list(zip(*scores))
C:\ProgramData\Anaconda3\lib\site-packages\joblib\parallel.py in __call__(self, iterable)
919 # remaining jobs.
920 self._iterating = False
921 if self.dispatch_one_batch(iterator):
922 self._iterating = self._original_iterator is not None
923
C:\ProgramData\Anaconda3\lib\site-packages\joblib\parallel.py in dispatch_one_batch(self, iterator)
757 return False
758 else:
759 self._dispatch(tasks)
760 return True
761
C:\ProgramData\Anaconda3\lib\site-packages\joblib\parallel.py in _dispatch(self, batch)
714 with self._lock:
715 job_idx = len(self._jobs)
716 job = self._backend.apply_async(batch, callback=cb)
717 # A job can complete so quickly than its callback is
718 # called before we get here, causing self._jobs to
C:\ProgramData\Anaconda3\lib\site-packages\joblib\_parallel_backends.py in apply_async(self, func,
callback)
180 def apply_async(self, func, callback=None):
181 """Schedule a func to be run"""
182 result = ImmediateResult(func)
183 if callback:
184 callback(result)
C:\ProgramData\Anaconda3\lib\site-packages\joblib\_parallel_backends.py in __init__(self, batch)
547 # Don't delay the application, to avoid keeping the input
548 # arguments in memory
549 self.results = batch()
550
551 def get(self):
C:\ProgramData\Anaconda3\lib\site-packages\joblib\parallel.py in __call__(self)
223 with parallel_backend(self._backend, n_jobs=self._n_jobs):
224 return [func(*args, **kwargs)
225 for func, args, kwargs in self.items]
226
227 def __len__(self):
C:\ProgramData\Anaconda3\lib\site-packages\joblib\parallel.py in <listcomp>(.0)
223 with parallel_backend(self._backend, n_jobs=self._n_jobs):
224 return [func(*args, **kwargs)
225 for func, args, kwargs in self.items]
226
227 def __len__(self):
C:\ProgramData\Anaconda3\lib\site-packages\sklearn\model_selection\_validation.py in
_fit_and_score(estimator, X, y, scorer, train, test, verbose, parameters, fit_params,
return_train_score, return_parameters, return_n_test_samples, return_times, return_estimator,
error_score)
554 fit_time = time.time() - start_time
555 # _score will return dict if is_multimetric is True
556 test_scores = _score(estimator, X_test, y_test, scorer, is_multimetric)
557 score_time = time.time() - start_time - fit_time
558 if return_train_score:
C:\ProgramData\Anaconda3\lib\site-packages\sklearn\model_selection\_validation.py in _
score(estimator, X_test, y_test, scorer, is_multimetric)
597 """
598 if is_multimetric:
599 return _multimetric_score(estimator, X_test, y_test, scorer)
600 else:
601 if y_test is None:
C:\ProgramData\Anaconda3\lib\site-packages\sklearn\model_selection\_validation.py in _
multimetric_score(estimator, X_test, y_test, scorers)
627 score = scorer(estimator, X_test)
628 else:
629 score = scorer(estimator, X_test, y_test)
630
631 if hasattr(score, 'item'):
C:\ProgramData\Anaconda3\lib\site-packages\sklearn\metrics\scorer.py in
_passthrough_scorer(estimator, *args, **kwargs)
238 def _passthrough_scorer(estimator, *args, **kwargs):
239 """Function that wraps estimator.score"""
240 return estimator.score(*args, **kwargs)
241
242
C:\ProgramData\Anaconda3\lib\site-packages\sklearn\multioutput.py in score(self, X, y, sample_weight)
294 from .metrics import r2_score
295 return r2_score(y, self.predict(X), sample_weight=sample_weight,
296 multioutput='uniform_average')
297
298
C:\ProgramData\Anaconda3\lib\site-packages\sklearn\metrics\regression.py in r2_score(y_true, y_pred,
sample_weight, multioutput)
536 """
537 y_type, y_true, y_pred, multioutput = _check_reg_targets(
538 y_true, y_pred, multioutput)
539 check_consistent_length(y_true, y_pred, sample_weight)
540
C:\ProgramData\Anaconda3\lib\site-packages\sklearn\metrics\regression.py in _
check_reg_targets(y_true, y_pred, multioutput)
75
76 """
77 check_consistent_length(y_true, y_pred)
78 y_true = check_array(y_true, ensure_2d=False)
79 y_pred = check_array(y_pred, ensure_2d=False)
C:\ProgramData\Anaconda3\lib\site-packages\sklearn\utils\validation.py in
check_consistent_length(*arrays)
203 if len(uniques) > 1:
204 raise ValueError("Found input variables with inconsistent numbers of"
205 " samples: %r" % [int(l) for l in lengths])
206
207
ValueError: Found input variables with inconsistent numbers of samples: [1523, 14]