How to fix 'tensor is out of scope and cannot be use here' ? in Python

794 Views Asked by At

I need help to fix this error. I got code of SSD (single shot multibox detector) in GitHub and I convert some TF 1.x function to TF 2.x function, everything works well until this chunk of code.

I've got this code from https://github.com/ccasadei/SSD-Keras.git

and this code below is in train.py file

I use this chunk of code :

model.fit(x=train_generator, steps_per_epoch=ceil(n_train_samples / config.batch_size), epochs=config.epochs, callbacks=callbacks, validation_data=val_generator, validation_steps=ceil(n_val_samples / config.batch_size))

and when I run it in Google Colab, this code produce some error :

TypeError: <tf.Tensor 'compute_loss/Const:0' shape=() dtype=int32> is out of scope and cannot be used here. Use return values, explicit Python locals, or TensorFlow collections to access it. Please see https://www.tensorflow.org/guide/function#all_outputs_of_a_tffunction_must_be_return_values for more information.

The error part is in here : validation_steps=ceil(n_val_samples / config.batch_size))

please help me, I struggle with this error for a week

please comment if u need more information

Thank you in advance

1

There are 1 best solutions below

1
On

you need to add @tf.function above the compute_loss function. In your case it is inside the file loss.py.