fastai/courses

cannot import name activity_l2 from keras.regularizers

Jayoprell opened this issue · 5 comments

I' ve see this problem in the course forums and it has been solved now. The version of keras are caused the problem. So all you need to do is as follows:
Make some changes to utils.py if you are using keras 2.0+:
from keras.regularizers import l2, activity_l2, l1, activity_l1 ----> from keras.regularizers import l2, l1
from keras.utils.layer_utils import layer_from_config -----> from keras.layers import deserialize as layer_from_config

I think you are using keras 2.0+. You can try install the keras 1.2.2

pip install keras=1.2.2

otherwise you may need to make some changes to the utils.py as @Jayoprell mentioned above. I also need to modify the vgg16.py to make it work in my laptop.

--- a/deeplearning1/nbs/vgg16.py
+++ b/deeplearning1/nbs/vgg16.py
@@ -174,7 +174,7 @@ class Vgg16():
                 batches : A keras.preprocessing.image.ImageDataGenerator object.
                           See definition for get_batches().
         """
-        self.ft(batches.nb_class)
+        self.ft(batches.num_class)
         classes = list(iter(batches.class_indices)) # get a list of all the class labels
         
         # batches.class_indices is a dict with the class name as key and an index as value
@@ -209,8 +209,8 @@ class Vgg16():
             Fits the model on data yielded batch-by-batch by a Python generator.
             See Keras documentation: https://keras.io/models/model/
         """
-        self.model.fit_generator(batches, samples_per_epoch=batches.nb_sample, nb_epoch=nb_epoch,
-                validation_data=val_batches, nb_val_samples=val_batches.nb_sample)
+        self.model.fit_generator(batches, samples_per_epoch=batches.samples, nb_epoch=nb_epoch,
+                validation_data=val_batches, nb_val_samples=val_batches.samples)
 
 
     def test(self, path, batch_size=8):
@@ -227,5 +227,5 @@ class Vgg16():
     
         """
         test_batches = self.get_batches(path, shuffle=False, batch_size=batch_size, class_mode=None)
-        return test_batches, self.model.predict_generator(test_batches, test_batches.nb_sample)
+        return test_batches, self.model.predict_generator(test_batches, test_batches.samples)

I faced the same issue on trying to run the lesson 1 notebook on Crestle. Here's what I did

  1. On a terminal
    pip install keras==1.2.2

  2. Restart Jupyter notebook.

  3. Verify Keras version using python -c 'import keras; print(keras.__version__)'

Hope it helps.

image

In keras 2 even after changing nb_class to num_class i am getting the error as above

@royshouvik after doing pip install I am getting AttributeError: 'module' object has no attribute 'HiddenLayer'