使用 CNTK 定型模型Train a model with CNTK

在本教學課程中,我們將會使用 Visual Studio Tools for AI (可供建置、測試及部署深度學習和 AI 解決方案的開發擴充功能) 來定型模型。In this tutorial, we'll use Visual Studio Tools for AI, a development extension for building, testing, and deploying Deep Learning & AI solutions, to train a model.

我們將使用 Microsoft Cognitive Toolkit (CNTK) 架構和 MNIST 資料集來訓練模型,其中有 60000 個範例的訓練組和 10000 個手寫數字範例的測試組。We'll train the model with the Microsoft Cognitive Toolkit (CNTK) framework and the MNIST dataset, which has a training set of 60,000 examples and a test set of 10,000 examples of handwritten digits. 我們接著將使用開放類神經網路 Exchange (ONNX) 格式儲存模型以利於與 Windows ML 一起使用。We'll then save the model using the Open Neural Network Exchange (ONNX) format to use with Windows ML.

必要條件Prerequisites

安裝 Visual Studio Tools for AIInstall Visual Studio Tools for AI

若要開始,您需要下載並安裝 Visual StudioTo get started, you'll need to download and install Visual Studio. 打開 Visual Studio 後,啟用 Visual Studio Tools for AI 擴充功能:Once you have Visual Studio open, activate the Visual Studio Tools for AI extension:

  1. 按一下 Visual Studio 中的選單列,並選取 [擴充功能與更新...]。Click on the menu bar in Visual Studio and select "Extensions and Updates..."
  2. 按一下 [線上] 標籤並選取 [搜尋 Visual Studio Marketplace]。Click on "Online" tab and select "Search Visual Studio Marketplace."
  3. 搜尋「Visual Studio Tools for AI」。Search for "Visual Studio Tools for AI."
  4. 按一下 [下載] 按鈕。Click on the Download button.
  5. 安裝後,請重新開機 Visual Studio。After installation, restart Visual Studio.

Visual Studio 重新開機後,該擴充功能將會使用中。The extension will be active once Visual Studio restarts. 如果遇到問題,請查看 尋找 Visual Studio 的擴充功能If you're having trouble, check out Finding Visual Studio extensions.

下載範例程式碼Download sample code

在 GitHub 中下載 AI 範例存放庫。Download the Samples for AI repo on GitHub. 本範例涵蓋了取得開始使用跨 TensorFlow、CNTK、Theano 等等範圍的深入學習。The samples cover getting started with deep learning across TensorFlow, CNTK, Theano and more.

安裝 CNTKInstall CNTK

安裝適用於 Windows 的 Python CNTKInstall CNTK for Python on Windows. 請注意,如果您尚未安裝 Python,則必須安裝 Python。Note that you'll also have to install Python if you haven't already.

另外,為了讓您的機器準備深度學習模型開發,請參閱準備您的開發環境以取得安裝 Python、CNTK、TensorFlow、NVIDIA GPU 驅動程式 (選用) 等等的簡化安裝程序。Alternatively, to prepare your machine for deep learning model development, see Preparing your development environment for a simplified installer for installing Python, CNTK, TensorFlow, NVIDIA GPU drivers (optional) and more.

1.開啟專案1. Open project

啟動 Visual Studio,然後選取 [檔案] > [開啟] > [專案/解決方案] 。Launch Visual Studio and select File > Open > Project/Solution. 從 AI 存放庫樣本中選擇 examples\cntk\python 資料夾,然後打開 CNTKPythonExamples.sln 檔案。From the Samples for AI repository, select the examples\cntk\python folder, and open the CNTKPythonExamples.sln file.

開啟解決方案

2.將模型定型2. Train the model

若要將 MNIST 專案設定為啟動專案,請按右鍵 python 專案並選擇 [設定為啟動專案] 。To set the MNIST project as the startup project, right-click on the python project and select Set as Startup Project.

開啟解決方案

接著,按下 F5 或綠色 [執行] 按鈕,以開啟 train_mnist_onnx.py 檔案並執行專案。Next, open the train_mnist_onnx.py file and Run the project by pressing F5 or the green Run button.

3.檢視模型並將其新增到您的應用程式3. View the model and add it to your app

現在,受過訓練的 mnist.onnx 模型檔案應在 samples-for-ai/examples/cntk/python/MNIST 資料夾中。Now, the trained mnist.onnx model file should be in the samples-for-ai/examples/cntk/python/MNIST folder.

4.深入了解4. Learn more

若要了解如何使用 Azure GPU 虛擬電腦 及其他來加速訓練深度學習模型,請造訪 Microsoft 的人工智慧Microsoft 機器學習技術To learn how to speed up training deep learning models by using Azure GPU Virtual Machines and more, visit Artificial Intelligence at Microsoft and Microsoft Machine Learning Technologies.

注意

使用下列資源取得 Windows ML 的說明:Use the following resources for help with Windows ML:

  • 如需詢問或回答有關 Windows ML 的技術問題,請使用 Stack Overflow 上的 windows-machine-learning 標籤。To ask or answer technical questions about Windows ML, please use the windows-machine-learning tag on Stack Overflow.
  • 如需回報錯誤 (bug),請在 GitHub 上提出問題。To report a bug, please file an issue on our GitHub.
  • 如需要求功能,請前往 Windows 開發人員意見反應To request a feature, please head over to Windows Developer Feedback.