ImportError: cannot import name normalize_data_format - python

I am very new to use github. I have installed github in ubuntu 16.04, I installed python 2.7.12, tensorflow 1.9 and keras. I want to use my own custom activation and optimizer in keras RNN. I searched in web and came to know i need to install keras-contrib package to use advanced activation and custom activation function.
So, I install the keras-contrib from github. But I don't know how to work with it and how to run the program using keras-contrib.
But i tried with following commands
git clone https://www.github.com/keras-team/keras-contrib.git
cd keras-contrib
python setup.py install
then I tried with this following code
from keras.models import Sequential
from keras.layers import Dense
import numpy as np
from keras_contrib.layers.advanced_activations import PELU
it showing the following error
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "keras_contrib/__init__.py", line 4, in <module>
from . import layers
File "keras_contrib/layers/__init__.py", line 3, in <module>
from .convolutional import *
File "keras_contrib/layers/convolutional.py", line 15, in <module>
from keras.utils.conv_utils import normalize_data_format
ImportError: cannot import name normalize_data_format
Anyone please check this error and help me to sort out this error.

I update the keras contribute source code installed in my linux. Follow the changes:
https://github.com/ekholabs/keras-contrib/commit/0dac2da8a19f34946448121c6b9c8535bfb22ce2
Now, it works well.

I had the same problem. I installed keras 2.2.2 version using the following command and problem solved.
pip install -q keras==2.2.2
Refer this PR.
https://github.com/keras-team/keras-contrib/pull/292

Had the same issue. The problem is that normalize_data_format function was moved to keras.backend.common from keras.utils.conv_utils in later versions of keras. You can use
import keras
and then in your code use
keras.utils.conv_utils.normalize_data_format

I found that in keras version 2.6.0 the normalize function is not lost, it is just "stored" in a file "np_utils.py", so what we need to do is just change
"
from keras.utils import normalize
to
from keras.utils.np_utils import normalize

It must be because the keras_contrib you have downloaded is not compatible with updated version of keras. Check this link https://github.com/keras-team/keras/blob/master/keras/utils/conv_utils.py
There is no function here like normalise_data_format, that is where it is throwing error.

It must be because the keras_contrib you have downloaded is not compatible with updated version of keras. Check this link https://github.com/keras-team/keras/blob/master/keras/utils/conv_utils.py
It does not work...

This bug is reported and fixed here: https://github.com/keras-team/keras-contrib/issues/291

On my Windows 10 system and in Colaboratory, using Python 3.7, I solved this problem updating Keras and installing git version of keras-contrib.
pip install -q keras==2.2.2
pip install git+https://www.github.com/keras-team/keras-contrib.git
Check your Keras version with
import keras
print(keras.__version__)

I had the same problem. I solved it by using this :
from tensorflow.keras.utils import normalize
instead of :
from keras.utils import normalize

Related

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

I am trying to run the following:
from keras.backend import theano_backend
But I get this error:
Traceback (most recent call last):
File "<ipython-input-64-39e623866e51>", line 1, in <module>
from keras.backend import theano_backend
ImportError: cannot import name 'theano_backend' from 'keras.backend' (C:\Users\Dr. Sunil Singla\anaconda3\lib\site-packages\keras\backend.py)
I cloned this repo: https://github.com/titu1994/DenseNet.git and attempted to run it on my image data.
The latest Keras versions are just a wrapper on top of tf.keras, they are not the multi-backend keras you are expecting.
For this code to work, you should downgrade Keras to a version that is still multi-backend, like 2.2.x versions. I think 2.3.x still have multiple backends too, but versions 2.4 are TensorFlow only.

How to alter gpt-2 code to work with Tensorflow 2.0?

