cant install Adam from keras.optimizer - python

Was working on google colab yesterday and everything works fine. but now I got a problem when trying to import Adam.
this is what I try to import
from keras.optimizers import Adam, SGD, RMSprop
this is what I got
ImportError Traceback (most recent call last)
<ipython-input-1-36a1fe725448> in <module>()
30 from keras.layers import GlobalAveragePooling2D
31 from keras.layers import BatchNormalization, Activation, MaxPooling2D
---> 32 from keras.optimizers import Adam, SGD, RMSprop
ImportError: cannot import name 'Adam' from 'keras.optimizers' (/usr/local/lib/python3.7/dist-packages/keras/optimizers.py)
---------------------------------------------------------------------------
NOTE: If your import is failing due to a missing package, you can
manually install dependencies using either !pip or !apt.
To view examples of installing some common dependencies, click the
"Open Examples" button below.

Try to import from tf.keras as follows
from tensorflow.keras.optimizers import Adam, SGD, RMSprop

You can call the Adam optimizer function using TensorFlow:
from tensorflow.keras.optimizers import Adam

Related

When importing from keras in Jupyter, getting error 'NoneType' object has no attribute 'message_types_by_name'

I am trying to do several imports from the keras library. I am doing this on a Jupyter notebook using an Anaconda installed Python. I have used keras before fine, and I just checked for updates but found none. Here are the import statements.
from keras.preprocessing.text import Tokenizer
from keras.utils import to_categorical
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import SimpleRNN
from keras.layers import Embedding
I get a warning when I run this that says 'Using TensorFlow backend. Then I get a long error output that ends with 'NoneType' object has no attribute 'message_types_by_name'. Any ideas on what is wrong?
Since Tensorflow supports Keras as High level API, suggested way to import Keras libraries/modules is as below.
import tensorflow as tf
from tf.keras.preprocessing.text import Tokenizer
from tf.keras.utils import to_categorical
from tf.keras.models import Sequential
from tf.keras.layers import Dense
from tf.keras.layers import SimpleRNN
from tf.keras.layers import Embedding
Check the document from Keras here for reference.

ImportError: cannot import name '__version__' from partially initialized module 'keras' (most likely due to a circular import)

I have imported the following libraries for my machine learning project but have problem when I try to run my model in the command prompt...
from tensorflow.python.keras import Model
from tensorflow.python.keras.layers import Layer, Input, Conv2D, MaxPooling2D, Conv2DTranspose, concatenate, Lambda
from tensorflow.python.keras.initializers import TruncatedNormal
from keras.optimizers import Adam
from tensorflow.python.keras.callbacks import ModelCheckpoint, LearningRateScheduler, CSVLogger, Callback
from tensorflow.python.keras.models import load_model
from tensorflow.python.keras.utils import Sequence
This is the error message which I get when trying to run the model in the command prompt.
ImportError: cannot import name '__version__' from partially initialized module 'keras' (most likely due to a circular import) (C:\Users\gurun\AppData\Local\Programs\Python\Python39\lib\site-packages\keras\__init__.py)
It is always recommend to use tensorflow.keras.* instead of tensorflow.python.*.
This is because anything under tensorflow.python.* is private, intended for development only, rather than for public use.
import tensorflow as tf
print(tf.__version__)
from tensorflow.keras import Model
from tensorflow.keras.layers import Layer, Input, Conv2D, MaxPooling2D, Conv2DTranspose, concatenate, Lambda
from tensorflow.keras.initializers import TruncatedNormal
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.callbacks import ModelCheckpoint, LearningRateScheduler, CSVLogger, Callback
from tensorflow.keras.models import load_model
from tensorflow.keras.utils import Sequence
Output:
2.5.0
You should be consistent and import keras only from one source and the recommended way, as #TFer2 said, is from tensorflow.keras . Your editor maybe can complain that tensorflow.keras cannot be resolved but usually that warning can be ignored and all works fine. If not try to uninstall keras and reinstall it accordingly to the version of your tensorflow installation.

Trouble with Installing Keras

