I am getting error when importing torch and torch vision - python-3.x

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"

Related

module 'tensorflow_core.compat.v2' has no attribute '__internal__'

I am using python 3.6.9 , keras 2.3.1, and (tf 2.0.0 or tf 2.1.0).
Got the compatibility versions from this table.
When I import keras I get this error.
Error:
AttributeError: module 'tensorflow_core.compat.v2' has no attribute '__internal__'
Solution:
Install Libraries
!pip install tensorflow==2.1
!pip install keras==2.3.1
Import
from tensorflow import keras
I was able to replicate your issue with the below combination as shown below
!pip install tensorflow==2.1.0
!pip install keras==2.3.1
import keras
Output:
Using TensorFlow backend.
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-3-88d96843a926> in <module>()
----> 1 import keras
8 frames
/usr/local/lib/python3.7/dist-packages/keras/initializers/__init__.py in populate_deserializable_objects()
47
48 LOCAL.ALL_OBJECTS = {}
---> 49 LOCAL.GENERATED_WITH_V2 = tf.__internal__.tf2.enabled()
50
51 # Compatibility aliases (need to exist in both V1 and V2).
AttributeError: module 'tensorflow_core.compat.v2' has no attribute '__internal__'
Fixed code:
Here issue is due to incompatibility between TF2.1 and Keras 2.3.1. Your issue can be resolved in two ways
Upgrade to Keras2.5.0rc0 and import keras
import keras from tensorflow as from tensorflow import keras
I faced a similar error while trying to import Tokenizer from keras.preprocessing
This caused error -
from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences
This fixed it-
from tensorflow.keras import preprocessing
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences

Module Not Found Error when importing Pytorch_Transformers

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

Tensorflow Transformer Colab: AttributeError: module 'tensorflow_core' has no attribute 'compiler'

I tried to import the packages in the Transformer code in Transformer Colab Notebook. Is there a updated tensorflow core version?
from __future__ import absolute_import, division, print_function, unicode_literals
try:
!pip install tf-nightly-2.0-preview
except Exception:
pass
import tensorflow_datasets as tfds
import tensorflow as tf
import time
import numpy as np
import matplotlib.pyplot as plt
I got the following error.
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-9-22c51fa8bb84> in <module>()
5 except Exception:
6 pass
----> 7 import tensorflow_datasets as tfds
8 import tensorflow as tf
9
3 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/__init__.py in <module>()
96
97 # We still need all the names that are toplevel on tensorflow_core
---> 98 from tensorflow_core import *
99
100 # These should not be visible in the main tf module.
AttributeError: module 'tensorflow_core' has no attribute 'compiler'
The error is from not having tensorflow datasets installed. Use pip3 install tensorflow-datasets for installing and run the script again. Here is a useful TF-datasets offical link.

Python module incompatibilities

