Reputation: 1
I have written a code in Modelica to train a feedforward two-layer neural network for my Master thesis. The code receives a vector of six elements (u[nin]) and provides an output vector of two elements y[nout]. When I translate the code in dymola, I receive an error saying that the dimensionality of the parts included in a specific line of code must be equal. Once I remove this particular line, the code is translated successfully. I really spent much effort trying to resolve this error but in vain! Any help regarding the problem would be very much appreciated.
The code can be found below: (Please note that the line which causes the generation of the translation error is marked with // in the code).
model NN_block
Modelica.Blocks.Interfaces.RealInput
u[nin] "Connector of Real input signals"
annotation (Placement(transformation(extent={{-140,-20},{-100,20}},
rotation=0)));
Modelica.Blocks.Interfaces.RealOutput y[nout]
annotation (Placement(transformation(extent={{100,-10},{120,10}})));
parameter Integer nin2=1;
parameter Integer nin=6;
parameter Integer nout=2;
Real wji[10,6];
Real delta_wij[6,10];
Real bj[10,1];
Real delta_bjT[1,10];
Real wkj[2,10];
Real delta_wjk[10,2];
Real bk[2,1];
Real delta_bkT[1,2];
Real E;
Real ek[1,2];
Real yj[10,1];
Modelica.Blocks.Interfaces.BooleanOutput Input_trigger
annotation (Placement(
transformation(extent={{100,-46},{120,-26}}), iconTransformation(extent=
{{100,-46},{120,-26}})));
Modelica.Blocks.Interfaces.RealInput eTau1 annotation (Placement(
transformation(extent={{-182,36},{-142,76}}), iconTransformation(
extent={{10,-10},{-10,10}},
rotation=90,
origin={-44,90})));
Modelica.Blocks.Interfaces.RealInput eTau2 annotation (Placement(
transformation(extent={{-148,46},{-108,86}}), iconTransformation(
extent={{10,-10},{-10,10}},
rotation=90,
origin={50,90})));
annotation (Placement(transformation(extent={{-10,-10},{10,10}},
rotation=-90,
origin={-50,90}), iconTransformation(
extent={{-10,-10},{10,10}},
rotation=-90,
origin={0,90})));
algorithm
E:=1;
wji:=0.5*ones(10,6);
bj:=0.25*ones(10,1);
wkj:=0.75*ones(2,10);
bk:=0.6*ones(2,1);
delta_wij:=zeros(6,10);
delta_bjT:=zeros(1,10);
delta_wjk:=zeros(10,2);
delta_bkT:=zeros(1,2);
while E>0.01 loop
Input_trigger:=true;
y:=wkj*NeuralNetwork.Utilities.LogSig(wji*u+bj[:,1])+bk[:,1];
yj[:,1]:=NeuralNetwork.Utilities.LogSig(wji*u+bj[:,1]);
ek:=[eTau1,eTau2];
E:=0.5*(eTau1^2+eTau2^2);
if E>0.01 then
// delta_wij:=0.01*u*transpose(yj)*(ones(10,1)-yj)*ek*wkj+0.9*delta_wij;
delta_bjT:=0.01*transpose(yj)*(ones(10,1)-yj)*ek*wkj + 0.9*delta_bjT;
delta_wjk:=0.01*yj*ek + 0.9*delta_wjk;
delta_bkT:=0.01*ek + 0.9*delta_bkT;
wji:=wji+transpose(delta_wij);
bj:=bj+transpose(delta_bjT);
wkj:=wkj+transpose(delta_wjk);
bk:=bk+transpose(delta_bkT);
else
break;
end if;
end while;
annotation (Placement(transformation(extent={{-140,44},{-100,84}})),
uses(Modelica(version="3.2")), Icon(graphics={Rectangle(
extent={{-100,-100},{100,80}},
lineColor={0,0,255},
lineThickness=1), Text(
extent={{-68,20},{78,-16}},
lineColor={0,0,255},
lineThickness=1,
textString="Two-LayerNeural Network")}),
Diagram(graphics));
end NN_block;
Upvotes: 0
Views: 639
Reputation: 485
Have you tested this line in the command line? This could be done by creating the variables used in your model in the workspace and then running the commented out line in the command line to check the sizing is correct. You can do this in the Dymola command line but I am not familiar enough with OpenModelica to tell if you can perform the same check there.
Upvotes: 0
Reputation: 1
Looking at the code, it appears that there is mismatch in the sizes of the matrices on the right hand side of the equation concerned.
Try defining u as a 2D matrix of size 6x1.
Upvotes: 0
Reputation: 3523
Once I remove the lines using LogSig I could get OpenModelica to produce an error-message for you:
[a.mo:59:3-59:71] Error: Incompatible argument types to operation matrix multiplication, left type: Real[6], right type: Real[1, 10]
[a.mo:59:3-59:71] Error: Cannot resolve type of expression 0.01 * u * transpose(yj). The operands have types Real[6], Real[1, 10] in component .
u
and yj'
have dimensions 6
and 1,10
Upvotes: 1