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

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.

Related

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.

ImportError: cannot import name normalize_data_format

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

Import error with tensorflow after following all the dowload steps

I was on a robot class so i need both opencv and tensorflow, so i dowloaded both of them on window 10 64-bit. this is my code:
import cv2
import numpy as np
import tensorflow
#code here
which return this trace back:
Traceback (most recent call last):
File "C:/Users/Dell/PycharmProjects/SignClassifer/.idea/source_code.py", line 3, in <module>
import tensorflow
File "C:\Users\Dell\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\__init__.py", line 24, in <module>
from tensorflow.python import *
ImportError: No module named 'tensorflow.python'
Process finished with exit code 1
i was following those steps for downloading tensorflow with GPU for python on window. The "native" pip way: running this command:
C:\> pip3 install --upgrade tensorflow-gpu
It downloaded and installed fine. But when i try to import tensorflow this error happened, in an attempt to fix this error i also downloaded a tensorflow GPU packet with pycharm, doesnt seem to be any better, i ve looking all over the internet, doesnt seem to find anything.

Tensorflow, Python: ImportError: undefined symbol: _PyUnicode_AsWideCharString

I keep getting the following error, when using tensorflow in PyCharm:
/home/user/tensorflow/bin/python /home/user/PycharmProjects /TensorPlay/hello.py
Traceback (most recent call last):
File "/home/user/PycharmProjects/TensorPlay/hello.py", line 2, in <module>
import tensorflow as tf
File "/home/user/tensorflow/lib/python3.5/site-packages/tensorflow/__init__.py", line 24, in <module>
from tensorflow.python import *
File "/home/user/tensorflow/lib/python3.5/site-packages/tensorflow/python/__init__.py", line 27, in <module>
import ctypes
File "/usr/lib/python3.5/ctypes/__init__.py", line 7, in <module>
from _ctypes import Union, Structure, Array
ImportError: /home/user/tensorflow/lib/python3.5/lib-dynload/_ctypes.cpython-35m-x86_64-linux-gnu.so: undefined symbol: _PyUnicode_AsWideCharString
Process finished with exit code 1
hello.py is this simple example code:
import tensorflow as tf
node1 = tf.constant(3.0, tf.float32)
node2 = tf.constant(4.0)
print(node1, node2)
PyCharm detects all the Tensorflow elements and autocomplete everything i want to.
I also tried to run the virtualenv in the console.
Any Python related leads to the same error. I tried to upgrade tensorflow with
source ~/tensorflow/bin/activate
pip3 install --upgrade tensorflow
and had the exact same error too (Just instead of hello.py there was an error in file pip3)
Any suggestions?
EDIT:
I guess I see the problem. Might it be that my virtualenv wants Python 3.5.3? I thing with the last upgrade my Linux upgraded to Python 3.5.4 How can I fix it without creating a new virtualenv? And how I can make sure it doesn't happen on future updates?
I could only fix the issue with deleting the old virtualenv and setting up a new one.

ImportError: cannot import name rewriter_config_pb2

I am simply following the tutorials of tensorflow and while performing following cmd :
python ptb_word_lm.py --data_path=/home/priyankit/data/validsrc2-hi --
model=small
It is showing following error:
Traceback (most recent call last):
File "ptb_word_lm.py", line 68, in
import util
File "/home/priyankit/models-master/tutorials/rnn/ptb/util.py", line
23, in
from tensorflow.core.protobuf import rewriter_config_pb2
ImportError: cannot import name rewriter_config_pb2
link to the repository
You need to do two things. First, upgrade tensorflow by doing:
pip install --upgrade tensorflow
Second, if you are not using a GPU, then execute the script with 0 GPUs as follows:
python ptb_word_lm.py --data_path=/home/priyankit/data/validsrc2-hi --model=small --num_gpus=0
I had exactly the same error loop when I tried doing the RNN Tensorflow tutorial on their website.
What worked for me (I don't have a GPU) was including this at the end "--num_gpus=0" when running my program on the terminal.

Categories

Resources