【问题标题】:Module 'tensorflow' has no attribute 'placeholder' but tfv1 is imported模块 'tensorflow' 没有属性 'placeholder' 但已导入 tfv1
【发布时间】:2020-07-10 21:36:10
【问题描述】:

我正在尝试运行以下代码:

input_img = Input((height, width, 1), name='img')
model = get_unet(input_img, n_filters=16, dropout=0.05, batchnorm=True)
model.compile(optimizer=Adam(), loss="binary_crossentropy", metrics=["accuracy"])

但我收到“AttributeError: module 'tensorflow' has no attribute 'placeholder'”。 任何地方都推荐使用

import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()

但我仍然不断收到这样的错误。

【问题讨论】:

  • 你在使用 Anaconda 发行版
  • 是的,对于 Python 3.7
  • 那么问题可能是 conda 分布。尝试重新安装tenserflow
  • 首先使用“conda remove tensorflow-gpu tensorflow tensorflow-base”卸载tensorflow
  • 然后在 Anaconda Prompt (Anaconda3) 中使用“conda install -c anaconda tensorflow”进行安装

标签: python-3.x tensorflow neural-network


【解决方案1】:

如果您使用 Anaconda 发行版,请尝试使用以下命令重新安装 TensorFlow

移除张力流

conda remove tensorflow-gpu tensorflow tensorflow-base

重新安装张量流

conda install -c anaconda tensorflow

参考:-https://anaconda.org/anaconda/tensorflow

如果您的代码需要在 GPU 上运行,最好使用安装 tensorflow-gpu

conda install -c anaconda tensorflow-gpu

参考:-https://anaconda.org/anaconda/tensorflow-gpu

此外,如果您的项目使用 keras(或发行版有 keras(例如:-Colab)),那么您可以通过使用“tensorflow.keras”在 keras 上使用 tensorflow insides 例如:-

from keras.models import Sequential

改成

from tensorflow.keras.models import Sequential

【讨论】:

    【解决方案2】:

    Colab 的 Python 版本已更新。 添加这个

    !pip uninstall keras-nightly
    
    !pip install h5py==2.10.0
    
    !pip install q keras==2.1.6
    
    %tensorflow_version 1.x
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-17
      • 1970-01-01
      • 2021-09-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多