AttributeError: partially initialized module 'torch' has no attribute 'autograd' (most likely due to a circular import) - torch

I'm trying to run code for sentiment analysis of a file using python flair. However it gives me AttributeError in torch on ubutnu 22.04. Traceback:
Traceback (most recent call last):
File "/home/ayush/myproj/vaahan/scrape/sentimentF.py", line 9, in <module>
from flair.models import TextClassifier
File "/home/ayush/.local/lib/python3.10/site-packages/flair/__init__.py", line 2, in <module>
import torch
File "/home/ayush/.local/lib/python3.10/site-packages/torch/__init__.py", line 759, in <module>
from .serialization import save, load
File "/home/ayush/.local/lib/python3.10/site-packages/torch/serialization.py", line 21, in <module>
from transformers.models.bert.modeling_bert import BertModel,BertForMaskedLM
File "/home/ayush/.local/lib/python3.10/site-packages/transformers/models/bert/modeling_bert.py", line 26, in <module>
import torch.utils.checkpoint
File "/home/ayush/.local/lib/python3.10/site-packages/torch/utils/checkpoint.py", line 55, in <module>
class CheckpointFunction(torch.autograd.Function):
AttributeError: partially initialized module 'torch' has no attribute 'autograd' (most likely due to a circular import)
this is my code:
from flair.models import TextClassifier
from flair.data import Sentence
sia = TextClassifier.load('en-sentiment')
def getSentiment3():
with open("/home/ayush/myproj/vaahan/scrape/scrapeListFile.txt","r") as f:
content = f.read()
flair_analysis = Sentence(content)
sia.predict(flair_analysis)
score = flair_analysis.labels[0]
return score
print(getSentiment3())

The error got removed with removing and adding torch
pip uninstall torch
pip install torch

Related

Using Camembert pre-trained model with DeepPavlov

