Azure Machine Learning notebooks: ModuleNotFound error - torch

I'm working through a Python exercise using Azure Machine Learning notebooks. I'm unable to import torch even after !pip install torch.
Notebook says Requirement already satisfied, then errors out with:
!pip install torch
import torch
data = torch.tensor(encode(text), dtype=torch.long)
print(data.shape, data.dtype)
print(data[:100])
4 sec
ModuleNotFoundError: No module named 'torch'
Requirement already satisfied: torch in /anaconda/envs/azureml_py38/lib/python3.8/site-packages (1.12.0)
Requirement already satisfied: typing-extensions in /anaconda/envs/azureml_py38/lib/python3.8/site-packages (from torch) (4.4.0)
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
Input In [22], in <cell line: 4>()
1 # Encode the entire dataset and store it into a torch.Tensor
3 get_ipython().system('pip install torch')
----> 4 import torch
5 data = torch.tensor(encode(text), dtype=torch.long)
6 print(data.shape, data.dtype)
ModuleNotFoundError: No module named 'torch'
I opened up a terminal in Azure ML Studio and tried pip install torch too, same Requirement already satisfied message showed.
How do I get torch (and any other Python modules where this occurs) working in AML notebooks?

I've found creating environments and installing packages through the terminal to be a much more reliable experience than doing it from an AML notebook.
I suggest using one of the provided terminals (either the one available in the compute instance's details, or the one available in JupyterLab) to create a new conda environment which you can customize to your liking.
Something like:
conda create -n my_tutorial python=3.10
conda activate my_tutorial
pip install --user ipykernel
python -m ipykernel install --user --name=my_tutorial
# Do a complete install of PyTorch, take a look at the available versions here https://pytorch.org/get-started/previous-versions/
conda install pytorch=1.13 torchvision=0.14 torchaudio=0.13 pytorch-cuda=11.7 -c pytorch -c nvidia -y
Afterwards, just make sure your notebooks use the my_tutorial kernel and you should be good to go. Whenever you want to pip install something new, just go back to the terminal, activate your kernel, and install the thing, then it should be available in your notebooks as well.

The issue was lined out in the docs here. Don't use !pip within the notebook. Instead use %pip.

Related

No module named 'torch' in Jupyter Notebook

I am fairly new to using jupyter notebook, and I've read every forum available for this issue, with no luck.
I am running Windows 11, and I installed Anaconda, then created and activated a virtual environment called pytorchenv. My .yml file includes the following
name: pytorchenv
channels:
defaults
pytorch
dependencies:
numpy=1.16.2
pandas=0.24.2
matplotlib=3.0.3
pillow=5.4.1
pip=19.0
plotly=3.7.0
scikit-learn=0.20.3
seaborn=0.9.0
python=3.7.3
jupyter=1.0.0
pytorch=1.1.0
torchvision=0.2.2
If I list all of the included packages in the command prompt, using
conda list -n pytorch
it shows that pytorch is installed as...
Name Version Build Channel
pytorch 1.1.0 py3.7_cuda100_cudnn7_1 pytorch
What's more, if I enable the virtual environment in the Command Prompt, It seems that I am able to import pytorch successfully
C:\\Users\\Nathaniel\>conda activate pytorchenv
(pytorchenv) C:\\Users\\Nathaniel\>python
Python 3.7.3 (default, Apr 24 2019, 15:29:51) \[MSC v.1915 64 bit (AMD64)\] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>import torch
>>>
However, when I try to import pytorch into jupyter notebook, I get the message
import torch
ModuleNotFoundError
Traceback (most recent call last)
Input In \[1\], in \<cell line: 1\>()
1 import torch
ModuleNotFoundError: No module named 'torch'
I have been able to import pandas and numpy in jupyter notebook without issue, but I have not figured out a way to import pytorch successfully.
In case this is helpful, jupyter notebook shows that it is running on
Python 3 (ipykernel)
and running the following in jupyter notebook
import sys
print(sys.executable)
results in
C:\\Users\\Nathaniel\\anaconda3\\python.exe
I've tried checking if there were multiple versions of jupyter notebook on my computer (there aren't).
I also checked that pytorch is installed in the same place as my other anaconda packages (it is).
What am I doing wrong?
The notebook is running with your base environment, as indicated by sys.executable.
Two methods:
Start Jupyter from your environment
Register your environment to be usable by all jupyter notebooks
For that you can find the necessary steps here
Basic step is this
source activate pytorchenv # or activate the environment via conda
python -m ipykernel install --user --name pytorchenv --display-name "Python (pytorchenv)"
Afterwards be sure to restart Juypter, then you should be able to select your pytorchenv kernel.

Kernel Died when running Neuralcoref

