AttributeError: module 'tensorflow' has no attribute 'feature_column' - python

So I am new to machine learning and was trying out the TensorFlow Linear Model Tutorial given here:
https://www.tensorflow.org/tutorials/wide
I literally just downloaded their tutorial and tried to run it in my computer but I got the error:
AttributeError: module 'tensorflow' has no attribute 'feature_column'
I searched online and got to know that this can happen on older versions of tensorflow, but I am running the latest version: 1.3.0
So why am I getting this error and how to fix it?

Tensorflow 1.3 should support feature_column well. You might accidentally used an old version. Try the following code to verify your version:
import tensorflow as tf
print(tf.__version__)
print(dir(tf.feature_column))

If you're importing Tensorflow in a project that uses Keras, import Keras modules first, then Tensorflow. That solved the problem for me.
Do this: (notice the order)
from keras.backend.tensorflow_backend import set_session
from keras.models import Sequential
from keras import applications
import tensorflow as tf
Do not do this:
import tensorflow as tf
from keras.backend.tensorflow_backend import set_session
from keras.models import Sequential
from keras import applications

Upgrading your tensorflow might help.
pip install --upgrade tensorflow

I faced a similar error while running a session using the Tensorflow 2.0 beta. I used the following form for running a session:
import tensorflow as tf
constant = tf.constant([[1, 2, 3],[4, 5, 6],[7, 8, 9]])
with tf.compat.v1.Session() as sess:
print(sess.run(constant))
instead of:
import tensorflow as tf
constant = tf.constant([[1, 2, 3],[4, 5, 6],[7, 8, 9]])
with tf.Session() as sess:
print(sess.run(constant))
Also,
tf.compat.v1.Session()
is backward compatible. You might face a similar error when using other functions in Tensorflow 2.0 beta like print, get_variable, etc. Use a similar form as shown above in the example.

Related

cannot import name 'binary_weighted_focal_crossentropy' from 'keras.backend'

im trying to import categorical_dqn
when i try the following
from tf_agents.agents.categorical_dqn import categorical_dqn_agent
i get
ImportError: cannot import name 'binary_weighted_focal_crossentropy' from 'keras.backend' (C:\Users\tgmjack\anaconda3\lib\site-packages\keras\backend.py)
the advice i find around the internet Error importing binary_weighted_focal_crossentropy from keras backend: Cannot import name is to try importing this stuff first
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras.metrics import binary_focal_crossentropy
i end up with the exact same error caused by the second line of this suggestion however.
ImportError: cannot import name 'binary_weighted_focal_crossentropy' from 'keras.backend' (C:\Users\tgmjack\anaconda3\lib\site-packages\keras\backend.py)
####### bonus info ########
im running all this on anaconda
tensorflow version = 2.9.2
tf agents version = 0.5.0
keras version = 2.9.0
im trying to follow this tutorial = https://github.com/tensorflow/agents/blob/master/docs/tutorials/9_c51_tutorial.ipynb
I had a similar problem with tf_agents a few months ago. Doing this fixed it for me:
pip install tf-agents[reverb]
I have the following packages with their respective versions:
keras 2.9.0
tensorflow 2.9.2
tf-agents 0.13.0

ImportError: cannot import name 'dtensor' from 'tensorflow.compat.v2.experimental' import Keras

I'm getting the following error while importing Keras:
ImportError: cannot import name 'dtensor' from 'tensorflow.compat.v2.experimental' (C:\Users\User\AppData\Roaming\Python\Python38\site-packages\tensorflow\_api\v2\compat\v2\experimental\__init__.py)
Tensorflow v. 2.6, Keras v. 2.6
Does anyone have an idea how to solve this error?
DTensor is part of the TensorFlow 2.9.0 release. To import dtensor you can upgrade tensorflow 2.6 to tensorflow 2.9 as follows:
pip install --upgrade tensorflow==2.9
Now, you can import dtensor either from tensorflow.experimental or from tensorflow.keras as follows:
#Using tensorflow.experimental
from tensorflow.experimental import dtensor
#Using tensorflow.keras
from tensorflow.keras import dtensor
For more information, please refer to this guide. Thank you.

TF 1 can't use tf.Tensor as bool condition

I am learning TF 2, but for some reason, I need to use TF 1 in a project.
However, there was a problem coming out when I used tensor as bool condition in if.
For simply demonstrating, I will use the following example.
import tensorflow as tf
a = tf.constant(2.0)
if a :
print('hi')
And the error:
using a `tf.Tensor` as a Python `bool` is not allowed in Graph execution. Use Eager execution or decorate this function with #tf.function.
I want the type to actually be float, do you have any idea?
As traceback error shows, tf.Tensor as a Python bool is not allowed in Graph execution`.
To execute this code you may need to upgrade the Tensorflow into TF 2.x (eager mode) by using below command:
!pip install --upgrade tensorflow
!pip install tensorflow==2.1.0 # to install specific TF version
This code ran successfully in latest TF version.
import tensorflow as tf
print(tf.__version__)
import tensorflow as tf
a = tf.constant(2.0)
if a :
print('hi')
print(a)
Output:
2.7.0
hi
tf.Tensor(2.0, shape=(), dtype=float32)

Keras Import Error when loading a pre trained model

I am trying to use a pre-trained model in Tensorflow. I am using the following code:
import tensorflow as tf
from tensorflow import keras
from keras.applications import mobilenet_v2
I get the following error:
ModuleNotFoundError: No module named 'keras'
However, the following codes do work:
from tensorflow.keras.applications import mobilenet_v2
OR
from keras_applications import mobilenet_v2
The 2 methods mentioned above work but the 1st one doesn't. Why does this happen?
I've solved this problem by downgrading tensorflow to version 2.0 using this command:
pip install tensorflow==2.0
I hope it helps you.

Seq2Seq Training helper module in tensorflow

In the latest tf 2.0 update tensorflow removed the contrib module from the framework. Thought they have compensated for most of the function from it in tf.compat.v1 but I couldn't find the substitute for the functions such as tf.contrib.seq2seq.BasicDecoder , tf.contrib.seq2seq.dynamic_decode , tf.contrib.seq2seq.GreedyEmbeddingHelper and tf.contrib.seq2seq.TrainingHelper.
How to use these functions in my model?
Check out TensorFlow-Addons:
https://github.com/tensorflow/addons,
https://www.tensorflow.org/addons/api_docs/python/tfa/seq2seq?hl=en
the functions you are looking for are all there.
pip install tensorflow-addons
Then:
import tensorflow as tf
import tensorflow_addons as tfa

Categories

Resources