importError: cannot import name 'L2' from 'tensorflow.keras.regularizers' (/usr/local/lib/python3.7/dist-packages/tensorflow/keras/regularizers/__init__.py)

I have just installed Tensorflow 2.2.0 from a source on my Raspberry Pi 4B running Raspbian 32-bit (armv7l) OS, but when I try to run python code, it gives me the following error, I don’t know if it is something about the version as it doesn’t say anything specifically about the version of TF.

importError: cannot import name ‘L2’ from ‘tensorflow.keras.regularizers’ (/usr/local/lib/python3.7/dist-packages/tensorflow/keras/regularizers/init.py)

Check 2.2 api docs. Changing L2 to l2 should work. tf.keras.regularizers.l2  |  TensorFlow Core v2.2.0

1 Like

Verified - Divvya Saxena

Do I have to replace all L2 with L1 throughout in the following the code.

This file is MACHINE GENERATED! Do not edit.

Generated by: tensorflow/python/tools/api/generator/create_python_api.py script.

“”"Built-in regularizers.

“”"

from future import print_function as _print_function

import sys as _sys

from tensorflow.python.keras.regularizers import L1L2
from tensorflow.python.keras.regularizers import Regularizer
from tensorflow.python.keras.regularizers import deserialize
from tensorflow.python.keras.regularizers import get
from tensorflow.python.keras.regularizers import l1
from tensorflow.python.keras.regularizers import l1_l2
from tensorflow.python.keras.regularizers import l2
from tensorflow.python.keras.regularizers import serialize

del _print_function

Whenever I try to make changes it says, you cannot edit this file as it is a machine generated file.

No, you change it in your training code. You can also switch to tf 2.3 or higher and dont change anything.

1 Like

Yes, I was thinking about shifting to tf 2.5.0 but I didn’t find any article or anything to install tf 2.5.0 on RPi 4B running Raspbian 32-bit (armv7l).

Just an update: Just in case anyone runs into the same issue in the future, I had to install Raspbian 64-bit (aarch64) which is the latest version of the Raspbian OS for Raspberry Pi’s having a 64-bit architecture support, then installed Tensorflow 2.5.0 and Opencv 4.5.2 both from a source. I never recommend installing Tensorflow and OpenCV using the pip commands. Always go for sources.

1 Like