简体   繁体   English

Tensorflow==2.0.0a0 - AttributeError: 模块“tensorflow”没有属性“global_variables_initializer”

[英]Tensorflow==2.0.0a0 - AttributeError: module 'tensorflow' has no attribute 'global_variables_initializer'

I'm using Tensorflow==2.0.0a0 and want to run the following script:我正在使用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))
    ])

All my older notebooks work with TF 1.13.我所有的旧笔记本都使用 TF 1.13。 However, I want to develop a notebook where I use Model Optimization (Neural net pruning) + TF Probability, which require Tensorflow > 1.13 .但是,我想开发一个笔记本,在其中使用模型优化(神经网络修剪)+ TF 概率,这需要Tensorflow > 1.13

All libraries are imported but init = tf.global_variables_initializer() generates the error:所有库都已导入,但init = tf.global_variables_initializer()生成错误:

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

Also, tf.Session() generates the error:此外, tf.Session()生成错误:

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

So I guess it may be something related to Tensorflow itself, but I don't have older versions confliciting in my Anaconda environment.所以我想这可能与Tensorflow本身有关,但我的 Anaconda 环境中没有旧版本冲突。

Outputs for libraries' versions:库版本的输出:

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

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

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

Any ideas on this issue ?关于这个问题的任何想法?

Tensorflow 2.0 goes away from session and switches to eager execution. Tensorflow 2.0 脱离会话并切换到急切执行。 You can still run your code using session if you refer to tf.compat library and disable eager execution:如果您引用 tf.compat 库并禁用 Eager Execution,您仍然可以使用 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))
    ])

You can convert any python script in that manner using:您可以使用以下方式以这种方式转换任何 python 脚本:

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

I believe "Session()" has been removed with TF 2.0.我相信“Session()”已随 TF 2.0 删除。

Instead, use Functions to graph (as per TensorFlow documentation): https://www.tensorflow.org/alpha/tutorials/eager/tf_function相反,使用函数来绘制图形(根据 TensorFlow 文档): https : //www.tensorflow.org/alpha/tutorials/eager/tf_function

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

use this用这个

init = tf.compat.v1.global_variables_initializer()

event you get error after this then run the following事件你在这之后得到错误然后运行以下

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

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

相关问题 TensorFlow 'module' 对象没有属性 'global_variables_initializer' - TensorFlow 'module' object has no attribute 'global_variables_initializer' 在TensorFlow中调用global_variables_initializer时出错 - Error when calling global_variables_initializer in TensorFlow TensorFlow2.0.0 Alpha-模块'tensorflow'没有属性'gfile' - TensorFlow2.0.0 Alpha- module 'tensorflow' has no attribute 'gfile' Tensorflow 2.0.0: AttributeError: 'TensorSliceDataset' 对象没有属性 'as_numpy_iterator' - Tensorflow 2.0.0: AttributeError: 'TensorSliceDataset' object has no attribute 'as_numpy_iterator' AttributeError: 模块“tensorflow”没有属性“InteractiveSession” - AttributeError: module 'tensorflow' has no attribute 'InteractiveSession' AttributeError:模块“tensorflow”没有属性“app”:错误 - AttributeError: module 'tensorflow' has no attribute 'app': error AttributeError: 模块“tensorflow”没有属性“log” - AttributeError: module 'tensorflow' has no attribute 'log' Tensorflow AttributeError:'模块'对象没有属性'DNNClassifier' - Tensorflow AttributeError: 'module' object has no attribute 'DNNClassifier' AttributeError:模块“tensorflow”没有属性“io” - AttributeError: module 'tensorflow' has no attribute 'io' AttributeError: 模块“tensorflow”没有属性“value” - AttributeError: module 'tensorflow' has no attribute 'value'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM