XIU*_*111 4 python conv-neural-network valueerror
因此,我尝试在我的数据集上使用预先训练的模型,然后将其与我自己的 cnn 模型进行比较。但是,当我尝试做模型时,我立即看到错误。非常适合 ((None, 4, 4, 1) vs (None,))。这个错误从何而来?我是否应该编辑预调整 cnn.
我使用的模型是ResNET50,除了输入层改为128并且有2个输出外,没有任何修改。
欢迎任何帮助,
代码:
history = modelB.fit_generator(train_data,
validation_data = test_data,
epochs=5,
steps_per_epoch = 1714,)
Run Code Online (Sandbox Code Playgroud)
错误:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-89-89a7f1c1eb60> in <module>()
2 validation_data = test_data,
3 epochs=5,
----> 4 steps_per_epoch = 1714,)
2 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/func_graph.py in autograph_handler(*args, **kwargs)
1145 except Exception as e: # pylint:disable=broad-except
1146 if hasattr(e, "ag_error_metadata"):
-> 1147 raise e.ag_error_metadata.to_exception(e)
1148 else:
1149 raise
ValueError: in user code:
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1021, in train_function *
return step_function(self, iterator)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1010, in step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1000, in run_step **
outputs = model.train_step(data)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 860, in train_step
loss = self.compute_loss(x, y, y_pred, sample_weight)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 919, in compute_loss
y, y_pred, sample_weight, regularization_losses=self.losses)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/compile_utils.py", line 201, in __call__
loss_value = loss_obj(y_t, y_p, sample_weight=sw)
File "/usr/local/lib/python3.7/dist-packages/keras/losses.py", line 141, in __call__
losses = call_fn(y_true, y_pred)
File "/usr/local/lib/python3.7/dist-packages/keras/losses.py", line 245, in call **
return ag_fn(y_true, y_pred, **self._fn_kwargs)
File "/usr/local/lib/python3.7/dist-packages/keras/losses.py", line 1932, in binary_crossentropy
backend.binary_crossentropy(y_true, y_pred, from_logits=from_logits),
File "/usr/local/lib/python3.7/dist-packages/keras/backend.py", line 5247, in binary_crossentropy
return tf.nn.sigmoid_cross_entropy_with_logits(labels=target, logits=output)
ValueError: `logits` and `labels` must have the same shape, received ((None, 4, 4, 1) vs (None,)).
Run Code Online (Sandbox Code Playgroud)
问题在于编译模型时使用的损失函数。
将编译替换为以下代码:
model.compile(optimizer='adam',loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['accuracy'])
Run Code Online (Sandbox Code Playgroud)