Feedforwardnet custom loss function setup
7 views (last 30 days)
Show older comments
Hello everyone, I have an implementation problem I hope you can help me to solve. Basically I want to train a feedforward neural network to place a hot circular source on a predefined mesh. To generate training data, I randomly create one hot source for each iteration, and compute the temperature of each node of the mesh. This is the code I wrote to do this
t = 0:0.1:1;
n_samples = 1000;
a = -.5;
b = .5;
c = .005;
output = zeros(3,n_samples);
temp_input = {};
i = 1;
min_len = 2000;
P = [2;6;-1;1;1;0;0;-1;-1;-1;1;1;0;0]; %L-Shape
while i < n_samples
try
thermalmodel = createpde("thermal");
pos = a + (b-a).*rand(2,1);
max_radius = min(abs(pos));
radius = c + (max_radius-c)*rand(1,1);
C1 =[1;pos;radius ];
C1 = [C1;zeros(length(P) - length(C1),1)];
output(:,i) = C1(2:4);
gd = [P,C1];
[g,h] = decsg(gd);
geom = geometryFromEdges(thermalmodel,g);
thermalProperties(thermalmodel,"ThermalConductivity",1,...
"MassDensity",.0001,...
"SpecificHeat",1);
FaceID = nearestFace(geom,[C1(2),C1(3)]);
internalHeatSource(thermalmodel,1,'Face',FaceID);
thermalBC(thermalmodel,"Edge",1:4,"Temperature",1);
thermalIC(thermalmodel,0.5);
msh = generateMesh(thermalmodel,'GeometricOrder','quadratic');
results = solve(thermalmodel,t);
min_len = min(min_len,length(results.Temperature));
temp_input{i} = results.Temperature;
i = i+1;
end
end
%N.B. the number of nodes in the mesh is not always tha same, even if the
%circle is inside the L-Shape geometry, so I take the minimum length
input = zeros(min_len,n_samples);
for i=1:length(temp_input)
input(:,i) = temp_input{i}(1:min_len);
end
net = feedforwardnet([10 10 10]);
net.trainParam.max_fail = 10;
net = train(net,input,output, 'useParallel','yes')
As you can see from the code, I'm using nodes temperature (results.Temperature) as input for the net, and the position of the random hot sources as output. However I'm cheating, since in a real case I wouldn't know how the sources are placed on the surface, and I will not have the output vector as described in the code. My idea is to feed the network with the temperatures (as is done now), obtain the circle position as output (as is done now), but then compute the loss between the input and the temperature obtained by placing the predicted output on the surface. I don't know if I was clear enough :')
Is it possible to specify a loss function like that?
Thanks in advance for your help
0 Comments
Accepted Answer
Aniketh
on 8 Jul 2023
Hi Antonio, you have an interesting approach to the problem, there could be a few ways to tackle this problem, essentially you want to do here is implement a custom loss function which evaluates the performance using reconstruction loss implicitly training the feed forward network to learn to predict the position of the heat source. Catch being the loss has to be computed against node temperatures instead of the value you want the network to predict.
I looked into the capabilities matlab offers to define a custom loss function but could not find any implementation for doing this using the train() function, I think the easiest work around to this is just write/pick up a custom training loop which gives you a lot more flexibility.
Given your case it would be best to do this since you are using some intractable functions to compute the temperatures from the heat source position, which you do not want involved in the computation of the gradients. There are many resources online on how to do this in matlab.
Hope this helped, let me know if you have any follow-ups!
2 Comments
Aniketh
on 10 Jul 2023
Yeah, your idea makes sense. By training the network to reproduce the temperature profile from the given input, it might be able to generalize well and handle more complex scenarios with multiple thermal sources and more complex meshes. You would have to figure the implementation of that, seems a reasonable way to proced.
More Answers (0)
See Also
Categories
Find more on Define Shallow Neural Network Architectures in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!