I'm learning how to use DeepPavlov and can't figure how to use it for NER using the Camembert (french) pre-trained model. My goal is to tag a short paragraph in french.
The docs from deeppavlov explicitly list the Camembert model as a viable transformer architecture. I tried to follow as best as I could but I keep getting this error when I try to build the model.
>>> ner_model = build_model('ner_ontonotes_bert_mult')
/home/philippe/.local/lib/python3.10/site-packages/torch/nn/init.py:405: UserWarning: Initializing zero-element tensors is a no-op
warnings.warn("Initializing zero-element tensors is a no-op")
Some weights of the model checkpoint at camembert-base were not used when initializing CamembertForTokenClassification: ['lm_head.dense.weight', 'lm_head.bias', 'lm_head.layer_norm.bias', 'lm_head.layer_norm.weight', 'lm_head.decoder.weight', 'lm_head.dense.bias']
- This IS expected if you are initializing CamembertForTokenClassification from the checkpoint of a model trained on another task or with another architecture (e.g. initializing a BertForSequenceClassification model from a BertForPreTraining model).
- This IS NOT expected if you are initializing CamembertForTokenClassification from the checkpoint of a model that you expect to be exactly identical (initializing a BertForSequenceClassification model from a BertForSequenceClassification model).
Some weights of CamembertForTokenClassification were not initialized from the model checkpoint at camembert-base and are newly initialized: ['classifier.weight', 'classifier.bias']
You should probably TRAIN this model on a down-stream task to be able to use it for predictions and inference.
2023-01-29 09:58:13.308 ERROR in 'deeppavlov.core.common.params'['params'] at line 108: Exception in <class 'deeppavlov.models.torch_bert.torch_transformers_sequence_tagger.TorchTransformersSequenceTagger'>
Traceback (most recent call last):
File "/home/philippe/.local/lib/python3.10/site-packages/deeppavlov/core/common/params.py", line 102, in from_params
component = obj(**dict(config_params, **kwargs))
File "/home/philippe/.local/lib/python3.10/site-packages/deeppavlov/models/torch_bert/torch_transformers_sequence_tagger.py", line 182, in __init__
super().__init__(optimizer=optimizer,
File "/home/philippe/.local/lib/python3.10/site-packages/deeppavlov/core/models/torch_model.py", line 98, in __init__
self.load()
File "/home/philippe/.local/lib/python3.10/site-packages/deeppavlov/models/torch_bert/torch_transformers_sequence_tagger.py", line 295, in load
self.crf = CRF(self.n_classes).to(self.device)
File "/home/philippe/.local/lib/python3.10/site-packages/deeppavlov/models/torch_bert/crf.py", line 13, in __init__
super().__init__(num_tags=num_tags, batch_first=batch_first)
File "/home/philippe/.local/lib/python3.10/site-packages/torchcrf/__init__.py", line 40, in __init__
raise ValueError(f'invalid number of tags: {num_tags}')
ValueError: invalid number of tags: 0
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/philippe/.local/lib/python3.10/site-packages/deeppavlov/core/commands/infer.py", line 55, in build_model
component = from_params(component_config, mode=mode)
File "/home/philippe/.local/lib/python3.10/site-packages/deeppavlov/core/common/params.py", line 102, in from_params
component = obj(**dict(config_params, **kwargs))
File "/home/philippe/.local/lib/python3.10/site-packages/deeppavlov/models/torch_bert/torch_transformers_sequence_tagger.py", line 182, in __init__
super().__init__(optimizer=optimizer,
File "/home/philippe/.local/lib/python3.10/site-packages/deeppavlov/core/models/torch_model.py", line 98, in __init__
self.load()
File "/home/philippe/.local/lib/python3.10/site-packages/deeppavlov/models/torch_bert/torch_transformers_sequence_tagger.py", line 295, in load
self.crf = CRF(self.n_classes).to(self.device)
File "/home/philippe/.local/lib/python3.10/site-packages/deeppavlov/models/torch_bert/crf.py", line 13, in __init__
super().__init__(num_tags=num_tags, batch_first=batch_first)
File "/home/philippe/.local/lib/python3.10/site-packages/torchcrf/__init__.py", line 40, in __init__
raise ValueError(f'invalid number of tags: {num_tags}')
ValueError: invalid number of tags: 0
I downloaded the camembert-base model from https://huggingface.co/camembert-base and copied the files in .deeppavlov/models/camembert-base directory.
Then I figured the deeppavlov's model 'ner_ontonotes_bert_mult' was the best for my use so I edited the config file and changed thoses lines in the metadata section at the end. The docs from DeepPavlov ask to change the TRANSFORMER value, witch I did, and I changed the MODEL_PATH so it point to the files I downloaded previously.
"variables": {
"ROOT_PATH": "~/.deeppavlov",
"DOWNLOADS_PATH": "{ROOT_PATH}/downloads",
"MODELS_PATH": "{ROOT_PATH}/models",
"TRANSFORMER": "camembert-base",
"MODEL_PATH": "{MODELS_PATH}/camembert-base"
},
I am aware that I should have copied the config file to a new one with a different name but this should not be a problem.
Then in python I did the following :
from deeppavlov import configs, build_model
build_model('ner_ontonotes_bert_mult')`
And then I get the error mentioned before. I am lost and don't know where to look from now.

I was trying to make an excel sheet but it seems that it's not working

import openpyxl
import pathlib
a = pathlib.Path("G:/myCourse_mhanned/19. SUPERHERO LEVEL Automate Excel Tasks with Python 3/1.1 Employees.xlsx.xlsx")
workbook = openpyxl.load_workbook(a)
print(workbook.sheetnames)
print(workbook.active)
sheet = workbook["EmployeeData"]
workbook.create_sheet("test_file")
workbook.save(a)
I expected to ad a new sheet but otherwise it gave me this permission error:
['EmployeeData', 'Salaries', 'Skills']
Traceback (most recent call last):
File "C:/Users/BMRL/PycharmProjects/pytrial/anuhabd.py", line 10, in
workbook.save(a)
File "C:\Users\BMRL\PycharmProjects\pytrial\venv\lib\site-packages\openpyxl\workbook\workbook.py", line 397, in save
save_workbook(self, filename)
File "C:\Users\BMRL\PycharmProjects\pytrial\venv\lib\site-packages\openpyxl\writer\excel.py", line 292, in save_workbook
archive = ZipFile(filename, 'w', ZIP_DEFLATED, allowZip64=True)
File "C:\Users\BMRL\AppData\Local\Programs\Python\Python37-32\lib\zipfile.py", line 1204, in init
self.fp = io.open(file, filemode)
PermissionError: [Errno 13] Permission denied: 'G:\myCourse_mhanned\19. SUPERHERO LEVEL Automate Excel Tasks with Python 3\1.1 Employees.xlsx.xlsx'
Use backslash instead of forward one that's due to windows paths that uses backslash \

Cantera 'module' object has no attribute 'Solution'

My question is the same as the one here.
Module object has no attribute [CANTERA]
Ray Speth commented but the OP of that post never responded so I'm hoping that maybe Ray could help me out.
I installed Cantera and did tried to make a gas doing the following
import cantera as ct
gas1 = ct.Solution('gri30.xml')
and I got the error
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'Solution'
I did as the comment suggested and got the following outputs
print(ct.__file__)
/usr/local/lib/python2.7/site-packages/cantera/__init__.py
print(ct.__version__)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute '__version__'
Check the versions and compatibility of your Python and Cantera.
Also, check that your mechanism file is correctly merged (the thermo and kinetics files to make the .cti file).

Can't perform this operation for unregistered loader type - Is there any workaround the fact that pkg_resources are not supported by pyinstaller?

I'm new to pyinstaller and I'm getting this error (NotImplementedError: Can't perform this operation for unregistered loader type) when I try to import a file with my App.
The complete traceback is:
Exception in Tkinter callback
Traceback (most recent call last):
File "tkinter\__init__.py", line 1702, in __call__
File "BioRank.py", line 190, in load
File "site-packages\pandas\core\frame.py", line 710, in style
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
File "C:\Users\tizma\Anaconda3\lib\site-
packages\PyInstaller\loader\pyimod03_importers.py", line 631, in exec_module
exec(bytecode, module.__dict__)
File "site-packages\pandas\io\formats\style.py", line 50, in <module>
File "site-packages\pandas\io\formats\style.py", line 111, in Styler
File "site-packages\jinja2\environment.py", line 830, in get_template
File "site-packages\jinja2\environment.py", line 804, in _load_template
File "site-packages\jinja2\loaders.py", line 113, in load
File "site-packages\jinja2\loaders.py", line 234, in get_source
File "site-packages\pkg_resources\__init__.py", line 1396, in has_resource
File "site-packages\pkg_resources\__init__.py", line 1449, in _has
NotImplementedError: Can't perform this operation for unregistered loader type
I did some research and found out that pyinstaller does not support pkg_resources. Is there any workaround this issue?
I have faced this problem using pyinstaller. The solution was to edit your-python-path\Lib\site-packages\pandas\io\formats\stytle:
Go to line 120 and change
template = env.**get_template**("html.tpl")
to
template = env.**from_string**("html.tpl")
Then try again.
My problem was I was using background color in pd.Series("backgroud....) and the pyinstaller was not building it, so after the change it works.
I experienced the exact same issue with pyinstaller not including pkg_resources. Rafael's answer above sorted it out for me. Much simpler than the other solutions I found related to this issue. I needed to edit the style.py file. This solution also doesn't require any additional datas in the spec file which is convenient.
Path:
your_path\venv\Lib\site-packages\pandas\io\formats\style.py
Before:
template = env.get_template("html.tpl")
After:
template = env.from_string("html.tpl")

How can I overcome this key word error?

enter code here
# -*- coding: utf-8 -*-
import math
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
import matplotlib.animation as animation
fig1=plt.figure()
ax=plt.axes(xlim=(-10,10), ylim=(-10,10))
line,=ax.plot([],[],lw=1)
"""def init ():
line.set_data([],[])
return line,"""
dt=0.001
X=[]
Y=[]
r=float(input("Enter the radius :: "))
w=float(input("Enter angular frequency :: "))
def run(data):
t=0
while w*t<=2*math.pi:
x=r*math.cos(w*t)
y=r*math.sin(w*t)
X.append(x)
Y.append(y)
t=t+dt
line.set_data(X,Y)
return line,
line,=ax.plot(X,Y,lw=2)
FFMpegWriter = animation.writers['ffmpeg']
writer = FFMpegWriter(fps=15, metadata=dict(artist='Me'), bitrate=1800)
anim=animation.FuncAnimation(fig1,run,frames=200,interval=20,blit=True)
anim.save('amim.mp4',writer=writer)
The error message shown is ::
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/tathagata/anaconda3/lib/python3.4/site- packages/spyderlib/widgets/externalshell/sitecustomize.py", line 685, in runfile
execfile(filename, namespace)
File "/home/tathagata/anaconda3/lib/python3.4/site- packages/spyderlib/widgets/externalshell/sitecustomize.py", line 85, in execfile
exec(compile(open(filename, 'rb').read(), filename, 'exec'), namespace)
File "/home/tathagata/Documents/Python scripts/circleamim.py", line 35, in <module>
FFMpegWriter = animation.writers['ffmpeg']
File "/home/tathagata/anaconda3/lib/python3.4/site-packages/matplotlib/animation.py", line 81, in __getitem__
return self.avail[name]
KeyError: 'ffmpeg'
I use anacoda distribution and SPYDER as my IDE. I have seen the many solutions related to key errors. But the movie wont run. How can I make the movie to run? I hope there are no other logical errors.
First install ffmpeg and add path to ffmpeg
# on windows
plt.rcParams['animation.ffmpeg_path'] = 'C:\\ffmpeg\\bin\\ffmpeg.exe'
# on linux
plt.rcParams['animation.ffmpeg_path'] = u'/home/username/anaconda/envs/env_name/bin/ffmpeg'
Note for linux users: The path for ffmpeg can be found by simply using which: which ffmpeg
Also instead of
FFMpegWriter = animation.writers['ffmpeg']
writer = FFMpegWriter(fps=15, metadata=dict(artist='Me'), bitrate=1800)
I just used writer = animation.FFMpegWriter()
It seems that ffmpegis not installed on your system. Try the following code:
import matplotlib.animation as animation
print(animation.writers.list())
It will print out a list of all available MovieWriters. If ffmpegis not among it, you need to install it first from the ffmpeg homepage.
If you have Homebrew, literally just run the command
brew install ffmpeg
And Homebrew will take care of the rest (dependencies, etc). If you don't, I would recommend getting Homebrew or something like it (apt-get on Linux is built in, or an alternative on OS X would be Macports)
I have also posed with same problem(keyError: 'ffmpeg') but instead of using anakonda, I used IDLE3. So, first i checked for 'ffmpeg' in terminal it wasn't installed so installed it.
Using: sudo apt install ffmpeg
and when I run my save_animation program, it worked generating animation files in '.mpeg' format.

Resources