Skip to content

Add Thermal Neural Network (TNN) SciML example#18

Open
mmarkows17 wants to merge 1 commit intomatlab-deep-learning:mainfrom
mmarkows17:publish/thermal-neural-network
Open

Add Thermal Neural Network (TNN) SciML example#18
mmarkows17 wants to merge 1 commit intomatlab-deep-learning:mainfrom
mmarkows17:publish/thermal-neural-network

Conversation

@mmarkows17
Copy link
Copy Markdown
Collaborator

Adds a Thermal Neural Network (TNN) example demonstrating SciML-based virtual temperature sensor modeling.

% This layer holds a TNNCell instance, and performs prediction over
% tbptt_size time steps.
%
% Copyright 2025 The MathWorks, Inc.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We tend to leave the copyright out of the m-help so it doesn't display on help DiffEqLayer

Comment on lines +1 to +2
classdef DiffEqLayer < nnet.layer.Layer & nnet.layer.Formattable
% DiffEqLayer Differential equation layer
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really see a differential equation when I look at the predict implementation - I see either something like recurrence or autoregressive behaviour. I guess in the context of this method it can be seen like an ODE solve.

% DiffEqLayer Differential equation layer
%
% This layer holds a TNNCell instance, and performs prediction over
% tbptt_size time steps.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tbptt_size isn't defined, and it looks like we loop over all steps.

numSteps = size(input, 3); % Assuming input is [features, batch, time]

% Preallocate the outputs:
outputs = dlarray(zeros(size(state,[1 2 3]),'single'),'CBT');
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd use zeros(sz, like=input) here. Besides taking the precision from the input I think it also helps out the dlarray autodiff system.

@@ -0,0 +1,37 @@
function generatePlots(profileLengths,test_tensor,prediction,scale,targetNames)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mix of conventions with test_tensor and targetNames

mseError = mse(groundTruth,currentPred);
plot(0:profileLengths(ii)-1,groundTruth, ...
Color = "green",LineWidth = 1.5)
hold on
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should there be a hold off somewhere too? I think running this function will leave the current figure with a hold on the axes, so any subsequent plot commands by the user would end up on the same axes unless they create a fresh one or turn hold off themselves.

- Stator tooth
- Stator winding

![image_0.png](README_media/image_0.png)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A descriptive alt text would be better.


A simplified discrete state equation for each component $i$ of a LPTN model is:

$$ T_i (k+1)=T_i (k)+t_s \kappa_i (k)\left(\pi_i (k)+\sum_{m\;\in \;components} \gamma_{im} (k)\Delta T_{im} (k)+\sum_{n\;\in \;ancillary} \gamma_{in} (k)\Delta T_{in} (k)\right) $$
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The stuff below the sums didn't render so well on the GitHub diff.


methods

function this = TNNCell(inputStruct)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using a struct as an input doesn't really tell a user anything about what fields that struct has to contain. This would be clearer with required arguments and optional NVPs with an arguments block.

@mmarkows17 mmarkows17 requested a review from jess-42 March 13, 2026 16:09
Copy link
Copy Markdown

@jess-42 jess-42 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Taken a pass over the repo, focusing on the ReadMe.


The data set contains various operational measurements from an electric motor, including currents, voltages, torque, speed, and ambient conditions. The TNN model learns to estimate the internal temperatures that would typically require embedded temperature sensors, which are often impractical in production motors.

