12

I am trying to run this line of code :

var_init_1 = tf.get_variable("var_init_1", [1, 2], dtype=tf.int32,  initializer=tf.zeros_initializer)
print(var_init_1.shape)

It should give an output the shape of tensor of zeros.

But why I'm getting an error like this:

AttributeError                            Traceback (most recent call last)
<ipython-input-37-3cc73aa1818e> in <module>
----> 1 var_init_1 = tf.get_variable("var_init_1", [1, 2], dtype=tf.int32,  initializer=tf.zeros_initializer)
      2 print(var_init_1.shape)

AttributeError: module 'tensorflow' has no attribute 'get_variable'
4
  • 1
    You're probably using TF2. TF2 doesn't support get_variable() anymore (supports only for compatibility reasons tf.compat.v1.get_variable) Commented Dec 7, 2019 at 20:38
  • I've installed tenserflow using "pip3 install tenserflow", should i upgrade it? Commented Dec 8, 2019 at 3:23
  • 1
    you need to downgrade to 1.X version Commented Dec 8, 2019 at 4:06
  • thanks for your help downgrading worked !! Commented Dec 8, 2019 at 6:48

4 Answers 4

16

Replace tf.get_variable with tf.Variable.

Sign up to request clarification or add additional context in comments.

2 Comments

(which NB has different argument syntax)
This is not working for me. Not sure why it's accepted answer. Another method worked by converting v1 to v2: tf_upgrade_v2 \ --intree my_project/ \ --outtree my_project_v2/ \ --reportfile report.txt
4

tf.Variable does not work for initilizer. Use this instead of tf.compat.v1.get_variable instead of tf.Variable. This works tensorflow 2.0 and above.

Comments

3

Mentioning the Solution for the benefit of the community.

Downgrading to Tensorflow 1.X Version (1.14 or 1.15) has resolved the issue, as Tensorflow version 2.0 doesn't support get_variable().

Comments

0

I ran into this problem and other issue with tf v1 vs. tf v2. I found the best solution is proposed at this overstock link: Module 'tensorflow' has no attribute 'contrib'

$tf_upgrade_v2 \
--intree my_project/ \
--outtree my_project_v2/ \
--reportfile report.txt

Basically you just convert tf v1 project to tf v2. Most of the time it will work. You do need to inspect get_variable conversion and make sure it's what you want. Examine report.txt on how it's converted.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.