Tensorflow==2.0.0a0 - AttributeError: module 'tensorflow' has no attribute 'global_variables_initializer'
Asked Answered
M

3

11

I'm using Tensorflow==2.0.0a0 and want to run the following script:

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. However, I want to develop a notebook where I use Model Optimization (Neural net pruning) + TF Probability, which require Tensorflow > 1.13.

All libraries are imported but init = tf.global_variables_initializer() generates the error:

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

Also, tf.Session() generates the error:

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.

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 ?

Moultrie answered 17/5, 2019 at 20:13 Comment(3)
On a GitHub forum I saw this mentioned pip3 install --upgrade --force-reinstall tensorflow-gpu ... Also what version of python are you using perhaps you need to use a newer version?Spry
Since you are using tensorflow veriso 2.0.x.x , you no longer need to use tf.global_variables_initializer. Check this migration guide linkDug
Perfect, @Vishal, I accept your answer as the best. Solved the problemMoultrie
M
14

Tensorflow 2.0 goes away from session and switches to eager execution. You can still run your code using session if you refer to tf.compat library and disable eager execution:

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:

tf_upgrade_v2 --infile in.py --outfile out.py
Midinette answered 18/5, 2019 at 3:21 Comment(1)
Thanks @y.selivonchyk. I used import tensorflow.compat.v1 as tf+ tf.disable_v2_behavior() and kept code as usualMoultrie
T
1

I believe "Session()" has been removed with TF 2.0.

Instead, use Functions to graph (as per TensorFlow documentation): https://www.tensorflow.org/alpha/tutorials/eager/tf_function

Log of similar issue: https://github.com/tensorflow/community/pull/20/commits/9645a1249d3bdbe8e930af62d1958120a940c31d

Tricho answered 17/5, 2019 at 20:29 Comment(0)
T
0

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()
Treatment answered 18/4, 2021 at 9:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.