Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keras and Tensorflow: UnboundLocalError: local variable 'self' referenced before assignment

I am following a tutorial for TensorFlow and Keras.

When I run the following syntax:

from keras.models import Sequential
from keras.layers import Dense

model = Sequential()

model.add(Dense(units=64, activation='relu', input_dim=100))
model.add(Dense(units=10, activation='softmax'))

(This is not full stacktrace as StackOverflow wont let me saying it has code only) I get following error:

--------------------------------------------------------------------
UnboundLocalError                  Traceback (most recent call last)
<ipython-input-49-30ebc8793948> in <module>()
      5 from keras.layers import Dense
      6 
----> 7 model.add(Dense(units=64, activation='relu', input_dim=100))

--> 164                     layer(x)
    165                     set_inputs = True
    166                 else:


/usr/local/lib/python3.7/site-packages/keras/initializers.py in __call__(self, shape, dtype)
   self._message_listener.Modified()

UnboundLocalError: local variable 'self' referenced before assignment

Could this be because of python 3.7?

like image 253
lpt Avatar asked Dec 29 '25 23:12

lpt


1 Answers

Yes, I had to uninstall and reinstall Python 3.6. Python 3.7 looks like it introduces unexpected behavior. Thankfully, my issue is now resolved.

like image 130
lpt Avatar answered Jan 02 '26 02:01

lpt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!