Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UnboundLocalError: local variable 'batch_outputs' referenced before assignment

I am writing machine learning code using Keras to grade the severity of prostate cancer. After running it the following error appears:

---------------------------------------------------------------------------
UnboundLocalError                         Traceback (most recent call last)
<ipython-input-14-0e08590512ec> in <module>
      8     for file in column:
      9         data = generate_tiles(file)
---> 10         prediction = model.predict(data)
     11         max_score = prediction.max()
     12 

/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py in _method_wrapper(self, *args, **kwargs)
     86       raise ValueError('{} is not supported in multi-worker mode.'.format(
     87           method.__name__))
---> 88     return method(self, *args, **kwargs)
     89 
     90   return tf_decorator.make_decorator(

/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py in predict(self, x, batch_size, verbose, steps, callbacks, max_queue_size, workers, use_multiprocessing)
   1283             callbacks.on_predict_batch_end(step, {'outputs': batch_outputs})
   1284       callbacks.on_predict_end()
-> 1285     all_outputs = nest.map_structure_up_to(batch_outputs, concat, outputs)
   1286     return tf_utils.to_numpy_or_python_type(all_outputs)
   1287 

UnboundLocalError: local variable 'batch_outputs' referenced before assignment

Does anyone know what batch outputs would be refering too? I don't have such a variable in my code.

like image 361
user10055134 Avatar asked Aug 11 '20 18:08

user10055134


People also ask

How do you fix UnboundLocalError local variable referenced before assignment?

The Python "UnboundLocalError: Local variable referenced before assignment" occurs when we reference a local variable before assigning a value to it in a function. To solve the error, mark the variable as global in the function definition, e.g. global my_var .

What does UnboundLocalError mean in Python?

The UnboundLocalError: local variable referenced before assignment error is raised when you try to assign a value to a local variable before it has been declared. You can solve this error by ensuring that a local variable is declared before you assign it a value.

What does local variable referenced before assignment mean?

It does so by a simple rule: If there is an assignment to a variable inside a function, that variable is considered local . In most cases “local variable referenced before assignment” will occur when trying to modify a local variable before it is actually assigned within the local scope.

How do you prevent UnboundLocalError?

UnboundLocalError can be solved by changing the scope of the variable which is complaining. You need to explicitly declare the variable global. Variable x's scope in function printx is global. You can verify the same by printing the value of x in terminal and it will be 6.


1 Answers

This error is usually thrown when you pass an empty array to Keras. Check the array you are passing.

like image 71
Gauranga Avatar answered Oct 04 '22 03:10

Gauranga