ValueError:密集层的输入0与该层不兼容::预期的min_ndim = 2,找到的ndim = 1。收到的完整形状:[无]

时间:2019-03-28 03:54:33

标签: python tensorflow

我是Tensorflow的新手,并尝试使用以下示例代码对其进行处理:

def build_model():
    model = keras.Sequential([
            layers.Dense(10, activation=tf.nn.relu),
            layers.Dense(10, activation=tf.nn.relu),
            layers.Dense(1)
        ])
    optimizer = tf.keras.optimizers.RMSprop(0.001)

    model.compile(loss='mean_squared_error',
            optimizer=optimizer,
            metrics=['mean_absolute_error', 'mean_squared_error'])
    return model

model = build_model()
model.fit(training_dataset, epochs=5, steps_per_epoch=179)

training_dataset如下,具有179行:

features:[29225 29259 29210 29220] Label:2
features:[29220 29236 29201 29234] Label:1
features:[29234 29241 29211 29221] Label:2
features:[29221 29224 29185 29185] Label:2
features:[29185 29199 29181 29191] Label:2
features:[29191 29195 29171 29195] Label:1
features:[29195 29228 29189 29225] Label:1
features:[29225 29236 29196 29199] Label:2
features:[29199 29222 29197 29218] Label:1
features:[29218 29235 29207 29224] Label:1
features:[29224 29244 29223 29234] Label:1
features:[29234 29247 29222 29240] Label:1
features:[29240 29264 29240 29263] Label:1
features:[29263 29267 29234 29237] Label:1
features:[29237 29270 29232 29267] Label:0
features:[29267 29270 29252 29253] Label:2

运行它时出现以下错误:

  

“ ValueError:密集层的输入0与该层不兼容::预期的min_ndim = 2,找到的ndim = 1。收到的完整图形:[无]”。

有人可以建议如何解决它吗?

Traceback (most recent call last):
  File "ML.py", line 145, in <module>
model.fit(training_dataset, epochs=5, steps_per_epoch=179)
  File "/home/sandbox/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py", line 851, in fit
initial_epoch=initial_epoch)
  File "/home/sandbox/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/engine/training_generator.py", line 191, in model_iteration
batch_outs = batch_function(*batch_data)
  File "/home/sandbox/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py", line 1175, in train_on_batch
x, y, sample_weight=sample_weight, class_weight=class_weight)
  File "/home/sandbox/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py", line 2289, in _standardize_user_data
self._set_inputs(cast_inputs)
  File "/home/sandbox/anaconda3/lib/python3.7/site-packages/tensorflow/python/training/checkpointable/base.py", line 442, in _method_wrapper
method(self, *args, **kwargs)
  File "/home/sandbox/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py", line 2529, in _set_inputs
outputs = self.call(inputs, training=training)
  File "/home/sandbox/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/engine/sequential.py", line 233, in call
inputs, training=training, mask=mask)
  File "/home/sandbox/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/engine/sequential.py", line 254, in _call_and_compute_mask
layer._maybe_build(x)
  File "/home/sandbox/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/engine/base_layer.py", line 1591, in _maybe_build
self.input_spec, inputs, self.name)
 File "/home/sandbox/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/engine/input_spec.py", line 139, in assert_input_compatibility
str(x.shape.as_list()))
 ValueError: Input 0 of layer dense is incompatible with the layer: : expected min_ndim=2, found ndim=1. Full shape received: [None]

1 个答案:

答案 0 :(得分:3)

您本该使用tf.data.Dataset.from_tensor_slices()时最有可能使用tf.data.Dataset.from_tensors()

或者,您必须将.batch(16)放在tf.data.Dataset的末尾。