Vscode keras intellisense(suggestion) not working properly - python

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()

Related

VSCode Autocomplete And Suggestion (IntelliSense) Doesn't Work For Tensorflow And Keras Libraries?

The VSCode autocomplete option doesn't work for tensorflow and keras libraries; However i've installed python and pylance extension on it; is there any solution to make it work or not, without install new extension or something like as AI autocomplete; Kite and tabinine?
For instance, here i'm trying to use layers or preprocessing from keras API but it doesn't show anything at all
Also notice here the tensorflow version and python version
A potentially useful fix:
try adding this to the bottom of your tensorflow/__init__.py
# 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
Find the location of the tensorflow package and open the __init__.py file in the tensorflow folder
Add the above codes at the bottom of the file
It should be noted that it will be useful to import in the following way
import tensorflow as tf
tf.keras
Importing in the previous way still can't get intellisense

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

restore_best_weights issue Keras EarlyStopping

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.

AttributeError: module 'tensorflow_core._api.v2.train' has no attribute 'Optimizer' when importing BERT

I'm am getting this error just in the being of importing my packages. I haven't been able to find the correct remedy to fix the issue. Any help is greatly appreciated.
From what I can tell it looks to maybe be a Tensorflow issue?
from sklearn.model_selection import train_test_split
import pandas as pd
import tensorflow as tf
import tensorflow_hub as hub
from datetime import datetime
import bert
from bert import run_classifier
from bert import optimization
from bert import tokenization
found this
Background
Colab has two versions of TensorFlow pre-installed: a 2.x version and a 1.x version. Colab uses TensorFlow 2.x by default, though you can switch to 1.x by the method shown below.
Specifying the TensorFlow version
Running import tensorflow will import the default version (currently 2.x). You can use 1.x by running a cell with the tensorflow_version magic before you run import tensorflow.
[ ]
%tensorflow_version 1.x
TensorFlow 1.x selected.
more detail :
https://colab.research.google.com/notebooks/tensorflow_version.ipynb#scrollTo=NeWVBhf1VxlH
You seem to have TensorFlow 2.x while the bert module uses TensorFlow 1.x. You can verify here that Tensorflow 1.x has the tf.train.Optimizer module while according to this, Tensorflow 2.x has no such module.
Make sure you install the Tensorflow version that bert requires

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