0

Last night, I made a virtual environment and the only installs I made were as follows:

conda install jupyter
conda install notebook
pip install transformers

And I was able to run huggingface transformers perfectly, but today I reactivate my conda virtual environment and I'm met with this puzzling error: Pytorch and Tensorflow not found

And this is the runtime error

Runtime error

I even tried making a new virtual environment with the same depencies and today I got these errors upon performing pip install transformers

New virtual environment errors upon pip install

And this in jupyter notebook No module named keras

Frank G
  • 49
  • 6
  • I've updated my answer with the commands you can try in your fresh environment – ClaudiaR Aug 01 '22 at 12:32
  • 2
    Generally, please consider copying and pasting the actual code/text into the question instead of screenshots. It not only makes it easier to read for everyone, but also saves a lot of storage space. – André Aug 01 '22 at 12:39
  • Thank you for informing me of this, I wasn't familiar with that convention @André – Frank G Aug 01 '22 at 13:35

1 Answers1

0

HuggingFace Transformers need to be installed in conjunction to either Tensorflow or Pytorch. From the HuggingFace installation instructions:

First you need to install one of, or both, TensorFlow 2.0 and PyTorch.

So you should choose which machine learning platform you like more and install that too in your virtual environment.

For Tensorflow either pip install tensorflow or pip install tensorflow-gpu and for Pytorch pip install torch torchvision

For example, in a fresh environment, the following instructions should make your errors disappear (I'm assuming you have a GPU):

conda install jupyter
conda install notebook
pip install tensorflow-gpu
pip install transformers
ClaudiaR
  • 3,108
  • 2
  • 13
  • 27
  • I tried that, and it worked, but when I restart my PC (and yes GPU is present), and reactivate my environment, I get this error Failed to import transformers.models.distilbert.modeling_tf_distilbert because of the following error (look up to see its traceback): No module named 'keras' – Frank G Aug 01 '22 at 12:41
  • Can you please check the Tensorflow version installed? Use `import tensorflow as tf` and then `print(tf.__version__)`. It is a bit strange that keras is missing, since from Tensorflow > 2.0, keras is installed along Tensorflow. @FrankG – ClaudiaR Aug 01 '22 at 12:49
  • @claudia is a keras installation from tensorflow actually identical to a seperate keras installation? I have a feeling that a "tensorflow-keras" has to be imported with `import tensorflow.keras` and not `import keras`, but I never tried it out. – André Aug 01 '22 at 13:01
  • It printed 2.6.0 and if it helps at all I was using python 3.9.13 in my venv @claudia – Frank G Aug 01 '22 at 13:05
  • @André yes keras and tf.keras need to be imported separately. At this point it might be worth trying to install keras, (`pip install keras`), see if this fixes it @FrankG – ClaudiaR Aug 01 '22 at 13:11
  • `pip install keras` changes the error message to `Failed to import transformers.models.distilbert.modeling_tf_distilbert because of the following error (look up to see its traceback): cannot import name 'dtensor' from 'tensorflow.compat.v2.experimental' (C:\Users\User\.conda\envs\zzz\lib\site-packages\tensorflow\_api\v2\compat\v2\experimental\__init__.py)` however, I see a potential fix that I will work on here https://stackoverflow.com/questions/72255562/cannot-import-name-dtensor-from-tensorflow-compat-v2-experimental @claudia – Frank G Aug 01 '22 at 13:33
  • 1
    @FrankG Seems to be a version mismatch, see this [related question](https://stackoverflow.com/questions/72255562/cannot-import-name-dtensor-from-tensorflow-compat-v2-experimental) – André Aug 01 '22 at 13:37
  • 1
    see also [this issue](https://github.com/huggingface/transformers/issues/11262). Someone solved it downgrading python. @FrankG – ClaudiaR Aug 01 '22 at 13:43