Alternative to 'tensorflow.contrib' with tensorflow=2.0.0 - python

I am using TensorFlow version=2.0.0, python version=3.7.9 and I was trying to import the following:
import tensorflow.contrib.layers as layers
I get this error:
Exception has occurred: ModuleNotFoundError
No module named 'tensorflow.contrib'
As I read in this thread, tensorflow.contrib doesn't exist in 2.0. Does anyone know an alternative to it? I am trying to upgrade my code from 1.0 to 2.0 as I don't want to use tf 1.0. I am using this package to build up a RNN cell.

tf.contrib is deprecated in Tensorflow 2.x.
Replace import tensorflow.contrib.layers as layers
with
from tensorflow.keras.layers import RNN

Related

"Cannot find reference" when import from Keras

Following this example to start https://machinelearningmastery.com/tutorial-first-neural-network-python-keras/
# first neural network with keras tutorial
from numpy import loadtxt
from keras.models import Sequential
from keras.layers import Dense
Installed numpy and keras, numpy is ok, but there are red lines under "Sequential" and "Dense".
Here's the Error messages:
Cannot find reference 'Sequential' in 'models.py'
Cannot find reference 'Dense' in 'init.py'
Wonder how I can fix this? I had a look here but think it might be a different problem?
Also, on a completely different note, I can not install tensorflow for some reason? ...
As of October 2020 Tensorflow only supports the 64-bit version of Python and Tensorflow only supports Python 3.5 to 3.8.
Update your project in PyCharm to run Python 64-bit and this should solve your problem.

YOLOv3 ModuleNotFoundError: No module named 'tensorflow.python.keras.engine.base_layer_v1'

I need to train a model using YOLO. I am using the Kaggle kernel and I am training custom images.
When I execute the train module, I get this error.
ModuleNotFoundError: No module named 'tensorflow.python.keras.engine.base_layer_v1'
preinstalled is Tensorflow 2.+. I read somewhere that Yolov3 does not work with TensorFlow 2.+ so I installed Tensor 1.14.
I also tried using ImageAi and got exactly the same error.
Does anyone understand this basekayer_v1? Is it the tensorflow version that give the error?
Thanks
I found the solution
Well, it looks that I am doing things wrong or more difficult since no one encounter this error. I found the solution.
from keras.preprocessing import image
from keras.models import Model
from keras.layers import Dense, GlobalAveragePooling2D
from keras import backend as K
to:
from tensorflow.keras.preprocessing import image
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Dense, GlobalAveragePooling2D
from tensorflow.keras import backend as K
If you have the same problem.
YOLO files are outdated. They are said that they are not compatible with Tensorflow 1.14 but for me it worked. I needed to fork the git and edit the model.py. As shown above the important thing is add tensorflow.keras
I also needed to edit Change K.controlflowops.whileloop to tf.whileloop in the code or will give you an error.
I used Kaggle kernel. Using Tensorflow 2.0.2
It took me 4 days to solve this, now it is finally training. I hope it helps.

ModuleNotFoundError: No module named 'tensorflow.contrib' with tensorflow=2.0.0

I am using TensorFlow version=2.0.0
python version=3.7.3
I am trying to import the below statement
from tensorflow.contrib import rnn
And it gives error as
Module 'tensorflow' has no attribute 'contrib'
How can I resolve this?
from tensor flow
https://www.tensorflow.org/guide/upgrade#compatibility_modules
Because of TensorFlow 2.x module deprecations (for example, tf.flags and tf.contrib), some changes can not be worked around by switching to compat.v1. Upgrading this code may require using an additional library (for example, absl.flags) or switching to a package in tensorflow/addons.
and as describe in this thread
tensorflow.contrib doesn't exist in 2.0.
https://github.com/tensorflow/tensorflow/issues/31350#issuecomment-518749548
I haven't used older versions of tensorflow. Is this what you're looking for?
from tensorflow.keras.layers import RNN
Info on contrib:
https://www.tensorflow.org/guide/migrate#a_note_on_slim_contriblayers

How do I import the fft2d in tensorflow 2.0.0?

I want to use the fft2d function of tensorflow 2.0.0.
In 1.14.0, I could just do the following:
from tensorflow.signal import fft2d
In 2.0.0, I get the following error: ModuleNotFoundError: No module named 'tensorflow.signal'.
I can however do:
import tensorflow as tf
tf.signal.fft2d
However, I feel that it is a bit heavy, I would like to just use fft2d. Why can't I use the previous method anymore?
Importing signal in Tensorflow 2.0 can be done in a way given below.
from tensorflow.python.ops.signal.fft_ops import fft2d, ifft2d
For more info please refer to the following link and also the following gist.
I am closing this issue as it has been resolved.

Error when running tensorflow in virtualenv: module 'tensorflow' has no attribute 'truncated_normal'

I have the following error when running a CNN made in keras
File
"venv/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py",
line 4185, in truncated_normal
return tf.truncated_normal(shape, mean, stddev, dtype=dtype, seed=seed) AttributeError: module 'tensorflow' has no attribute
'truncated_normal'
I have already installed and reinstalled Tensorflow 2.0 several times. What could be happening?
In Tensorflow v2.0 and above, "tf.truncated_normal" replaced with "tf.random.truncated_normal"
For TewnsorFlow 2.x and keras 2.3 or the above, the following usage is more adaptable. Users do not need to downgrade both TensorFlow and Keras.
# Add the import statements
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
# Change it to the sample expression as follows.
init = tf.compat.v1.random.truncated_normal()
Cheers!
Keras 2.2.4 does not support TensorFlow 2.0 (it was released much before TF 2.0), so you can either downgrade TensorFlow to version 1.x, or upgrade Keras to version 2.3, which does support TensorFlow 2.0.

Categories