AttributeError: module 'tensorflow' has no attribute 'contrib' - python-3.x

I am using someone else code (Code Link) which is implemented in Tensorflow1. I want to run this code into Tensorflow2 However I am getting this error:
mnist = tf.contrib.learn.datasets.load_dataset("mnist")
AttributeError: module 'tensorflow' has no attribute 'contrib'
I upgraded this code by using this instruction:
!tf_upgrade_v2 \
--infile /research/dept8/gds/anafees/MyTest.py \
--outfile /research/dept8/gds/anafees/MyTest2.py
Most things are updated, however generated report showed:
168:21: ERROR: Using member tf.contrib.distribute.MirroredStrategy in deprecated module tf.contrib. tf.contrib.distribute.MirroredStrategy cannot be converted automatically. tf.contrib will not be distributed with TensorFlow 2.0, please consider an alternative in non-contrib TensorFlow, a community-maintained repository such as tensorflow/addons, or fork the required code.
I search google; however, I could not find any suitable solution. I do not want to move back to Tensorflow1. Is there any alternate solution? Can anyone help?

Few libraries are deprecated in Tensorflow 2.x such tf.contrib, To make the code compatible with Tf 2 needs some changes in library.
Replace
tf.contrib.learn.datasets.load_dataset("mnist")
with
tf.keras.datasets.mnist.load_data()
And Replace
tf.contrib.distribute.MirroredStrategy
with
tf.distribute.MirroredStrategy(devices=None, cross_device_ops=None)

Related

AttributeError: module 'torch.utils' has no attribute 'make_grid'

I tried following along with the PyTorch transfer learning tutorial and found it pulled an error code when I brought up the make grid.
It's because it's out of date. As of 1.11.0 it has been moved to torchvision.utils.make_grid instead of torch.utils.make_grid
https://pytorch.org/vision/main/generated/torchvision.utils.make_grid.html?highlight=make_grid#torchvision.utils.make_grid

module 'torch.cuda' has no attribute 'memory_summary'

I'm trying to measure the available space on each of my GPUs using torch.cuda module. However it is returning me the following error.
module 'torch.cuda' has no attribute 'memory_summary'
My code is below
if torch.cuda.is_available():
for i in range(torch.cuda.device_count()):
print(torch.cuda.get_device_name(i))
a = torch.cuda.memory_summary(torch.device('cuda:{}'.format(i)))
print(a)
Similarly memory_stats, mem_get_info and memory_reserved all are failing.
torch.cuda.memory_summary is introduced in Pytorch 1.4.0. So if your torch install is older than that you won't be able to use it.

module 'statsmodels.tsa.api' has no attribute 'arima_model'

I'm trying to use "statsmodels.api" to work with time series data and trying to fit a simple ARIMA model using
sm.tsa.arima_model.ARIMA(dta,(4,1,1)).fit()
but I got the following error
module 'statsmodels.tsa.api' has no attribute 'arima_model'
I'm using 'statsmodels' version 0.9.0 with 'spyder' version 3.2.8 I'd be pleased to get your help thanks
The correct path is :
import statsmodels.api as sm
sm.tsa.ARIMA()
You can view it using a shell that allows autocomplete like ipython.
It is also viewable in the example provided by statsmodels such as this one.
And more informations about package structure may be found here.

module 'tensorflow' has no attribute 'placeholder'

I am using Tensorflow version 1.3.0 and Tensorboard (0.1.8), and when I am using tf.placeholder(), tf.GLOBAL_VARIABLES() or tf.global_variables_initializer, it get an error like the one in the title. I know that there is an update in the function's syntax for this version, but I can't find what is wrong.
Can anyone help me?

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

accuracy = tf.streaming_accuracy (y_pred,y_true,name='acc')
recall = tf.streaming_recall (y_pred,y_true,name='acc')
precision = tf.streaming_precision(y_pred,y_true,name='acc')
confusion = tf.confuson_matrix(Labels, y_pred,num_classes=10,dtype=tf.float32,name='conf')
For the above code, I have received the same error in past few days.
Isn't the syntax same as it is in the API documentation for tensorflow?
try to use this instead (in a fresh python file, I would suggest create a /tmp/temp.py and run that)
from tensorflow.contrib.metrics import streaming_accuracy
and if this doesn't work then
either there is an installation problem (In which case reinstall)
or you are importing the wrong tensorflow module.

Resources