'Getting "ValueError: 'outputs' must be defined before the loop." error while fitting model

I made a CNN model for training b/w images for training it on TPU on dimension of 100*100. Added the basic callback but after running it, it was giving outputs must be defined error.

def create_model():
    model = Sequential()
    model.add(InputLayer(input_shape=(100, 100, 1)))

# Building the neural network
    model = Sequential()
    model.add(InputLayer(input_shape=(100, 100, 1)))
    model.add(Conv2D(8, (3, 3), activation='relu', padding='same', strides=2))
    model.add(Conv2D(8, (3, 3), activation='relu', padding='same'))
    model.add(Conv2D(16, (3, 3), activation='relu', padding='same'))
    model.add(Conv2D(16, (3, 3), activation='relu', padding='same', strides=2))
    model.add(Conv2D(32, (3, 3), activation='relu', padding='same'))
    model.add(Conv2D(32, (3, 3), activation='relu', padding='same', strides=2))
    model.add(UpSampling2D((2, 2)))
    model.add(Conv2D(32, (3, 3), activation='relu', padding='same'))
    model.add(UpSampling2D((2, 2)))
    model.add(Conv2D(16, (3, 3), activation='relu', padding='same'))
    model.add(UpSampling2D((2, 2)))
    model.add(Conv2D(2, (3, 3), activation='tanh', padding='same'))
    model.compile(
    optimizer='adam',
    loss = 'binary_crossentropy',
    metrics=['accuracy'], experimental_steps_per_execution = 20)
    return model
    
with tpu_strategy.scope(): # creating the model in the TPUStrategy scope means we will train the model on the TPU
  model = create_model()
model.summary()

history = model.fit(X_train,y_train,validation_data = (X_test,y_test),steps_per_epoch=20, epochs=100, callbacks=[lr_callback])

final_accuracy = history.history["val_accuracy"][-5:]
print("FINAL ACCURACY MEAN-5: ", np.mean(final_accuracy))

but I am getting this error

ValueError: in user code:

    /opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py:811 train_function  *
        for _ in math_ops.range(self._steps_per_execution):
    /opt/conda/lib/python3.7/site-packages/tensorflow/python/autograph/operators/control_flow.py:414 for_stmt
        symbol_names, opts)
    /opt/conda/lib/python3.7/site-packages/tensorflow/python/autograph/operators/control_flow.py:629 _tf_range_for_stmt
        opts)
    /opt/conda/lib/python3.7/site-packages/tensorflow/python/autograph/operators/control_flow.py:1059 _tf_while_stmt
        body, get_state, set_state, init_vars, nulls, symbol_names)
    /opt/conda/lib/python3.7/site-packages/tensorflow/python/autograph/operators/control_flow.py:1032 _try_handling_undefineds
        _verify_loop_init_vars(init_vars, symbol_names, first_iter_vars)
    /opt/conda/lib/python3.7/site-packages/tensorflow/python/autograph/operators/control_flow.py:193 _verify_loop_init_vars
        raise ValueError(error_msg)

    ValueError: 'outputs' must be defined before the loop.


Solution 1:[1]

Remove the "experimental_steps_per_execution" to see the true error message. When you use experimental_steps_per_execution>1 a GPU loop is defined to optimize performance and not send back the data to the CPU, but this loop hides errors.

Fix the error first and then use experimental_steps_per_execution>1

Good luck ;D

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Cristian Romero