{"id":6584,"date":"2021-05-21T08:00:50","date_gmt":"2021-05-21T12:00:50","guid":{"rendered":"https:\/\/blogs.mathworks.com\/deep-learning\/?p=6584"},"modified":"2021-05-21T08:52:47","modified_gmt":"2021-05-21T12:52:47","slug":"deep-learning-based-surrogate-models","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/deep-learning\/2021\/05\/21\/deep-learning-based-surrogate-models\/","title":{"rendered":"Deep Learning Based Surrogate Models"},"content":{"rendered":"Today\u2019s guest blogger is Shyam Keshavmurthy, Application Engineer focused on AI applications, here to talk about Surrogate Models.\r\n<h6><\/h6>\r\n<h2>Background<\/h2>\r\n<p style=\"font-size: 14px;\">System modeling is used in applications such as electric vehicles and energy systems, and plays a pivotal role in understanding system behavior, system degradation, and maximizing system utilization. The behavior of these systems is dictated by multi-physics complex interactions well suited for <em>finite-element simulations<\/em>, but modeling system behavior and system response is computationally intensive and requires high-performance computing resources. Additionally, such models cannot be deployed to hardware to predict real time system response. Another alternative is <em>reduced order modeling<\/em>, which makes system models computationally feasible; However, in many critical systems, this approach is not preferred as these surrogate models are less accurate and do not represent full spectrum of component behavior.<\/p>\r\n<p style=\"font-size: 14px;\">With deep learning, we can now rely on data to develop small footprint, detailed models of components without approaching the problem from first principles. In this blog, we walk through developing a deep learning based surrogate model for a <strong>Permanent Magnet Synchronous Motor<\/strong>, (PMSM) a popular component for electric vehicles and future green transportation.<\/p>\r\n<img decoding=\"async\" loading=\"lazy\" width=\"150\" height=\"150\" class=\"alignnone size-thumbnail wp-image-6598\" src=\"https:\/\/blogs.mathworks.com\/deep-learning\/files\/2021\/03\/GettyImages-95769783-150x150.jpg\" alt=\"\" \/>\r\n<h2>Load and Understand Large Data Set<\/h2>\r\n<p style=\"font-size: 14px;\">We\u2019ll start by using a dataset that is about 50 Mbytes with runs that span over days to runs that are few minutes. This data represents PMSM temperature changes to interactions between electrical and thermal systems that have varying time constants. As we see from table change to ambient temperature effects not only the internal temperatures but the current and the voltages available to generate required torque.<\/p>\r\n<img decoding=\"async\" loading=\"lazy\" width=\"624\" height=\"143\" class=\"alignnone size-full wp-image-6586\" src=\"https:\/\/blogs.mathworks.com\/deep-learning\/files\/2021\/03\/Picture1.png\" alt=\"\" \/>\r\n<h2>Data Preprocessing and Feature Engineering<\/h2>\r\n<p style=\"font-size: 14px;\">Using the raw experimental data from above we now compute add additional features such as Power, magnitudes of voltage and current along with moving average features within a given four-time windows. These additional features allow coupling of electrical and thermal parameters that effect systems performance.<\/p>\r\n\r\n<pre><span class=\"comment\">% create derived features using raw voltages and currents<\/span>\r\nderivedinputs =computedrivedfeatures(tt_data);\r\n<span class=\"comment\">% check the noise in the data<\/span>\r\ntt_data=[tt_data derivedinputs];\r\nVnames=tt_data.Properties.VariableNames;\r\ns1=620;s2=2915;s3=4487;s4=8825;\r\n<span class=\"comment\">% preprocess exponentially weighted moving average<\/span>\r\n[t_s1,t_s2,t_s3,t_s4]=preprocmovavg(tt_data,s1,s2,s3,s4,Vnames);\r\n<span class=\"comment\">% preprocess exponentially weighted moving variance <\/span>\r\n[t_v1,t_v2,t_v3,t_v4]=preprocmovvar(tt_data,s1,s2,s3,s4,Vnames);\r\n<span class=\"comment\">% attach features to the original table<\/span>\r\npredictors=[tt_data,t_s1,t_s2,t_s3,t_s4,t_v1,t_v2,t_v3,t_v4,tt_profileid];\r\nresponses=[tt(:,9:12) tt_profileid];\r\nVResponsenames=responses.Properties.VariableNames;<\/pre>\r\n<h2>Prepare Training Data<\/h2>\r\n<p style=\"font-size: 14px;\">Define the profiles to withhold from training. These will be used in testing and validation.<\/p>\r\n\r\n<pre>holdOuts = [65 72 58];  <span class=\"comment\">% define profiles that are withheld from training. <\/span>\r\n[xtrain,ytrain] = prepareDataTrain(predictors,responses,holdOuts);<\/pre>\r\n<h2>Prepare Data for Padding<\/h2>\r\n<p style=\"font-size: 14px;\">To minimize the amount of padding added to the mini-batches, sort the training data by profile length. Then, choose a mini-batch size which divides the training data evenly and reduces the amount of padding in the mini-batches.\r\nSort the training data by profile length.<\/p>\r\n<img decoding=\"async\" loading=\"lazy\" width=\"420\" height=\"315\" class=\"alignnone size-full wp-image-6588\" src=\"https:\/\/blogs.mathworks.com\/deep-learning\/files\/2021\/03\/Picture2.png\" alt=\"\" \/>\r\n<h2>Define Network Architecture<\/h2>\r\n<p style=\"font-size: 14px;\">We will use profile_id 58 as a validation set, which includes 4.64 hours of data.<\/p>\r\n\r\n<pre>validationdata = 58;  <span class=\"comment\">% profile_id 58 is selected as validation set, which includes 4.64 hours of data. <\/span>\r\n[xvalidation, yvalidation] = prepareDataValidation(predictors,responses,validationdata);\r\n\r\n numResponses = size(ytrain{1},1);\r\n featureDimension = size(xtrain{1},1);\r\n\r\nnumHiddenUnits=125;<\/pre>\r\n<img decoding=\"async\" loading=\"lazy\" width=\"420\" height=\"315\" class=\"alignnone size-full wp-image-6590\" src=\"https:\/\/blogs.mathworks.com\/deep-learning\/files\/2021\/03\/Picture3.png\" alt=\"\" \/>\r\n<p style=\"font-size: 14px;\">This DAG network architecture modeling capability in MATLAB allows us to model many complex components. The DAG network architecture helps us to model coupling between physical behaviors that are time-history dependent as well as those physical behaviors that follow Markov chain. <strong>Long Short Term Memory<\/strong> (LSTM) captures historical effects phenomena.<\/p>\r\n\r\n<h2>Predict plot and calculate error to confirm we have a good model<\/h2>\r\n<img decoding=\"async\" loading=\"lazy\" width=\"560\" height=\"420\" class=\"alignnone size-full wp-image-6592\" src=\"https:\/\/blogs.mathworks.com\/deep-learning\/files\/2021\/03\/Picture4.png\" alt=\"\" \/>\r\n<p style=\"font-size: 14px;\">The plots show above the match between actual tests and predicted results the test results are shown in red, the additional plots on the right show the difference between the two as we can see errors are well below 1% after 350 samples and hard to measure temperatures of permanent magnet and yoke and stator temperatures track very well with respect to actual tests. Additionally, they track both over test regimes that have fast and slow variations, which shows that the model has retained necessary fidelity.<\/p>\r\n\r\n<h2>Export the Model to Simulink<\/h2>\r\n<img decoding=\"async\" loading=\"lazy\" width=\"624\" height=\"252\" class=\"alignnone size-full wp-image-6594\" src=\"https:\/\/blogs.mathworks.com\/deep-learning\/files\/2021\/03\/Picture5.png\" alt=\"\" \/>\r\n<p style=\"font-size: 14px;\">This process involves saving the trained model as a .MAT file and importing this as Simulink Deep Neural Network Predict block, with this, we now have a component model which is less than (50 Kbyte) foot-print which can mimic component behavior in detail readily available for system modeling. The Figure below illustrates the complete workflow.<\/p>\r\n<img decoding=\"async\" loading=\"lazy\" width=\"624\" height=\"284\" class=\"alignnone size-full wp-image-6596\" src=\"https:\/\/blogs.mathworks.com\/deep-learning\/files\/2021\/03\/Picture6.png\" alt=\"\" \/>\r\n<p style=\"font-size: 14px;\">This blog illustrates a deep learning-based template that can be adopted to develop high-fidelity, small foot print surrogate models that capture multi-physics behavior of components and systems such as PMSM motors. To learn more, see this detailed video about <strong><a href=\"https:\/\/www.mathworks.com\/support\/search.html\/videos\/deep-learning-in-simulink-1599214701480.html\">importing models into Simulink<\/a><\/strong>.<\/p>","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/deep-learning\/files\/2021\/03\/GettyImages-95769783-150x150.jpg\" onError=\"this.style.display ='none';\" \/><\/div><p>Today\u2019s guest blogger is Shyam Keshavmurthy, Application Engineer focused on AI applications, here to talk about Surrogate Models.\r\n\r\nBackground\r\nSystem modeling is used in applications such as... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/deep-learning\/2021\/05\/21\/deep-learning-based-surrogate-models\/\">read more >><\/a><\/p>","protected":false},"author":156,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[9],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/deep-learning\/wp-json\/wp\/v2\/posts\/6584"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/deep-learning\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/deep-learning\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/deep-learning\/wp-json\/wp\/v2\/users\/156"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/deep-learning\/wp-json\/wp\/v2\/comments?post=6584"}],"version-history":[{"count":7,"href":"https:\/\/blogs.mathworks.com\/deep-learning\/wp-json\/wp\/v2\/posts\/6584\/revisions"}],"predecessor-version":[{"id":7378,"href":"https:\/\/blogs.mathworks.com\/deep-learning\/wp-json\/wp\/v2\/posts\/6584\/revisions\/7378"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/deep-learning\/wp-json\/wp\/v2\/media?parent=6584"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/deep-learning\/wp-json\/wp\/v2\/categories?post=6584"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/deep-learning\/wp-json\/wp\/v2\/tags?post=6584"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}