【问题标题】:Tensorflow==2.0.0a0 - AttributeError: module 'tensorflow' has no attribute 'global_variables_initializer'Tensorflow==2.0.0a0 - AttributeError:模块“tensorflow”没有属性“global_variables_initializer”
【发布时间】:2019-10-05 04:11:38
【问题描述】:

我正在使用Tensorflow==2.0.0a0 并想运行以下脚本:

import tensorflow as tf
import tensorboard
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import tensorflow_probability as tfp
from tensorflow_model_optimization.sparsity import keras as sparsity
from tensorflow import keras

tfd = tfp.distributions

init = tf.global_variables_initializer()

with tf.Session() as sess:
    sess.run(init)

    model = tf.keras.Sequential([
      tf.keras.layers.Dense(1,kernel_initializer='glorot_uniform'),
      tfp.layers.DistributionLambda(lambda t: tfd.Normal(loc=t, scale=1))
    ])

我所有的旧笔记本都使用 TF 1.13。但是,我想开发一个使用模型优化(神经网络修剪)+ TF概率的笔记本,这需要Tensorflow > 1.13

所有库都已导入,但init = tf.global_variables_initializer() 生成错误:

AttributeError: module 'tensorflow' has no attribute 'global_variables_initializer'

另外,tf.Session() 会产生错误:

AttributeError: module 'tensorflow' has no attribute 'Session'

所以我猜这可能与 Tensorflow 本身有关,但我的 Anaconda 环境中没有旧版本冲突。

库版本的输出:

tf.__version__
Out[16]: '2.0.0-alpha0'

tfp.__version__
Out[17]: '0.7.0-dev20190517'

keras.__version__
Out[18]: '2.2.4-tf'

对这个问题有什么想法吗?

【问题讨论】:

  • 在一个 GitHub 论坛上我看到了这个提到的 pip3 install --upgrade --force-reinstall tensorflow-gpu ...另外你使用的是什么版本的 python 也许你需要使用更新的版本?
  • 由于您使用的是 tensorflow veriso 2.0.x.x ,您不再需要使用tf.global_variables_initializer。查看此迁移指南link
  • 完美,@Vishal,我认为你的回答是最好的。解决了问题

标签: python tensorflow tensorflow-estimator tf.keras


【解决方案1】:

Tensorflow 2.0 脱离会话并切换到渴望执行。如果您参考 tf.compat 库并禁用急切执行,您仍然可以使用 session 运行您的代码:

import tensorflow as tf
import tensorboard
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import tensorflow_probability as tfp
from tensorflow_model_optimization.sparsity import keras as sparsity
from tensorflow import keras


tf.compat.v1.disable_eager_execution()


tfd = tfp.distributions

init = tf.compat.v1.global_variables_initializer()

with tf.compat.v1.Session() as sess:
    sess.run(init)

    model = tf.keras.Sequential([
      tf.keras.layers.Dense(1,kernel_initializer='glorot_uniform'),
      tfp.layers.DistributionLambda(lambda t: tfd.Normal(loc=t, scale=1))
    ])

您可以使用以下方式转换任何 python 脚本:

tf_upgrade_v2 --infile in.py --outfile out.py

【讨论】:

  • 谢谢@y.selivonchyk。我使用了import tensorflow.compat.v1 as tf+ tf.disable_v2_behavior() 并像往常一样保持代码
【解决方案2】:

我相信“Session()”已被 TF 2.0 删除。

改为使用函数来绘制图形(根据 TensorFlow 文档): https://www.tensorflow.org/alpha/tutorials/eager/tf_function

类似问题的日志:https://github.com/tensorflow/community/pull/20/commits/9645a1249d3bdbe8e930af62d1958120a940c31d

【讨论】:

    【解决方案3】:

    使用这个

    init = tf.compat.v1.global_variables_initializer()
    

    如果在此之后出现错误,然后运行以下命令

    tf.compat.v1.disable_eager_execution()
    init = tf.compat.v1.global_variables_initializer()
    

    【讨论】:

      猜你喜欢
      • 2017-03-23
      • 2019-08-04
      • 1970-01-01
      • 2020-12-06
      • 2017-04-08
      • 2020-02-04
      • 2020-02-12
      • 2019-01-10
      • 2019-10-01
      相关资源
      最近更新 更多