Transformers AutoModelForCasualLM cannot be imported - python

I am trying to follow this article to use the AutoModelForCasualLM from transformers to generate text with bloom. But I keep getting an error saying that python cannot AutoModelForCasualLM from transformers. I have tried multiple computers and multiple versions of transformers but I always get the following error. (Traceback from latest version of transformers)
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
Cell In[28], line 1
----> 1 from transformers import AutoTokenizer, AutoModelForCasualLM, BloomConfig
2 from transformers.models.lboom.modeling_bloom import BloomBlock, build_alibi_tensor
ImportError: cannot import name 'AutoModelForCasualLM' from 'transformers' (/mnt/MLDr/venv/lib/python3.10/site-packages/transformers/__init__.py)
code snippet from where the error occurs (first ~10 lines):
import os
import torch
import torch.nn as nn
from collections import OrderedDict
def get_state_dict(shard_num, prefix=None):
d = torch.load(os.path.join(model_path, f"pytorch_model_{shard_num:05d}-of-00072.bin"))
return d if prefix is None else OrderedDict((k.replace(prefix, ''), v) for k, v in d.items())
from transformers import AutoTokenizer, AutoModelForCasualLM, BloomConfig
from transformers.models.lboom.modeling_bloom import BloomBlock, build_alibi_tensor
model = "./bloom"
config = BloomConfig.from_pretrained(model_path)
device = 'cpu'
transformers-cli env results:
transformers version: 4.25.1
Platform: Linux-5.15.0-58-generic-x86_64-with-glibc2.35
Python version: 3.10.6
Huggingface_hub version: 0.11.1
PyTorch version (GPU?): 1.13.1+cu117 (False)
Tensorflow version (GPU?): 2.11.0 (False)
Flax version (CPU?/GPU?/TPU?): not installed (NA)
Jax version: not installed
JaxLib version: not installed
Using GPU in script?: <fill in>
Using distributed or parallel set-up in script?: <fill in>

Related

What do I do wrong when install OpenKiwi?

I tried to work with OpenKiwi to Anaconda3
and after installation (pip install openkiwi)
I execute following code
(I do this because I want to create openkiwi vocabulary) :
import warnings
from collections import defaultdict
import torchtext
from kiwi.constants import PAD, START, STOP, UNALIGNED, UNK, UNK_ID
And than I have an error message:
ImportError Traceback (most recent call last)
<ipython-input-6-ea850b280bef> in <module>
4 import torchtext
5
----> 6 from kiwi.constants import PAD, START, STOP, UNALIGNED, UNK, UNK_ID
ImportError: cannot import name 'UNK_ID' from 'kiwi.constants' (C:\Users\Mike\anaconda3\lib\site-packages\kiwi\constants.py)
Anaconda3 has following versions: >!
pytorch-lightning: 1.7.6
pytorch-nlp: 0.5.0
torch: 1.4.0
torch metrics 0.9.3
torch: text 0.13.1
transformers: 3.5.1
UNK_ID is no longer a constant in the latest version of OpenKiwi. That second link shows some code targeting version 1.4 or so, while OpenKiwi is at version 2.1 now. Just drop the UNK_ID from the import line, and replace the one in the code with 0.

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

im trying to import categorical_dqn
when i try the following
from tf_agents.agents.categorical_dqn import categorical_dqn_agent
i get
ImportError: cannot import name 'binary_weighted_focal_crossentropy' from 'keras.backend' (C:\Users\tgmjack\anaconda3\lib\site-packages\keras\backend.py)
the advice i find around the internet Error importing binary_weighted_focal_crossentropy from keras backend: Cannot import name is to try importing this stuff first
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras.metrics import binary_focal_crossentropy
i end up with the exact same error caused by the second line of this suggestion however.
ImportError: cannot import name 'binary_weighted_focal_crossentropy' from 'keras.backend' (C:\Users\tgmjack\anaconda3\lib\site-packages\keras\backend.py)
####### bonus info ########
im running all this on anaconda
tensorflow version = 2.9.2
tf agents version = 0.5.0
keras version = 2.9.0
im trying to follow this tutorial = https://github.com/tensorflow/agents/blob/master/docs/tutorials/9_c51_tutorial.ipynb
I had a similar problem with tf_agents a few months ago. Doing this fixed it for me:
pip install tf-agents[reverb]
I have the following packages with their respective versions:
keras 2.9.0
tensorflow 2.9.2
tf-agents 0.13.0

ModuleNotFoundError: No module named 'keras.backend.tensorflow_backend'

