InvalidArgumentError:找到2个根错误.(0)无效参数:不兼容的形状:[4,3]与[4,4]



在尝试为图像数据集训练多类分类模型(4类(时,我面临以下错误。尽管我的输出张量的形状是4,但我仍面临以下问题。请让我知道如何解决这个问题。

Epoch 1/10
---------------------------------------------------------------------------
InvalidArgumentError                      Traceback (most recent call last)
<ipython-input-30-01c6f78f4d4f> in <module>
4     epochs=epochs,
5     validation_data=val_data_gen,
----> 6     validation_steps=total_val // batch_size
7 )
/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/keras/engine/training.py in fit_generator(self, generator, steps_per_epoch, epochs, verbose, callbacks, validation_data, validation_steps, validation_freq, class_weight, max_queue_size, workers, use_multiprocessing, shuffle, initial_epoch)
1294         shuffle=shuffle,
1295         initial_epoch=initial_epoch,
-> 1296         steps_name='steps_per_epoch')
1297 
1298   def evaluate_generator(self,
/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/keras/engine/training_generator.py in model_iteration(model, data, steps_per_epoch, epochs, verbose, callbacks, validation_data, validation_steps, validation_freq, class_weight, max_queue_size, workers, use_multiprocessing, shuffle, initial_epoch, mode, batch_size, steps_name, **kwargs)
263 
264       is_deferred = not model._is_compiled
--> 265       batch_outs = batch_function(*batch_data)
266       if not isinstance(batch_outs, list):
267         batch_outs = [batch_outs]
/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/keras/engine/training.py in train_on_batch(self, x, y, sample_weight, class_weight, reset_metrics)
1015       self._update_sample_weight_modes(sample_weights=sample_weights)
1016       self._make_train_function()
-> 1017       outputs = self.train_function(ins)  # pylint: disable=not-callable
1018 
1019     if reset_metrics:
/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/keras/backend.py in __call__(self, inputs)
3474 
3475     fetched = self._callable_fn(*array_vals,
-> 3476                                 run_metadata=self.run_metadata)
3477     self._call_fetch_callbacks(fetched[-len(self._fetches):])
3478     output_structure = nest.pack_sequence_as(
/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/client/session.py in __call__(self, *args, **kwargs)
1470         ret = tf_session.TF_SessionRunCallable(self._session._session,
1471                                                self._handle, args,
-> 1472                                                run_metadata_ptr)
1473         if run_metadata:
1474           proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)
InvalidArgumentError: 2 root error(s) found.
(0) Invalid argument: Incompatible shapes: [4,3] vs. [4,4]
[[{{node loss_2/predictions_loss/logistic_loss/mul}}]]
[[loss_2/mul/_19047]]
(1) Invalid argument: Incompatible shapes: [4,3] vs. [4,4]
[[{{node loss_2/predictions_loss/logistic_loss/mul}}]]
0 successful operations.
0 derived errors ignored.

我的批量大小是4,下面是我模型的最后几层

conv5_block16_2_conv (Conv2D)   (None, 16, 16, 32)   36864       conv5_block16_1_relu[0][0]       
__________________________________________________________________________________________________
conv5_block16_concat (Concatena (None, 16, 16, 1024) 0           conv5_block15_concat[0][0]       
conv5_block16_2_conv[0][0]       
__________________________________________________________________________________________________
bn (BatchNormalization)         (None, 16, 16, 1024) 4096        conv5_block16_concat[0][0]       
__________________________________________________________________________________________________
relu (Activation)               (None, 16, 16, 1024) 0           bn[0][0]                         
__________________________________________________________________________________________________
avg_pool (GlobalAveragePooling2 (None, 1024)         0           relu[0][0]                       
__________________________________________________________________________________________________
predictions (Dense)             (None, 4)            4100        avg_pool[0][0]                   
==================================================================================================

损失函数

model.compile(optimizer='adam',
loss=tf.keras.losses.BinaryCrossentropy(from_logits=True))

我认为,形状没有什么问题,但对于loss函数,您正在尝试使用。理想情况下,对于多类分类,最后一层必须具有softmax激活(使您的logits总和为1(,并且如果您的标签是一个热标签,则使用类别交叉熵作为损失函数,如果您的标签是整数,则使用稀疏类别交叉熵。Tensorflow文件附于下文。https://www.tensorflow.org/api_docs/python/tf/keras/losses/CategoricalCrossentropy

要对代码进行的更改

# adding softmax activation to final dense layer 
predictions = Dense(4, activation='softmax')(avg_pool)
# assuming you have one-hot labels 
model.compile(optimizer='adam',loss=tf.keras.losses.CategoricalCrossentropy(from_logits=True)) 

相关内容

  • 没有找到相关文章

最新更新