Tensorflow: module 'tensorflow' has no attribute 'constant' - python

I have attempted to install Tensorflow on Ubuntu 18.04 by following this installation guide from the Tensorflow website. However, after importing tensorflow in python/jupyter notebook, none of the functionality appears to be working.
For example if I run:
#import tensorflow
import tensorflow as tf
#initialize two constants
x1 = tf.constant([1,2,3,4])
x2 = tf.constant([5,6,7,8])
I get the following error:
AttributeError: module 'tensorflow' has no attribute 'constant'
Does anyone know why this attribute is unavailable? on the tensorflow website it should be still be a fully functional command.
This is my first time attempting to run tensorflow so any help on the topic would be greatly appreciated!

You have to change your folder name to something else except tensorflow.
The reason is when you use import x python first searches for a folder named x and imports __init__.py file inside it. In your case you don't have that file with predefined constant in it and above that you want the tensorflow itself!

I had the same error. After I installed tensorflow-eigen the problem is solved.

Related

I imported tensorflow as tf in notebook and run it successfully, but when I used tf.__version__ it says NameError: name 'tf' is not defined. Help me

Image here The first block ran without any errors. but second says name error tf not defined even though i have imported tensorflow as tf.
Yes, simply
import tensorflow as tf
print(tf.__version__)
Additionally, check this is installed.
Open the CMD in the administrator mode and install the libraries using pip install
pip show to check the path where it is installed.
Import the path using the following code:
import sys
sys.path.append('c:/users/admin/appdata/roaming/python/python39/site-packages')
sys.path.append('c:/python/python39/lib/site-packages')
The path will vary as in step 2.
I would suggest rolling tensorflow back to a previous stable version, in this case. It might be a bug with the current version.

Handling "AttributeError: module 'tensorflow' has no attribute 'app'" with out downgrading

The error mentioned in the title is happening due to the call
FLAGS = tf.app.flags.FLAGS
There are multiple questions related to this:
How could I solve the flags=tensorflow.app.flags error
AttributeError: module 'tensorflow' has no attribute 'app'
It can be learned that it is error caused due to the change in Tensorflow version. But, I don't want to downgrade the Tensorflow version as mentioned in almost all answers since it is causing further error in the code I want to run.
The answer provided here suggested the following solution
from absl import app
if __name__ == '__main__':
app.run(main)
But, it is not working.
Is there any way to modify mode without downgrading Tensorflow version?
you can still use version 1.x features without downgrading by adding below two lines to your code.
import tensorflow.compat.v1 as tf
tf.compat.v1.disable_v2_behavior()
FLAGS = tf.app.flags.FLAGS

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

I'm a beginner.
I'm working with python - TensorFlow '2.2.0' on python IDLE.
run_opts = tf.RunOptions(report_tensor_allocations_upon_oom = True)
I got the following error while running the previous code.:
AttributeError: module 'tensorflow' has no attribute 'RunOptions'"
however, according to example 18 from this link on the official page on Tensorflow, there's no error!
what's wrong in my case? How should I resolve this issue?
This is a compatibility issue between tensorflow 1.x and tensorflow 2.x. In other words, the syntax that you wrote works fine with tensorflow 1.x. But as you mentioned, you're using tensorflow 2.2 which is incompatible.
So, you have can solve this issue by either one of the following two options:
Uninstall tensorflow 2.2 and install tensorflow 1.15 which will save you a lot of the headache knowing that the link that you've posted is using tensorflow 1.13.1 as mentioned in the README file.
Or you can use tf.compat.v1.RunOptions instead of just tf.RunOptions.

which python and tensorflow version is used to train DeepLab v3+ using tensorflow api?

I have been getting multiple errors which are due to conflicts in the TensorFlow version installed in my system and the version used to write the code in Tensorflow API.
I am using python 3.6.7 and Tensorflow 2.0 to get started with the code https://github.com/tensorflow/models/blob/master/research/deeplab/g3doc/installation.md
But I am getting several errors :
flags = tf.app.flags
AttributeError: module 'tensorflow' has no attribute 'app.
As I am using 2.0 , I replaced tf.app.flags with tf.compat.v1.flags.
from tensorflow.contrib import slim as contrib_slim
ModuleNotFoundError: No module named 'tensorflow.contrib'
I am not able to solve the second one.
Can I get help to know which python and tensorflow version should be used to run DeepLab v3+?
You should use the Tensorflow version 1.x to run the DeepLabV3+ model because it uses a session to run and also the slim library which is based on the TensorFlow 1.x. And so your two problems can be solved as:
Do not need to replace tf.app.flags with tf.compat.v1.flags.
To run DeepLabV3+ model, you need to put deeplab and slim folder in a folder (deeplab_slim),
and export them by running following export commands from this parent folder (deeplab_slim):
export PYTHONPATH=$PYTHONPATH:`pwd`:`pwd`/slim
export PYTHONPATH=$PYTHONPATH:`pwd`:`pwd`/deeplab

tensorflow can be imported but it shows undefined when use it

I installed tensorflow (GPU version) several days ago and it worked well. However, today I imported tensorflow as import tensorflow as tf and when I try to use it such as tf.constant(), it shows NameError: name 'tf' is not defined. I'm new to tensorflow. Anyone knows the reason?
I found I have the same issue https://github.com/tensorflow/tensorflow/issues/8676
I can run my script without any error message but there is no result.

Categories

Resources