-
Notifications
You must be signed in to change notification settings - Fork 48
Add Thermal Neural Network (TNN) SciML example #18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
mmarkows17
merged 4 commits into
matlab-deep-learning:main
from
mmarkows17:publish/thermal-neural-network
Apr 17, 2026
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| classdef DiffEqLayer < nnet.layer.Layer & nnet.layer.Formattable | ||
| % DiffEqLayer Differential equation layer | ||
| % | ||
| % This layer holds a TNNCell instance, and performs prediction over | ||
| % tbptt_size time steps. | ||
|
mmarkows17 marked this conversation as resolved.
Outdated
|
||
| % | ||
| % Copyright 2025 The MathWorks, Inc. | ||
|
mmarkows17 marked this conversation as resolved.
|
||
|
|
||
| properties (Learnable) | ||
| Cell | ||
| end | ||
|
|
||
| methods | ||
| function obj = DiffEqLayer(cell) | ||
| % Constructor to store the cell | ||
| obj.Cell = cell; | ||
| obj.NumInputs = 2; | ||
| obj.NumOutputs = 2; | ||
| end | ||
|
|
||
| function [outputs, state] = predict(this, input, state) | ||
| % Initialize cell array for outputs | ||
| numSteps = size(input, 3); % Assuming input is [features, batch, time] | ||
|
|
||
| % Preallocate the outputs: | ||
| outputs = dlarray(zeros(size(state,[1 2 3]),'single'),'CBT'); | ||
|
mmarkows17 marked this conversation as resolved.
Outdated
|
||
|
|
||
| % Iterate over each time step | ||
| thisCell = this.Cell; | ||
| for tt = 1:numSteps | ||
| outputs(:,:,tt) = predict(thisCell,squeeze(input(:, :, tt)),state); | ||
| state = squeeze(outputs(:, :, tt)); | ||
| end | ||
| end | ||
| end | ||
| end | ||
|
|
||
|
|
||
Large diffs are not rendered by default.
Oops, something went wrong.
|
mmarkows17 marked this conversation as resolved.
|
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| classdef TNNCell < nnet.layer.Layer | ||
|
mmarkows17 marked this conversation as resolved.
|
||
| % TNNCell Thermal neural network cell | ||
| % | ||
| % TNNCell performs the TNN forward pass for a single time step. | ||
|
|
||
| % Copyright 2025 The MathWorks, Inc. | ||
|
|
||
| properties | ||
| SampleTime (1,1) double = 0.5; % in seconds | ||
| OutputSize (1,1) double | ||
| IncidenceMatrix_x (:,:) double {mustBeInteger} | ||
| IncidenceMatrix_u (:,:) double {mustBeInteger} | ||
| TemperatureIndices (:,1) double {mustBePositive,mustBeInteger} | ||
| NonTemperatureIndices (:,1) double {mustBePositive,mustBeInteger} | ||
| InputColumns (:,1) string | ||
| TargetColumns (:,1) string | ||
| TemperatureColumns (:,1) string | ||
| end | ||
|
|
||
| properties (Learnable) | ||
| ConductanceNet | ||
| PowerLoss | ||
| Capacitance | ||
| end | ||
|
|
||
| methods | ||
|
|
||
| function this = TNNCell(inputStruct) | ||
|
mmarkows17 marked this conversation as resolved.
|
||
| % Construct TNNCell | ||
| this.NumInputs = 2; | ||
| this.OutputSize = length(inputStruct.targetCols); | ||
| nTemps = length(inputStruct.temperatureCols); | ||
|
|
||
| % Build incidence matrices for fully connected graph | ||
| [this.IncidenceMatrix_x, this.IncidenceMatrix_u] = buildIncidenceMatrices(this.OutputSize, this.NumInputs); | ||
|
|
||
| % Store column info | ||
| this.InputColumns = strtrim(string(inputStruct.inputCols))'; | ||
| this.TargetColumns = strtrim(string(inputStruct.targetCols))'; | ||
| this.TemperatureColumns = strtrim(string(inputStruct.temperatureCols))'; | ||
|
|
||
| % Indices for temperature and non-temperature columns | ||
| this.TemperatureIndices = find(ismember(this.InputColumns, this.TemperatureColumns)); | ||
| this.NonTemperatureIndices = find(~ismember(this.InputColumns, [this.TemperatureColumns; "profile_id"])); | ||
| end | ||
|
|
||
|
|
||
| function this = generateNetworks(this) | ||
| nTemps = length(this.TemperatureColumns); | ||
| nConds = 0.5 * nTemps * (nTemps - 1) - 1; % fully connected except between the two external nodes | ||
| numNeurons = 16; | ||
|
|
||
| % By default, just use one dense layer + sigmoid activations | ||
| this.ConductanceNet = dlnetwork([featureInputLayer(length(this.InputColumns) + this.OutputSize),... | ||
| fullyConnectedLayer(nConds,Name = "conduc_fc1"),sigmoidLayer]); | ||
|
|
||
| % By default, use two dense layers + tanh activations | ||
| this.PowerLoss = dlnetwork([featureInputLayer(length(this.InputColumns) + this.OutputSize),... | ||
| fullyConnectedLayer(numNeurons,Name = "ploss_fc1"),... | ||
| tanhLayer,... | ||
| fullyConnectedLayer(this.OutputSize,Name="ploss_fc2")]); | ||
|
|
||
| this.Capacitance = dlarray(randn(this.OutputSize, 1,'single') * 0.5 - 9.2); % Initialize caps | ||
| end | ||
|
|
||
|
|
||
| function out = predict(this, input, prevOut) | ||
| % Extract temperatures | ||
| tempsInternal = prevOut; % internal nodes | ||
| tempsExternal = input(this.TemperatureIndices,:); % external nodes | ||
| subNNInput = [input; prevOut]; | ||
|
|
||
| E_x = this.IncidenceMatrix_x; | ||
| E_u = this.IncidenceMatrix_u; | ||
|
|
||
| % Conductance network forward pass | ||
| g = abs(predict(this.ConductanceNet, subNNInput'))'; | ||
|
|
||
| % Power loss network forward pass | ||
| q = abs(predict(this.PowerLoss, subNNInput'))'; | ||
|
|
||
| % Compute temperature differences across edges | ||
| dT = E_x' * tempsInternal + E_u' * tempsExternal; | ||
|
|
||
| % Heat flow on edges | ||
| phi = g .* dT; | ||
|
|
||
| % Net outflow from internal nodes | ||
| netOutflow = E_x * phi; | ||
|
|
||
| % State derivative using incidence-based formulation | ||
| dx = exp(this.Capacitance) .* (-netOutflow + q); | ||
|
|
||
| % Update temperatures | ||
| out = prevOut + this.SampleTime .* dx; | ||
|
|
||
| % Clip output | ||
| out = max(min(out, 5), -1); | ||
| end | ||
|
|
||
| end | ||
| end | ||
|
|
||
|
|
||
| function [E_x, E_u] = buildIncidenceMatrices(numInternal, numExternal) | ||
| % numInternal: number of internal nodes | ||
| % numExternal: number of external nodes | ||
| % Output: | ||
| % E_x: [numInternal x L] incidence matrix for internal nodes (fully | ||
| % connected graph) | ||
| % E_u: [numExternal x L] incidence matrix for external nodes (fully | ||
| % connected) | ||
|
|
||
| % Calculate number of edges for fully connected internal graph | ||
| L_internal = nchoosek(numInternal, 2); % fully connected internal nodes | ||
| L_external = numInternal * numExternal; % each external node connected to all internal nodes | ||
| L = L_internal + L_external; | ||
|
|
||
| % Initialize matrices | ||
| E_x = zeros(numInternal, L); | ||
| E_u = zeros(numExternal, L); | ||
|
|
||
| edgeIdx = 1; | ||
|
|
||
| % Internal edges (fully connected) | ||
| for i = 1:numInternal | ||
| for j = i+1:numInternal | ||
| E_x(i, edgeIdx) = 1; % source | ||
| E_x(j, edgeIdx) = -1; % target | ||
| edgeIdx = edgeIdx + 1; | ||
| end | ||
| end | ||
|
|
||
| % External edges (connect each external node to all internal nodes) | ||
| for ext = 1:numExternal | ||
| for int = 1:numInternal | ||
| E_x(int, edgeIdx) = 1; % internal node as source | ||
| E_u(ext, edgeIdx) = -1; % external node as target | ||
| edgeIdx = edgeIdx + 1; | ||
| end | ||
| end | ||
| end | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.