How to customize a conda environment
To perform meaningful calculations, we have to install additional packages in our conda environment. If you don’t have a conda environment installed, refer to: How to create and activate a conda environment. Here, we restrict ourselves to a simple example. The code we are going to work with is the following:
import torch
# setting device on GPU if available, else CPU
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
print('Using device:', device)
print()
# additional info when using cuda
if device.type == 'cuda':
print(torch.cuda.get_device_name(0))
You can copy the code to a Jupyter notebook or use your own example. Here the file is mycode.ipynb
.
-
Your Jupyter Notebook file must be in your
home/<username>
directory. -
In this example, the
Torch
library is required. Compatible versions are listed in https://download.pytorch.org/whl/torch_stable.html. -
Install
PyTorch
. To do so, activate the conda environment that you want to work with and then install the package in it:conda activate <name> conda install pytorch -c pytorch -y
-
In this example, you may need to install
numpy
also, thenpip install numpy
-
Once the installation is completed you can list all your packages considering
conda list
-
Your environment now has the necessary packages to run
mycode.ipynb
. To run it, refer to: How to run a Jupyter notebook file.