tensorflow:Model 是用形状 (None, None, 6) 构建的,但它在形状不兼容的输入上被调用

时间:2021-02-10 17:06:15

标签: python tensorflow

完整的错误是

WARNING:tensorflow:Model was constructed with shape (None, None, 6) for input KerasTensor(type_spec=TensorSpec(shape=(None, None, 6), dtype=tf.float32, name='dense_input'), name='dense_input', description="created by layer 'dense_input'"), but it was called on an input with incompatible shape (None, 6).
2021-02-10 17:56:08.288146: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library libcublas.so.11
2021-02-10 17:56:08.544550: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library libcublasLt.so.11
WARNING:tensorflow:Model was constructed with shape (None, None, 6) for input KerasTensor(type_spec=TensorSpec(shape=(None, None, 6), dtype=tf.float32, name='dense_4_input'), name='dense_4_input', description="created by layer 'dense_4_input'"), but it was called on an input with incompatible shape (None, 6).
WARNING:tensorflow:Model was constructed with shape (None, None, 6) for input KerasTensor(type_spec=TensorSpec(shape=(None, None, 6), dtype=tf.float32, name='dense_input'), name='dense_input', description="created by layer 'dense_input'"), but it was called on an input with incompatible shape (20, 6).
WARNING:tensorflow:Model was constructed with shape (None, None, 6) for input KerasTensor(type_spec=TensorSpec(shape=(None, None, 6), dtype=tf.float32, name='dense_input'), name='dense_input', description="created by layer 'dense_input'"), but it was called on an input with incompatible shape (20, 6).

我确定这是输入形状错误,这是相关代码:

def create_model(self):
    model = tf.keras.models.Sequential()  # a basic feed-forward model
    model.add(tf.keras.layers.Dense(6, activation=tf.nn.relu, input_shape=env.OBSERVATION_SPACE_VALUES))  # a simple fully-connected layer, 6 units, relu activation
    model.add(tf.keras.layers.Dense(128, activation=tf.nn.relu))  # a simple fully-connected layer, 128 units, relu activation
    model.add(tf.keras.layers.Dense(64, activation=tf.nn.relu))  # a simple fully-connected layer, 64 units, relu activation
    model.add(tf.keras.layers.Dense(27, activation=tf.nn.softmax))  # our output layer. 27 units for 27 actions 
    model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])


    return model

env.OBSERVATION_SPACE_VALUES 是 (N0NE, 6) 输入基本上是一个具有 6 个值的一维数组

1 个答案:

答案 0 :(得分:1)

您不应在 Layer 构造函数的 input_shape 参数中指定批处理维度。

env.OBSERVATION_SPACE_VALUES = (6,)

documentation of the Input layer 声明(重点是我的):

<块引用>

input_shape:形状元组(不包括批处理轴),或TensorShape实例(不包括批处理轴)