Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I get AttributeError: module 'tensorflow' has no attribute 'placeholder'?

Tags:

tensorflow

I was able to run my python program three weeks ago but now every time I try to run it, I get the following error:

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

I have tensorflow installed (version '2.0.0-alpha0'). I have read a couple of posts related to this issue. They say I should uninstall TensorFlow and re-install it again. The problem is that I am running this on a cluster computer and I do not have sudo permissions.

Any idea?

like image 716
user11400799 Avatar asked May 20 '19 18:05

user11400799


People also ask

Which TensorFlow version has placeholder?

The problem is with TensorFlow version; the one you are running is 2.0 or something above 1.5 , while placeholder can only work with 1.4 .

What is placeholder in TensorFlow?

A placeholder is simply a variable that we will assign data to at a later date. It allows us to create our operations and build our computation graph, without needing the data. In TensorFlow terminology, we then feed data into the graph through these placeholders.

What is TF compat v1?

tf. compat allows you to write code that works both in TensorFlow 1.


2 Answers

After including the tensorflow compat v1 libraries:

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

use the v1 syntax like this:

X = tf.compat.v1.placeholder(dtype="float",shape=[None, n_H0, n_W0, n_C0])
Y = tf.compat.v1.placeholder(dtype="float",shape=[None, n_y]) 
like image 158
mruanova Avatar answered Oct 21 '22 00:10

mruanova


In addition to the @Vishnuvardhan Janapati's answer, you can update folders ("*TREE") and/or files to version 2 of TensorFlow. The upgrade tool tf_upgrade_v2 is automatically included in TensorFlow 1.13 and later.

tf_upgrade_v2 [-h] [--infile INPUT_FILE] [--outfile OUTPUT_FILE]
                   [--intree INPUT_TREE] [--outtree OUTPUT_TREE]
                   [--copyotherfiles COPY_OTHER_FILES] [--inplace]
                   [--reportfile REPORT_FILENAME] [--mode {DEFAULT,SAFETY}]
                   [--print_all]

An illustration of how the conversion fixed the "placeholder" error:

enter image description here

Note: this fixes similar complaints module 'tensorflow' has no attribute 'xxxxx' (not just the "placeholder").

like image 28
NellieK Avatar answered Oct 20 '22 23:10

NellieK