简体   繁体   English

Vscode keras 智能感知(建议)无法正常工作

[英]Vscode keras intellisense(suggestion) not working properly

Intellisense works fine on importing phrase Intellisense 可以很好地导入短语

图片1

But when it comes with chaining method, it shows different suggestions但是当它带有链接方法时,它会显示不同的建议

图片2

Python & Pylance extensions are installed. Python & Pylance 扩展已安装。

From this issue on github try adding this to the bottom of your tensorflow/__init__.py (in .venv/Lib/site-packages/tensorflow for me)github 上的这个问题尝试将其添加到tensorflow/__init__.py的底部(在.venv/Lib/site-packages/tensorflow中)

# 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.问题是因为 keras 是一个启用延迟加载的特殊类,而不是普通模块。

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.编辑:随着 tf、vscode 或其他内容的更新,我没有这个问题,也不需要再使用上述修复了。 I just have to use keras = tf.keras instead of from tensorflow import keras and I have Intellisense working now.我只需要使用keras = tf.keras而不是from tensorflow import keras并且我现在可以使用 Intellisense。

您是否尝试清除系统上的缓存?

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在此更改之后,一切都已修复并开始显示更好的建议,包括function 文档

tensorflow.python.keras is for developers only and should not be used, but I think it is fine to be used as "type". tensorflow.python.keras仅供开发人员使用,不应使用,但我认为用作“类型”是可以的。 I have also read it is a different version than the tensorflow.keras so have this in mind.我还读到它是与tensorflow.keras不同的版本,所以请记住这一点。

# 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.虽然keras = tf.keras可以解决问题,但令我震惊的是我家用机器上的 IntelliSense 无法正常工作。 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.事实证明,由于 window 重新加载或其他原因,我使用的 Jupyter notebook 没有使用正确的 Python 解释器(conda 环境,tf 和 keras 都是@ 2.11.0 )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM