keras: get two errors: cannot import activity_l2, cannot import layer_from_config

Hi, I installed the latest keras(version 2.0.1) in docker. but the job failed when I try to import these two:

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-6-72e860176cbd> in <module>()
     40 from keras.layers import TimeDistributed, Activation, SimpleRNN, GRU
     41 from keras.layers.core import Flatten, Dense, Dropout, Lambda
---> 42 from keras.regularizers import l2, activity_l2, l1, activity_l1
     43 from keras.layers.normalization import BatchNormalization
     44 from keras.optimizers import SGD, RMSprop, Adam

ImportError: cannot import name activity_l2



ImportError                               Traceback (most recent call last)
<ipython-input-7-a9899ee596df> in <module>()
     43 from keras.layers.normalization import BatchNormalization
     44 from keras.optimizers import SGD, RMSprop, Adam
---> 45 from keras.utils.layer_utils import layer_from_config
     46 from keras.metrics import categorical_crossentropy, categorical_accuracy
     47 from keras.layers.convolutional import *

ImportError: cannot import name layer_from_config

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 23 (1 by maintainers)

Commits related to this issue

Most upvoted comments

After change the import command, the code pass the compile.

#from keras.utils.layer_utils import layer_from_config
from keras.layers import deserialize as layer_from_config

Reference(http://forums.fast.ai/t/keras-2-released/1956)

Instead of activity_l2 you can use l2: https://github.com/fchollet/keras/commit/1c630c3e3c8969b40a47d07b9f2edda50ec69720#diff-6e3349d3f432641cc969b702bcd3edafL111

I am not familiar with layer_from_config, but it seams that it is replaced with layers.deserialize https://github.com/fchollet/keras/commit/ebe84eb3a1c3beb44e25fa4f00a89f2c71dde2cc#diff-98d8790a4331685d2a81334c398c4b58R229 from ..layers import deserialize as deserialize_layer

You can find here https://github.com/fchollet/keras/commit/1c630c3e3c8969b40a47d07b9f2edda50ec69720 that activity_l2 is deleted and was equal to l2.

I experienced the same issue. But when installing keras==1.2.2, issue fixed. I guess there’re some breaking issues in 2.0.1

I was using a code from fast ai (utills.py) and ran into several issues related to keras version. Downgrading was a simple fix. ( pip install keras == 1.2.2 )

Fast.ai student here. I found out that after you install Keras 1.2.2. you must restart Jupyter server.

I changed the code to this to make it work:

from keras.regularizers import L1L2, l2, l1
activity_l1 = L1L2(l1=1)
activity_l2 = L1L2(l2=1)

I’ve just grep’d the entirety of deeplearning1 and there is no reference to the imported module. Consequently I suspect that “from keras import layer_from_config” can safely be commented out.

I had the exact same issue. I tried downgrading Keras to 1.2.2. I was using the utils file from fast.ai. This is what i did and that worked to remove the error from activity_l2 but i still get the layer_from_config error

from keras.regularizers import L1L2, l2, l1 activity_l1 = L1L2(l1=1) activity_l2 = L1L2(l2=1)

That solved the activity_l1 but the error for layer_from_config shill persists