This example is intended to be an instructional resource demonstrating how to build and train a TNN in MATLAB. For integration and use with Simulink, see the Github repository [here](https://github.com/wkirgsn/thermal-nn/tree/main/matlab). For video content on this topic, check out this recorded MATLAB Expo talk on [Thermal Neural Network for Temperature Modeling in E-Motors](https://www.mathworks.com/videos/thermal-neural-network-for-temperature-modeling-in-e-motors-1762251869161.html) and this [YouTube video](https://youtu.be/ySRjX9GUiCA?si=s7slG-9Ekt6y9wUe).
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we spell out where the link goes to? I.e.,

For an example showing how to integrate and use this model with Simulink, see the Thermal neural network for electric motor temperature estimation GitHub repository.


The data set contains various operational measurements from an electric motor, including currents, voltages, torque, speed, and ambient conditions. The TNN model learns to estimate the internal temperatures that would typically require embedded temperature sensors, which are often impractical in production motors.

This example is intended to be an instructional resource demonstrating how to build and train a TNN in MATLAB. For integration and use with Simulink, see the Github repository [here](https://github.com/wkirgsn/thermal-nn/tree/main/matlab). For video content on this topic, check out this recorded MATLAB Expo talk on [Thermal Neural Network for Temperature Modeling in E-Motors](https://www.mathworks.com/videos/thermal-neural-network-for-temperature-modeling-in-e-motors-1762251869161.html) and this [YouTube video](https://youtu.be/ySRjX9GUiCA?si=s7slG-9Ekt6y9wUe).
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to other comments, it is good to say where a link is going, rather than just "YouTube video". How about:

For video content on this topic, see the recorded MATLAB Expo talk on Thermal Neural Network for Temperature Modeling in E‑Motors, as well as a related YouTube video.


# References
<a id="M_560c"></a>
1. Wilhelm Kirchgässner, Oliver Wallscheid, Joachim Böcker, Thermal neural networks: Lumped\-parameter thermal modeling with state\-space machine learning, Engineering Applications of Artificial Intelligence 117 (2023) 105537. [https://doi.org/10.1016/j.engappai.2022.105537](https://doi.org/10.1016/j.engappai.2022.105537)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kirchgässner, Wilhelm, et al. “Thermal Neural Networks: Lumped-Parameter Thermal Modeling with State-Space Machine Learning.” Engineering Applications of Artificial Intelligence, vol. 117, Jan. 2023, p. 105537. DOI.org (Crossref), https://doi.org/10.1016/j.engappai.2022.105537.




The data set contains measurements from various sensors on an electric motor across multiple test profiles. Each row represents a measurement at a specific time point, and each column represents a different sensor or calculated value. In addition to the measured data the `profile_id` column identifies different test profile. You can use the `profile_id` value to separate the training and testing data.
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add comma and plural:
"In addition to the measured data, the profile_id column identifies different test profiles."

end
```

Normalize all non\-temperature input columns by dividing by the maximum values.
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is "by their maximum values" clearer?

@@ -0,0 +1,142 @@
classdef TNNCell < nnet.layer.Layer
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be nice throughout to give a 1-line about what each function does, similar to prepareSequenceData.m

Run the example by running [`ThermalNeuralNetwork.m`](ThermalNeuralNetwork_code.m).

# **Requirements**
MATLAB version R2025a is strongly recommended for full functionality. At a minimum, this example requires MATLAB release R2024a or newer as well as Deep Learning Toolbox.
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do I miss out out by not having the latest version? Does this example also only require MATLAB + DLT?


This example is intended to be an instructional resource demonstrating how to build and train a TNN in MATLAB. For integration and use with Simulink, see the Github repository [here](https://github.com/wkirgsn/thermal-nn/tree/main/matlab). For video content on this topic, check out this recorded MATLAB Expo talk on [Thermal Neural Network for Temperature Modeling in E-Motors](https://www.mathworks.com/videos/thermal-neural-network-for-temperature-modeling-in-e-motors-1762251869161.html) and this [YouTube video](https://youtu.be/ySRjX9GUiCA?si=s7slG-9Ekt6y9wUe).

# **Setup**
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider changing this to "Get Started" as this is all you need to do to get started with the repo.

<a id="M_560c"></a>
1. Wilhelm Kirchgässner, Oliver Wallscheid, Joachim Böcker, Thermal neural networks: Lumped\-parameter thermal modeling with state\-space machine learning, Engineering Applications of Artificial Intelligence 117 (2023) 105537. [https://doi.org/10.1016/j.engappai.2022.105537](https://doi.org/10.1016/j.engappai.2022.105537)

# **Prepare Data for Training**
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm assuming you have the full example here for discoverability, but it does give a bit of whiplash to suddenly start preparing data after reading the references.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider resizing the plot during training, this will make the text easier to read.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants