Cannot update variable with shape [2] using a Tensor with shape [512,1], shapes must be equal. [Op:AssignAddVariableOp]

My model takes one input and gives two outputs , I’m passing it in a dictionary:

train_dataset = tf.data.Dataset.from_tensor_slices(
    (
        {"input_1": atr},
        {"ed": wtr, "sd": wbtr},
    )
)
train_dataset = train_dataset.batch(100).repeat(3)

Shape of all the three arrays atr , wtr and wbtr is (7838, 512, 1).
This is my model:

import tensorflow as tf
import tensorflow_addons as tfa
from tensorflow.keras import Input, Model
input1 = tf.keras.layers.Input(shape=(None,1),name="input_1")
x = tf.keras.layers.Conv1D(filters=16, kernel_size=3, strides=1, padding="causal", activation="relu",input_shape=[None,1])(input1)
x = tf.keras.layers.Bidirectional(tf.keras.layers.GRU(128, activation="tanh", return_sequences=True))(x)
x = tf.keras.layers.Bidirectional(tf.keras.layers.GRU(256, activation="tanh", return_sequences=True))(x)
x = tf.keras.layers.Dense(128, activation="tanh")(x)
o1 = tf.keras.layers.Dense(1, activation="linear",name="ed")(x)
o2 = tf.keras.layers.Dense(1, activation="sigmoid",name="sd")(x)

model = Model(inputs=[input1], outputs=[o1, o2])

model.compile(loss={'ed': 'mean_squared_error', 
                    'sd': 'binary_crossentropy'},
              loss_weights={'ed':0.4,
                            'sd':0.6},
              optimizer='adam',
              metrics={'ed': tf.keras.metrics.MeanAbsoluteError(name="mean_absolute_error", dtype=None),
                       'sd': tfa.metrics.F1Score(name="f1_score",num_classes=2, threshold=0.5)})

Here is the model summary:

__________________________________________________________________________________________________
Layer (type)                    Output Shape         Param #     Connected to                     
==================================================================================================
input_1 (InputLayer)            [(None, None, 1)]    0                                            
__________________________________________________________________________________________________
conv1d_18 (Conv1D)              (None, None, 16)     64          input_1[0][0]                    
__________________________________________________________________________________________________
bidirectional_33 (Bidirectional (None, None, 256)    112128      conv1d_18[0][0]                  
__________________________________________________________________________________________________
bidirectional_34 (Bidirectional (None, None, 512)    789504      bidirectional_33[0][0]           
__________________________________________________________________________________________________
dense_17 (Dense)                (None, None, 128)    65664       bidirectional_34[0][0]           
__________________________________________________________________________________________________
ed(Dense)                      (None, None, 1)      129         dense_17[0][0]                   
__________________________________________________________________________________________________
sd (Dense)                      (None, None, 1)      129         dense_17[0][0]                   
==================================================================================================
Total params: 967,618
Trainable params: 967,618
Non-trainable params: 0
__________________________

Finally my model.fit() method:

history = model.fit(train_dataset,epochs=3,verbose=1,steps_per_epoch= 78)

Here is the error:

    InvalidArgumentError: Cannot update variable with shape [2] using a Tensor with shape [512,1], shapes must be equal. [Op:AssignAddVariableOp]

I don’t where I’m going wrong , pls help.

Hi,Guitar_George!
I applied

‘sd’: tfa.metrics.F1Score(name=“f1_score”, num_classes=2, threshold=0.5, average=“micro”)})

and in this case there was no error.

1 Like

As probably we have a similar not so good fast fail message as for:

1 Like