如何安装 ML.NET 命令行接口 (CLI) 工具How to install the ML.NET Command-Line Interface (CLI) tool

了解如何在 Windows、Mac 或 Linux 上安装 ML.NET CLI(命令行接口)。Learn how to install the ML.NET CLI (command-line interface) on Windows, Mac, or Linux.

ML.NET CLI 使用自动化机器学习 (AutoML) 和训练数据集生成质量良好的 ML.NET 模型和源代码。The ML.NET CLI generates good quality ML.NET models and source code using automated machine learning (AutoML) and a training dataset.

备注

本主题涉及目前处于预览状态的 ML.NET CLI 和 ML.NET AutoML,且材料可能会有所变化。This topic refers to ML.NET CLI and ML.NET AutoML, which are currently in Preview, and material may be subject to change.

先决条件Pre-requisites

可以使用 Visual Studio 按 F5 键或 dotnet run (.NET Core CLI) 运行生成的 C# 代码项目。You can run the generated C# code projects with Visual Studio by pressing the F5 key or with dotnet run (.NET Core CLI).

注意:如果在安装 .NET Core SDK 后,dotnet tool 命令不起作用,请从 Windows 注销并再次登录。Note: If after installing .NET Core SDK the dotnet tool command is not working, sign out from Windows and sign in again.

安装Install

ML.NET CLI 的安装方式与任何其他 dotnet 全局工具一样。The ML.NET CLI is installed like any other dotnet Global Tool. 使用 dotnet tool install .NET Core CLI 命令。You use the dotnet tool install .NET Core CLI command.

以下示例显示如何在默认 NuGet 源位置安装 ML.NET CLI:The following example shows how to install the ML.NET CLI in the default NuGet feed location:

dotnet tool install -g mlnet

如果无法安装该工具(即,如果它在默认 NuGet 源中不可用),则会显示错误消息。If the tool can't be installed (that is, if it is not available at the default NuGet feed), error messages are displayed. 检查所需源是否正在被检查。Check that the feeds you expected are being checked.

如果安装成功,会出现一条消息,显示用于调用工具的命令以及所安装的版本,类似于以下示例:If installation is successful, a message is displayed showing the command used to call the tool and the version installed, similar to the following example:

You can invoke the tool using the following command: mlnet
Tool 'mlnet' (version 'X.X.X') was successfully installed.

可以通过键入以下命令来确认安装是否成功:You can confirm the installation was successful by typing the following command:

mlnet

应看到 mlnet 工具的可用命令帮助,例如“classification”命令。You should see the help for available commands for the mlnet tool such as the 'classification' command.

安装特定版本Install a specific release version

如果想安装工具的预发布版本或特定版本,可以采用以下格式指定框架If you're trying to install a pre-release version or a specific version of the tool, you can specify the framework using the following format:

dotnet tool install -g mlnet --framework <FRAMEWORK>

还可以通过键入以下命令来检查包是否已正确安装:You can also check if the package is properly installed by typing the following command:

dotnet tool list -g

卸载 CLI 包Uninstall the CLI package

键入以下命令,从本地计算机卸载包:Type the following command to uninstall the package from your local machine:

dotnet tool uninstall mlnet -g

更新 CLI 包Update the CLI package

键入以下命令,从本地计算机更新包:Type the following command to update the package from your local machine:

dotnet tool update -g mlnet

设置 CLI 建议(Tab 自动补全)Set up CLI suggestions (tab-based auto-completion)

由于 ML.NET CLI 基于 System.CommandLine,它具有对 Tab 补全的内置支持。Since the ML.NET CLI is based on System.CommandLine, it has built-in support for tab completion.

Tab 自动补全工作原理的示例如以下动画所示:An example of how tab auto completion works is shown in the following animation:

图像

“Tab 自动补全”(参数建议)适用于 Windows PowerShellmacOS/Linux bash,但它不适用于 Windows CMD'Tab-based auto-completion' (parameter suggestions) works on Windows PowerShell and macOS/Linux bash but it won't work on Windows CMD.

若要启用它,在当前预览版本中,最终用户必须为每个 shell 执行一次相关步骤,如下所述。To enable it, in the current preview version, the end user has to take a few steps once per shell, outlined below. 完成此操作后,补全将适用于使用 System.CommandLine 编写的所有应用,例如 ML.NET CLI。Once this is done, completions will work for all apps written using System.CommandLine such as the ML.NET CLI.

在想要启用补全的计算机上,需要执行两个操作。On the machine where you'd like to enable completion, you'll need to do two things.

  1. 通过运行以下命令安装 dotnet-suggest 全局工具:Install the dotnet-suggest global tool by running the following command:

    dotnet tool install dotnet-suggest -g
    
  2. 将适当的填充码脚本添加到 shell 配置文件中。Add the appropriate shim script to your shell profile. 可能必须创建一个 shell 配置文件。You may have to create a shell profile file. 填充码脚本会将补全请求从 shell 转发到 dotnet-suggest 工具,其会委托到相应的基于 System.CommandLine 的应用。The shim script will forward completion requests from your shell to the dotnet-suggest tool, which delegates to the appropriate System.CommandLine-based app.

    • 对于 bash,将 dotnet-suggest-shim.bash 的内容添加到 ~/.bash_profileFor bash, add the contents of dotnet-suggest-shim.bash to ~/.bash_profile.

    • 对于 PowerShell,将 dotnet-suggest-shim.ps1 的内容添加到 PowerShell 配置文件中。For PowerShell, add the contents of dotnet-suggest-shim.ps1 to your PowerShell profile. 可以通过在控制台中运行以下命令来查找 PowerShell 配置文件的预期路径:You can find the expected path to your PowerShell profile by running the following command in your console:

    echo $profile
    

(对于其他 shell,查找或创建问题。)(For other shells, look for or open an issue.)

安装目录Installation directory

ML.NET CLI 可安装在默认目录或特定位置。The ML.NET CLI can be installed in the default directory or in a specific location. 默认目录为:The default directories are:

(OS)OS 路径Path
Linux/macOSLinux/macOS $HOME/.dotnet/tools
WindowsWindows %USERPROFILE%\.dotnet\tools

这些位置在首次运行 SDK 时添加到用户的路径,所以可以直接调用安装在这里的全局工具。These locations are added to the user's path when the SDK is first run, so Global Tools installed there can be called directly.

注意:全局工具特定于用户,而不针对计算机全局。Note: the Global Tools are user-specific, not machine global. 特定于用户的意思是无法在一台计算机上安装所有用户都可用的全局工具。Being user-specific means you cannot install a Global Tool that is available to all users of the machine. 只有安装了该工具的用户配置文件才能使用它。The tool is only available for each user profile where the tool was installed.

也可在特定目录安装全局工具。Global Tools can also be installed in a specific directory. 如果在特定目录安装全局工具,用户必须确保命令是可用的,方法是将该目录添加至路径、使用指定目录调用命令或从特定目录调用该工具。When installed in a specific directory, the user must ensure the command is available, by including that directory in the path, by calling the command with the directory specified, or calling the tool from within the specified directory. 在这种情况下,.NET Core CLI 不将此地址自动添加至 PATH 环境变量。In this case, the .NET Core CLI doesn't add this location automatically to the PATH environment variable.

请参阅See also