{"id":205,"date":"2021-08-18T14:33:55","date_gmt":"2021-08-18T21:33:55","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/windowsai\/?p=205"},"modified":"2021-08-18T14:35:58","modified_gmt":"2021-08-18T21:35:58","slug":"build-a-machine-learning-data-analysis-application","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/windowsai\/build-a-machine-learning-data-analysis-application\/","title":{"rendered":"Build a Machine Learning Data Analysis Application"},"content":{"rendered":"<p style=\"text-align: justify;\">Neural networks are very powerful to perform predictive analysis and solve analytical tasks. They are widely used for data classification to detect patterns in the input data and make predictions. The business cases are varied from customer data classification and protection, text classification, consumer behavior, and many others.<\/p>\n<p style=\"text-align: justify;\">To show how to create an application to perform data analysis to solve classification tasks, the Windows ML team has created a \u201c<a href=\"https:\/\/docs.microsoft.com\/en-us\/windows\/ai\/windows-ml\/tutorials\/pytorch-analysis-intro\">Data Analysis with Pytorch and Windows Machine Learning<\/a>\u201d tutorial. This tutorial shows how to train a neural network model based on a tabular dataset using the PyTorch library, and how to deploy that model within a Windows Machine Learning application that can run on any Windows device.<\/p>\n<p style=\"text-align: justify;\">While this guide uses a dataset in Excel or csv formats, the process it outlines works for any tabular dataset and will teach you how to run predictions and leverage Windows M capabilities for your own unique business case.<\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-size: 14pt;\"><strong>Train a multilabel regression model with PyTorch<\/strong><\/span><\/p>\n<p>The tutorial showcases these data analysis capabilities by predicting the species of Iris flower based on that flower\u2019s numerical measurements. For training, we\u2019ve used a famous Fisher\u2019s dataset that includes the records of Iris species.<\/p>\n<p style=\"text-align: justify;\">You can use any other set of tabular data to train your model and predict the desired outcome. However, it\u2019s important to specify the model input and output based on your dataset and your scenario. During the training process, the model will look at the relationship between the inputs and the output and will learn to predict the outcome.<\/p>\n<p style=\"text-align: justify;\"><span class=\"NormalTextRun BCX8 SCXW147058900\">In the last step in data preparation, you\u2019ll\u00a0<\/span><span class=\"NormalTextRun BCX8 SCXW147058900\">convert model inputs and outputs to the Tensor format, <\/span>as machine learning models require Tensor (multi-dimensional array) representation of values. Luckily, it\u2019s very easy to convert model inputs to Tensors with <a href=\"https:\/\/pytorch.org\/docs\/stable\/generated\/torch.nn.Module.html\">PyTorch<\/a> using the <a href=\"https:\/\/pytorch.org\/docs\/stable\/tensors.html#torch.Tensor\">torch.Tensor<\/a> PyTorch package. In this tutorial, we convert our dataset input with just a few lines of code.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/windowsai\/wp-content\/uploads\/sites\/71\/2021\/07\/Picture2.png\"><img decoding=\"async\" class=\"aligncenter wp-image-207\" src=\"https:\/\/devblogs.microsoft.com\/windowsai\/wp-content\/uploads\/sites\/71\/2021\/07\/Picture2.png\" alt=\"Image Picture2\" width=\"690\" height=\"106\" srcset=\"https:\/\/devblogs.microsoft.com\/windowsai\/wp-content\/uploads\/sites\/71\/2021\/07\/Picture2.png 644w, https:\/\/devblogs.microsoft.com\/windowsai\/wp-content\/uploads\/sites\/71\/2021\/07\/Picture2-300x46.png 300w\" sizes=\"(max-width: 690px) 100vw, 690px\" \/><\/a><\/p>\n<p style=\"text-align: center;\"><span style=\"font-size: 10pt;\"><em>Figure 1: Covert your model inputs and output to the Tensor format<\/em><\/span><\/p>\n<p>To train the neural network model, the\u00a0tutorial will take you through the process of loading the data, defining the model parameters, a model, and a loss function, training the model on the training set and validating the model with the validation set. At the end of the training process, you\u2019ll learn how to test the model using the test dataset.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/windowsai\/wp-content\/uploads\/sites\/71\/2021\/08\/Picture3.png\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-239\" src=\"https:\/\/devblogs.microsoft.com\/windowsai\/wp-content\/uploads\/sites\/71\/2021\/08\/Picture3.png\" alt=\"Image Picture3\" width=\"1681\" height=\"227\" srcset=\"https:\/\/devblogs.microsoft.com\/windowsai\/wp-content\/uploads\/sites\/71\/2021\/08\/Picture3.png 1681w, https:\/\/devblogs.microsoft.com\/windowsai\/wp-content\/uploads\/sites\/71\/2021\/08\/Picture3-300x41.png 300w, https:\/\/devblogs.microsoft.com\/windowsai\/wp-content\/uploads\/sites\/71\/2021\/08\/Picture3-1024x138.png 1024w, https:\/\/devblogs.microsoft.com\/windowsai\/wp-content\/uploads\/sites\/71\/2021\/08\/Picture3-768x104.png 768w, https:\/\/devblogs.microsoft.com\/windowsai\/wp-content\/uploads\/sites\/71\/2021\/08\/Picture3-1536x207.png 1536w\" sizes=\"(max-width: 1681px) 100vw, 1681px\" \/><\/a><\/p>\n<p style=\"text-align: center;\"><span style=\"font-size: 10pt;\"><em>Figure 2: Train the multilabel regression model process with Pytorch<\/em><\/span><\/p>\n<p style=\"text-align: justify;\">The Pytorch <a href=\"https:\/\/pytorch.org\/docs\/stable\/generated\/torch.nn.Module.html\">nn.Module<\/a> neural network class provides an easy way to build the neural network. You only need to define the forward function, as the backward function will be automatically defined.<\/p>\n<p style=\"text-align: justify;\">Review the\u00a0tutorial to learn how to create a training function to loop over the data, feed the inputs to the network, and optimize. After you run several iterations over the training set, you should expect to achieve relatively high accuracy.<a href=\"https:\/\/devblogs.microsoft.com\/windowsai\/wp-content\/uploads\/sites\/71\/2021\/08\/Picture9.png\"><img decoding=\"async\" class=\"aligncenter wp-image-234\" src=\"https:\/\/devblogs.microsoft.com\/windowsai\/wp-content\/uploads\/sites\/71\/2021\/08\/Picture9.png\" alt=\"Image Picture9\" width=\"1044\" height=\"505\" srcset=\"https:\/\/devblogs.microsoft.com\/windowsai\/wp-content\/uploads\/sites\/71\/2021\/08\/Picture9.png 1549w, https:\/\/devblogs.microsoft.com\/windowsai\/wp-content\/uploads\/sites\/71\/2021\/08\/Picture9-300x145.png 300w, https:\/\/devblogs.microsoft.com\/windowsai\/wp-content\/uploads\/sites\/71\/2021\/08\/Picture9-1024x495.png 1024w, https:\/\/devblogs.microsoft.com\/windowsai\/wp-content\/uploads\/sites\/71\/2021\/08\/Picture9-768x371.png 768w, https:\/\/devblogs.microsoft.com\/windowsai\/wp-content\/uploads\/sites\/71\/2021\/08\/Picture9-1536x743.png 1536w\" sizes=\"(max-width: 1044px) 100vw, 1044px\" \/><\/a><\/p>\n<p style=\"text-align: center;\"><span style=\"font-size: 10pt;\"><em>Figure 3: The training result of our neural network model <\/em><\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-size: 14pt;\"><strong>Deploy model with Windows Machine Learning APIs<\/strong><\/span><\/p>\n<p><span class=\"TextRun SCXW29585026 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"auto\"><span class=\"NormalTextRun SCXW29585026 BCX8\">After you\u2019ve trained the model,\u00a0<\/span><span class=\"NormalTextRun SCXW29585026 BCX8\">you can go ahead and\u00a0<\/span><span class=\"NormalTextRun SCXW29585026 BCX8\">deploy\u00a0<\/span><span class=\"NormalTextRun SCXW29585026 BCX8\">it<\/span><span class=\"NormalTextRun SCXW29585026 BCX8\">\u00a0in a Windows Machine Learning application that can run on any Windows device.\u00a0<\/span><span class=\"NormalTextRun SCXW29585026 BCX8\">The <\/span><\/span><span class=\"TextRun SCXW29585026 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"auto\"><span class=\"NormalTextRun SCXW29585026 BCX8\">tutorial<\/span><span class=\"NormalTextRun SCXW29585026 BCX8\">\u00a0<\/span><span class=\"NormalTextRun SCXW29585026 BCX8\">shows you how to create all the classes and methods to call the Windows Machine Learning APIs, which load,\u00a0<\/span><span class=\"NormalTextRun CommentStart CommentHighlightPipeRest CommentHighlightRest SCXW29585026 BCX8\">bind<\/span><span class=\"NormalTextRun CommentHighlightPipeRest SCXW29585026 BCX8\">\u00a0inputs and outputs<\/span><span class=\"NormalTextRun SCXW29585026 BCX8\">, and evaluate your machine learning model.\u00a0<\/span><\/span><span class=\"EOP SCXW29585026 BCX8\" data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/windowsai\/wp-content\/uploads\/sites\/71\/2021\/07\/Picture60.png\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-229\" src=\"https:\/\/devblogs.microsoft.com\/windowsai\/wp-content\/uploads\/sites\/71\/2021\/07\/Picture60.png\" alt=\"Image Picture60\" width=\"1693\" height=\"229\" srcset=\"https:\/\/devblogs.microsoft.com\/windowsai\/wp-content\/uploads\/sites\/71\/2021\/07\/Picture60.png 1693w, https:\/\/devblogs.microsoft.com\/windowsai\/wp-content\/uploads\/sites\/71\/2021\/07\/Picture60-300x41.png 300w, https:\/\/devblogs.microsoft.com\/windowsai\/wp-content\/uploads\/sites\/71\/2021\/07\/Picture60-1024x139.png 1024w, https:\/\/devblogs.microsoft.com\/windowsai\/wp-content\/uploads\/sites\/71\/2021\/07\/Picture60-768x104.png 768w, https:\/\/devblogs.microsoft.com\/windowsai\/wp-content\/uploads\/sites\/71\/2021\/07\/Picture60-1536x208.png 1536w\" sizes=\"(max-width: 1693px) 100vw, 1693px\" \/><\/a><\/p>\n<p style=\"text-align: center;\"><span style=\"font-size: 10pt;\"><em>Figure 4: Integrate and evaluate a machine learning model with Windows ML APIs<\/em><\/span><\/p>\n<p style=\"text-align: justify;\">WindowsML API accepts and supports all ONNX feature types of four descriptive classes: tensors, sequence, map, and image. This tutorial will help you to define the correct inputs based on your model requirements. To create a tensor input with Windows ML API, you can use <a href=\"https:\/\/docs.microsoft.com\/en-us\/uwp\/api\/windows.ai.machinelearning.tensorfloat?view=winrt-19041\">TensorFloat<\/a> class to define a 32-bit float tensor object. This class provides several methods to construct a tensor &#8211; in this tutorial, we use the <a href=\"https:\/\/docs.microsoft.com\/en-us\/uwp\/api\/windows.ai.machinelearning.tensorfloat.createfromarray?view=winrt-19041#Windows_AI_MachineLearning_TensorFloat_CreateFromArray_Windows_Foundation_Collections_IIterable_System_Int64__System_Single___\">CreateFromArray<\/a> method to build a tensor input in the exact size your model requires.<\/p>\n<p style=\"text-align: justify;\">CreateFromArray method requires two parameters &#8211; shape array and tensor data array. The tensor itself is just a list of values &#8211; our data, and the shape array tells you how to interpret the data array. If you\u2019ve followed the tutorial, the network regression model you built in the previous part has four input values, each representing the possible sizes of four physical features of an iris flower. The batch size defined the number of samples that will be propagated through the network \u2013 in our case, the batch size is 1. So, the input tensor shape is [1&#215;4]. This shape array tells you that the data array has 2 dimensions. The first dimension has length 1, and the second dimension has length 4.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/windowsai\/wp-content\/uploads\/sites\/71\/2021\/07\/Picture7.png\"><img decoding=\"async\" class=\"aligncenter wp-image-212\" src=\"https:\/\/devblogs.microsoft.com\/windowsai\/wp-content\/uploads\/sites\/71\/2021\/07\/Picture7.png\" alt=\"Image Picture7\" width=\"643\" height=\"250\" srcset=\"https:\/\/devblogs.microsoft.com\/windowsai\/wp-content\/uploads\/sites\/71\/2021\/07\/Picture7.png 568w, https:\/\/devblogs.microsoft.com\/windowsai\/wp-content\/uploads\/sites\/71\/2021\/07\/Picture7-300x117.png 300w\" sizes=\"(max-width: 643px) 100vw, 643px\" \/><\/a><\/p>\n<p style=\"text-align: center;\"><em><span style=\"font-size: 10pt;\">Figure 5: Define the model input tensor with Windows ML API<\/span> <\/em><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-size: 14pt;\"><strong>Getting Started<\/strong><\/span><\/p>\n<p style=\"text-align: justify;\">Once you complete the machine learning part of the code, you can easily integrate your model with the Windows application. And if you want a shortcut to see it all in action, you can review the complete sample code at our GitHub repository &#8211; &#8220;<a href=\"https:\/\/github.com\/microsoft\/Windows-Machine-Learning\/tree\/master\/Samples\/Tutorial%20Samples\/PyTorch%20Data%20Analysis\">Data analysis sample<\/a>&#8221; to get access to the pre-built Windows application. This pre-built app includes the application GUI and UI control code &#8211; everything you need to test your model!<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/windowsai\/wp-content\/uploads\/sites\/71\/2021\/08\/Picture8.png\"><img decoding=\"async\" class=\"aligncenter wp-image-237\" src=\"https:\/\/devblogs.microsoft.com\/windowsai\/wp-content\/uploads\/sites\/71\/2021\/08\/Picture8.png\" alt=\"Image Picture8\" width=\"527\" height=\"309\" srcset=\"https:\/\/devblogs.microsoft.com\/windowsai\/wp-content\/uploads\/sites\/71\/2021\/08\/Picture8.png 664w, https:\/\/devblogs.microsoft.com\/windowsai\/wp-content\/uploads\/sites\/71\/2021\/08\/Picture8-300x176.png 300w\" sizes=\"(max-width: 527px) 100vw, 527px\" \/><\/a><\/p>\n<p style=\"text-align: center;\"><em><span style=\"font-size: 10pt;\">Figure 6: Windows ML application to test regression network<\/span> <\/em><\/p>\n<p style=\"text-align: justify;\">As you try out the tutorial, please let us know if you have any suggestions or questions by leaving feedback on the documentation or samples. And stay tuned to the Windows AI blog for more updates!<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Learn how to create a Window Machine Learning application that uses a neural network to analyze tabular data and run predictions. <\/p>\n","protected":false},"author":45626,"featured_media":234,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[9,7],"class_list":["post-205","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-windows-ai","tag-pytorch","tag-windows-ml"],"acf":[],"blog_post_summary":"<p>Learn how to create a Window Machine Learning application that uses a neural network to analyze tabular data and run predictions. <\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/windowsai\/wp-json\/wp\/v2\/posts\/205","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/windowsai\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/windowsai\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/windowsai\/wp-json\/wp\/v2\/users\/45626"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/windowsai\/wp-json\/wp\/v2\/comments?post=205"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/windowsai\/wp-json\/wp\/v2\/posts\/205\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/windowsai\/wp-json\/wp\/v2\/media\/234"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/windowsai\/wp-json\/wp\/v2\/media?parent=205"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/windowsai\/wp-json\/wp\/v2\/categories?post=205"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/windowsai\/wp-json\/wp\/v2\/tags?post=205"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}