InvalidArgumentError: 2 root error(s) found. (0) Invalid argument: Incompatible shapes: [4,3] vs. [4,4]

六月ゝ 毕业季﹏ 提交于 2021-01-29 18:54:42

问题


I am facing below error when trying to train a multi-class classification model ( 4 classes) for Image dataset. Even though my output tensor is of shape 4 I am facing below issue. Please let me know how to fix this issue.

    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.

My batch size is 4 and below is last few layers of my model.

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]                   
==================================================================================================

Loss function

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

回答1:


I think, there is nothing wrong with the shapes, but with the loss function, you are trying to use. Ideally for multiclass classification, the final layer has to have softmax activation (for your logits to sum up to 1) and use CategoricalCrossentropy as your loss function if your labels are one-hot and SparseCategoricalCrossentropy if your labels are integers. Tensorflow documentation attached below. https://www.tensorflow.org/api_docs/python/tf/keras/losses/CategoricalCrossentropy

Changes that are to be done to your code

    # 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)) 


来源:https://stackoverflow.com/questions/61054359/invalidargumenterror-2-root-errors-found-0-invalid-argument-incompatible

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!