Feedforwardnet custom loss function setup
显示 更早的评论
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 个)
类别
在 帮助中心 和 File Exchange 中查找有关 PDE Solvers 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!