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.
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 .
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.
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.
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.
This error is usually thrown when you pass an empty array to Keras. Check the array you are passing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With