restore_best_weights issue Keras EarlyStopping - python

I am trying to use Keras EarlyStopping however whenever I try to use restore_best_weights it comes up with an error:
_init_() got an unexpected keyword argument 'restore_best_weights'
I am using the most up to date Keras so I'm really unsure why this is happening.

What version of Keras are you using?
If you still do use Keras 2.3.1, please do ensure that your are not mixing keras and tensorflow versions.
More precisely, avoid using imports like from keras.layers import Conv2D and from tensorflow.keras.layers import MaxPool2D.
If the problem still persists after this issue, please use all the imports from tensorflow, i.e. from tensorflow.keras import .....
Avoid using the plain Keras package, as it will become obsolete in the foreseeable future; the additional problem is that is nowhere near as well maintained as keras inside tensorflow.
You can uninstall it altogether from your environment.

Related

Python won't let me import tensorflow.keras even though tensorflow and keras are both installed?

I've installed tensorflow and keras in every possible way I can think of, I have updated pip, used brew, tried a virtualenv but for some reason it won't let me import specific methods from tensorflow.keras (see image). What can I do?
Import issue
from tensorflow.python.keras.models import model_from_json

Vscode keras intellisense(suggestion) not working properly

Intellisense works fine on importing phrase
But when it comes with chaining method, it shows different suggestions
Python & Pylance extensions are installed.
From this issue on github
try adding this to the bottom of your tensorflow/__init__.py (in .venv/Lib/site-packages/tensorflow for me)
# Explicitly import lazy-loaded modules to support autocompletion.
# pylint: disable=g-import-not-at-top
if _typing.TYPE_CHECKING:
from tensorflow_estimator.python.estimator.api._v2 import estimator as estimator
from keras.api._v2 import keras
from keras.api._v2.keras import losses
from keras.api._v2.keras import metrics
from keras.api._v2.keras import optimizers
from keras.api._v2.keras import initializers
# pylint: enable=g-import-not-at-top
The problem is because keras is a special class that enables lazy loading and not a normal module.
Edit: With updates to tf, vscode, or something else I'm not having this issue and don't need to use the above fix anymore. I just have to use keras = tf.keras instead of from tensorflow import keras and I have Intellisense working now.
did you try clearing the cache on your system?
Try this
Don't import it directly like this
import tensorflow as tf
import tensorflow.keras as keras
Instead Do
import tensorflow as tf
keras = tf.keras
After this change, Everything was fixed and started showing better suggestions including function documentations
tensorflow.python.keras is for developers only and should not be used, but I think it is fine to be used as "type". I have also read it is a different version than the tensorflow.keras so have this in mind.
# Those are the imports, that actualy load the correct code
import tensorflow.keras as tfk
import tensorflow.keras.layers as layers
# This is for typehinting and intllisense
import tensorflow.python.keras as _tfk
import tensorflow.python.keras.layers as _layers
# This gets highlighted as error by my linter, but it runs
tfk: _tfk
layers: _layers
# from now on, the intellisense and docstrings work
# ...
While keras = tf.keras does the trick, I was dumbstruck that IntelliSense on my home machine wasn't working. Turns out, the Jupyter notebook I was using wasn't using the right Python interpreter (conda environment with tf and keras both # 2.11.0) due to a window reload or whatever.
This worked for me using conda with cuda and tensoflow:
import tensorflow as tf
from tensorflow import keras
from keras.api._v2 import keras as KerasAPI
KerasAPI.applications.ResNet50()

How to use "keras" directly rather than to use "tensorflow.keras" in importing lines

I am trying to use a third party library that is using tensorflow and keras internally.
I encountered one issue in using this library.
In that library, it is assumed that keras is available as a top-level module name.
For example, the following lines are frequently used inside this library.
from keras.layers import Dense
In my configuration, it is somewhat different, so I need to import lines as follows:
from tensorflow.keras.layers import Dense
Since that library is not that small, it is not efficient for me to manually edit all such lines. What would be the best way to resolve this problem?
You will need to install keras:
If you are using Windows or virtual environment:
pip install keras
For Linux or Mac environment:
sudo pip install keras
Then, afterwards you can use from keras.layers import Dense instead.

How to choose from "tensorflow.keras" and "tensorflow.python.keras" in tensorflow 2.0?

Currently in tensorflow 2.0, Keras can be imported by both tensorflow.keras and tensorflow.python.keras. What's the difference and how should I choose from these two imports?
from tensorflow.keras import *
from tensorflow.python.keras import *
From my understanding, tensorflow.python.keras is pretty much the same as the python package Keras, while tensorflow.keras is implemented by TensorFlow which might be more compatible with TF, but do not have all packages as in Keras such as Layer, InputSpec, etc.
One other issue is that for some IDEs like PyCharm, they cannot find or auto-complete packages from tensorflow.keras, and the temporary solution is to use tensorflow.python.keras instead. (from tensorflow issue)
In TensorFlow 2.0, tf.keras is recommended. This version is stable now.
tensorflow.keras and tensorflow.python.keras are exactly the same package, what tells you that they are different?
You are not supposed to use tensorflow.python imports directly, as per this answer: https://stackoverflow.com/a/47306203/6108843
As of the latest version of Tensorflow, it is recommended to use tensorflow.keras
This is likely due to depreciation since TF is evolving rapidly.

tensorflow.keras like imports show warnings in PyCharm, work well on command line

I get this error when I import modules from TensorFlow in PyCharm.
Cannot find reference 'keras' in 'init.py'
But when I use the tensorflow.python.keras prefix, the warning is not shown. Also, in the command line, no such warning is shown.
>> from tensorflow.keras import activations
>> # No errors!
How can I fix that warning in PyCharm and freely use tensorflow.keras and not tensorflow.python.keras? I'm using Windows 10, TF version 1.12.0 installed using Anaconda and PyCharm 2018.2.5.
After tensorflow gobbled keras, there were problem with tf.keras imports on IDEs although the code works.This was then raised an as issue (Issue #26502)
It seems that there is no import command for keras module in __init_.py of tensorflow package.
When I added from tensorflow.python import keras to __init__.py manually, everything work well.
Maybe there are some problem for package importing after keras was moved from _api to python.
As of tensorflow 2.0, even from tensorflow.python import keras wouldn't work: basically there is no way of making PyCharm / IDEA help you with syntax. However, this issue is fixed in IDEA 2019.3+ (currently in EAP, so should be in PyCharm EAP)

Categories

Resources