minimaxir / textgenrnn

Easily train your own text-generating neural network of any size and complexity on any text dataset with a few lines of code.
Other
4.94k stars 752 forks source link

Import error: keras.backend.tensorflow_backend #197

Open mtib opened 4 years ago

mtib commented 4 years ago

When trying to run:

from textgenrnn import textgenrnn

I get

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "venv/env/lib/python3.7/site-packages/textgenrnn/__init__.py", line 1, in <module>
    from .textgenrnn import textgenrnn
  File "venv/env/lib/python3.7/site-packages/textgenrnn/textgenrnn.py", line 14, in <module>
    from keras.backend.tensorflow_backend import set_session
ModuleNotFoundError: No module named 'keras.backend.tensorflow_backend'; 'keras.backend' is not a package

I've tested in python 3.8.2 and python 3.7.7 under Ubuntu 20.04 after running pip3 install in a virtualenv for textgenrnn, keras and tensorflow.

The only hint I have to go off is the following error in the pip3 install for textgenrnn:

Successfully built textgenrnn pyyaml wrapt absl-py termcolor
ERROR: tensorflow 2.2.0 has requirement scipy==1.4.1; python_version >= "3", but you'll have scipy 1.5.0 which is incompatible.
Installing collected packages: numpy, six, h5py, scipy, joblib, threadpoolctl, scikit-learn, tqdm, pyyaml, keras, protobuf, zipp, importlib-metadata, markdown, tensorboard-plugin-wit, absl-py, certifi, urllib3, chardet, idna, requests, pyasn1, rsa, pyasn1-modules, cachetools, google-auth, oauthlib, requests-oauthlib, google-auth-oauthlib, grpcio, werkzeug, tensorboard, astunparse, keras-preprocessing, tensorflow-estimator, opt-einsum, wrapt, google-pasta, termcolor, gast, tensorflow, textgenrnn
Successfully installed absl-py-0.9.0 astunparse-1.6.3 cachetools-4.1.0 certifi-2020.6.20 chardet-3.0.4 gast-0.3.3 google-auth-1.18.0 google-auth-oauthlib-0.4.1 google-pasta-0.2.0 grpcio-1.30.0 h5py-2.10.0 idna-2.9 importlib-metadata-1.6.1 joblib-0.15.1 keras-2.4.3 keras-preprocessing-1.1.2 markdown-3.2.2 numpy-1.19.0 oauthlib-3.1.0 opt-einsum-3.2.1 protobuf-3.12.2 pyasn1-0.4.8 pyasn1-modules-0.2.8 pyyaml-5.3.1 requests-2.24.0 requests-oauthlib-1.3.0 rsa-4.6 scikit-learn-0.23.1 scipy-1.5.0 six-1.15.0 tensorboard-2.2.2 tensorboard-plugin-wit-1.6.0.post3 tensorflow-2.2.0 tensorflow-estimator-2.2.0 termcolor-1.1.0 textgenrnn-2.0.0 threadpoolctl-2.1.0 tqdm-4.46.1 urllib3-1.25.9 werkzeug-1.0.1 wrapt-1.12.1 zipp-3.1.0
jhwblender commented 4 years ago

I had this issue too 4 days after you did. I was able to resolve it by going to textgenrnn.py located in the python installation folder\Lib\site-packages\textgenrnn I changed line 14 from from keras.backend.tensorflow_backend import set_session to from tensorflow.python.keras.backend import set_session.

I think this is possibly due to TensorFlow changing things up.

jkatofsky commented 4 years ago

Made a PR to fix, since it may not always be easy to tweak the local installation of textgenrnn, for example when using this module on a cloud service and modules get installed programmatically based on config files.

ShadiestGoat commented 4 years ago

wait so what was this issue's solution, if using a cloud service? (I cant access the package files)

jkatofsky commented 4 years ago

@ShadiestGoat now both the master branch and the TF 2.0 branch are not using the outdated import. Not sure about the status of the PyPi package, but you can always point pip directly to the GitHub repo.

hakanu commented 4 years ago

I think pypi package also needs to be updated. Package there was published on 2nd of feb, keras fix was issued on 11th of feb.

For whom coming here through pip install textgenrnn, I recommend this (from clean virtualenv without any keras version is installed):

git clone https://github.com/minimaxir/textgenrnn.git
cd textgenrnn
python setup.py install
jackk25 commented 4 years ago

Any update on the status of the PyPi package?

fernaoguerra commented 3 years ago

Did you guys manage to get this right? I am using: python3.8 tensorflow=2.2.0 And installed textgenrnn with pip3 install textgenrnn I still get: No module named 'keras.backend.tensorflow_backend'; 'keras.backend' is not a package

goshawk22 commented 3 years ago

Try installing the keras package as well

fernaoguerra commented 3 years ago

I just did and did not work :( I managed to get it working locally by doing this:

git clone https://github.com/minimaxir/textgenrnn.git cd textgenrnn python setup.py install

The problem is that I want to deploy on heroku and heroku reads the requirements.txt file and use pip to install, then we are back to the problem... Any thoughts?

elitejake commented 3 years ago

Any update on this?

thtroyer commented 3 years ago

Until a new pypi package is released, I have a janky solution that worked for me to be able to install it with pip (defined in requirements.txt).

pip allows you to define dependencies that point to to git repositories. This is the line that I put in my requirements.txt:

git+git://github.com/minimaxir/textgenrnn@master#egg=textgenrnn

You should also be able to use a different branch or a commit hash instead of master. Also, the #egg portion seems to tell pip what the name of the dependency is.

Obviously, this is a workaround until a new pypi package is published. Apologies if there's any misinformation above as I'm still a python newbie.

sujaykadam commented 3 years ago

I had this issue too 4 days after you did. I was able to resolve it by going to textgenrnn.py located in the python installation folder\Lib\site-packages\textgenrnn I changed line 14 from from keras.backend.tensorflow_backend import set_session to from tensorflow.python.keras.backend import set_session.

I think this is possibly due to TensorFlow changing things up.

You the MVP dogg