I am trying to install neuralcoref and following the instructions given here.
I created a jupyter notebook and try to run the following code.
# Load your usual SpaCy model (one of SpaCy English models)
import spacy
nlp = spacy.load('en')
# Add neural coref to SpaCy's pipe
import neuralcoref
neuralcoref.add_to_pipe(nlp)
# You're done. You can now use NeuralCoref as you usually manipulate a SpaCy
document annotations.
doc = nlp(u'My sister has a dog. She loves him.')
doc._.has_coref
doc._.coref_clusters
I get an error message from the jupyter, that kernel died. Even I try to run in the in a python file but still its not working.
O.S - Windows 10
RAM : 16GB
Note: I did try out to updating numpy but still it didn't worked.
Can anyone help me with that. Appreciate your time.
Thanks
As per here: https://github.com/huggingface/neuralcoref/issues/189.
You can get it to work fine if you downgrade Spacy to 2.1.0.
pip uninstall spacy
pip uninstall neuralcoref
pip install spacy==2.1.0
pip install neuralcoref --no-binary neuralcoref
Has worked for others, including myself. Notebook now runs fine.
There is no need to downgrade spacy at all. Build from source, because neuralcoref installed with pip is built against spacy==2.1.0.
Proof:
Build:
git clone https://github.com/huggingface/neuralcoref.git
cd neuralcoref
pip install -r requirements.txt # correct for the desired versions of Cython and SpaCy
python setup.py install
Test:
import spacy
import neuralcoref
nlp = spacy.load('en_core_web_md')
neuralcoref.add_to_pipe(nlp)
print(spacy.__version__)
doc = nlp(u'My sister has a dog. She loves him.')
print(doc._.has_coref)
print(doc._.coref_clusters)
2.3.2
True
[My sister: [My sister, She], a dog: [a dog, him]]

Getting Altair to work with Jupyter Notebook

Trying to get Altair to work with Jupyter Notebook, I installed it using
conda install -c conda-forge altair vega_datasets notebook vega
But when I try to do an example plot
import altair as alt
from vega_datasets import data
# for the notebook only (not for JupyterLab) run this command once per session
alt.renderers.enable('notebook')
iris = data.iris()
alt.Chart(iris).mark_point().encode(
x='petalLength',
y='petalWidth',
color='species'
)
as seen in their quick start guide, I get
ValueError:
To use the 'notebook' renderer, you must install the vega package
and the associated Jupyter extension.
See https://altair-viz.github.io/getting_started/installation.html
for more information.
even though I have installed vega using Conda. I can make vega example plots though. I am unable to enable the Jupyter extension though, as Jupyter says it is incompatible.
Any help is appreciated.
For the current release of altair (Version 2.2), use
conda install -c conda-forge vega=1.3
or
pip install vega==1.3
and then restart the notebook.
The vega python extension was mistakenly updated this week to only support vega-lite 3.0, which is not yet released and thus not yet supported by Altair.
See https://github.com/altair-viz/altair/issues/1114 for the initial bug report.

can't import 'torchtext' module in jupyter notebook while using pytorch

I installed pytorch using anaconda3 and my created virtual conda environment named 'torchTest'.
I installed all the modules needed but, codes doesn't work in jupyter python.
I installed torchtext using
1.pip install https://github.com/pytorch/text/archive/master.zip
2.and also pip install torchtext too.
all I mentioned successfully downloaded in my MAC OS X, but can't get what's wrong with my Jupyter notebook..
After having the same issue with torchtext from within my jupyterlab, I opened an issue on Github at the jupyterlab project as well as at the torchtext repository.
My current solution is to add the PYTHONPATH from the Anaconda env.
The Anaconda path is usually like that $HOME/anaconda/bin
You can add it from within Jupyter Lab/Notebook like that:
import sys
sys.path.append("/some/path/to/add")
import torchtext

What directory should packages/modules be written to?

When installing GLPK from (from https://www.gnu.org/software/glpk), I get the following error:
libtool: install: /usr/bin/install -c .libs/libglpk.36.dylib /usr/local/lib/libglpk.36.dylib
install: /usr/local/lib/libglpk.36.dylib: Permission denied
I have tried installing to other locations successfully, but python does not find the package. I would like to run. Note: I am using Enghought Canopy to run Python on Mac OS X version 10.9.4.
You don't install a non-python package into Python. You install it on your system (not a python issue, maybe someone else will have a suggestion), and then install a python package which "wraps" it -- provides access to it in python. If you have already successfully installed GLPK, then you should simply be able to open a Canopy Terminal window from the Canopy Tools menu, and type:
pip install glpk
EDIT: It looks like this is the best place for examples of using it: http://www.tfinley.net/software/pyglpk/examples.html

Resources