Search code examples
tensorflowkerasgoogle-cloud-datalab

Cannot import Keras from TensorFlow contrib package in Datalab


I'm trying to import Keras from the TensorFlow contrib package in a Datalab instance (I am using Tensorflow 1.1.0 version and Python 2.7)

from tensorflow.contrib.keras.python.keras.regularizers import l1,l2
from tensorflow.contrib.keras.python.keras.models import Sequential, load_model
from tensorflow.contrib.keras.python.keras.layers import Dense, Dropout, Activation
from tensorflow.contrib.keras.python.keras.constraints import max_norm
from tensorflow.contrib.keras.python.keras.optimizers import RMSprop, Adam
from tensorflow.contrib.keras.python.keras.layers.normalization import BatchNormalization

But I get the following error:

ImportErrorTraceback (most recent call last)
<ipython-input-52-adf3061b6b33> in <module>()
      1 #from tensorflow.contrib.keras import *
----> 2 from tensorflow.contrib.keras.python.keras.regularizers import l1,l2
      3 from tensorflow.contrib.keras.python.keras.models import Sequential, load_model
      4 from tensorflow.contrib.keras.python.keras.layers import Dense, Dropout, Activation
      5 from tensorflow.contrib.keras.python.keras.constraints import max_norm

/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/keras/__init__.py in <module>()
     24 
     25 # pylint: disable=wildcard-import
---> 26 from tensorflow.contrib.keras.api.keras import *
     27 
     28 try:

/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/keras/api/keras/__init__.py in <module>()
     23 from __future__ import print_function
     24 
---> 25 from tensorflow.contrib.keras.api.keras import activations
     26 from tensorflow.contrib.keras.api.keras import applications
     27 from tensorflow.contrib.keras.api.keras import backend

/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/keras/api/keras/activations/__init__.py in <module>()
     20 
     21 # Activation functions.
---> 22 from tensorflow.contrib.keras.python.keras.activations import elu
     23 from tensorflow.contrib.keras.python.keras.activations import hard_sigmoid
     24 from tensorflow.contrib.keras.python.keras.activations import linear

/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/keras/python/keras/__init__.py in <module>()
     19 from __future__ import print_function
     20 
---> 21 from tensorflow.contrib.keras.python.keras import activations
     22 from tensorflow.contrib.keras.python.keras import applications
     23 from tensorflow.contrib.keras.python.keras import backend

ImportError: cannot import name activations

I've tried to upgrade Tensorflow to 1.2.1 version but it still doesn't work.

Any ideas why I get this error? Thanks!


Solution

  • I'll answer my own question: it seems that the python packages installed in the Datalab instance are all reset to the default packages everytime I restart the instance (I haven't found out why this happens), so I was executing the code with the 1.0.0 tensorflow version instead. I just wrote in a cell:

    !pip install tensorflow==1.1.0
    

    and I restarted the Datalab kernel (Restart session button). After that, everything works fine and I can import Keras from the Tensorflow contrib package.