How to fix torchtext.datasets SSTS - ModuleNotFoundError? - python

I am learning at https://pytorch.org/text/stable/tutorials/sst2_classification_non_distributed.html#sphx-glr-tutorials-sst2-classification-non-distributed-py . Source code
Install pre-requistes
!pip install matplotlib-venn
!apt-get -qq install -y libfluidsynth1
!apt-get -qq install -y libarchive-dev && pip install -U libarchive
import libarchive
!apt-get -qq install -y graphviz && pip install pydot
import pydot
!pip install cartopy
import cartopy
then
import torch
import torch.nn as nn
DEVICE = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")
##########################
import torchtext.transforms as T
from torch.hub import load_state_dict_from_url
padding_idx = 1
bos_idx = 0
eos_idx = 2
max_seq_len = 256
xlmr_vocab_path = r"https://download.pytorch.org/models/text/xlmr.vocab.pt"
xlmr_spm_model_path = r"https://download.pytorch.org/models/text/xlmr.sentencepiece.bpe.model"
text_transform = T.Sequential(
T.SentencePieceTokenizer(xlmr_spm_model_path),
T.VocabTransform(load_state_dict_from_url(xlmr_vocab_path)),
T.Truncate(max_seq_len - 2),
T.AddToken(token=bos_idx, begin=True),
T.AddToken(token=eos_idx, begin=False),
)
from torch.utils.data import DataLoader
##########################
from torchtext.datasets import SST2
batch_size = 16
train_datapipe = SST2(split="train")
dev_datapipe = SST2(split="dev")
# Transform the raw dataset using non-batched API (i.e apply transformation line by line)
def apply_transform(x):
return text_transform(x[0]), x[1]
train_datapipe = train_datapipe.map(apply_transform)
train_datapipe = train_datapipe.batch(batch_size)
train_datapipe = train_datapipe.rows2columnar(["token_ids", "target"])
train_dataloader = DataLoader(train_datapipe, batch_size=None)
dev_datapipe = dev_datapipe.map(apply_transform)
dev_datapipe = dev_datapipe.batch(batch_size)
dev_datapipe = dev_datapipe.rows2columnar(["token_ids", "target"])
dev_dataloader = DataLoader(dev_datapipe, batch_size=None)
Source https://colab.research.google.com/github/pytorch/text/blob/gh-pages/main/_downloads/764dcd36c4948c7e9a28df09d761099d/sst2_classification_non_distributed.ipynb#scrollTo=Z0fBwUD08CPW
Error
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-5-9d9df9caa562> in <module>
3 batch_size = 16
4
----> 5 train_datapipe = SST2(split="train")
6 dev_datapipe = SST2(split="dev")
7
2 frames
/usr/local/lib/python3.8/dist-packages/torchtext/datasets/sst2.py in SST2(root, split)
86 # TODO Remove this after removing conditional dependency
87 if not is_module_available("torchdata"):
---> 88 raise ModuleNotFoundError(
89 "Package `torchdata` not found. Please install following instructions at https://github.com/pytorch/data"
90 )
ModuleNotFoundError: Package `torchdata` not found. Please install following instructions at https://github.com/pytorch/data
---------------------------------------------------------------------------
NOTE: If your import is failing due to a missing package, you can
manually install dependencies using either !pip or !apt.
To view examples of installing some common dependencies, click the
"Open Examples" button below.
---------------------------------------------------------------------------
How to fix?

Related

No module named 'torcheval'

I am trying this on Kaggle:
from torcheval.metrics.functional import binary_f1_score
metric = binary_f1_score
And i get an Error:
ModuleNotFoundError Traceback (most recent call last)
/tmp/ipykernel_28/3857382439.py in <module>
----> 1 from torcheval.metrics.functional import binary_f1_score
2 metric = binary_f1_score
3 model_no_CV = unet_1D().to(device)
4 optimizer = torch.optim.Adam(unet_1D.parameters(), lr=0.001)
5 epochs_num = 30
ModuleNotFoundError: No module named 'torcheval'
I tried to install the latest version of torch, didn't help
Other torch modules seem to work correctly
You have to install torcheval library in Kaggle. First of all make sure your internet option is enable to use the kernel then you can try this command:
!pip install torcheval

How to fix: ImportError: cannot import name LiveSpeech?

