60

Hi I just installed Tensorflow on my Mac and I want to use tf.contrib.slim but when I use it I get this

import tensorflow as tf

slim = tf.contrib.slim

Error:

File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/tensorflow_estimator/python/estimator/export/export_lib.py", line 25, in from tensorflow.python.saved_model.model_utils import build_all_signature_defs ModuleNotFoundError: No module named 'tensorflow.python.saved_model.model_utils'

I don't know what to do, please help me

I use Tensorflow.13.1 and python 3.7

Trevor Reid
  • 3,310
  • 4
  • 27
  • 46
Daromogo
  • 601
  • 1
  • 5
  • 4

15 Answers15

80

For anyone who is trying some old codes from github with Tensorflow 1.x.x versions while having Tensorflow 2.0.x please note that tf.contrib no longer exist in Tensorflow 2.0.x and it's modules were moved.
Please google the name of the module without the tf.contrib part to know it's new location and thus migrating your code accordingly by correcting the import statement.

Hope this helped!

Yehdhih ANNA
  • 1,222
  • 1
  • 11
  • 22
  • Classic reply, but IMHO not so helpful, at least in my case. For some GPT-2 model I have an error in: from tensorflow.contrib.training import HParams ModuleNotFoundError: No module named 'tensorflow.contrib' Should I google for "tensorflow" :) ? (I think I need to upgrade the code, since I am on apple silicon w/ tf. __version__, '2.11.0'). – Nuno Mar 17 '23 at 09:44
50

first unistall tensorflow

pip uninstall tensorflow

then install 1.13.2 version

pip install tensorflow==1.13.2

it works.. had the same issue.. but installing tensorflow 1.13.2 solved it!
the newer version of tensorflow doesn't have

PemaGrg
  • 722
  • 7
  • 5
  • Thanks, but want to mention that `tf.contrib` also exists in version 1.14. [link](https://github.com/tensorflow/tensorflow/issues/32229#issuecomment-528459764) – weiwen Feb 21 '20 at 06:15
  • 14
    i tried it but it says ERROR: No matching distribution found for tensorflow==1.13.2 – Raj Singh Oct 22 '20 at 05:41
  • Same. Why can't we install old versions? – pete Jan 14 '22 at 02:37
  • Because of your python version. For example, python 3.8 requires a version of tensorflow 2.2 or more. See pip system requirements for tensorflow [here](https://www.tensorflow.org/install/pip#system-requirements) – Miguel Nov 18 '22 at 16:17
19

first:

pip install --upgrade tf_slim

then:

import tf_slim as slim
Roshin Raphel
  • 2,612
  • 4
  • 22
  • 40
Sudhanshu
  • 199
  • 1
  • 3
10

If the following command doesn't work

pip3 install tensorflow==1.14.0 

then we can try the following command

pip3 install https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-1.14.0-py3-none-any.whl
Siwei Luo
  • 189
  • 2
  • 6
7

I solved this by below method.

  pip uninstall tensorflow_estimator
  pip install tensorflow_estimator

the reference is : https://github.com/tensorflow/tensorflow/issues/27079

Jasper Chih
  • 107
  • 2
7

Currently the default install of tensorflow is 2.x while your code is for 1.x. The contrib module has been removed from tf 2.x. Check the warnings:

"The TensorFlow contrib module will not be included in TensorFlow 2.0"

Uninstall tensorflow and then install the 1.x version with

pip install tensorflow==1.15
Ganesh Krishnan
  • 7,155
  • 2
  • 44
  • 52
  • Works nicely. Sidenote: when I ran the command I got three lines like this `ERROR: tensorflow-cpu 2.2.0 has requirement gast==0.3.3, but you'll have gast 0.2.2 which is incompatible.` not sure why since I am removing the 2.x version. – Axel Bregnsbo Jun 08 '20 at 14:54
  • 6
    ERROR: No matching distribution found for tensorflow==1.15 – pete Jan 14 '22 at 02:28
3

tf.contrib has moved out of TF starting TF 2.0 alpha.

You can upgrade your TF 1.x code to TF 2.x using the tf_upgrade_v2 script https://www.tensorflow.org/alpha/guide/upgrade

Mayur Deshmukh
  • 394
  • 2
  • 4
2

As suggested in the Migration Guide from TensorFlow 1.x to TensorFlow 2 page, install TF-Slim, available on Google-research Github page and then use

import tf_slim as slim

instead of tf.contrib.layers

1

I solved this using following steps:

First i do check current version of my tf using

import tensorflow
print(tensorflow.__version__)
2.5.0

Then contrib is part of older version of tensorflow as it removed from 2.X.

so we need to use tf version 1.X

that will be done using following snippet

%tensorflow_version 1.x
import tensorflow
print(tensorflow.__version__)

and you will get output

**TensorFlow 1.x selected.
1.15.2**

now you can use

from tensorflow.contrib import seq2seq
from tensorflow.contrib.rnn import DropoutWrapper
cavalcantelucas
  • 1,362
  • 3
  • 12
  • 34
1

Editing the code may be a bit tedious but if you are willing to make the effort - just add this in all tensorflow links

compat.v1

so tf.assign -> tf.compat.v1.assign

etc

1

Contrib has been taken from tensorflow in 2.x version we have to switch to an older version.

If you are using in collab you can directly switch from 2.x version to 1.x with this command

%tensorflow_version 1.x

Doing this solved the problem.

Apart from collab you can just uninstall your current version and install 1.15.2 with the following commands.

pip uninstall tensorflow

pip install tensorflow==1.15.2

iso42
  • 21
  • 1
  • 7
0

As mentioned in the answers, tensorflow.contrib is not supported in tensorflow 2. I fixed the problem with creating an environment in conda with its python version set to 3.7 and setting tensorflow version to 1.14. You may face a one or two bugs related to package compatibility but eventually it'll work.

Samadi
  • 41
  • 6
0

I think majority of solution is suggesting to downgrade tensorflow version. At first place why does TF 2 does not support WALS factorization?One blog suggest its because official recommendataion is NCF Matrix Factorization in tensorflow 2.0 using WALS Method.

sakeesh
  • 919
  • 1
  • 10
  • 24
-4

Starting from tensorflow 13.1 there's no contrib. You can use without it slim = tf.slim or you can install pip install tensorflow==1.13 and use it

ElSheikh
  • 321
  • 6
  • 28
zcseho
  • 13
-5

for running it on python3 I used pip3 to install

pip3 install tensorflow

This worked for me

Rich Rajah
  • 2,256
  • 1
  • 12
  • 14