I have just installed gdal into my Python3 root environment using conda:
conda install -c conda-forge gdal
Installation went fine with the usual updates of some dependencies. Now returning to some of my scripts, both netCDF4 and mpl_toolkits.basemap have stopped working. These are the errors I get:
from netCDF4 import Dataset
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-7-09694a7f0e01> in <module>()
----> 1 from netCDF4 import Dataset
2 import numpy as np
3 import matplotlib.pyplot as plt
4 from scipy.interpolate import griddata
5 from mpl_toolkits.basemap import Basemap
/anaconda3/lib/python3.6/site-packages/netCDF4/__init__.py in <module>()
1 # init for netCDF4. package
2 # Docstring comes from extension module _netCDF4.
----> 3 from ._netCDF4 import *
4 # Need explicit imports for names beginning with underscores
5 from ._netCDF4 import __doc__, __pdoc__
ImportError: dlopen(/anaconda3/lib/python3.6/site-packages/netCDF4/_netCDF4.cpython-36m-darwin.so, 2): Library not loaded: #rpath/libhdf5.101.dylib
Referenced from: /anaconda3/lib/python3.6/site-packages/netCDF4/_netCDF4.cpython-36m-darwin.so
Reason: image not found
from mpl_toolkits.basemap import Basemap
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-8-5fb601138328> in <module>()
3 import matplotlib.pyplot as plt
4 from scipy.interpolate import griddata
----> 5 from mpl_toolkits.basemap import Basemap
6 import numpy.ma as ma
7 import glob
/anaconda3/lib/python3.6/site-packages/mpl_toolkits/basemap/__init__.py in <module>()
35 import numpy as np
36 import numpy.ma as ma
---> 37 import _geoslib
38 import functools
39
ImportError: dlopen(/anaconda3/lib/python3.6/site-packages/_geoslib.so, 2): Library not loaded: #rpath/libgeos-3.5.0.dylib
Referenced from: /anaconda3/lib/python3.6/site-packages/_geoslib.so
Reason: image not found
I have no idea what these errors mean, since those paths do exist. What is most baffling is that the netCDF4 and mpl_toolkits modules did not change (i.e. were not updated/downgraded) with the installation of gdal, so why now are they failing?
I'm at the end of my tether with problems like this. Fortunately I made a copy of my Python root directory before attempting this installation so I can now revert back to it (I've been here many times before).
I know about working in separate conda environments, but am I seriously supposed to install a new environment every time I want to use a new Python module? This not only takes up a lot of harddrive space but most inconveniently means adding new functionality to old codes is impossible (I want to use GDAL to plot some geotiffs using Basemap, which now does not work!).
I cannot imagine proficient Python users battling with these issues - so what am I doing wrong?

libgfortran.so.1: cannot open shared object file

I am very new to installing my own programs and packages on linux, so please be gentle ;)
I am working on Linux Mint 17.3 (Rosa) 64 bit (Ubuntu Trusty) and am trying to run through the scikitlearn tutorials located here:
https://github.com/savarin/pyconuk-introtutorial
I have installed all the relevant packages using:
pip install numpy pandas sklearn ipython
and everything looks great until I start trying to import some things from related packages i.e.
from scipy.stats import mode
I see the following error code:
sam#samputer ~/Desktop $ ipython
In [1]: from scipy.stats import mode
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-1-a8b1ace850b6> in <module>()
----> 1 from scipy.stats import mode
/home/sam/anaconda2/lib/python2.7/site-packages/scipy/stats/__init__.py in <module>()
336 from __future__ import division, print_function, absolute_import
337
--> 338 from .stats import *
339 from .distributions import *
340 from .morestats import *
/home/sam/anaconda2/lib/python2.7/site-packages/scipy/stats/stats.py in <module>()
178 from scipy._lib.six import callable, string_types
179 from numpy import array, asarray, ma, zeros
--> 180 import scipy.special as special
181 import scipy.linalg as linalg
182 import numpy as np
/home/sam/anaconda2/lib/python2.7/site-packages/scipy/special/__init__.py in <module>()
625 from __future__ import division, print_function, absolute_import
626
--> 627 from ._ufuncs import *
628
629 from .basic import *
ImportError: libgfortran.so.1: cannot open shared object file: No such file or directory
I'm also running into issues when I try to link through to R for visualisation. I add this as it may shed further light on the issue:
In [11]: %%R
....: library(ggplot2)
....:
/home/sam/anaconda2/lib/python2.7/site-packages/rpy2/robjects /functions.py:106: UserWarning: Error in library(ggplot2) : there is no package called ‘ggplot2’
res = super(Function, self).__call__(*new_args, **new_kwargs)
Error in library(ggplot2) : there is no package called ‘ggplot2’
If anyone can help me understand these issues and offer me some lines/links to fix them, I would be extremely grateful, as my googling hasn't made much progress.
Cheers!
If you're using Anaconda you should try installing packges with conda install first rather than pip install if you can — actually all of the the packages you installed come with the standard Anaconda distribution, so I would try reinstalling this first (did you install miniconda?)
For your second question I'm guessing you'll need to install ggplot in R, I think you do this by running install.packages("ggplot2") in R.

Resources