I have tried several things, and I am experiencing many difficulties in trying to install keras to Python.
First, I tried by simply trying to import it into my Jupyter Notebook using the following:
from keras.models import Sequential
from keras.layers import Dense
which resulted in the following error:
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-17-b9e2c8277ae4> in <module>
----> 1 from keras.models import Sequential
2 from keras.layers import Dense
ModuleNotFoundError: No module named 'keras'
Then, I tried importing tensorflow.
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense,Dropout,Activation, Flatten, Conv2D, MaxPooling2D
This didn't work either, for I received a similar error as before.
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-18-9d4e366b5092> in <module>
----> 1 from tensorflow.keras.models import Sequential #used for model building
2 from tensorflow.keras.layers import Dense,Dropout,Activation, Flatten, Conv2D, MaxPooling2D #used for model building
ModuleNotFoundError: No module named 'tensorflow'
I finally tried to use the terminal through Anaconda. This failed as well.
UnsatisfiableError: The following specifications were found
to be incompatible with the existing python installation in your environment:
I have Python 3.8, but apparently keras requires python[version='>=2.7,<2.8.0a0|>=3.5,<3.6.0a0|>=3.6,<3.7.0a0']. What can I do to import keras?
For whatever reason, keras and tensorflow don't work well with Python 3.8. I would try reverting to Python 3.7.

import error of ArcFace using python in jupyternotebbok

When I am importing the package ArcFace.
from arcface.metrics import ArcFace
It is showning the import error. Like this,
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-4-fe2759a1802e> in <module>
15 from keras.layers.convolutional import MaxPooling2D
16 from keras.layers.core import Activation, Flatten, Dropout, Dense
---> 17 from arcface.metrics import ArcFace
18 from keras.layers import Input
19 from keras import backend as K
ModuleNotFoundError: No module named 'arcface'
I have searched for importing it for Anaconda and pip too. I have not found any results. Please help me to solve this.
This is part of project keras-arcface but it is not part of keras so you have to install/copy it separatelly.
If you put it in your project in subfolder arcface then it should work.
You can run ArcFace within deepface. The framework is mainly based on keras and tensorflow. It handles model building and downloading pre-trained weights in the background. Besides, it covers face recognition pipeline stages includign detect, align.
#!pip install deepface
from deepface import DeepFace
resp = DeepFace.verify("img1.jpg", "img2.jpg", model_name = 'ArcFace')

Cannot import 'swish' in 'tensorflow.python.keras.activations'

I am running a jupyter notebook from an Anaconda Prompt (Anaconda 3), and I am trying to use tensorflow keras.
I am trying to run the import statement:
from tensorflow.keras.models import Sequential
I get the following error:
ImportError Traceback (most recent call last)
<ipython-input-1-d23f18c08372> in <module>
4 from keras.models import Model
5
----> 6 from tensorflow.keras.models import Sequential
7
8 from tensorflow.keras.layers import Dense, Activation, Dropout
~\Anaconda3\lib\site-packages\tensorflow\keras\__init__.py in <module>
12 import sys as _sys
13
---> 14 from . import activations
15 from . import applications
16 from . import backend
~\Anaconda3\lib\site-packages\tensorflow\keras\activations\__init__.py in <module>
21 from tensorflow.python.keras.activations import softplus
22 from tensorflow.python.keras.activations import softsign
---> 23 from tensorflow.python.keras.activations import swish
24 from tensorflow.python.keras.activations import tanh
25
ImportError: cannot import name 'swish' from 'tensorflow.python.keras.activations' (C:\Users\FlamePrinz\Anaconda3\lib\site-packages\tensorflow\python\keras\activations.py)
Try this way
from keras.utils.generic_utils import get_custom_objects
from keras.layers import Activation
get_custom_objects().update({'swish': Activation(swish)})
swish function needs to be passed into activation class to actually build it.
And then you can do
model.add(Dense(64, activation = "swish"))
You can download swish activation from pypi. Try the following:
pip install swish-activation
import swish_package
from swish_package import swish
This is an older question, but I ended up taking Dr. Snoopy's suggestion uninstalling and reinstalling TensorFlow which solved my issue.

Categories

Resources