MR 和 Azure 307:机器学习MR and Azure 307: Machine learning


备注

混合现实学院教程在制作时考虑到了 HoloLens(第一代)和混合现实沉浸式头戴显示设备。The Mixed Reality Academy tutorials were designed with HoloLens (1st gen) and Mixed Reality Immersive Headsets in mind. 因此,对于仍在寻求这些设备的开发指导的开发人员而言,我们觉得很有必要保留这些教程。As such, we feel it is important to leave these tutorials in place for developers who are still looking for guidance in developing for those devices. 我们 不会 在这些教程中更新 HoloLens 2 所用的最新工具集或集成相关的内容。These tutorials will not be updated with the latest toolsets or interactions being used for HoloLens 2. 我们将维护这些教程,使之持续适用于支持的设备。They will be maintained to continue working on the supported devices. 将来会发布一系列新教程,这些教程将演示如何针对 HoloLens 2 进行开发。There will be a new series of tutorials that will be posted in the future that will demonstrate how to develop for HoloLens 2. 此通知将在发布时通过指向这些教程的链接进行更新。This notice will be updated with a link to those tutorials when they are posted.


最终产品-启动

在本课程中,你将了解如何使用 Azure 机器学习 Studio (经典) 将机器学习 (ML) 功能添加到混合现实应用程序。In this course, you will learn how to add Machine Learning (ML) capabilities to a mixed reality application using Azure Machine Learning Studio (classic).

Azure 机器学习 Studio (经典) 是一项 Microsoft 服务,它为开发人员提供了大量机器学习算法,可帮助进行数据输入、输出、准备和可视化。Azure Machine Learning Studio (classic) is a Microsoft service, which provides developers with a large number of machine learning algorithms, which can help with data input, output, preparation, and visualization. 然后,可以通过这些组件开发预测分析试验,对其进行循环访问,并使用它来训练模型。From these components, it is then possible to develop a predictive analytics experiment, iterate on it, and use it to train your model. 完成培训后,可以在 Azure 云中操作模型,使其可以对新数据进行评分。Following training, you can make your model operational within the Azure cloud, so that it can then score new data. 有关详细信息,请访问 Azure 机器学习 Studio (经典) "页For more information, visit the Azure Machine Learning Studio (classic) page.

完成本课程后,你将拥有一个混合现实沉浸式耳机式应用程序,并将了解如何执行以下操作:Having completed this course, you will have a mixed reality immersive headset application, and will have learned how do the following:

  1. Azure 机器学习 Studio (经典) 门户提供销售数据表,并设计一个算法来预测未来的热门项目的销售情况。Provide a table of sales data to the Azure Machine Learning Studio (classic) portal, and design an algorithm to predict future sales of popular items.
  2. 创建可从 ML 服务接收和解释预测数据的 Unity 项目Create a Unity Project, which can receive and interpret prediction data from the ML service.
  3. 通过在货位上提供最常用的销售项,直观显示 Unity 项目 中的断言而数据。Display the predication data visually within the Unity Project, through providing the most popular sales items, on a shelf.

在您的应用程序中,您将由您来决定如何将结果与您的设计相集成。In your application, it is up to you as to how you will integrate the results with your design. 本课程旨在向您介绍如何将 Azure 服务与 Unity 项目集成。This course is designed to teach you how to integrate an Azure Service with your Unity Project. 您可以使用您在本课程中获得的知识来增强混合现实应用程序的工作。It is your job to use the knowledge you gain from this course to enhance your mixed reality application.

本课程是一个自包含教程,并不直接涉及任何其他混合现实实验室。This course is a self-contained tutorial, which does not directly involve any other Mixed Reality Labs.

设备支持Device support

课程Course HoloLensHoloLens 沉浸式头戴显示设备Immersive headsets
MR 和 Azure 307:机器学习MR and Azure 307: Machine learning ✔️✔️ ✔️✔️

备注

尽管本课程主要侧重于 Windows Mixed Reality 沉浸式 (VR) 耳机,但你也可以将本课程中学习的内容应用于 Microsoft HoloLens。While this course primarily focuses on Windows Mixed Reality immersive (VR) headsets, you can also apply what you learn in this course to Microsoft HoloLens. 在本课程中,你将看到有关支持 HoloLens 时可能需要执行的任何更改的说明。As you follow along with the course, you will see notes on any changes you might need to employ to support HoloLens. 使用 HoloLens 时,可能会在语音捕获过程中注意到某些回声。When using HoloLens, you may notice some echo during voice capture.

先决条件Prerequisites

备注

本教程专为具有 Unity 和 c # 基本经验的开发人员设计。This tutorial is designed for developers who have basic experience with Unity and C#. 请注意,本文档中的先决条件和书面说明表明了编写 (2018) 时测试和验证的内容。Please also be aware that the prerequisites and written instructions within this document represent what has been tested and verified at the time of writing (May 2018). 您可以随意使用最新的软件(如 安装工具一文中所述),但不应假定本课程中的信息将与下面列出的内容完全匹配。You are free to use the latest software, as listed within the install the tools article, though it should not be assumed that the information in this course will perfectly match what you'll find in newer software than what's listed below.

本课程建议采用以下硬件和软件:We recommend the following hardware and software for this course:

开始之前Before you start

若要避免在生成此项目时遇到问题,强烈建议你在根或近乎根文件夹中创建本教程中所述的项目 (长文件夹路径在生成时) 会导致问题。To avoid encountering issues building this project, it is strongly suggested that you create the project mentioned in this tutorial in a root or near-root folder (long folder paths can cause issues at build-time).

第1章-Azure 存储帐户设置Chapter 1 - Azure Storage Account setup