I am trying to run the code
import keras
And I am getting this stack trace.
I have tried reinstalling keras and tensorflow but nothing in working.
Here is the stack trace.
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-10-88d96843a926> in <module>
----> 1 import keras
~\Anaconda3\lib\site-packages\keras\__init__.py in <module>
1 from __future__ import absolute_import
2
----> 3 from . import utils
4 from . import activations
5 from . import applications
~\Anaconda3\lib\site-packages\keras\utils\__init__.py in <module>
4 from . import data_utils
5 from . import io_utils
----> 6 from . import conv_utils
7 from . import losses_utils
8 from . import metrics_utils
~\Anaconda3\lib\site-packages\keras\utils\conv_utils.py in <module>
7 from six.moves import range
8 import numpy as np
----> 9 from .. import backend as K
10
11
~\Anaconda3\lib\site-packages\keras\backend\__init__.py in <module>
----> 1 from .load_backend import epsilon
2 from .load_backend import set_epsilon
3 from .load_backend import floatx
4 from .load_backend import set_floatx
5 from .load_backend import cast_to_floatx
~\Anaconda3\lib\site-packages\keras\backend\load_backend.py in <module>
88 elif _BACKEND == 'tensorflow':
89 sys.stderr.write('Using TensorFlow backend.\n')
---> 90 from .tensorflow_backend import *
91 else:
92 # Try and load external backend.
ModuleNotFoundError: No module named 'keras.backend.tensorflow_backend'
Try:
pip install tensorflow==2.2.0
and then
pip install Keras==2.2.0
This worked for me with Python 3.7.
instead of use something like
from keras.backend.tensorflow_backend import set_session
Try to use it like
from keras.backend import set_session
In Tensorflow 2.0.0+ versions you should just put "compat.v1" after tf and dont use "tensorflow_backend" name. Like this:
tf.keras.backend.tensorflow_backend.set_session() -> tf.compat.v1.keras.backend.set_session()
I tried to use anaconda or pip to install tensorflow and keras, and each method met the same problem.
At last I found the problem is because the version of tensorflow or keras. When I install tensorflow==2.2 and keras==2.4.3(latest), no matter which tools I used I will meet this problem.When I install tensorflow==1.14 and keras==2.2, the code works well.
My python version is 3.5.2 under ubuntu 16.04
Just install tensorflow 2.1.0 or 2.2.0 It already has Keras inside. Dont mix using pip and conda. Carry on with what you have started.
pip install tensorflow==2.2.0
or,
conda install tensorflow==2.2.0
Uninstall Keras and reinstall the version 2.2.0 in your system, it will definately work with Tensorflow 2.2. Then you won't have to downgrade you tensorflow ie. less pain of changing codes ;)
pip uninstall keras
pip install Keras==2.2.0
For my case, I had Python 3.7(latest bug fix)
for tensorflow==2.4.1 this works:
from tensorflow.python.keras.backend import set_session
In my case, it was solved by installing a given specific version of Keras.
pip install Keras==2.2.4

Getting numpy error when using azureml-sdk

hy I followed the installation instructions here
and installed with
pip install --upgrade azureml-sdk[notebooks,automl] azureml-dataprep --ignore-installed PyYAML
It seem to work but a simple
import azureml.core
azureml.core.VERSION
Throws me a numpy error
> AttributeError Traceback (most recent call
> last) <ipython-input-3-08b704cd5542> in <module>
> ----> 1 import azureml.core
> 2 azureml.core.VERSION
c:\users\werth\appdata\local\continuum\anaconda3\envs\azuresdk\lib\site-packages\azureml\core\__init__.py in <module>
4
5 """Setup file for core package."""
----> 6 from azureml.core.workspace import Workspace
7 from azureml.core.experiment import Experiment
8 from azureml.core.runconfig import RunConfiguration
... I did not include the total traceback as it is apparently a Azure import problem.
AttributeError: type object 'numpy.ndarray' has no attribute '__array_function__'
It seems that the workspace has a problem. But I cannot think why. The Notebook is in a subfolder of the working directory. Numpy is installed.
If you would have an idea I would be thankfull.
Hi greatest Floridaman,
the answer is simple. During the installation of the azureml-train-automl 1.0.8 package, the numpy package needs to be maximum at version 1.15.0
So just downgrade numpy to that version
conda install numpy=1.15.0

Import skflow on a Windows machine

I am trying to import skflow on my Windows PC. I have already install and used Anaconda on Python (3.5). I have no trouble to use tensorflow but when I want to use skflow I get the error:
ImportError Traceback (most recent call last)
<ipython-input-1-04faecc7c0de> in <module>()
8 import tensorflow as tf
9 from tensorflow.contrib import learn
---> 10 from tensorflow.contrib import skflow
ImportError: cannot import name 'skflow'
Does anyone know how to fix this?
Thanks in advance!
Please try this:
>>> import tensorflow as tf
>>> from tensorflow.contrib import learn as skflow
FYI, skflow is not any special package/module/... It's an alias for tensorflow.contrib.learn which is in 'sklearn' style.

Categories

Resources