Module Not Found Error when importing Pytorch_Transformers - nlp

After downloading pytorch_transformers through Anaconda and executing the import command through the Jupyter Notebook, I am facing several errors related to missing modules.
I tried searching sacremoses to import the package via Anaconda, but it is only available for Linux machines. Has anyone else faced similar issues? Thanks in advance!
from pytorch_transformers import BertTokenizer, BertModel, BertForMaskedLM
This is the error:
<ipython-input-5-218d0858d00f> in <module>
----> 1 from pytorch_transformers import BertTokenizer, BertModel, BertForMaskedLM
~\Anaconda3\lib\site-packages\pytorch_transformers\__init__.py in <module>
1 __version__ = "1.2.0"
----> 2 from .tokenization_auto import AutoTokenizer
3 from .tokenization_bert import BertTokenizer, BasicTokenizer, WordpieceTokenizer
4 from .tokenization_openai import OpenAIGPTTokenizer
5 from .tokenization_transfo_xl import (TransfoXLTokenizer, TransfoXLCorpus)
~\Anaconda3\lib\site-packages\pytorch_transformers\tokenization_auto.py in <module>
24 from .tokenization_transfo_xl import TransfoXLTokenizer
25 from .tokenization_xlnet import XLNetTokenizer
---> 26 from .tokenization_xlm import XLMTokenizer
27 from .tokenization_roberta import RobertaTokenizer
28 from .tokenization_distilbert import DistilBertTokenizer
~\Anaconda3\lib\site-packages\pytorch_transformers\tokenization_xlm.py in <module>
25 from io import open
26
---> 27 import sacremoses as sm
28
29 from .tokenization_utils import PreTrainedTokenizer
ModuleNotFoundError: No module named 'sacremoses'```

Please try to create a conda environment and install the packages in the created environment using the below steps:
conda create -n env_pytorch -c intel python=3.5
source activate env_pytorch
pip install pytorch-transformers

You are missing the pytorch-transformers dependency. Try installing it with the following command:
pip install pytorch-transformers

Try using python -m pip install transformers rather than using pip install

Related

Python 3.8: Error running different Python environments from base directory's Jupyter Notebook

I've recently had to do a reformat of my work computer, and am reinstalling Anaconda. I generally keep Anaconda's root (base) folder untouched, and create separate environments when I need to work with specialist Python modules instead of cluttering the (base) environment.
In the past, I was able to successfully run these different environments from the Jupyter Notebook installed in the (base) environment. I would go about doing so by installing ipykernel in the new environment (e.g. my-env), and then running the following commands:
(base) activate my-env
(my-env) conda install ipykernel
(my-env) python -m ipykernel install --name "my-env" --display-name "My Python"
This would be done successfully, and give me the following message:
Installed kernelspec my-env in C:\ProgramData\jupyter\kernels\my-env
However, when I tried testing out the link in Jupyter Notebook using a standard import matplotlib.pyplot as plt command, I get the following error message:
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-1-a0d2faabd9e9> in <module>
----> 1 import matplotlib.pyplot as plt
C:\Anaconda3\envs\my-env\lib\site-packages\matplotlib\__init__.py in <module>
105 # cbook must import matplotlib only within function
106 # definitions, so it is safe to import from it here.
--> 107 from . import cbook, rcsetup
108 from matplotlib.cbook import MatplotlibDeprecationWarning, sanitize_sequence
109 from matplotlib.cbook import mplDeprecation # deprecated
C:\Anaconda3\envs\my-env\lib\site-packages\matplotlib\cbook\__init__.py in <module>
26 import weakref
27
---> 28 import numpy as np
29
30 import matplotlib
C:\Anaconda3\envs\my-env\lib\site-packages\numpy\__init__.py in <module>
141 from .core import *
142 from . import compat
--> 143 from . import lib
144 # NOTE: to be revisited following future namespace cleanup.
145 # See gh-14454 and gh-15672 for discussion.
C:\Anaconda3\envs\my-env\lib\site-packages\numpy\lib\__init__.py in <module>
23 # Private submodules
24 from .type_check import *
---> 25 from .index_tricks import *
26 from .function_base import *
27 from .nanfunctions import *
C:\Anaconda3\envs\my-env\lib\site-packages\numpy\lib\index_tricks.py in <module>
9 from numpy.core.numerictypes import find_common_type, issubdtype
10
---> 11 import numpy.matrixlib as matrixlib
12 from .function_base import diff
13 from numpy.core.multiarray import ravel_multi_index, unravel_index
C:\Anaconda3\envs\my-env\lib\site-packages\numpy\matrixlib\__init__.py in <module>
2
3 """
----> 4 from .defmatrix import *
5
6 __all__ = defmatrix.__all__
C:\Anaconda3\envs\my-env\lib\site-packages\numpy\matrixlib\defmatrix.py in <module>
9 # While not in __all__, matrix_power used to be defined here, so we import
10 # it for backward compatibility.
---> 11 from numpy.linalg import matrix_power
12
13
C:\Anaconda3\envs\my-env\lib\site-packages\numpy\linalg\__init__.py in <module>
71 """
72 # To get sub-modules
---> 73 from .linalg import *
74
75 from numpy._pytesttester import PytestTester
C:\Anaconda3\envs\my-env\lib\site-packages\numpy\linalg\linalg.py in <module>
31 from numpy.core import overrides
32 from numpy.lib.twodim_base import triu, eye
---> 33 from numpy.linalg import lapack_lite, _umath_linalg
34
35
ImportError: DLL load failed: The specified module could not be found.
Could someone advise me on what might be the issue? If it helps, my (base) environment has a python version of 3.8.3 and a notebook version of 6.0.3, whereas my new my-env environment has modules downloaded from conda-forge. It has a python version of 3.7.8 and an ipykernel version of 5.3.4.
Thanks in advance!
UPDATE 26 Oct 2020
As requested, I have included the list of modules I have in both the (base) environment and the (my-env) environment. In general, the packages in (base) have been kept updated with respect to the anaconda module, whereas the packages in (my-env) are kept up-to-date with respect to hyperspy, which is stored in the conda-forge repository.
I have created PasteBin entries for them, as they exceed the character limit for this post.
Link to list of modules in (base)
Link to list of modules in (my-env)
I also tried importing modules other than matplotlib and numpy, and was able to import abc and time without issue, for example. This seems to be an issue with the (base) version of Jupyter Notebook not being compatible with the numpy found in the (my-env) environment.
The error message is indicating the error is arising from the numpy library. The fact that your Python and Ipkernel versions are different between your (base) and your (my-env) is further indication that there is incompatibility between your environments.
Can you provide the output from
conda list
from each environment?
When I tried to create a Python=3.8.3 environment the numpy version installed is numpy-1.19.2-py38hf89b668_1
I used the command
conda create -n foo -c conda-forge python=3.8.3 numpy
When I tried to create a Python=3.7.8 environment the numpy version installed is numpy-1.19.2-py37h7008fea_1
I used the command
conda create -n foo -c conda-forge python=3.7.8 numpy
In addition, why don't you consider installing ipkernel / jupyter notebook libraries that are consistent with respective versions of Python in each environment? This would always be the best solution to ensure dependencies are correctly aligned.
I also attempted to install ipykernel in both python=3.8.3 and python=3.7.8 along with ipykernel without specifying version number.
Here are the versions of ipykernel that condaautomatically chooses:
for python=3.8.3: ipykernel-5.3.4 | py38h1cdfbd6_1
for python=3.7.8: ipykernel-5.3.4 | py37hc6149b9_1
From what you have written your ipykernel versions are different. I do think this discrepancy is most likely coming from these differences of ipykernel versions
When you check your environments, verify that the channel source for ipykernel is the same.
One Solution: Consider downgrading the ipykernel in (base) to 5.3.4 version.

Cannot import name 'network' from 'tensorflow.python.keras.engine'

When trying to load BERT QA I get the following ImportError:
"Cannot import name 'network' from 'tensorflow.python.keras.engine'"
The full error log follows below
Following this post,
ImportError: cannot import name 'network' from 'tensorflow.python.keras.engine'
I have tried the following steps,
pip uninstall tf-agents
pip install tf-agents-nightly
and then in Python,
from tf_agents.environments import suite_gym
However, this did not resolve the problem. Any suggestions would be very welcome!
In case it is helpful, I'm running TensorFlow version 2.3.0. Also, the script init.py referred to below does exist, however the file is empty, i.e. 0 bytes.
ImportError Traceback (most recent call last)
<ipython-input-2-323bac0cb5ba> in <module>
----> 1 from bert_qa import squad
~/anaconda3/lib/python3.8/site-packages/bert_qa/squad.py in <module>
34 from . import model_training_utils
35 from . import bert_modeling as modeling
---> 36 from . import bert_models
37 from . import optimization
38 from . import input_pipeline
~/anaconda3/lib/python3.8/site-packages/bert_qa/bert_models.py in <module>
25 from . import bert_modeling
26 from . import weighted_sparse_categorical_crossentropy
---> 27 from . transformer_encoder import TransformerEncoder
28 from . albert_transformer_encoder import AlbertTransformerEncoder
29 from . import bert_classifier
~/anaconda3/lib/python3.8/site-packages/bert_qa/transformer_encoder.py in <module>
22 import tensorflow as tf
23
---> 24 from tensorflow.python.keras.engine import network # pylint: disable=g-direct-tensorflow-import
25
26 from . import activations
ImportError: cannot import name 'network' from 'tensorflow.python.keras.engine' (/home/user810643/anaconda3/lib/python3.8/site-packages/tensorflow/python/keras/engine/__init__.py)
Install tf-agents 0.6.0 version for Tebsorflow 2.3.
pip install tf-agents==0.6.0
Looks like the issue was caused due to TF-Agents and Tensorflow version incompatibility. For more information, please take a look at this release notes. Thanks!

Impossible to import pandas ! "Is this due to the latest release?"

I'm trying to import pandas and I'm facing some issues
import numpy as np
import multiprocessing as mp
import pandas as pd
result:
C:\Anaconda\lib\site-packages\pandas\core\indexes\base.py in <module>
57 import pandas.core.sorting as sorting
58 from pandas.io.formats.printing import pprint_thing
---> 59 from pandas.core.ops import _comp_method_OBJECT_ARRAY
60 from pandas.core import strings, accessor
61 from pandas.core.config import get_option
ImportError: cannot import name 'ops'
OR
AttributeError: module 'pandas' has no attribute 'core'
Please try the following step and restart the jupyter notebook:
conda upgrade --all
If it is still showing the same error, try downgrading the pandas version:
pip install pandas==0.22.0
Hope this helps.

I am getting error when importing torch and torch vision

I installed torch and torchvision using pip3 on MAC. When I imported the same, getting the below error.
Environment:
OS : macOS High Sierra
Python : 3.7
pip : 3
Pytorch 1.1
code:
import torch
import torchvision
Error:
ImportError Traceback (most recent call
last) in
1 import numpy as np
2 from glob import glob
----> 3 import torch
4 import torchvision
5 from torchvision import datasets
/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/torch/init.py
in
77 del _dl_flags
78
---> 79 from torch._C import *
80
81 all += [name for name in dir(_C)
ImportError:
dlopen(/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/torch/_C.cpython-37m-darwin.so,
9): Library not loaded: /usr/local/opt/libomp/lib/libomp.dylib
Referenced from:
/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/torch/lib/libshm.dylib
Reason: image not found
Any help on solving this error would be appreciated.
i think
$ brew install libomp
can help u, cause i solve the same problem by it.
according
github-issue-"libomp.dylib can't be loaded"

ImportError: No module named keras.preprocessing

Following the tutorial:
http://www.pyimagesearch.com/2016/08/10/imagenet-classification-with-python-and-keras/#comment-419896
Using these files:
https://github.com/fchollet/deep-learning-models
I get 2 separate errors depending on how I execute:
Running in PyCharm:
Using TensorFlow backend.
usage: test_imagenet.py [-h] -i IMAGE
test_imagenet.py: error: the following arguments are required: -i/--image
Running in cmd line:
C:\Users\AppData\Local\Programs\Python\Python35\Scripts>python deep-learning-models/test_imagenet.py --image deep-learning-models/images/dog.jpg
Traceback (most recent call last):
File "deep-learning-models/test_imagenet.py", line 2, in <module>
from keras.preprocessing import image as image_utils
ImportError: No module named keras.preprocessing
How do I resolve?
Its best if you solve this problem outside running the above script... Here is what you can try in your command line environment to make sure it works outside your script:
>>> import keras
Using TensorFlow backend.
>>> keras.__version__
'1.2.1'
>>> keras.preprocessing
<module 'keras.preprocessing' from '/usr/local/lib/python2.7/dist-packages/keras/preprocessing/__init__.pyc'>
>>> from keras.preprocessing import image as image_utils
>>>
Make sure you have latest version of keras installed. If you get above working then it could be the environment issue where above script is not able to find the keras package. However if above does not work or work partially you would need to install keras again by removing it first..
$ pip install keras --user
Every dependency in a python project need to be installed using pip or easy_install or from the source code. You will have to install the keras module as mentioned here.
This happened to me. It turned out I was working in a pyvenv which wasn't activated. Just run source bin/activate on Linux/Mac or Scripts\activate.bat on Windows
from keras.models import Sequential
from keras import legacy_tf_layer
from keras.preprocessing import image as image_utils
from keras.preprcessing.text import Toknizer
import pandas as pd
from sklearn.model_selection import train_test_spli

Resources