若要使用 Azure Translator API,你将需要配置服务的实例,使其可用于你的应用程序。To use the Azure Translator API, you will need to configure an instance of the service to be made available to your application.

  1. 登录到 Azure 门户Log in to the Azure Portal.

    备注

    如果还没有 Azure 帐户,则需要创建一个。If you do not already have an Azure account, you will need to create one. 如果在课堂或实验室中按照本教程进行学习,请咨询教师或 proctors,以获得设置新帐户的帮助。If you are following this tutorial in a classroom or lab situation, ask your instructor or one of the proctors for help setting up your new account.

  2. 登录后,单击左侧菜单中的 " 存储帐户 "。Once you are logged in, click on Storage Accounts in the left menu.

    Azure 存储帐户设置

    备注

    在较新的门户中,可能已将 " 新建 " 一词替换为 " 创建资源"。The word New may have been replaced with Create a resource, in newer portals.

  3. 在 " 存储帐户 " 选项卡上,单击 " 添加"。On the Storage Accounts tab, click on Add.

    Azure 存储帐户设置

  4. 在 " 创建存储帐户 " 面板中:In the Create Storage Account panel:

    1. 插入帐户的 名称 ,请注意,此字段仅接受数字和小写字母。Insert a Name for your account, be aware this field only accepts numbers, and lowercase letters.

    2. 对于 部署模型,选择 " 资源管理器"。For Deployment model, select Resource manager.

    3. 对于 " 帐户类型",请选择 " 存储 (常规用途 v1)For Account kind, select Storage (general purpose v1).

    4. 对于“性能”,请选择“标准”。For Performance, select Standard.

    5. 对于 复制 ,请选择 " 读取-访问-异地冗余存储" (GRS)For Replication select Read-access-geo-redundant storage (RA-GRS).

    6. 使 安全传输 保持 禁用状态Leave Secure transfer required as Disabled.

    7. 选择一个“订阅” 。Select a Subscription.

    8. 选择一个 资源组 ,或创建一个新的资源组。Choose a Resource Group or create a new one. 资源组提供一种监视、控制访问、预配和管理 Azure 资产集合的计费的方法。A resource group provides a way to monitor, control access, provision and manage billing for a collection of Azure assets. 建议保留与单个项目关联的所有 Azure 服务 (例如,这些实验室) 在常见资源组) 下。It is recommended to keep all the Azure services associated with a single project (e.g. such as these labs) under a common resource group).

      若要了解有关 Azure 资源组的详细信息,请 访问资源组一文If you wish to read more about Azure Resource Groups, please visit the resource group article.

    9. 如果要创建新的资源组) ,请确定资源组 (的 位置Determine the Location for your resource group (if you are creating a new Resource Group). 此位置理想情况下会在应用程序运行所在的区域中。The location would ideally be in the region where the application would run. 某些 Azure 资产仅在特定区域提供。Some Azure assets are only available in certain regions.

  5. 还需要确认是否已了解应用于此服务的条款和条件。You will also need to confirm that you have understood the Terms and Conditions applied to this Service.

    Azure 存储帐户设置

  6. 单击 " 创建" 后,需要等待创建服务,这可能需要一分钟时间。Once you have clicked on Create, you will have to wait for the service to be created, this might take a minute.

  7. 创建服务实例后,门户中将显示一个通知。A notification will appear in the portal once the Service instance is created.

    Azure 存储帐户设置

第2章-Azure 机器学习 Studio (经典) Chapter 2 - The Azure Machine Learning Studio (classic)

若要使用 Azure 机器学习,需要配置机器学习服务的实例,使其可用于你的应用程序。To use the Azure Machine Learning, you will need to configure an instance of the Machine Learning service to be made available to your application.

  1. 在 Azure 门户中,单击左上角的 " 新建 ",然后搜索 机器学习 Studio 工作区,按 enterIn the Azure Portal, click on New in the top left corner, and search for Machine Learning Studio Workspace, press Enter.

    Azure 机器学习 Studio (经典)

  2. 新页将提供 机器学习 Studio 工作区 服务的说明。The new page will provide a description of the Machine Learning Studio Workspace service. 在此提示符的左下方,单击 " 创建 " 按钮以创建与此服务的关联。At the bottom left of this prompt, click the Create button, to create an association with this service.

  3. 单击 " 创建" 后,将显示一个面板,需要在其中提供有关新 机器学习 Studio 服务 的一些详细信息:Once you have clicked on Create, a panel will appear where you need to provide some details about your new Machine Learning Studio service:

    1. 为此服务实例插入所需的 工作区名称Insert your desired Workspace name for this service instance.

    2. 选择一个“订阅” 。Select a Subscription.

    3. 选择一个 资源组 ,或创建一个新的资源组。Choose a Resource Group or create a new one. 资源组提供一种监视、控制访问、预配和管理 Azure 资产集合的计费的方法。A resource group provides a way to monitor, control access, provision and manage billing for a collection of Azure assets. 建议保留与单个项目关联的所有 Azure 服务 (例如,这些实验室) 在常见资源组) 下。It is recommended to keep all the Azure services associated with a single project (e.g. such as these labs) under a common resource group).

      若要了解有关 Azure 资源组的详细信息,请 访问资源组一文If you wish to read more about Azure Resource Groups, please visit the resource group article.

    4. 如果要创建新的资源组) ,请确定资源组 (的 位置Determine the Location for your resource group (if you are creating a new Resource Group). 此位置理想情况下会在应用程序运行所在的区域中。The location would ideally be in the region where the application would run. 某些 Azure 资产仅在特定区域提供。Some Azure assets are only available in certain regions. 应使用在上一章中用于创建 Azure 存储的同一资源组。You should use the same resource group that you used for creating the Azure Storage in the previous Chapter.

    5. 对于 " 存储帐户 " 部分,单击 " 使用现有 项",然后单击下拉菜单,然后从那里单击你在上一章中创建的 存储帐户For the Storage account section, click Use existing, then click the dropdown menu, and from there, click the Storage Account you created in the last Chapter.

    6. 从下拉菜单中选择合适的 工作区定价层Select the appropriate Workspace pricing tier for you, from the dropdown menu.

    7. 在 " Web 服务计划 " 部分中 ,单击 " 新建", 然后在 "文本" 字段中插入它的名称。Within the Web service plan section, click Create new, then insert a name for it in the text field.

    8. 从 " Web 服务计划定价层 " 部分,选择所选的价格层。From the Web service plan pricing tier section, select the price tier of your choice. 名为 开发测试 Standard 的开发测试层应免费提供给您。A development testing tier called DEVTEST Standard should be available to you at no charge.

    9. 还需要确认是否已了解应用于此服务的条款和条件。You will also need to confirm that you have understood the Terms and Conditions applied to this Service.

    10. 单击“创建”。Click Create.

      Azure 机器学习 Studio (经典)

  4. 单击 " 创建" 后,需要等待创建服务,这可能需要一分钟时间。Once you have clicked on Create, you will have to wait for the service to be created, this might take a minute.

  5. 创建服务实例后,门户中将显示一个通知。A notification will appear in the portal once the Service instance is created.

    Azure 机器学习 Studio (经典)

  6. 单击通知以浏览新服务实例。Click on the notification to explore your new Service instance.

    Azure 机器学习 Studio (经典)

  7. 单击通知中的 " 中转到资源 " 按钮以浏览新服务实例。Click the Go to resource button in the notification to explore your new Service instance.

  8. 在显示的页面的 " 其他链接 " 部分下,单击 " 启动机器学习 Studio",这会将浏览器定向到 机器学习 studio 门户。In the page displayed, under the Additional Links section, click Launch Machine Learning Studio, which will direct your browser to the Machine Learning Studio portal.

    Azure 机器学习 Studio (经典)

  9. 使用右上角的 " 登录 " 按钮登录到机器学习 Studio (经典) 。Use the Sign In button, at the top right or in the center, to log into your Machine Learning Studio (classic).

    Azure 机器学习 Studio (经典)

第3章-机器学习 Studio (经典) :数据集设置Chapter 3 - The Machine Learning Studio (classic): Dataset setup

机器学习算法的一种方法是分析现有数据,然后根据现有数据集尝试预测未来结果。One of the ways Machine Learning algorithms work is by analyzing existing data and then attempting to predict future results based on the existing data set. 这通常意味着您拥有的现有数据越多,算法的预测未来结果就越好。This generally means that the more existing data you have, the better the algorithm will be at predicting future results.

本课程提供了一个示例表,此课程称为 ProductsTableCSV, 可在此处下载A sample table is provided to you, for this course, called ProductsTableCSV and can be downloaded here.

重要

上述 .zip 文件包含 ProductsTableCSVunitypackage,这将在 第6章中需要。The above .zip file contains both the ProductsTableCSV and the .unitypackage, which you will need in Chapter 6. 这一章中也提供了此包,但它与 csv 文件分离。This package is also provided within that Chapter, though separate to the csv file.

此示例数据集包含2017年每一天的每个小时的最佳销售对象的记录。This sample data set contains a record of the best-selling objects at every hour of each day of the year 2017.

机器学习 Studio (经典) :数据集设置

例如,在2017的第1天, (小时 13) ,最佳销售项是 salt 和辣椒。For example, on day 1 of 2017, at 1pm (hour 13), the best-selling item was salt and pepper.

此示例表包含9998个条目。This sample table contains 9998 entries.

  1. 返回到 机器学习 Studio (经典) 门户,并将此表添加为 ML 的 数据集Head back to the Machine Learning Studio (classic) portal, and add this table as a Dataset for your ML. 单击屏幕左下角的 " + 新建 " 按钮即可执行此操作。Do this by clicking the + New button in the bottom left corner of the screen.

    机器学习 Studio (经典) :数据集设置

  2. 部分将从底部开始,并在左侧有导航面板。A section will come up from the bottom, and within that there is navigation panel on the left. 单击 "数据集",然后依次单击 " 数据集"、" 本地文件"。Click Dataset, then to the right of that, From Local File.

    机器学习 Studio (经典) :数据集设置

  3. 按照以下步骤上传新的 数据集Upload the new Dataset by following these steps:

    1. 此时将显示 "上传" 窗口,您可以在其中 浏览 硬盘驱动器以查找新的数据集。The upload window will appear, where you can Browse your hard drive for the new dataset.

      机器学习 Studio (经典) :数据集设置

    2. 选择后,返回到 "上传" 窗口,将复选框保留为 "未选中"。Once selected, and back in the upload window, leave the checkbox unticked.

    3. 在下面的文本字段中,输入 ProductsTableCSV.csv 作为数据集的名称 (不过,) 自动添加。In the text field below, enter ProductsTableCSV.csv as the name for the dataset (though should automatically be added).

    4. 使用 " 类型" 下拉菜单,选择 " 带有标题 ( 的通用 CSV 文件)Using the dropdown menu for Type, select Generic CSV File with a header (.csv).

    5. 按上传窗口右下角的勾选标记,将上传 数据集Press the tick in the bottom right of the upload window, and your Dataset will be uploaded.

第4章-机器学习 Studio (经典) :试验Chapter 4 - The Machine Learning Studio (classic): The Experiment

在构建机器学习系统之前,你需要构建一个试验来验证你的数据的理论。Before you can build your machine learning system, you will need to build an experiment, to validate your theory about your data. 在结果中,你将知道是否需要更多数据,或者数据与可能的结果之间是否没有关联。With the results, you will know whether you need more data, or if there is no correlation between the data and a possible outcome.

开始创建试验:To start creating an experiment:

  1. 再次单击页面左下角的 " + 新建" 按钮,然后单击 "试验 > 空白试验"。Click again on the + New button on the bottom left of the page, then click on Experiment > Blank Experiment.

    机器学习 Studio (经典) :试验

  2. 将显示一个新页面,其中包含一个空白实验:A new page will be displayed with a blank Experiment:

  3. 在左侧面板中,展开 "保存的数据集" " > 数据集",并将 ProductsTableCSV 拖到 试验画布 上。From the panel on the left expand Saved Datasets > My Datasets and drag the ProductsTableCSV on to the Experiment Canvas.

    机器学习 Studio (经典) :试验

  4. 在左侧面板中,展开 "数据转换 > 示例并拆分"。In the panel on the left, expand Data Transformation > Sample and Split. 然后将 拆分 数据项拖到 试验画布 中。Then drag the Split Data item in to the Experiment Canvas. 拆分数据项会将数据集拆分为两个部分。The Split Data item will split the data set into two parts. 用于训练机器学习算法的部分。One part you will use for training the machine learning algorithm. 第二部分将用于计算生成的算法的准确性。The second part will be used to evaluate the accuracy of the algorithm generated.

    机器学习 Studio (经典) :试验

  5. 在右侧面板中 (选择 ") 上的" 拆分数据项 "时,将 第一个输出数据集中的行部分 编辑为 0.7In the right panel (while the Split Data item on the canvas is selected), edit the Fraction of rows in the first output dataset to 0.7. 这会将数据拆分为两个部分,第一部分将为70% 的数据,第二个部分将是剩余的30%。This will split the data into two parts, the first part will be 70% of the data, and the second part will be the remaining 30%. 若要确保数据随机拆分,请确保 随机拆分 复选框保持选中状态。To ensure that the data is split randomly, make sure the Randomized split checkbox remains checked.

    机器学习 Studio (经典) :试验

  6. 将连接从画布上 ProductsTableCSV 项的基拖到拆分数据项的顶部。Drag a connection from the base of the ProductsTableCSV item on the canvas to the top of the Split Data item. 这会将这些项连接起来,并将 ProductsTableCSV 数据集输出 (数据) 发送到拆分数据输入。This will connect the items and send the ProductsTableCSV dataset output (the data) to the Split Data input.

    机器学习 Studio (经典) :试验

  7. 在左侧的 试验 面板中,展开 机器学习 > 训练"。In the Experiments panel on the left side, expand Machine Learning > Train. 训练模型 项拖出到试验画布。Drag the Train Model item out in to the Experiment canvas. 画布应与下面的外观相同。Your canvas should look the same as the below.

    机器学习 Studio (经典) :试验

  8. "_ Split 数据项" 的 左下方 _ 将连接拖到 "定型模型" 项的 右上方From the **bottom left_ of the _* Split Data* item drag a connection to the top right of the Train Model item. 定型模型将使用数据集中的第一个70% 拆分来训练该算法。The first 70% split from the dataset will be used by the Train Model to train the algorithm.

    机器学习 Studio (经典) :试验

  9. 在画布上选择 " 训练模型 " 项,然后在 " 属性 " 面板中 (浏览器窗口的右侧) 单击 " 启动列选择器 " 按钮。Select the Train Model item on the canvas, and in the Properties panel (on the right-hand side of your browser window) click the Launch column selector button.

  10. 在文本框中键入 product ,然后按 enter产品 将设置为用于定型预测的列。In the text box type product and then press Enter, product will be set as a column to train predictions. 在此之后,单击右下角的 勾选标记 以关闭选择对话框。Following this, click on the tick in the bottom-right corner to close the selection dialog.

    机器学习 Studio (经典) :试验

  11. 你将定型 多类逻辑回归 算法,根据一天中的小时和日期预测最销售的 产品You are going to train a Multiclass Logistic Regression algorithm to predict the most sold product based on the hour of the day and the date. 此文档超出了本文档的讨论范围,可以说明 Azure 机器学习 studio 提供的不同算法的详细信息,但你可以从机器学习算法备忘单中了解详细信息It is beyond the scope of this document to explain the details of the different algorithms provided by the Azure Machine Learning studio, though, you can find out more from the Machine Learning Algorithm Cheat Sheet

  12. 从左侧的 "试验项" 面板中,展开 "机器学习 > 初始化模型 > 分类,然后将"多类逻辑回归"项拖到试验画布上。From the experiment items panel on the left, expand Machine Learning > Initialize Model > Classification, and drag the Multiclass Logistic Regression item on to the experiment canvas.

  13. 多类逻辑回归 的底部的输出连接到 定型模型 项的左上方输入。Connect the output, from the bottom of the Multiclass Logistic Regression, to the top-left input of the Train Model item.

    机器学习 Studio (经典) :试验

  14. 在左侧面板中的实验项列表中,展开 "机器学习 > 分数",然后将 "评分模型" 项拖到画布上。In list of experiment items in the panel on the left, expand Machine Learning > Score, and drag the Score Model item on to the canvas.

  15. 将来自 定型模型 底部的输出连接到 评分模型 的左上方输入。Connect the output, from the bottom of the Train Model, to the top-left input of the Score Model.

  16. 将从 拆分数据 向右下的输出连接到 评分模型 项的右上方输入。Connect the bottom-right output from Split Data, to the top-right input of the Score Model item.

    机器学习 Studio (经典) :试验

  17. 在左侧面板中的 实验 项列表中,展开 "机器学习 > 评估",然后将 "评估模型" 项拖到画布上。In the list of Experiment items in the panel on the left, expand Machine Learning > Evaluate, and drag the Evaluate Model item onto the canvas.

  18. 评分模型 的输出连接到 评估模型 的左上方输入。Connect the output from the Score Model to the top-left input of the Evaluate Model.

    机器学习 Studio (经典) :试验

  19. 您已经生成了第一个机器学习试验。You have built your first Machine Learning Experiment. 你现在可以保存并运行试验。You can now save and run the experiment. 在页面底部的菜单中,单击 " 保存 " 按钮以保存实验,并单击 " 运行 " 开始试验。In the menu at the bottom of the page, click on the Save button to save your experiment and then click Run to the start the experiment.

    机器学习 Studio (经典) :试验

  20. 可以在画布的右上方查看试验 状态You can see the status of the experiment in the top-right of the canvas. 请稍等片刻,等待试验结束。Wait a few moments for the experiment to finish.

    如果有很大的 (实际) 数据集,则可能会出现试验可能需要数小时才能运行。If you have a big (real world) dataset it is likely that the experiment could take hours to run.

    机器学习 Studio (经典) :试验

  21. 右键单击画布中的 " 评估模型 " 项,然后从上下文菜单中将鼠标悬停在 " 评估结果" 上,然后选择 " 可视化"。Right click on the Evaluate Model item in the canvas and from the context menu hover the mouse over Evaluation Results, then select Visualize.

    机器学习 Studio (经典) :试验

  22. 将显示计算结果,显示预测结果与实际结果。The evaluation results will be displayed showing the predicted outcomes versus the actual outcomes. 这将使用之前拆分的原始数据集的30% 来计算模型。This uses the 30% of the original dataset, that was split earlier, for evaluating the model. 您可以看到,结果并不是很好,理想情况下,每行中的最大数目是列中突出显示的项。You can see the results are not great, ideally you would have the highest number in each row be the highlighted item in the columns.

    机器学习 Studio (经典) :试验

  23. 关闭 结果Close the Results.

  24. 若要使用新训练的机器学习模型,你需要将其公开为 Web 服务To use your newly trained Machine Learning model you need to expose it as a Web Service. 为此,请单击页面底部菜单中的 " 设置 Web 服务 " 菜单项,然后单击 " 预测 Web 服务"。To do this, click on the Set Up Web Service menu item in the menu at the bottom of the page, and click on Predictive Web Service.

    机器学习 Studio (经典) :试验

  25. 将创建一个新选项卡,并合并定型模型以创建新的 web 服务。A new tab will be created, and the train model merged to create the new web service.

  26. 在页面底部的菜单中,单击 " 保存",然后单击 " 运行"。In the menu at the bottom of the page click Save, then click Run. 你将在实验画布的右上角显示状态 "已更新"。You will see the status updated in the top-right corner of the experiment canvas.

    机器学习 Studio (经典) :试验

  27. 运行完毕后,将在页面底部显示 " 部署 Web 服务 " 按钮。Once it has finished running, a Deploy Web Service button will appear at the bottom of the page. 你已准备好部署 web 服务。You are ready to deploy the web service. 单击页面底部菜单中的 " 部署 Web 服务 (经典) 。Click Deploy Web Service (Classic) in the menu at the bottom of the page.

    机器学习 Studio (经典) :试验

    你的浏览器可能会提示你允许弹出窗口(你应该 允许 这样做),尽管你可能需要再次按 " 部署 Web 服务 " (如果未显示 "部署" 页)。Your browser may prompt to allow a pop-up, which you should allow, though you may need to press Deploy Web Service again, if the deploy page does not show.

  28. 一旦创建了实验,你会被重定向到 " 仪表板 " 页面,你将在其中显示 API 密钥Once the Experiment has been created you will be redirected to a Dashboard page where you will have your API Key displayed. 将其复制到记事本中,你将很快在代码中需要它。Copy it into a notepad for the moment, you will need it in your code very soon. 记下 API 密钥后,请单击密钥下面的 "默认终结点" 部分中的 "请求/响应" 按钮。Once you have noted your API Key, click on the REQUEST/RESPONSE button in the Default Endpoint section underneath the Key.

    机器学习 Studio (经典) :试验

    备注

    如果单击此页中的 "测试",将能够输入输入数据并查看输出。If you click Test in this page, you will be able to enter input data and view the output. 输入 日期****和时间Enter the day and hour. 产品 条目留空。Leave the product entry blank. 然后单击 " 确认 " 按钮。Then click the Confirm button. 页面底部的输出将显示 JSON,表示每个产品选择的可能性。The output on the bottom of the page will show the JSON representing the likelihood of each product being the choice.

  29. 随即打开一个新网页,其中显示了有关机器学习 Studio (经典) 所需的请求结构的说明和示例。A new web page will open up, displaying the instructions and some examples about the Request structure required by the Machine Learning Studio (classic). 将此页中显示的 请求 URI 复制到记事本中。Copy the Request URI displayed in this page, into your notepad.

    机器学习 Studio (经典) :试验

现在,你已构建了一个机器学习系统,它提供了最有可能使用的产品,这些产品是基于历史购买数据销售的,并与一天中的时间和日期相关联。You have now built a machine learning system that provides the most likely product to be sold based on historical purchasing data, correlated with the time of the day and day of the year.

若要调用 web 服务,你将需要服务终结点的 URL 和服务的 API 密钥。To call the web service, you will need the URL for the service endpoint and an API Key for the service. 单击顶部菜单中的 " 使用 " 选项卡。Click on the Consume tab, from the top menu.

" 消耗 信息" 页将显示从代码中调用 web 服务所需的信息。The Consumption Info page will display the information you will need to call the web service from your code. 获取 主密钥请求-响应 URL 的副本。Take a copy of the Primary Key and the Request-Response URL. 下一章将需要这些项。You will need these in the next Chapter.

第5章-设置 Unity 项目Chapter 5 - Setting up the Unity Project

设置并测试混合现实沉浸式耳机。Set up and test your Mixed Reality Immersive Headset.

备注

本课程 需要移动控制器。You will not require Motion Controllers for this course. 如果需要支持设置沉浸式耳机,请单击 此处If you need support setting up the Immersive Headset, please click HERE.

  1. 打开 Unity 并创建名为 MR _ Default-machinelearning-southcentralus 的新 Unity 项目。Open Unity and create a new Unity Project called MR_MachineLearning. 请确保 "项目类型" 设置为 " 3d"。Make sure the project type is set to 3D.

  2. 当 Unity 处于打开状态时,有必要选中 "默认 脚本编辑器 " 设置为 " Visual Studio"。With Unity open, it is worth checking the default Script Editor is set to Visual Studio. 转到 "编辑 > 首选项",然后在新窗口中导航到 "外部工具"。Go to Edit > Preferences and then from the new window, navigate to External Tools. 外部脚本编辑器 更改为 Visual Studio 2017Change External Script Editor to Visual Studio 2017. 关闭 " 首选项 " 窗口。Close the Preferences window.

  3. 接下来,转到 "文件 > 生成设置",并通过单击 "切换平台" 按钮将平台切换到 通用 Windows 平台Next, go to File > Build Settings and switch the platform to Universal Windows Platform, by clicking on the *Switch Platform _ button.

  4. 另外,请确保:Also make sure that:

    1. _ 目标设备* 设置为 " 任何设备"。_ Target Device* is set to Any Device.

      对于 Microsoft HoloLens,将 " 目标设备 " 设置为 " hololens"。For the Microsoft HoloLens, set Target Device to HoloLens.

    2. 生成类型 设置为 D3DBuild Type is set to D3D.

    3. SDK 设置为 " 最新安装"。SDK is set to Latest installed.

    4. Visual Studio 版本 设置为 " 最新安装"。Visual Studio Version is set to Latest installed.

    5. "生成并运行" 设置为 "本地计算机"。Build and Run is set to Local Machine.

    6. 请不要担心现在设置 场景 ,因为稍后会提供这些信息。Do not worry about setting up Scenes right now, as these are provided later.

    7. 其余设置应保留为默认值。The remaining settings should be left as default for now.

      设置 Unity 项目

  5. 在 " 生成设置 " 窗口中,单击 " 播放机设置 " 按钮,这会在 检查器 所在的空间中打开相关面板。In the Build Settings window, click on the Player Settings button, this will open the related panel in the space where the Inspector is located.

  6. 在此面板中,需要验证几项设置:In this panel, a few settings need to be verified:

    1. 在 " 其他设置 " 选项卡中:In the Other Settings tab:

      1. 脚本****运行时版本 应为 试验 ( .net 4.6 等效) Scripting Runtime Version should be Experimental (.NET 4.6 Equivalent)

      2. 脚本后端 应为 *.net Scripting Backend should be **.NET_* _

      3. _ API 兼容级别* 应为 .net 4.6_ API Compatibility Level* should be .NET 4.6

        设置 Unity 项目

    2. 在 " 发布设置 " 选项卡的 " 功能" 下,检查:Within the Publishing Settings tab, under Capabilities, check:

      • InternetClientInternetClient

        设置 Unity 项目

    3. 在面板中,在 " XR 设置" (的 "发布设置") 下面的 "发布设置" 下,请确保已添加 Windows Mixed reality SDK,确保已添加 Windows Mixed reality SDK Further down the panel, in XR Settings (found below Publish Settings), tick Virtual Reality Supported, make sure the Windows Mixed Reality SDK is added

      设置 Unity 项目

  7. 返回 生成设置 Unity c # 项目不再灰显;勾选此的旁边的复选框。Back in Build Settings Unity C# Projects is no longer greyed out; tick the checkbox next to this.

  8. 关闭“生成设置”窗口。Close the Build Settings window.

  9. 保存项目 (文件 > 保存项目) 。Save your Project (FILE > SAVE PROJECT).

第6章-导入 MLProducts Unity 包Chapter 6 - Importing the MLProducts Unity Package

在本课程中,你将需要下载名为 unitypackage的 Unity 资产包。For this course, you will need to download a Unity Asset Package called Azure-MR-307.unitypackage. 此包随场景一起提供,其中的所有对象都是预先构建的,因此你可以专注于使其正常工作。This package comes complete with a scene, with all objects in that prebuilt, so you can focus on getting it all working. 尽管仅出于场景设置结构的目的保留公共变量,但提供了 ShelfKeeper 脚本。The ShelfKeeper script is provided, though only holds the public variables, for the purpose of scene setup structure. 您将需要执行所有其他部分。You will need to do all other sections.

导入此包:To import this package:

  1. 在您前面的 "Unity" 面板中,单击屏幕顶部菜单中的 " 资产 ",然后单击 " 导入包、自定义包"。With the Unity dashboard in front of you, click on Assets in the menu at the top of the screen, then click on Import Package, Custom Package.

    导入 MLProducts Unity 包

  2. 使用文件选取器选择 unitypackage 包,并单击 " 打开"。Use the file picker to select the Azure-MR-307.unitypackage package and click Open.

  3. 此时将显示此资产的组件列表。A list of components for this asset will be displayed to you. 单击 " 导入" 确认导入。Confirm the import by clicking Import.

    导入 MLProducts Unity 包

  4. 导入完成后,你会注意到某些新文件夹已显示在 Unity 项目面板 中。Once it has finished importing, you will notice that some new folders have appeared in your Unity Project Panel. 这些是三维模型和各自的材料,它们是您将处理的预先准备的场景的组成部分。Those are the 3D models and the respective materials that are part of the pre-made scene you will work on. 在本课程中,您将编写大部分代码。You will write the majority of the code in this course.

    导入 MLProducts Unity 包

  5. 在 " 项目面板 " 文件夹中,单击 " 场景 " 文件夹,并双击 " (" MR_MachineLearningScene ") " 中的场景。Within the Project Panel folder, click on the Scenes folder and double click on the scene inside (called MR_MachineLearningScene). 此场景将打开 (参阅下图) 。The scene will open (see image below). 如果缺少红色菱形,只需单击 游戏面板 右上角的 " Gizmos " 按钮。If the red diamonds are missing, simply click the Gizmos button, at the top right of the Game Panel.

    导入 MLProducts Unity 包

第7章-检查 Unity 中的 DllChapter 7 - Checking the DLLs in Unity

若要利用 JSON 库 (用于序列化和反序列化) ,已使用你在中引入的包实现了 Newtonsoft.json 的 DLL。To leverage the use of JSON libraries (used for serializing and deserializing), a Newtonsoft DLL has been implemented with the package you brought in. 库应具有正确的配置,尽管需要 (检查,但如果你在) 的代码不起作用时遇到问题,则此功能尤其有用。The library should have the correct configuration, though it is worth checking (particularly if you are having issues with code not working).

为此,请执行以下操作:To do so:

  • 左键单击 "插件" 文件夹中的 Newtonsoft.json 文件,并查看 " 检查器" 面板Left-click on the Newtonsoft file inside the Plugins folder and look at the Inspector panel. 请确保勾选 任何平台Make sure Any Platform is ticked. 请在 " UWP" 选项卡上 ,确保 不勾选进程Go to the UWP tab and also ensure Don't process is ticked.

    导入 Unity 中的 Dll

第8章-创建 ShelfKeeper 类Chapter 8 - Create the ShelfKeeper class

ShelfKeeper 类承载控制在场景中生成的 UI 和产品的方法。The ShelfKeeper class hosts methods that control the UI and products spawned in the scene.

作为导入包的一部分,你将获得此类,但它是不完整的。As part of the imported package, you will have been given this class, though it is incomplete. 现在可以完成该类:It is now time to complete that class:

  1. 双击 Scripts 文件夹中的 ShelfKeeper 脚本,通过 Visual Studio 2017 将其打开。Double click on the ShelfKeeper script, within the Scripts folder, to open it with Visual Studio 2017.

  2. 将脚本中的所有现有代码替换为以下代码,这会设置时间和日期,并具有显示产品的方法。Replace all the code existing in the script with the following code, which sets the time and date and has a method to show a product.

    using UnityEngine;
    
    public class ShelfKeeper : MonoBehaviour
    {
        /// <summary>
        /// Provides this class Singleton-like behavior
        /// </summary>
        public static ShelfKeeper instance;
    
        /// <summary>
        /// Unity Inspector accessible Reference to the Text Mesh object needed for data
        /// </summary>
        public TextMesh dateText;
    
        /// <summary>
        /// Unity Inspector accessible Reference to the Text Mesh object needed for time
        /// </summary>
        public TextMesh timeText;
    
        /// <summary>
        /// Provides references to the spawn locations for the products prefabs
        /// </summary>
        public Transform[] spawnPoint;
    
        private void Awake()
        {
            instance = this;
        }
    
        /// <summary>
        /// Set the text of the date in the scene
        /// </summary>
        public void SetDate(string day, string month)
        {
            dateText.text = day + " " + month;
        }
    
        /// <summary>
        /// Set the text of the time in the scene
        /// </summary>
        public void SetTime(string hour)
        {
            timeText.text = hour + ":00";
        }
    
        /// <summary>
        /// Spawn a product on the shelf by providing the name and selling grade
        /// </summary>
        /// <param name="name"></param>
        /// <param name="sellingGrade">0 being the best seller</param>
        public void SpawnProduct(string name, int sellingGrade)
        {
            Instantiate(Resources.Load(name),
                spawnPoint[sellingGrade].transform.position, spawnPoint[sellingGrade].transform.rotation);
        }
    }
    
  3. 在返回到 Unity 之前,请务必保存 Visual Studio 中所做的更改。Be sure to save your changes in Visual Studio before returning to Unity.

  4. 返回 Unity 编辑器,检查 ShelfKeeper 类是否如下所示:Back in the Unity Editor, check that the ShelfKeeper class looks like the below:

    创建 ShelfKeeper 类

    重要

    如果脚本没有引用目标 (即 (文本网格)) ,只需将相应的对象从 " 层次结构" 面板 拖动到目标字段。If your script does not have the reference targets (i.e. Date (Text Mesh)), simply drag the corresponding objects from the Hierarchy Panel, into the target fields. 如有必要,请参阅下面的说明:See below for explanation, if needed:

    1. ShelfKeeper 组件脚本中,通过左键单击 生成点 数组,将其打开。Open the Spawn Point array within the ShelfKeeper component script by left-clicking it. 子节将出现 " 大小",指示数组的大小。A sub-section will appear called Size, which indicates the size of the array. 在 "大小" 旁边的文本框中键入 3 ,按 enter,然后将创建三个槽。Type 3 into the textbox next to Size and press Enter, and three slots will be created beneath.

    2. 层次结构 中,通过左键单击) 旁边的箭头来展开 时间显示 对象 (。Within the Hierarchy expand the Time Display object (by left-clicking the arrow beside it). 接下来,单击 " _" 层次结构中的 主相机 _,使 检查器 显示其信息。Next click the *Main Camera_ from within the _* Hierarchy**, so that the Inspector shows its information.

    3. 选择 "层次结构" 面板 中的 主相机Select the Main Camera in the Hierarchy Panel. 将 "日期时间" 对象从 "层次结构" 面板 拖动到 " ShelfKeeper " 组件中的 主摄像机****检查器 内的 日期文本时间文本 槽。Drag the Date and Time objects from the Hierarchy Panel to the Date Text and Time Text slots within the Inspector of the Main Camera in the ShelfKeeper component.

    4. 将 (货位 对象) 下的 "层次结构" 面板 中的 生成点 拖到 "生成点" 数组下的 3元素 引用目标,如图中所示。Drag the Spawn Points from the Hierarchy Panel (beneath the Shelf object) to the 3 Element reference targets beneath the Spawn Point array, as shown in the image.

      创建 ShelfKeeper 类

第9章-创建 ProductPrediction 类Chapter 9 - Create the ProductPrediction class

要创建的下一个类是 ProductPrediction 类。The next class you are going to create is the ProductPrediction class.

此类负责:This class is responsible for:

  • 查询 机器学习服务 实例,同时提供当前日期和时间。Querying the Machine Learning Service instance, providing the current date and time.

  • 将 JSON 响应反序列化为可用的数据。Deserializing the JSON response into usable data.

  • 解释数据,检索3个推荐的产品。Interpreting the data, retrieving the 3 recommended products.

  • 调用 ShelfKeeper 类方法以在场景中显示数据。Calling the ShelfKeeper class methods to display the data in the Scene.

若要创建此类:To create this class:

  1. 在 "项目" 面板 中,打开 "脚本" 文件夹。Go to the Scripts folder, in the Project Panel.

  2. 右键单击文件夹内的 "创建 > c # 脚本"。Right-click inside the folder, Create > C# Script. 调用脚本 ProductPredictionCall the script ProductPrediction.

  3. 双击新的 ProductPrediction 脚本以通过 Visual Studio 2017 将其打开。Double click on the new ProductPrediction script to open it with Visual Studio 2017.

  4. 如果弹出了 文件修改 对话框,请单击 "重新加载解决方案"。If the File Modification Detected dialog pops up, click *Reload Solution.

  5. 将以下命名空间添加到 ProductPrediction 类的顶部:Add the following namespaces to the top of the ProductPrediction class:

    using System;
    using System.Collections.Generic;
    using UnityEngine;
    using System.Linq;
    using Newtonsoft.Json;
    using UnityEngine.Networking;
    using System.Runtime.Serialization;
    using System.Collections;
    
  6. ProductPrediction 类中,插入以下两个由若干嵌套类组成的对象。Inside the ProductPrediction class insert the following two objects which are composed of a number of nested classes. 这些类用于序列化和反序列化机器学习服务的 JSON。These classes are used to serialize and deserialize the JSON for the Machine Learning Service.

        /// <summary>
        /// This object represents the Prediction request
        /// It host the day of the year and hour of the day
        /// The product must be left blank when serialising
        /// </summary>
        public class RootObject
        {
            public Inputs Inputs { get; set; }
        }
    
        public class Inputs
        {
            public Input1 input1 { get; set; }
        }
    
        public class Input1
        {
            public List<string> ColumnNames { get; set; }
            public List<List<string>> Values { get; set; }
        }
    
        /// <summary>
        /// This object containing the deserialised Prediction result
        /// It host the list of the products
        /// and the likelihood of them being sold at current date and time
        /// </summary>
        public class Prediction
        {
            public Results Results { get; set; }
        }
    
        public class Results
        {
            public Output1 output1;
        }
    
        public class Output1
        {
            public string type;
            public Value value;
        }
    
        public class Value
        {
            public List<string> ColumnNames { get; set; }
            public List<List<string>> Values { get; set; }
        }
    
  7. 然后,将以下变量添加到前面的代码 (,以便 JSON 相关代码位于脚本底部,在所有其他代码的下方,) :Then add the following variables above the previous code (so that the JSON related code is at the bottom of the script, below all other code, and out of the way):

        /// <summary>
        /// The 'Primary Key' from your Machine Learning Portal
        /// </summary>
        private string authKey = "-- Insert your service authentication key here --";
    
        /// <summary>
        /// The 'Request-Response' Service Endpoint from your Machine Learning Portal
        /// </summary>
        private string serviceEndpoint = "-- Insert your service endpoint here --";
    
        /// <summary>
        /// The Hour as set in Windows
        /// </summary>
        private string thisHour;
    
        /// <summary>
        /// The Day, as set in Windows
        /// </summary>
        private string thisDay;
    
        /// <summary>
        /// The Month, as set in Windows
        /// </summary>
        private string thisMonth;
    
        /// <summary>
        /// The Numeric Day from current Date Conversion
        /// </summary>
        private string dayOfTheYear;
    
        /// <summary>
        /// Dictionary for holding the first (or default) provided prediction 
        /// from the Machine Learning Experiment
        /// </summary>    
        private Dictionary<string, string> predictionDictionary;
    
        /// <summary>
        /// List for holding product prediction with name and scores
        /// </summary>
        private List<KeyValuePair<string, double>> keyValueList;
    

    重要

    请确保将机器学习门户中的 主密钥请求-响应终结点 插入到此处的变量中。Make sure to insert the primary key and request-response endpoint, from the Machine Learning Portal, into the variables here. 以下图像显示了从何处获取密钥和终结点的位置。The below images show where you would have taken the key and endpoint from.

    机器学习 Studio (经典) :试验

    机器学习 Studio (经典) :试验

  8. Start ( # B1 方法中插入此代码。Insert this code within the Start() method. 当类初始化时,将调用 Start ( # B1 方法:The Start() method is called when the class initializes:

        void Start()
        {
            // Call to get the current date and time as set in Windows
            GetTodayDateAndTime();
    
            // Call to set the HOUR in the UI
            ShelfKeeper.instance.SetTime(thisHour);
    
            // Call to set the DATE in the UI
            ShelfKeeper.instance.SetDate(thisDay, thisMonth);
    
            // Run the method to Get Predication from Azure Machine Learning
            StartCoroutine(GetPrediction(thisHour, dayOfTheYear));
        }
    
  9. 下面是收集 Windows 中的日期和时间,并将其转换为机器学习试验可用于与表中存储的数据进行比较的格式的方法。The following is the method that collects the date and time from Windows and converts it into a format that our Machine Learning Experiment can use to compare with the data stored in the table.

        /// <summary>
        /// Get current date and hour
        /// </summary>
        private void GetTodayDateAndTime()
        {
            // Get today date and time
            DateTime todayDate = DateTime.Now;
    
            // Extrapolate the HOUR
            thisHour = todayDate.Hour.ToString();
    
            // Extrapolate the DATE
            thisDay = todayDate.Day.ToString();
            thisMonth = todayDate.ToString("MMM");
    
            // Extrapolate the day of the year
            dayOfTheYear = todayDate.DayOfYear.ToString();
        }
    
  10. 可以 删除****更新 ( # B1 方法,因为此类将不会使用它。You can delete the Update() method since this class will not use it.

  11. 添加以下方法,该方法将当前日期和时间传递到机器学习终结点,并接收 JSON 格式的响应。Add the following method which will communicate the current date and time to the Machine Learning endpoint and receive a response in JSON format.

        private IEnumerator GetPrediction(string timeOfDay, string dayOfYear)
        {
            // Populate the request object 
            // Using current day of the year and hour of the day
            RootObject ro = new RootObject
            {
                Inputs = new Inputs
                {
                    input1 = new Input1
                    {
                        ColumnNames = new List<string>
                        {
                            "day",
                            "hour",
                        "product"
                        },
                        Values = new List<List<string>>()
                    }
                }
            };
    
            List<string> l = new List<string>
            {
                dayOfYear,
                timeOfDay,
                ""
            };
    
            ro.Inputs.input1.Values.Add(l);
    
            Debug.LogFormat("Score request built");
    
            // Serialize the request
            string json = JsonConvert.SerializeObject(ro);
    
            using (UnityWebRequest www = UnityWebRequest.Post(serviceEndpoint, "POST"))
            {
                byte[] jsonToSend = new System.Text.UTF8Encoding().GetBytes(json);
                www.uploadHandler = new UploadHandlerRaw(jsonToSend);
    
                www.downloadHandler = new DownloadHandlerBuffer();
                www.SetRequestHeader("Authorization", "Bearer " + authKey);
                www.SetRequestHeader("Content-Type", "application/json");
                www.SetRequestHeader("Accept", "application/json");
    
                yield return www.SendWebRequest();
                string response = www.downloadHandler.text;
    
                // Deserialize the response
                DataContractSerializer serializer;
                serializer = new DataContractSerializer(typeof(string));
                DeserialiseJsonResponse(response);
            }
        }
    
  12. 添加以下方法,该方法负责反序列化 JSON 响应,并将反序列化的结果传递给 ShelfKeeper 类。Add the following method, which is responsible for deserializing the JSON response, and communicating the result of the deserialization to the ShelfKeeper class. 此结果将是预测在当前日期和时间销售的三个项目的名称。This result will be the names of the three items predicted to sell the most at current date and time. 将下面的代码插入到前面方法下面的 ProductPrediction 类中。Insert the code below into the ProductPrediction class, below the previous method.

        /// <summary>
        /// Deserialize the response received from the Machine Learning portal
        /// </summary>
        public void DeserialiseJsonResponse(string jsonResponse)
        {
            // Deserialize JSON
            Prediction prediction = JsonConvert.DeserializeObject<Prediction>(jsonResponse);
            predictionDictionary = new Dictionary<string, string>();
    
            for (int i = 0; i < prediction.Results.output1.value.ColumnNames.Count; i++)
            {
                if (prediction.Results.output1.value.Values[0][i] != null)
                {
                    predictionDictionary.Add(prediction.Results.output1.value.ColumnNames[i], prediction.Results.output1.value.Values[0][i]);
                }
            }
    
            keyValueList = new List<KeyValuePair<string, double>>();
    
            // Strip all non-results, by adding only items of interest to the scoreList
            for (int i = 0; i < predictionDictionary.Count; i++)
            {
                KeyValuePair<string, string> pair = predictionDictionary.ElementAt(i);
                if (pair.Key.StartsWith("Scored Probabilities"))
                {
                    // Parse string as double then simplify the string key so to only have the item name
                    double scorefloat = 0f;
                    double.TryParse(pair.Value, out scorefloat);
                    string simplifiedName =
                        pair.Key.Replace("\"", "").Replace("Scored Probabilities for Class", "").Trim();
                    keyValueList.Add(new KeyValuePair<string, double>(simplifiedName, scorefloat));
                }
            }
    
            // Sort Predictions (results will be lowest to highest)
            keyValueList.Sort((x, y) => y.Value.CompareTo(x.Value));
    
            // Spawn the top three items, from the keyValueList, which we have sorted
            for (int i = 0; i < 3; i++)
            {
                ShelfKeeper.instance.SpawnProduct(keyValueList[i].Key, i);
            }
    
            // Clear lists in case of reuse
            keyValueList.Clear();
            predictionDictionary.Clear();
        }
    
  13. 保存 Visual Studio 并返回到 UnitySave Visual Studio and head back to Unity.

  14. ProductPrediction 类脚本从 脚本 文件夹拖到 主相机 对象上。Drag the ProductPrediction class script from the Script folder, onto the Main Camera object.

  15. 保存场景和项目 文件 > 保存场景/文件 > 保存项目Save your scene and project File > Save Scene/File > Save Project.

第10章-构建 UWP 解决方案Chapter 10 - Build the UWP Solution

现在可以将项目构建为 UWP 解决方案,使其可以作为独立的应用程序运行。It is now time to build your project as a UWP solution, so that it can run as a standalone application.

若要生成:To Build:

  1. 单击 "文件" " > 保存场景" 保存当前场景。Save the current scene by clicking on File > Save Scenes.

  2. 中转到 文件 > 生成设置Go to File > Build Settings

  3. 选中名为 " Unity c # 项目 " 的框 (这一点很重要,因为它将允许您在完成生成后编辑类) 。Check the box called Unity C# Projects (this is important because it will allow you to edit the classes after build is completed).

  4. 单击 " 添加打开的场景",Click on Add Open Scenes,

  5. 单击“生成”。Click Build.

    构建 UWP 解决方案

  6. 系统将提示您选择要在其中生成解决方案的文件夹。You will be prompted to select the folder where you want to build the Solution.

  7. 创建一个 生成 文件夹,在该文件夹中创建另一个文件夹,其中包含所选的适当名称。Create a BUILDS folder and within that folder create another folder with an appropriate name of your choice.

  8. 单击新文件夹,然后单击 " 选择文件夹",在该位置开始生成。Click your new folder and then click Select Folder, to begin the build at that location.

    构建 UWP 解决方案

    构建 UWP 解决方案

  9. Unity 完成生成后 (可能需要一些时间) ,它将在你的生成的位置上打开 " 文件资源管理器 " 窗口 (检查任务栏,因为它可能不会始终出现在 windows 上,但会通知你添加了新的窗口) 。Once Unity has finished building (it might take some time), it will open a File Explorer window at the location of your build (check your task bar, as it may not always appear above your windows, but will notify you of the addition of a new window).

第11章-部署应用程序Chapter 11 - Deploy your Application

若要部署应用程序:To deploy your application:

  1. 导航到新的 Unity 生成 (应用 文件夹) 并通过 Visual Studio 打开解决方案文件。Navigate to your new Unity build (the App folder) and open the solution file with Visual Studio.

  2. 在 Visual Studio 打开的情况下,你需要还原 NuGet 包,可以通过右键单击 MachineLearningLab_Build 解决方案来完成此操作,方法是从 Visual Studio) 右侧找到的解决方案资源管理器 (,然后单击 "还原 NuGet 包":With Visual Studio open, you need to Restore NuGet Packages, which can be done through right-clicking your MachineLearningLab_Build solution, from the Solution Explorer (found to the right of Visual Studio), and then clicking Restore NuGet Packages:

    添加 NuGet 包

  3. 在解决方案配置中,选择 " 调试"。In the Solution Configuration select Debug.

  4. 在解决方案平台中,选择 " x86本地计算机"。In the Solution Platform, select x86, Local Machine.

    对于 Microsoft HoloLens,你可能会发现将其设置为 远程计算机 会更容易,因此你不会受限到计算机上。For the Microsoft HoloLens, you may find it easier to set this to Remote Machine, so that you are not tethered to your computer. 不过,还需要执行以下操作:Though, you will need to also do the following:

    • 了解 HoloLens 的 IP 地址 ,可在 " 设置" > 网络 & Internet > Wi-Fi "> 高级选项" 中找到;IPv4 是应使用的地址。Know the IP Address of your HoloLens, which can be found within the Settings > Network & Internet > Wi-Fi > Advanced Options; the IPv4 is the address you should use.
    • 确保 开发人员模式打开;在 "设置" 中找到 > 更新开发人员 & 安全 >Ensure Developer Mode is On; found in Settings > Update & Security > For developers.

    添加 NuGet 包

  5. 中转到 " 生成" 菜单 ,然后单击 " 部署解决方案 ",将应用程序旁加载到你的电脑。Go to Build menu and click on Deploy Solution to sideload the application to your PC.

  6. 应用现在应显示在已安装的应用列表中,可以启动。Your App should now appear in the list of installed apps, ready to be launched.

运行混合现实应用程序时,你将看到在 Unity 场景中设置的工作台,并从初始化中获取在 Azure 中设置的数据。When you run the Mixed Reality application, you will see the bench that was set up in your Unity scene, and from initialization, the data you set up within Azure will be fetched. 数据将在应用程序中进行反序列化,当前日期和时间的三个最顶部结果将以直观的形式提供,作为工作台上的三个模型。The data will be deserialized within your application, and the three top results for your current date and time will be provided visually, as three models on the bench.

已完成的机器学习应用程序Your finished Machine Learning application

恭喜,你构建了一个混合现实应用,它利用 Azure 机器学习来进行数据预测,并将其显示在你的场景中。Congratulations, you built a mixed reality app that leverages the Azure Machine Learning to make data predictions and display it on your scene.

添加 NuGet 包

练习Exercise

练习1Exercise 1

试验您的应用程序的排序顺序并使这三个最低预测出现在货位上,因为此数据可能也很有用。Experiment with the sort order of your application and have the three bottom predictions appear on the shelf, as this data would potentially be useful also.

练习2Exercise 2

使用 Azure 表 时,将使用天气信息填充新表,并使用数据创建新试验。Using Azure Tables populate a new table with weather information and create a new experiment using the data.