LoK*_*LoK 5 python machine-learning keras 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)
Run Code Online (Sandbox Code Playgroud)
训练数据集如下,有 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
Run Code Online (Sandbox Code Playgroud)
并且我在运行时出现以下错误:
“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]
Run Code Online (Sandbox Code Playgroud)
    您很可能tf.data.Dataset.from_tensor_slices()在应该使用的时候使用了tf.data.Dataset.from_tensors().
或者,您必须将.batch(16)其放在tf.data.Dataset.