ValueError:层的输入 0 与层不兼容:输入形状的预期轴 -1 具有值 22,但收到的输入具有形状 (None, 21)

时间:2021-07-25 14:47:11

标签: tensorflow keras lambda tf.keras sequential

我用这个制作了训练有素的模型

train_x = train_data.drop(drop_cols + PRED_INSTANCES, axis=1)
train_simple_x = np.array(train_data['batch_latency']).reshape(-1, 1)
train_y = train_data[pred_instance]

test_x = test_data.drop(drop_cols + PRED_INSTANCES, axis=1)
test_simple_x = np.array(test_data['batch_latency']).reshape(-1, 1)
test_y = test_data[[pred_instance]].to_numpy()

# Modeling
callback = tf.keras.callbacks.EarlyStopping(monitor='loss', patience=5)
# Model 정의
model_dnn=tf.keras.Sequential()
model_dnn.add(tf.keras.layers.Dense(64, activation="relu", input_shape=(train_x.shape[1],)))
model_dnn.add(tf.keras.layers.Dense(32, activation="relu"))  
model_dnn.add(tf.keras.layers.Dense(16, activation="relu"))
model_dnn.add(tf.keras.layers.Dense(1))
model_dnn.compile(optimizer=tf.keras.optimizers.Adam(lr=0.001),
              loss=['mean_absolute_percentage_error', 'mean_squared_error'],
              loss_weights=[1., 1.])

model_rfr = en.RandomForestRegressor()
model_simple = lin.LinearRegression()  
# --------------------------------------------------------------------------------------------
# Fit
model_dnn.fit(train_x, train_y, epochs=200,
      callbacks=[callback],
      batch_size=32,
      verbose=0)
model_rfr.fit(train_x, train_y)
model_simple.fit(train_simple_x, train_y)    

我训练了 3 个模型并保存了

在我打开这个保存的训练模型后,预测第一行出现错误

       # Predict
dnn_pred_y = model_dnn.predict(test_x)
dnn_pred_y = dnn_pred_y.reshape(-1, 1)
rfr_pred_y = model_rfr.predict(test_x)
rfr_pred_y = rfr_pred_y.reshape(-1, 1)
simple_pred_y = model_simple.predict(test_simple_x)
simple_pred_y = simple_pred_y.reshape(-1, 1)
median_pred_y = np.median(np.stack([
    dnn_pred_y, rfr_pred_y, simple_pred_y
]), axis=0)

这是错误信息

    ValueError: Input 0 of layer sequential_44 is incompatible with the layer: expected axis -1 of input shape to have value 22 but received input with shape (None, 21)
[ERROR] ValueError: in user code:

    /mnt/efs/packages/keras/engine/training.py:1544 predict_function  *
        return step_function(self, iterator)
    /mnt/efs/packages/keras/engine/training.py:1527 run_step  *
        outputs = model.predict_step(data)
    /mnt/efs/packages/keras/engine/training.py:1500 predict_step  *
        return self(x, training=False)
    /mnt/efs/packages/keras/engine/base_layer.py:989 __call__  *
        input_spec.assert_input_compatibility(self.input_spec, inputs, self.name)
    /mnt/efs/packages/keras/engine/input_spec.py:248 assert_input_compatibility  *
        raise ValueError(

    ValueError: Input 0 of layer sequential_44 is incompatible with the layer: expected axis -1 of input shape to have value 22 but received input with shape (None, 21)

Traceback (most recent call last):
  File "/var/task/lambda_function.py", line 233, in lambda_handler
    pred_instance_dict = model_validation()
  File "/var/task/lambda_function.py", line 216, in model_validation
    test_y, pred_y, test_data, mape = train_test_model(
  File "/var/task/lambda_function.py", line 184, in train_test_model
    dnn_pred_y = model_dnn.predict(test_x)
  File "/mnt/efs/packages/keras/engine/training.py", line 1702, in predict
    tmp_batch_outputs = self.predict_function(iterator)
  File "/mnt/efs/packages/tensorflow/python/eager/def_function.py", line 889, in __call__
    result = self._call(*args, **kwds)
  File "/mnt/efs/packages/tensorflow/python/eager/def_function.py", line 933, in _call
    self._initialize(args, kwds, add_initializers_to=initializers)
  File "/mnt/efs/packages/tensorflow/python/eager/def_function.py", line 763, in _initialize
    self._stateful_fn._get_concrete_function_internal_garbage_collected(  # pylint: disable=protected-access
  File "/mnt/efs/packages/tensorflow/python/eager/function.py", line 3050, in _get_concrete_function_internal_garbage_collected
    graph_function, _ = self._maybe_define_function(args, kwargs)
  File "/mnt/efs/packages/tensorflow/python/eager/function.py", line 3444, in _maybe_define_function
    graph_function = self._create_graph_function(args, kwargs)
  File "/mnt/efs/packages/tensorflow/python/eager/function.py", line 3279, in _create_graph_function
    func_graph_module.func_graph_from_py_func(
  File "/mnt/efs/packages/tensorflow/python/framework/func_graph.py", line 999, in func_graph_from_py_func
    func_outputs = python_func(*func_args, **func_kwargs)
  File "/mnt/efs/packages/tensorflow/python/eager/def_function.py", line 672, in wrapped_fn
    out = weak_wrapped_fn().__wrapped__(*args, **kwds)
  File "/mnt/efs/packages/tensorflow/python/framework/func_graph.py", line 986, in wrapper
    raise e.ag_error_metadata.to_exception(e)END RequestId: 0e3ce053-0755-408c-9b12-6d0e8f88fd53

0 个答案:

没有答案