Search code examples
pythontensorflowerror-handlingconv-neural-networktaylor-series

InvalidArgumentError: tensorflow logits and labels still not broadcastable


I tried to build ResNet like a convolutional model for RGB image classification, I used cifar-10 images dataset for training my model. When I compiled and evaluated the model, I had an invalid argument error said that logits and labels must be broadcastable, I am not sure the source of this error. How can I get rid of this error? can anyone point me out what's wrong with me code? any idea to fix this? thanks

my current attempt:

    h = Conv2D(filters=32, kernel_size=(3,3), padding='same', activation='relu', input_shape=input_shape)(h)
    h = Conv2D(filters=32, kernel_size=(3,3), activation='relu')(h)
    h = MaxPooling2D(pool_size=(2,2))(h)
    h = Dropout(0.25)(h)
    h = Flatten()(h)
    h = Dense(512, activation='relu')(h)
    h = Dense(10, activation='softmax')(h)
    model = Model(inputs=x, outputs=h)
    return model

my_model = resNet_2(train_imgs)
my_model.compile(optimizer="adam", loss="categorical_crossentropy", metrics="accuracy")
model_history = my_model.fit(train_imgs, train_label_one_hot, batch_size=64, epochs=20, validation_data=(test_imgs, test_label_one_hot))
my_model.evaluate(train_imgs, train_label_one_hot)
my_model.evaluate(test_imgs, test_label_one_hot)

error

InvalidArgumentError                      Traceback (most recent call last)
<ipython-input-40-5a4cccc5d1ec> in <module>()
      3 # my_model.summary()
      4 # ## use cifar-10 image dataset
----> 5 my_history = my_model.fit(train_imgs, train_label_one_hot, batch_size=64, epochs=20, validation_data=(test_imgs,

test_label_one_hot)) 6 my_model.evaluate(train_imgs, train_label_one_hot) 7 my_model.evaluate(test_imgs, test_label_one_hot)

8 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/execute.py

in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name) 58 ctx.ensure_initialized() 59 tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name, ---> 60 inputs, attrs, num_outputs) 61 except core._NotOkStatusException as e: 62 if name is not None:

InvalidArgumentError:  logits and labels must be broadcastable: logits_size=[192,10] labels_size=[64,10]
   [[node categorical_crossentropy/softmax_cross_entropy_with_logits (defined at

:5) ]] [Op:__inference_train_function_119387]

I don't understand why this error happened, is is problem of my model or incorrect input/output dim setting? any quick shot to trace the error and make model evaluation works fine? any thought? thanks

update:

here I used Taylor expansion to build my computational model, using exp_order means that I used the Taylor series of 2 expansion terms or approximation order. can anyone why my model gave such an error? any way to fix this? thanks


Solution

  • The error is coming from this part of code

    def get_exp(x, exp_order):
            x_ = x[..., None] 
            x_ = tf.tile(x_, multiples=[1, 1, exp_order + 1]) 
            pows = tf.range(0, exp_order + 1, dtype=tf.float32)
            x_p = tf.pow(x_, pows)
            return x_p
    

    when exp_order = 2, then tf.tile is making the size of x_ to be three multiples. When your input data is of batch_size = 64, the above code is expecting size to be 192. When I input exp_order = 0, then it runs the code without any issue. So you need to update that part of the code.Hope it helps.