I am trying to use gpt-2 for text generation. I get compatibility errors, even after running the Tensorflow 2.0 code upgrade script.
Steps I've followed:
Clone repo
From here on out, follow the directions in DEVELOPERS.md
Run upgrade script on files in /src
In terminal run: sudo docker build --tag gpt-2 -f Dockerfile.gpu .
After building is done, run: sudo docker run --runtime=nvidia -it gpt-2 bash
Enter python3 src/generate_unconditional_samples.py | tee /tmp/samples
Get this traceback:
Traceback (most recent call last):
File "src/generate_unconditional_samples.py", line 9, in <module>
import model, sample, encoder
File "/gpt-2/src/model.py", line 4, in <module>
from tensorboard.plugins.hparams.api import HParam
ImportError: No module named 'tensorboard.plugins.hparams'
root#f8bdde043f91:/gpt-2# python3 src/generate_unconditional_samples.py | tee
/tmp/samples
Traceback (most recent call last):
File "src/generate_unconditional_samples.py", line 9, in <module>
import model, sample, encoder
File "/gpt-2/src/model.py", line 4, in <module>
from tensorboard.plugins.hparams.api import HParam
ImportError: No module named 'tensorboard.plugins.hparams'```
It appears that HParams has been deprecated and the new version in Tensorflow 2.0 is called HParam. However, the parameters are different. In model.py, the params are instantiated as follows:
def default_hparams():
return HParams(
n_vocab=0,
n_ctx=1024,
n_embd=768,
n_head=12,
n_layer=12,
)
There doesn't appear to be any 1:1 translation into Tensorflow 2.0. Does anyone know how to make gpt-2 work with Tensorflow 2.0?
My GPU is an NVIDIA 20xx.
Thank you.
If you want to take a look at my 1.x fork it's here, compiling:
https://github.com/timschott/gpt-2
I had the same issue but got it resolved by creating a separate hparams.py file in the folder and populating it with the content from here: https://github.com/tensorflow/tensor2tensor/blob/master/tensor2tensor/utils/hparam.py
then in your model.py you can add the following code and swap out this:
import tensorflow as tf
from tensorflow.contrib.training import HParams
with this:
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
from hparams import HParams
Then you will have to add "compat.v1" inbetween "tf" and the module (if that's what it's called)... For instance if it is "tf.Session" change it to "tf.compat.v1.Session" or if it is "tf.placeholder" change it to "tf.compat.v1.placeholder", etc.
I did this after trying to downgrade to tensorflow-gpu 1.13 and gpt-2 still wasn't working. Same was the case with running the environment in 3.6 version of Python.
P.S. This is my first answer, not sure if I formatted it correctly, but will learn as I go as well.
Automatically upgrading code rarely works out of the box. With that repo, you should use Tensorflow=1.15 maximum.
If you really want Tensorflow=2, you can look at this repo: https://github.com/akanyaani/gpt-2-tensorflow2.0
Note: that you won't get the pre-trained models (probably the most interesting part of gpt2 for the average user). Meaning no access to their 1554M or 778M models.
I know of no method to automatically upgrade pre-trained models from 1.15 to 2.3 or whatnot.

AttributeError: module 'tensorflow.python.training.checkpointable' has no attribute 'CheckpointableBase'

I've been working on learning artificial intelligence and how to code with Python.I was working on a project and I decided to update some packages of Python which were not new to work on then something happened and I can't compile my codes.I deleted Anaconda3 and set it up again but not worked. I've been seeing this problem which I wrote as a topic.If someone helps me,I would be appriciated to get some help.
>>> import tensorflow as tf
File "C:\Users\AliGalip\Anaconda3Yeni\lib\site-packages\tensorflow\__init__.py", line 24, in <module>
from tensorflow.python import pywrap_tensorflow # pylint: disable=unused-import
File "C:\Users\AliGalip\Anaconda3Yeni\lib\site-packages\tensorflow\python\__init__.py", line 63, in <module>
from tensorflow.python.framework.framework_lib import * # pylint: disable=redefined-builtin
File "C:\Users\AliGalip\Anaconda3Yeni\lib\site-packages\tensorflow\python\framework\framework_lib.py", line 104, in <module>
from tensorflow.python.framework.importer import import_graph_def
File "C:\Users\AliGalip\Anaconda3Yeni\lib\site-packages\tensorflow\python\framework\importer.py", line 32, in <module>
from tensorflow.python.framework import function
File "C:\Users\AliGalip\Anaconda3Yeni\lib\site-packages\tensorflow\python\framework\function.py", line 36, in <module>
from tensorflow.python.ops import resource_variable_ops
File "C:\Users\AliGalip\Anaconda3Yeni\lib\site-packages\tensorflow\python\ops\resource_variable_ops.py", line 35, in <module>
from tensorflow.python.ops import variables
File "C:\Users\AliGalip\Anaconda3Yeni\lib\site-packages\tensorflow\python\ops\variables.py", line 40, in <module>
class Variable(checkpointable.CheckpointableBase):
AttributeError: module 'tensorflow.python.training.checkpointable' has no attribute 'CheckpointableBase'
The same question has been posted as a GitHub issue. In particular, the solution suggested by #allanlavoie is likely relevant here as well:
Sounds like a half-updated version of TensorFlow. Could you try removing TensorFlow entirely (e.g. pip uninstall tf-nightly or whichever package is installed), making sure import tensorflow fails, then reinstalling?
Since it is apparent from your question that you're using Anaconda to manage your Python environments, if the above fails to solve your problem, you can try to install TensorFlow in a clean conda environment as follows:
Create a new environment through conda create --name tftest. (You can replace tftest with e.g. the name of your current project.)
Activate that new environment through activate tftest (or source activate tftest if you happen to be using MSYS2's bash, or something similar to that).
Install TF into this environment through conda install tensorflow.
Ensure that you're in the right environment through where python (which should produce a path containing "tftest").
Run Python through python.
import tensorflow as tf in a shell in that environment.
Since you are using PyCharm (cf. the comments for this answer), you will then want to set up PyCharm for using this new environment instead. Indeed, using a new environment for every project, while disk space-intensive, is a good way of avoiding dependency issues for these rather dependency-heavy numerical packages.
This is error may be dues to the version of the tensorflow, your code is not compatible with the latest version of the tensor flow. Try installing older version of Tensorflow such as 1.14.0 or 1.7.0 it worked for me.
pip3 uninstall tensorflow
And then:
pip3 install --upgrade "tensorflow==1.15"

ImportError: cannot import name '_ccallback_c'

Initially I was getting this error (No Module name was found scipy) So I installed a Scipy wheel file. Now I don't get the same error any more but I get cannot import name "_ccallback_c".
The error seems to be triggered at the fourth line of code. I have done my research and saw that other people suggested to try an environment such as Anaconda. I have seen it work on idle, and that solution isn't ideal for me.
Traceback:
Traceback (most recent call last):
File "C:\Users\joesh\Desktop\Python\Machine Learning\1st tutorial.py", line 4, in <module>
from sklearn import preprocessing, cross_validation, svm
File "C:\Users\joesh\Desktop\Python\lib\site-packages\sklearn\__init__.py", line 134, in <module>
from .base import clone
File "C:\Users\joesh\Desktop\Python\lib\site-packages\sklearn\base.py", line 10, in <module>
from scipy import sparse
File "C:\Users\joesh\Desktop\Python\lib\site-packages\scipy\__init__.py", line 118, in <module>
from scipy._lib._ccallback import LowLevelCallable
File "C:\Users\joesh\Desktop\Python\lib\site-packages\scipy\_lib\_ccallback.py", line 1, in <module>
from . import _ccallback_c
ImportError: cannot import name '_ccallback_c'
And the code:
import pandas as pd
import quandl, math
import numpy as np
from sklearn import preprocessing, cross_validation, svm
from sklearn.linear_model import LinearRegression
I had the same error on USING Anaconda, so I am not sure if using it would make any difference.
I solved it by just uninstalling scipy and re-installing it using pip:
pip uninstall scipy
you'll get this message:
Uninstalling scipy-1.1.0: Would remove:
c:\users\thesh\appdata\local\programs\python\python36-32\lib\site-packages\scipy-1.1.0.dist-info*
c:\users\thesh\appdata\local\programs\python\python36-32\lib\site-packages\scipy*
Proceed (y/n)?
press y, and after pip is done, type:
pip install scipy
Having just moved to MSVS 2017 for Python (ML) development, I encountered this and other errors related to missing modules. The problem (and all related problems like it) has a frustratingly simple solution: when I originally started coding in Python, I installed everything from the command line - apparently, MSVS 2017 doesn't "see" those installations (and, in fact, they sometimes conflict, since the underlying python may be tapping older libs); so, the solution is to:
Use the command line version of 'pip uninstall ...' where '...' is the library having missing dependencies (scipy, in this case). Then, in the MSVS 2017 command line on the Python environment window (usually, top right in the default display configuration), reload the library (in this case, typing 'scipy' will format a command line for execution [in the list control below the command textbox]) that will read something like 'pip install scipy' (or whatever library needs to be reinstalled for MSVS).
You may have to do this for many (or all) of your previous Python package installations where these missing module errors persist.
Can be resolved, by uninstalling and reinstalling using pip on Anaconda Prompt:
pip uninstall scipy
After the uninstall, you can reinstall with:
pip install scipy
When you installed scipy with pip in a Python version 3.6 and later try to run your code with Python 3.7 you will encounter this problem. So one solution is to uninstall scipy
pip3 uninstall scipy
and reinstall it (using an environment with Python 3.7):
pip3 install scipy
This will make sure that the installed version of scipy is compatible with your version of Python.
PS: When you updated Python from Python 3.6 to Python 3.7 it might be necessary to also reinstall pip, so that pip will use the correct version of Python internally.
I ran into this when I was following these instructions on how to use a virtual environment to use the pre-built version of SciPy. The simplest solution for me was to simply comment out from . import _ccallback_c under scipy\_lib\_ccallback.py.
I first had the error with scipy. So I ran the command python -m pip install -user numpy scipy matplotlib ipython jupyter pandas sympy noseand it worked perfectly. I was installing everything with pip, so I decided to use Anaconda. I installed and checked to add to the PATH. From there, the same code that was executed before normally stopped working and displays the error similar to that of the question. I uninstalled Anaconda and it is now working again.
Erro:
$ winpty python ia.py
Traceback (most recent call last):
File "ia.py", line 11, in <module>
from sklearn import tree #importando a biblioteca e a árvore p/ o classifica
dor
File "C:\Users\ferna\Anaconda3\lib\site-packages\sklearn\__init__.py", line 13
4, in <module>
from .base import clone
File "C:\Users\ferna\Anaconda3\lib\site-packages\sklearn\base.py", line 11, in
<module>
from scipy import sparse
File "C:\Users\ferna\AppData\Roaming\Python\Python36\site-packages\scipy\__ini
t__.py", line 118, in <module>
from scipy._lib._ccallback import LowLevelCallable
File "C:\Users\ferna\AppData\Roaming\Python\Python36\site-packages\scipy\_lib\
_ccallback.py", line 1, in <module>
from . import _ccallback_c
ImportError: cannot import name '_ccallback_c'
Código:
from sklearn import tree #importando a biblioteca e a árvore p/ o classificador
#COLLLECT TRAINING DATA
features = [[140,1],[130,1],[150,0],[170,0]]
labels = [0,0,1,1]
# TRAIN CLASSIFIER
clf = tree.DecisionTreeClassifier() #Classificador
clf = clf.fit(features, labels) #algoritmo de decisão p/ encontrar padrões
#MAKE PREDICTIONS
print(clf.predict([[160, 0]])) #entrada de dados para o tratamento
Try this:
python -m pip install --upgrade scipy
After digging in, to give the full background on this, first of all SciPy relies on having NumPy already installed. The SciPy wheel's setup.py file uses NumPy functionality to configure and install the wheel.
SciPy setup.py:
...
if __name__ == '__main__':
from numpy.distutils.core import setup
setup(**configuration(top_path='').todict())
Secondly, when just trying to use the wheel, if you run into this error, you can see after inspecting the wheel's files that the reason is the binary wheels have a naming convention where the shared object file, here it's called _ccallback_c.so, is instead named based on the architecture that the binary wheel supports. When trying to import the shared object by file name in /_lib/_ccallback.py it can't find it, hence this error (line 1 in /_lib/_ccallback.py) because, instead of being named _ccallback_c.so it's called _ccallback_c.cpython-36m-x86_64-linux-gnu.so or another architecture variation:
from . import _ccallback_c
These file names seem to be an artifact of libraries that are using Cython and Cython's adherence to PEP 3149 (ABI version tagged .so files). But the easiest fix is to change the .whl extension to .zip and rename all those relevant .so files to not contain the architecture snippet. Then change .zip -> .whl and it should be good to go unless it's the wrong architecture for the platform you're using, in which case you need to download the appropriate platform wheel for your platform.

Tensorflow: AttributeError: module 'tensorflow.python' has no attribute 'control_flow_ops'

I'm using keras ver 1.0.8 and tensorflow ver 0.12.0.
I ran python image_zooms_training.py -n 0
then it throws
`AttributeError: module 'tensorflow.python' has no attribute 'control_flow_ops'
please tell me how to solve . thank you for your help.
Following import works. You need to update that line
from tensorflow.python.ops import control_flow_ops
Official TensorFlow Support discourages use of tf.python.* as it is private and intended for development purposes only. While it may work in some cases, it will "break unannounced" in many others.
Instead, try importing tensorflow with the .python portion removed, e.g.:
from tensorflow.keras.models import Sequential

Categories

Resources