I am trying to use LiveSpeech with my model and dictionary which I have trained:
#!/usr/bin/env python
from pocketsphinx import LiveSpeech
hmm = '/home/ridwan/sphinx/other1/output/other1.ci_cont' #folder of the acoustic model
lm = '/home/ridwan/sphinx/other1/output/other1.lm.DMP' #language model
dict = '/home/ridwan/sphinx/other1/output/other1.dic' #the phonetic dictionary
recognizer = LiveSpeech (verbose = False, sampling_rate = 16000, buffer_size = 2048,
no_search = False, full_utt = False,
hmm = hmm, lm = lm, dic = dict)
for phrase in recognizer:
print (phrase)
But I am getting the following error:
Traceback (most recent call last):
File "./main.py", line 3, in
from pocketsphinx import LiveSpeech
ImportError: cannot import name LiveSpeech
NOTE: I have successfully installed pocketsphinx from CMU Sphinx
I had an old version of pocketsphinx.
Make sure to have the latest installed.
# Make sure we have up-to-date versions of pip, setuptools and wheel:
$ pip install --upgrade pip setuptools wheel
$ pip install --upgrade pocketsphinx

E053 Could not read config.cfg Resumeparser

spacy.load('en_core_web_sm')
from pyresparser import ResumeParser
data = ResumeParser('Resume.pdf').get_extracted_data()
OSError: [E053] Could not read config.cfg from C:\Users\Kumaran\anaconda3\envs\nlp\lib\site-packages\pyresparser\config.cfg
OSError Traceback (most recent call last)
<ipython-input-3-941548297df0> in <module>
1 from pyresparser import ResumeParser
----> 2 data = ResumeParser('Resume.pdf').get_extracted_data()
~\anaconda3\envs\nlp\lib\site-packages\pyresparser\resume_parser.py in __init__(self, resume, skills_file, custom_regex)
19 ):
20 nlp = spacy.load('en_core_web_sm')
---> 21 custom_nlp = spacy.load(os.path.dirname(os.path.abspath(__file__)))
22 self.__skills_file = skills_file
23 self.__custom_regex = custom_regex
~\anaconda3\envs\nlp\lib\site-packages\spacy\__init__.py in load(name, disable, exclude, config)
45 RETURNS (Language): The loaded nlp object.
46 """
---> 47 return util.load_model(name, disable=disable, exclude=exclude, config=config)
48
49
To solve the issues, install these versions:
pip install nltk
pip install spacy==2.3.5
pip install https://github.com/explosion/spacy-models/releases/download/en_core_web_sm-2.3.1/en_core_web_sm-2.3.1.tar.gz
pip install pyresparser
Just to explain what caused the E053 error: the installed models were not compatible with the installed spacy version. You can use python -m spacy validate to see your version and if you have any models for it, then install matching versions as in the above answer. Source: https://github.com/explosion/spaCy/issues/7453

Cannot import name 'Imputer' from 'sklearn.preprocessing' from pandas_ml

I am working on a project for my master and I was trying to get some stats on my calculations. I found a very cool tool to do this, called panda_ml, but when I import it in my cell on jupyter like this:
from pandas_ml import *
It gives me this output error:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-118-93009f7254d4> in <module>
3 from sklearn import *
4 from matplotlib.colors import LogNorm
----> 5 from pandas_ml import *
6 import math
7
~/anaconda3/envs/Lab1_B/lib/python3.7/site-packages/pandas_ml/__init__.py in <module>
1 #!/usr/bin/env python
2
----> 3 from pandas_ml.core import ModelFrame, ModelSeries # noqa
4 from pandas_ml.tools import info # noqa
5 from pandas_ml.version import version as __version__ # noqa
~/anaconda3/envs/Lab1_B/lib/python3.7/site-packages/pandas_ml/core/__init__.py in <module>
1 #!/usr/bin/env python
2
----> 3 from pandas_ml.core.frame import ModelFrame # noqa
4 from pandas_ml.core.series import ModelSeries # noqa
~/anaconda3/envs/Lab1_B/lib/python3.7/site-packages/pandas_ml/core/frame.py in <module>
8
9 import pandas_ml.imbaccessors as imbaccessors
---> 10 import pandas_ml.skaccessors as skaccessors
11 import pandas_ml.smaccessors as smaccessors
12 import pandas_ml.snsaccessors as snsaccessors
~/anaconda3/envs/Lab1_B/lib/python3.7/site-packages/pandas_ml/skaccessors/__init__.py in <module>
17 from pandas_ml.skaccessors.neighbors import NeighborsMethods # noqa
18 from pandas_ml.skaccessors.pipeline import PipelineMethods # noqa
---> 19 from pandas_ml.skaccessors.preprocessing import PreprocessingMethods # noqa
20 from pandas_ml.skaccessors.svm import SVMMethods # noqa
~/anaconda3/envs/Lab1_B/lib/python3.7/site-packages/pandas_ml/skaccessors/preprocessing.py in <module>
11 _keep_col_classes = [pp.Binarizer,
12 pp.FunctionTransformer,
---> 13 pp.Imputer,
14 pp.KernelCenterer,
15 pp.LabelEncoder,
AttributeError: module 'sklearn.preprocessing' has no attribute 'Imputer'
I am using Conda, I have my own env with all the packages, I have tried to install older versions of sklearn and pandas_ml but it did not solve the problem. I've searching around but it seems that no one had ever this problem...Do you have any suggestion?
You have to uninstall properly and downgrading will work.
pip uninstall -y scikit-learn
pip uninstall -y pandas
pip uninstall -y pandas_ml
pip install scikit-learn==0.21.1
pip install pandas==0.24.2
pip install pandas_ml
Then import
from pandas_ml import *
Tested in Python 3.8.2
I had scikit-learn version 0.22.1 installed recently and had a similar problem. Then I tried your solution under Python 3.7.2, maintained the versions for Pandas v0.25.1 and Pandas ML v0.6.1 and it work like a charm!. I wonder when would be it safe to turn to a newer version of scikit-learn
from sklearn.impute import SimpleImputer
imp = SimpleImputer(missing_values=np.nan, copy=False, strategy="mean", )
No axis value is needed anymore

Why can I see pip list sklearn but not in Jupyter when I run a code?

I am currently working on my first program with Jupyter. When I run a cell I receive the following
<ipython-input-6-7a7db0de4539> in <module>
1 try:
----> 2 from sklearn.datasets import fetch_openml
3 mnist = fetch_openml('mnist_784', version=1, cache=True)
c:\users\james\appdata\local\programs\python\python37\lib\site-packages\sklearn\__init__.py in <module>
63 from . import __check_build
---> 64 from .base import clone
65 from .utils._show_versions import show_versions
c:\users\james\appdata\local\programs\python\python37\lib\site-packages\sklearn\base.py in <module>
12 from .externals import six
---> 13 from .utils.fixes import signature
14 from . import __version__
c:\users\james\appdata\local\programs\python\python37\lib\site-packages\sklearn\utils\__init__.py in <module>
15 from ..exceptions import DataConversionWarning
---> 16 from .fixes import _Sequence as Sequence
17 from .deprecation import deprecated
c:\users\james\appdata\local\programs\python\python37\lib\site-packages\sklearn\utils\fixes.py in <module>
84 else:
---> 85 from scipy.special import boxcox # noqa
86
c:\users\james\appdata\local\programs\python\python37\lib\site-packages\scipy\special\__init__.py in <module>
640
--> 641 from ._ufuncs import *
642
ImportError: DLL load failed: The specified module could not be found.
I used pip install to get sklearn and when doing pip list in the cmd it is there. However when I even try import sklearn I get an error.
I have checked import sys then sys.path which gave me:
'C:\\Users\\James\\Documents\\Machine Learning Comp Vision\\Project 1',
'C:\\Users\\James\\Documents\\Machine Learning Comp Vision\\Project 1',
'c:\\users\\james\\appdata\\local\\programs\\python\\python37\\python37.zip',
'c:\\users\\james\\appdata\\local\\programs\\python\\python37\\DLLs',
'c:\\users\\james\\appdata\\local\\programs\\python\\python37\\lib',
'c:\\users\\james\\appdata\\local\\programs\\python\\python37',
'',
'c:\\users\\james\\appdata\\local\\programs\\python\\python37\\lib\\site-packages',
'c:\\users\\james\\appdata\\local\\programs\\python\\python37\\lib\\site-packages\\IPython\\extensions',
'C:\\Users\\James\\.ipython']
All of my modules are located in C:\Users\James\AppData\Local\Programs\Python\Python37\lib\site-packages
Would the capital letters in my file url be the cause of this or is it something to do with the path itself?
If you already have a working installation of numpy and scipy, then do this:
pip install -U scikit-learn
otherwise:
conda install scikit-learn
finally check for updates:
conda update pip
Use pip from inside your Jupyter notebook
!{sys.executable} -m pip install -U scikit-learn
This will use the sys.executable to run the right pip for your current kernel, instead of for some other Python install.

Categories

Resources