Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tensorflow invalid shape (InvalidArgumentError)

model.fit produces exception:

tensorflow.python.framework.errors_impl.InvalidArgumentError: Cannot update variable with shape [] using a Tensor with shape [32], shapes must be equal.
         [[{{node metrics/accuracy/AssignAddVariableOp}}]]
         [[loss/dense_loss/categorical_crossentropy/weighted_loss/broadcast_weights/assert_broadcastable/AssertGuard/pivot_f/_50/_63]] [Op:__inference_keras_scratch_graph_1408]

Model definition:

model = tf.keras.Sequential()

    model.add(tf.keras.layers.InputLayer(
        input_shape=(360, 7)
    ))

    model.add(tf.keras.layers.Conv1D(32, 1, activation='relu', input_shape=(360, 7)))
    model.add(tf.keras.layers.Conv1D(32, 1, activation='relu'))
    model.add(tf.keras.layers.MaxPooling1D(3))
    model.add(tf.keras.layers.Conv1D(512, 1, activation='relu'))
    model.add(tf.keras.layers.Conv1D(1048, 1, activation='relu'))
    model.add(tf.keras.layers.GlobalAveragePooling1D())
    model.add(tf.keras.layers.Dropout(0.5))
    model.add(tf.keras.layers.Dense(32, activation='softmax'))

Input Features Shape

(105, 360, 7)

Input Labels Shape

(105, 32, 1)

Compile statement

model.compile(optimizer='adam',
                  loss=tf.keras.losses.CategoricalCrossentropy(),
                  metrics=['accuracy'])

Model.fit statement

 model.fit(features,
              labels,
              epochs=50000,
              validation_split=0.2,
              verbose=1)

Any help would be much appreciated

like image 405
Tim Avatar asked Sep 14 '26 22:09

Tim


1 Answers

You can use model.summary() to see your model architecture.

print(model.summary())

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv1d (Conv1D)              (None, 360, 32)           256       
_________________________________________________________________
conv1d_1 (Conv1D)            (None, 360, 32)           1056      
_________________________________________________________________
max_pooling1d (MaxPooling1D) (None, 120, 32)           0         
_________________________________________________________________
conv1d_2 (Conv1D)            (None, 120, 512)          16896     
_________________________________________________________________
conv1d_3 (Conv1D)            (None, 120, 1048)         537624    
_________________________________________________________________
global_average_pooling1d (Gl (None, 1048)              0         
_________________________________________________________________
dropout (Dropout)            (None, 1048)              0         
_________________________________________________________________
dense (Dense)                (None, 32)                33568     
=================================================================
Total params: 589,400
Trainable params: 589,400
Non-trainable params: 0
_________________________________________________________________
None

The shape of your output layer is required to be (None,32), but the shape of your labels is (105,32,1). So you need to change the shape to (105,32). np.squeeze() function is used when we want to remove single-dimensional entries from the shape of an array.

like image 200
giser_yugang Avatar answered Sep 16 '26 16:09

giser_yugang