0

I'm using MS Azure ML and have found that when I start a Notebook (from the Azure ML Studio) it is executing in a a different environment than if I create a Python script and run it from the studio. I want to be able to create a specific environment and have the Notebook use that. The environment that the Notebook seems to run does not contain the packages I need and I want to preserve different environments.

4

2 回答 2

0

首先打开一个终端,使用与您的笔记本相同的计算目标,然后使用现有环境,您可以执行以下操作:

conda activate existing_env
conda install ipykernel
python -m ipykernel install --user --name existing_env --display-name "Python 3.8 - Existing Environment"   

但是,要创建新环境并在 AzureML Notebook 中使用它,您必须执行以下命令:

conda create --name new_env python=3.8
conda activate new_env
conda install pip
conda install ipykernel
python -m ipykernel install --user --name new_env --display-name "Python 3.8 - New Environment"

最后但并非最不重要的一点是,您必须编辑 Jupyter Kernel 显示名称:

重要请确保您能够轻松地执行所有这些步骤:

jupyter kernelspec list
cd <folder-that-matches-the-kernel-of-your-environment>
sudo nano kernel.json

然后编辑名称以匹配您想要的名称并保存文件。

于 2022-01-07T11:09:28.433 回答
0

使用 Pip、Conda、Docker映像Dockerfile,我们可以为您自己的脚本创建 ML 环境。

我希望能够创建一个特定的环境

要手动创建环境,

from azureml.core.environment import Environment
Environment(name="myenv")

使用 Conda 依赖项或 pip 要求文件使用以下代码创建环境:

#### From a Conda specification file
myenv = Environment.from_conda_specification(name = "myenv",
                                             file_path = "path-to-conda-specification-file")

#### From a pip requirements file
myenv = Environment.from_pip_requirements(name = "myenv",
                                          file_path = "path-to-pip-requirements-file")

Notebook 似乎运行的环境不包含我需要的包,我想保留不同的环境。

要将包添加到您的环境中,可以使用 Conda、pip 或私有 Wheel 文件,但建议使用CondaDependency类。

from azureml.core.environment import Environment
from azureml.core.conda_dependencies import CondaDependencies

myenv = Environment(name="myenv")
conda_dep = CondaDependencies()

#### Installs numpy version 1.17.0 conda package
conda_dep.add_conda_package("numpy==1.17.0")

#### Installs pillow package
conda_dep.add_pip_package("pillow")

#### Adds dependencies to PythonSection of myenv
myenv.python.conda_dependencies=conda_dep

使用命令在工作区中注册您的环境myenv.register(workspace=ws)

list(workspace)列出工作区中的环境

要运行您的特定环境:

from azureml.core import Run
Run.get_environment()

要将 Conda 环境作为内核安装到笔记本中,请参阅添加新的 Jupyter 内核,代码应按相同顺序执行:

conda create --name newenv
conda activate newenv
conda install pip
conda install ipykernel
python -m ipykernel install --user --name newenv --display-name "Python (newenv)"

要添加、更改或删除 Jupyter 内核,请参阅这篇文章

有关详细信息,请参阅此文档

于 2022-01-07T12:06:17.107 回答