在Keras中检查模型输入中的验证数据时出错

时间:2020-06-15 12:36:14

标签: python tensorflow keras deep-learning cross-validation

我有一个这样定义的模型:

model = Model(inputs=[inputs, y_true, is_weight], outputs=[y_pred])

我想指定训练和验证数据,因为我正在执行k倍交叉验证,并且我已经预先定义了拆分。 我拟合模型如下:

model.fit([X_train,  Y_train, wgt_map_train], batch_size=1, epochs=200,
              validation_data = [X_val, Y_val, wgt_map_val],
                    callbacks = [checkpointer, earlystopper])

用于训练和验证的输入的形状相同,除了第一维:

X_train.shape = (6732, 12, 86, 98, 1)
X_val.shape = (765, 12, 86, 98, 1)
etc.

但是,当我运行代码时,出现以下错误:

~/miniconda3/envs/segment/lib/python3.7/site-packages/keras/engine/training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, **kwargs)
    970                 val_x, val_y,
    971                 sample_weight=val_sample_weight,
--> 972                 batch_size=batch_size)
    973             if self._uses_dynamic_learning_phase():
    974                 val_ins = val_x + val_y + val_sample_weights + [0.]

~/miniconda3/envs/segment/lib/python3.7/site-packages/keras/engine/training.py in _standardize_user_data(self, x, y, sample_weight, class_weight, check_array_lengths, batch_size)
    749             feed_input_shapes,
    750             check_batch_axis=False,  # Don't enforce the batch size.
--> 751             exception_prefix='input')
    752 
    753         if y is not None:

~/miniconda3/envs/segment/lib/python3.7/site-packages/keras/engine/training_utils.py in standardize_input_data(data, names, shapes, check_batch_axis, exception_prefix)
    100                 'Expected to see ' + str(len(names)) + ' array(s), '
    101                 'but instead got the following list of ' +
--> 102                 str(len(data)) + ' arrays: ' + str(data)[:200] + '...')
    103         elif len(names) > 1:
    104             raise ValueError(

ValueError: Error when checking model input: the list of Numpy arrays that you are passing to your model is not the size the model expected. Expected to see 3 array(s), but instead got the following list of 1 arrays: [array([[[[[0.        ],
          [0.        ],
          [0.        ],
          ...,
          [0.        ],
          [0.        ],
          [0.        ]],

         [[0.        ],
          [0. ...

如果我删除了validation_data并将其替换为validation_split = 0.1可以正常工作,那么这将不是进行k折CV的适当方法。谢谢!

编辑 我还尝试在model.fit中添加“ validation_batch_size = 1”,但随后出现提示:

TypeError                                 Traceback (most recent call last)
<ipython-input-46-eefd2297c992> in <module>
     42               validation_data = [X_val, Y_val, wgt_map_val],
     43               validation_batch_size = 1,
---> 44                     callbacks = [checkpointer, earlystopper,tb])
     45     time2=time()
     46 

~/miniconda3/envs/segment/lib/python3.7/site-packages/keras/engine/training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, **kwargs)
    940             epochs = kwargs.pop('nb_epoch')
    941         if kwargs:
--> 942             raise TypeError('Unrecognized keyword arguments: ' + str(kwargs))
    943         if x is None and y is None and steps_per_epoch is None:
    944             raise ValueError('If fitting from data tensors, '

TypeError: Unrecognized keyword arguments: {'validation_batch_size': 1}

我检查了类似的帖子,例如: Deep Learning fit error (the list of Numpy arrays that you are passing to your model is not the size the model expected.)Keras functional model with multiple inputs

0 个答案:

没有答案