Skip to content
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

Updates #24

Merged
merged 3 commits into from
Jan 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Example/helloWorld.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@
"metadata": {},
"source": [
"Finally we need to copy these parameters into our new Modelica model\n",
"`NeuralNetwork.Examples.Utilities.PolynomNetwork` and set the number of inputs,\n",
"`NeuralNetwork.Examples.Utilities.SimpleNetwork` and set the number of inputs,\n",
"outputs and neurons for the network and each layer.\n",
"Finally we connect:\n",
" - the input to the input layer \n",
Expand All @@ -528,7 +528,7 @@
"metadata": {},
"source": [
"```modelica\n",
"block PolynomNetwork \"Neural Network approximating y = u*u + 0.5*u - 2.0 on interval [-1,1]\"\n",
"block SimpleNetwork \"Neural Network approximating y = u*u + 0.5*u - 2.0 on interval [-1,1]\"\n",
" extends NeuralNetwork.Networks.Interfaces.Network(numInputs = 1, numOutputs = 1);\n",
" Layer.Input inputLayer(\n",
" numInputs = 1,\n",
Expand All @@ -552,7 +552,7 @@
" connect(u, inputLayer.u);\n",
" connect(inputLayer.y, outputLayer.u);\n",
" connect(outputLayer.y, y);\n",
"end PolynomNetwork;\n",
"end SimpleNetwork;\n",
"```"
]
}
Expand Down
11 changes: 6 additions & 5 deletions NeuralNetwork/Examples/HelloWorld.mo
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ model HelloWorld
Placement(transformation(origin = {-60, 0}, extent = {{-20, -20}, {20, 20}})));
Utilities.SimpleEquation eq annotation(
Placement(transformation(origin = {40, 30}, extent = {{-20, -20}, {20, 20}})));
Utilities.PolynomNetwork nn annotation(
Utilities.SimpleNetwork nn annotation(
Placement(transformation(origin = {40, -30}, extent = {{-20, -20}, {20, 20}})));
Real reference = eq.y;
Real prediction = nn.y[1];
Real prediction = nn.y;
Real error = reference - prediction;
equation
connect(sine.y, eq.u) annotation(
Line(points = {{-38, 0}, {-20, 0}, {-20, 30}, {16, 30}}, color = {0, 0, 127}));
connect(sine.y, nn.u[1]) annotation(
connect(sine.y, nn.u) annotation(
Line(points = {{-38, 0}, {-20, 0}, {-20, -30}, {16, -30}}, color = {0, 0, 127}));
annotation(
Documentation(info = "<html><head></head><body>
Expand All @@ -22,10 +23,10 @@ equation
</p>
<p>y = u^2 + 0.5u - 2</p>
<p>with artificial neural network surrogate
<a href=\"modelica://NeuralNetwork.Examples.Utilities.PolynomNetwork\">PolynomNetwork</a>.
<a href=\"modelica://NeuralNetwork.Examples.Utilities.SimpleNetwork\">SimpleNetwork</a>.
</p>
<p>
A two-dimensional polynomial equation is approximmated with a dense feed-forward neural network.
A two-dimensional polynomial equation is approximated with a dense feed-forward neural network.
The network was generated using Python script from Notebook
<a href=\"https://github.com/AMIT-HSBI/NeuralNetwork/blob/main/Example/HelloWorld.ipynb\">HelloWorld.ipynb</a>.
</p>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
within NeuralNetwork.Examples.Utilities;

block PolynomNetwork "Neural Network approximating y = u*u + 0.5*u - 2.0 on interval [-1,1]"
extends NeuralNetwork.Networks.Interfaces.Network(final u = inputLayer.u, final y = outputLayer.y);
block SimpleNetwork "Neural Network approximating y = u*u + 0.5*u - 2.0 on interval [-1,1]"
extends NeuralNetwork.Networks.Interfaces.SISO(final u = inputLayer.u[1], final y = outputLayer.y[1]);
Layer.Dense inputLayer(
weights = layer_1_weights,
bias = layer_1_bias,
Expand All @@ -15,10 +15,10 @@ block PolynomNetwork "Neural Network approximating y = u*u + 0.5*u - 2.0 on inte
) annotation(
Placement(transformation(origin = {50, 0}, extent = {{-30, -30}, {30, 30}})));

parameter Real[2,1] layer_1_weights = {{-0.95248}, {-0.943175}};
parameter Real[2] layer_1_bias = {0.872633, -0.949252};
parameter Real[1,2] layer_2_weights = {{-2.25385, 1.40389}};
parameter Real[1] layer_2_bias = {0.60548};
parameter Real[2,1] layer_1_weights = {{-0.7039596170222842}, {-0.8750912730224503}};
parameter Real[2] layer_1_bias = {1.169784394444224, -1.344723440170712};
parameter Real[1,2] layer_2_weights = {{-4.513856227908402, 2.4932885356708683}};
parameter Real[1] layer_2_bias = {3.897109323824147};
equation
connect(inputLayer.y, outputLayer.u) annotation(
Line(points = {{-48, 0}, {30, 0}}, color = {0, 0, 127}, thickness = 0.5));
Expand All @@ -29,4 +29,4 @@ equation
<a href=\"https://github.com/AMIT-HSBI/NeuralNetwork/blob/main/Example/HelloWorld.ipynb\">HelloWorld.ipynb</a>.
</p><p>Trained with TensorFlow on 8000 data points from interval [-1,1].</p>
</body></html>"));
end PolynomNetwork;
end SimpleNetwork;
2 changes: 1 addition & 1 deletion NeuralNetwork/Examples/Utilities/package.order
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
NARX_Network
SamplerMIMO
TimeDelay
PolynomNetwork
SimpleNetwork
SimpleEquation
24 changes: 24 additions & 0 deletions NeuralNetwork/Networks/Interfaces/MISO.mo
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
within NeuralNetwork.Networks.Interfaces;

partial block MISO
Modelica.Blocks.Interfaces.RealInput u[:] "Connector of Real input signals" annotation(
Placement(transformation(extent = {{0, 0}, {0, 0}}), iconTransformation(extent = {{-140, -20}, {-100, 20}})));
Modelica.Blocks.Interfaces.RealOutput y "Connector of Real output signal" annotation(
Placement(transformation(extent = {{0, 0}, {0, 0}}), iconTransformation(extent = {{100, -10}, {120, 10}})));
annotation(
Icon(graphics = {Rectangle(fillColor = {255, 255, 255}, fillPattern = FillPattern.Solid, lineThickness = 1, extent = {{-100, 100}, {100, -100}}, radius = 20), Ellipse(origin = {-60, -40}, lineColor = {212, 0, 0}, fillColor = {255, 255, 255}, fillPattern = FillPattern.Sphere, extent = {{10, 10}, {-10, -10}}), Ellipse(origin = {-60, 0}, lineColor = {212, 0, 0}, fillColor = {255, 255, 255}, fillPattern = FillPattern.Sphere, extent = {{10, 10}, {-10, -10}}), Ellipse(origin = {-60, 40}, lineColor = {212, 0, 0}, fillColor = {255, 255, 255}, fillPattern = FillPattern.Sphere, extent = {{10, 10}, {-10, -10}}), Ellipse(origin = {-60, 40}, lineColor = {212, 0, 0}, fillColor = {255, 255, 255}, fillPattern = FillPattern.Sphere, extent = {{10, 10}, {-10, -10}}), Ellipse(origin = {60, -20}, lineColor = {0, 170, 0}, fillColor = {255, 255, 255}, fillPattern = FillPattern.Sphere, extent = {{10, 10}, {-10, -10}}), Ellipse(origin = {60, 20}, lineColor = {0, 170, 0}, fillColor = {255, 255, 255}, fillPattern = FillPattern.Sphere, extent = {{10, 10}, {-10, -10}}), Ellipse(origin = {0, -20}, lineColor = {0, 170, 255}, fillColor = {255, 255, 255}, fillPattern = FillPattern.Sphere, extent = {{10, 10}, {-10, -10}}), Ellipse(origin = {0, 20}, lineColor = {0, 170, 255}, fillColor = {255, 255, 255}, fillPattern = FillPattern.Sphere, extent = {{10, 10}, {-10, -10}}), Ellipse(origin = {0, 60}, lineColor = {0, 170, 255}, fillColor = {255, 255, 255}, fillPattern = FillPattern.Sphere, extent = {{10, 10}, {-10, -10}}), Ellipse(origin = {0, -60}, lineColor = {0, 170, 255}, fillColor = {255, 255, 255}, fillPattern = FillPattern.Sphere, extent = {{10, 10}, {-10, -10}}), Line(points = {{-48, 44}, {-12, 60}}, thickness = 0.5, arrow = {Arrow.None, Arrow.Filled}), Line(points = {{-48, 42}, {-12, 24}}, thickness = 0.5, arrow = {Arrow.None, Arrow.Filled}), Line(points = {{-48, 40}, {-12, -16}}, thickness = 0.5, arrow = {Arrow.None, Arrow.Filled}), Line(points = {{-48, 38}, {-12, -50}}, thickness = 0.5, arrow = {Arrow.None, Arrow.Filled}), Line(points = {{-48, 2}, {-12, 56}}, thickness = 0.5, arrow = {Arrow.None, Arrow.Filled}), Line(points = {{-48, 0}, {-12, 20}}, thickness = 0.5, arrow = {Arrow.None, Arrow.Filled}), Line(points = {{-48, -2}, {-12, -20}}, thickness = 0.5, arrow = {Arrow.None, Arrow.Filled}), Line(points = {{-48, -4}, {-12, -56}}, thickness = 0.5, arrow = {Arrow.None, Arrow.Filled}), Line(points = {{-48, -38}, {-12, 50}}, thickness = 0.5, arrow = {Arrow.None, Arrow.Filled}), Line(points = {{-48, -40}, {-12, 16}}, thickness = 0.5, arrow = {Arrow.None, Arrow.Filled}), Line(points = {{-48, -42}, {-12, -24}}, thickness = 0.5, arrow = {Arrow.None, Arrow.Filled}), Line(points = {{-48, -44}, {-12, -60}}, thickness = 0.5, arrow = {Arrow.None, Arrow.Filled}), Line(points = {{12, 56}, {48, 26}}, thickness = 0.5, arrow = {Arrow.None, Arrow.Filled}), Line(points = {{12, 52}, {48, -14}}, thickness = 0.5, arrow = {Arrow.None, Arrow.Filled}), Line(points = {{12, 22}, {48, 22}}, thickness = 0.5, arrow = {Arrow.None, Arrow.Filled}), Line(points = {{12, 18}, {48, -18}}, thickness = 0.5, arrow = {Arrow.None, Arrow.Filled}), Line(points = {{12, -18}, {48, 18}}, thickness = 0.5, arrow = {Arrow.None, Arrow.Filled}), Line(points = {{12, -22}, {48, -22}}, thickness = 0.5, arrow = {Arrow.None, Arrow.Filled}), Line(points = {{12, -52}, {48, 14}}, thickness = 0.5, arrow = {Arrow.None, Arrow.Filled}), Line(points = {{12, -56}, {48, -26}}, thickness = 0.5, arrow = {Arrow.None, Arrow.Filled}), Text(origin = {0, 120}, textColor = {0, 0, 255}, extent = {{-80, 20}, {80, -20}}, textString = "%name")}),
Documentation(info = "<html><head></head><body>
<p>
General interface for neural networks consisting of input <b>u</b> and output <b>y</b>.
</p>
<p>Building block to build neural networks. To create a new neural network:
</p>
<p>
<ol>
<li>Extend this partial block Network.</li>
<li>Add input, hidden and output layers from <a href=\"modelica://NeuralNetwork.NeuralNetwork.Layer\">NeuralNetwork.Layer</a>.</li>
<li>Connect layers internally as well as to network input <b>u</b> and output <b>y</b>.</li>
</ol>
</p>
</body></html>"));
end MISO;
6 changes: 3 additions & 3 deletions NeuralNetwork/Networks/Interfaces/Network.mo
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ partial block Network
</p>
<p>
<ol>
<li>Extend this partial block Network and set <b>numInputs</b> and <b>numOutputs</b>.</li>
<li>Add input, hidden and ouput layers from <a href=\"modelica://NeuralNetwork.NeuralNetwork.Layer\">NeuralNetwork.Layer</a>.</li>
<li>Connet layers internally as well as to network input <b>u</b> and output <b>y</b>.</li>
<li>Extend this partial block Network.</li>
<li>Add input, hidden and output layers from <a href=\"modelica://NeuralNetwork.NeuralNetwork.Layer\">NeuralNetwork.Layer</a>.</li>
<li>Connect layers internally as well as to network input <b>u</b> and output <b>y</b>.</li>
</ol>
</p>
</body></html>"));
Expand Down
24 changes: 24 additions & 0 deletions NeuralNetwork/Networks/Interfaces/SISO.mo
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
within NeuralNetwork.Networks.Interfaces;

partial block SISO
Modelica.Blocks.Interfaces.RealInput u "Connector of Real input signal" annotation(
Placement(transformation(extent = {{0, 0}, {0, 0}}), iconTransformation(extent = {{-140, -20}, {-100, 20}})));
Modelica.Blocks.Interfaces.RealOutput y "Connector of Real output signal" annotation(
Placement(transformation(extent = {{0, 0}, {0, 0}}), iconTransformation(extent = {{100, -10}, {120, 10}})));
annotation(
Icon(graphics = {Rectangle(fillColor = {255, 255, 255}, fillPattern = FillPattern.Solid, lineThickness = 1, extent = {{-100, 100}, {100, -100}}, radius = 20), Ellipse(origin = {-60, -40}, lineColor = {212, 0, 0}, fillColor = {255, 255, 255}, fillPattern = FillPattern.Sphere, extent = {{10, 10}, {-10, -10}}), Ellipse(origin = {-60, 0}, lineColor = {212, 0, 0}, fillColor = {255, 255, 255}, fillPattern = FillPattern.Sphere, extent = {{10, 10}, {-10, -10}}), Ellipse(origin = {-60, 40}, lineColor = {212, 0, 0}, fillColor = {255, 255, 255}, fillPattern = FillPattern.Sphere, extent = {{10, 10}, {-10, -10}}), Ellipse(origin = {-60, 40}, lineColor = {212, 0, 0}, fillColor = {255, 255, 255}, fillPattern = FillPattern.Sphere, extent = {{10, 10}, {-10, -10}}), Ellipse(origin = {60, -20}, lineColor = {0, 170, 0}, fillColor = {255, 255, 255}, fillPattern = FillPattern.Sphere, extent = {{10, 10}, {-10, -10}}), Ellipse(origin = {60, 20}, lineColor = {0, 170, 0}, fillColor = {255, 255, 255}, fillPattern = FillPattern.Sphere, extent = {{10, 10}, {-10, -10}}), Ellipse(origin = {0, -20}, lineColor = {0, 170, 255}, fillColor = {255, 255, 255}, fillPattern = FillPattern.Sphere, extent = {{10, 10}, {-10, -10}}), Ellipse(origin = {0, 20}, lineColor = {0, 170, 255}, fillColor = {255, 255, 255}, fillPattern = FillPattern.Sphere, extent = {{10, 10}, {-10, -10}}), Ellipse(origin = {0, 60}, lineColor = {0, 170, 255}, fillColor = {255, 255, 255}, fillPattern = FillPattern.Sphere, extent = {{10, 10}, {-10, -10}}), Ellipse(origin = {0, -60}, lineColor = {0, 170, 255}, fillColor = {255, 255, 255}, fillPattern = FillPattern.Sphere, extent = {{10, 10}, {-10, -10}}), Line(points = {{-48, 44}, {-12, 60}}, thickness = 0.5, arrow = {Arrow.None, Arrow.Filled}), Line(points = {{-48, 42}, {-12, 24}}, thickness = 0.5, arrow = {Arrow.None, Arrow.Filled}), Line(points = {{-48, 40}, {-12, -16}}, thickness = 0.5, arrow = {Arrow.None, Arrow.Filled}), Line(points = {{-48, 38}, {-12, -50}}, thickness = 0.5, arrow = {Arrow.None, Arrow.Filled}), Line(points = {{-48, 2}, {-12, 56}}, thickness = 0.5, arrow = {Arrow.None, Arrow.Filled}), Line(points = {{-48, 0}, {-12, 20}}, thickness = 0.5, arrow = {Arrow.None, Arrow.Filled}), Line(points = {{-48, -2}, {-12, -20}}, thickness = 0.5, arrow = {Arrow.None, Arrow.Filled}), Line(points = {{-48, -4}, {-12, -56}}, thickness = 0.5, arrow = {Arrow.None, Arrow.Filled}), Line(points = {{-48, -38}, {-12, 50}}, thickness = 0.5, arrow = {Arrow.None, Arrow.Filled}), Line(points = {{-48, -40}, {-12, 16}}, thickness = 0.5, arrow = {Arrow.None, Arrow.Filled}), Line(points = {{-48, -42}, {-12, -24}}, thickness = 0.5, arrow = {Arrow.None, Arrow.Filled}), Line(points = {{-48, -44}, {-12, -60}}, thickness = 0.5, arrow = {Arrow.None, Arrow.Filled}), Line(points = {{12, 56}, {48, 26}}, thickness = 0.5, arrow = {Arrow.None, Arrow.Filled}), Line(points = {{12, 52}, {48, -14}}, thickness = 0.5, arrow = {Arrow.None, Arrow.Filled}), Line(points = {{12, 22}, {48, 22}}, thickness = 0.5, arrow = {Arrow.None, Arrow.Filled}), Line(points = {{12, 18}, {48, -18}}, thickness = 0.5, arrow = {Arrow.None, Arrow.Filled}), Line(points = {{12, -18}, {48, 18}}, thickness = 0.5, arrow = {Arrow.None, Arrow.Filled}), Line(points = {{12, -22}, {48, -22}}, thickness = 0.5, arrow = {Arrow.None, Arrow.Filled}), Line(points = {{12, -52}, {48, 14}}, thickness = 0.5, arrow = {Arrow.None, Arrow.Filled}), Line(points = {{12, -56}, {48, -26}}, thickness = 0.5, arrow = {Arrow.None, Arrow.Filled}), Text(origin = {0, 120}, textColor = {0, 0, 255}, extent = {{-80, 20}, {80, -20}}, textString = "%name")}),
Documentation(info = "<html><head></head><body>
<p>
General interface for neural networks consisting of input <b>u</b> and output <b>y</b>.
</p>
<p>Building block to build neural networks. To create a new neural network:
</p>
<p>
<ol>
<li>Extend this partial block Network.</li>
<li>Add input, hidden and output layers from <a href=\"modelica://NeuralNetwork.NeuralNetwork.Layer\">NeuralNetwork.Layer</a>.</li>
<li>Connect layers internally as well as to network input <b>u</b> and output <b>y</b>.</li>
</ol>
</p>
</body></html>"));
end SISO;
2 changes: 2 additions & 0 deletions NeuralNetwork/Networks/Interfaces/package.order
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
Network
SISO
MISO
Loading
Loading