简体   繁体   中英

Why can I not import Tensorflow.contrib I get an error of No module named 'tensorflow.python.saved

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

For anyone who is trying some old codes from github with Tensorflow 1.xx 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!

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

first:

pip install --upgrade tf_slim

then:

import tf_slim as slim

I solved this by below method.

  pip uninstall tensorflow_estimator
  pip install tensorflow_estimator

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

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

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

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

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

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

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.

In GoogleColab try:

%tensorflow_version 1.x

and

!pip install q keras==2.0.5

then restart run time.

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 .

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

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

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

for running it on python3 I used pip3 to install

pip3 install tensorflow

This worked for me

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM