diff --git a/Example/helloWorld.ipynb b/Example/helloWorld.ipynb index 9c0e684..a82c90e 100644 --- a/Example/helloWorld.ipynb +++ b/Example/helloWorld.ipynb @@ -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", @@ -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", @@ -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", "```" ] } diff --git a/NeuralNetwork/Examples/HelloWorld.mo b/NeuralNetwork/Examples/HelloWorld.mo index 3f1d2d0..45f1199 100644 --- a/NeuralNetwork/Examples/HelloWorld.mo +++ b/NeuralNetwork/Examples/HelloWorld.mo @@ -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 = "
@@ -22,10 +23,10 @@ equationy = u^2 + 0.5u - 2
with artificial neural network surrogate - PolynomNetwork. + SimpleNetwork.
- 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 HelloWorld.ipynb.
diff --git a/NeuralNetwork/Examples/Utilities/PolynomNetwork.mo b/NeuralNetwork/Examples/Utilities/SimpleNetwork.mo similarity index 66% rename from NeuralNetwork/Examples/Utilities/PolynomNetwork.mo rename to NeuralNetwork/Examples/Utilities/SimpleNetwork.mo index 5582674..d5c9ff1 100644 --- a/NeuralNetwork/Examples/Utilities/PolynomNetwork.mo +++ b/NeuralNetwork/Examples/Utilities/SimpleNetwork.mo @@ -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, @@ -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)); @@ -29,4 +29,4 @@ equation HelloWorld.ipynb.Trained with TensorFlow on 8000 data points from interval [-1,1].
")); -end PolynomNetwork; +end SimpleNetwork; diff --git a/NeuralNetwork/Examples/Utilities/package.order b/NeuralNetwork/Examples/Utilities/package.order index 6566068..b66e0c2 100644 --- a/NeuralNetwork/Examples/Utilities/package.order +++ b/NeuralNetwork/Examples/Utilities/package.order @@ -1,5 +1,5 @@ NARX_Network SamplerMIMO TimeDelay -PolynomNetwork +SimpleNetwork SimpleEquation diff --git a/NeuralNetwork/Networks/Interfaces/MISO.mo b/NeuralNetwork/Networks/Interfaces/MISO.mo new file mode 100644 index 0000000..8b489ae --- /dev/null +++ b/NeuralNetwork/Networks/Interfaces/MISO.mo @@ -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 = " ++ General interface for neural networks consisting of input u and output y. +
+Building block to build neural networks. To create a new neural network: +
++