{ "cells": [ { "cell_type": "raw", "metadata": { "vscode": { "languageId": "raw" } }, "source": [ "\n", " \"Open\n", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Equation Learner Layer\n", "\n", "Represents a nnodely implementation of the Task-Parametrized Equation Learner block.\n", "\n", "Official Paper: [Task-Parametrized Equation Learner](https://www.sciencedirect.com/science/article/pii/S0921889022001981)" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "ExecuteTime": { "end_time": "2025-05-23T14:57:27.903804Z", "start_time": "2025-05-23T14:57:26.724109Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>-- nnodely_v1.5.0 --<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n" ] } ], "source": [ "# uncomment the command below to install the nnodely package\n", "#!pip install nnodely\n", "\n", "from nnodely import *" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Basic Usage\n", "Create an simple equation learner block using nnodely trigonometric functions (Tan, Sin, Cos). \n", "\n", "The initial linear layer is created using random initialization." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "ExecuteTime": { "end_time": "2025-05-23T14:57:39.602904Z", "start_time": "2025-05-23T14:57:39.592189Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[33m[check_names] The name 'x' is already in defined as NeuObj but it is overwritten.\u001b[0m\n" ] }, { "data": { "text/plain": [ "\u001b[1;32m==================================== Output ====================================\u001b[0m\n", "\u001b[32m{'Constants': {},\n", " 'Functions': {},\n", " 'Info': {},\n", " 'Inputs': {'x': {'dim': 1, 'sw': [-1, 0]}},\n", " 'Outputs': {'out': 'Concatenate10'},\n", " 'Parameters': {'PLinear4W': {'dim': [1, 3]}, 'PLinear4b': {'dim': 3}},\n", " 'Relations': {'Concatenate10': ['Concatenate', ['Concatenate7', 'Cos9']],\n", " 'Concatenate7': ['Concatenate', ['Tan4', 'Sin6']],\n", " 'Cos9': ['Cos', ['Select8']],\n", " 'Linear2': ['Linear',\n", " ['SamplePart1'],\n", " 'PLinear4W',\n", " 'PLinear4b',\n", " 0],\n", " 'SamplePart1': ['SamplePart', ['x'], -1, [-1, 0]],\n", " 'Select3': ['Select', ['Linear2'], 3, 0],\n", " 'Select5': ['Select', ['Linear2'], 3, 1],\n", " 'Select8': ['Select', ['Linear2'], 3, 2],\n", " 'Sin6': ['Sin', ['Select5']],\n", " 'Tan4': ['Tan', ['Select3']]}}\u001b[0m\n", "\u001b[1;32m--------------------------- out {'dim': 3, 'sw': 1} ----------------------------\u001b[0m" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = Input('x')\n", "equation_learner = EquationLearner(functions=[Tan, Sin, Cos])\n", "Output('out',equation_learner(x.last()))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Input layer\n", "Create an simple equation learner block using nnodely trigonometric functions and passing an input layer. \n", "\n", "In this case the 'output_dimension' must match the sum of number of inputs of the activation functions." ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "ExecuteTime": { "end_time": "2025-05-23T14:57:58.313163Z", "start_time": "2025-05-23T14:57:58.301945Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[33m[check_names] The name 'x' is already in defined as NeuObj but it is overwritten.\u001b[0m\n", "\u001b[33m[check_names] The name 'out' is already in defined as NeuObj but it is overwritten.\u001b[0m\n" ] }, { "data": { "text/plain": [ "\u001b[1;32m==================================== Output ====================================\u001b[0m\n", "\u001b[32m{'Constants': {},\n", " 'Functions': {},\n", " 'Info': {},\n", " 'Inputs': {'x': {'dim': 1, 'sw': [-1, 0]}},\n", " 'Outputs': {'out': 'Concatenate21'},\n", " 'Parameters': {'PLinear9W': {'dim': [1, 3]}},\n", " 'Relations': {'Concatenate18': ['Concatenate', ['Tan15', 'Sin17']],\n", " 'Concatenate21': ['Concatenate', ['Concatenate18', 'Cos20']],\n", " 'Cos20': ['Cos', ['Select19']],\n", " 'Linear13': ['Linear', ['SamplePart12'], 'PLinear9W', None, 0],\n", " 'SamplePart12': ['SamplePart', ['x'], -1, [-1, 0]],\n", " 'Select14': ['Select', ['Linear13'], 3, 0],\n", " 'Select16': ['Select', ['Linear13'], 3, 1],\n", " 'Select19': ['Select', ['Linear13'], 3, 2],\n", " 'Sin17': ['Sin', ['Select16']],\n", " 'Tan15': ['Tan', ['Select14']]}}\u001b[0m\n", "\u001b[1;32m--------------------------- out {'dim': 3, 'sw': 1} ----------------------------\u001b[0m" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = Input('x')\n", "input_layer = Linear(output_dimension=3)\n", "equation_learner = EquationLearner(functions=[Tan, Sin, Cos], linear_in=input_layer)\n", "Output('out', equation_learner(x.last()))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Input layer and output layer\n", "Create an simple equation learner block using nnodely trigonometric functions and passing an input layer and also a linear output layer\n", "\n", "(By default, there is no linear output layer)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "ExecuteTime": { "end_time": "2025-05-23T14:58:27.298498Z", "start_time": "2025-05-23T14:58:27.289645Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[33m[check_names] The name 'x' is already in defined as NeuObj but it is overwritten.\u001b[0m\n", "\u001b[33m[check_names] The name 'out' is already in defined as NeuObj but it is overwritten.\u001b[0m\n" ] }, { "data": { "text/plain": [ "\u001b[1;32m==================================== Output ====================================\u001b[0m\n", "\u001b[32m{'Constants': {},\n", " 'Functions': {},\n", " 'Info': {},\n", " 'Inputs': {'x': {'dim': 1, 'sw': [-1, 0]}},\n", " 'Outputs': {'out': 'Linear33'},\n", " 'Parameters': {'PLinear14W': {'dim': [1, 3]}, 'PLinear16W': {'dim': [3, 1]}},\n", " 'Relations': {'Concatenate29': ['Concatenate', ['Tan26', 'Sin28']],\n", " 'Concatenate32': ['Concatenate', ['Concatenate29', 'Cos31']],\n", " 'Cos31': ['Cos', ['Select30']],\n", " 'Linear24': ['Linear', ['SamplePart23'], 'PLinear14W', None, 0],\n", " 'Linear33': ['Linear', ['Concatenate32'], 'PLinear16W', None, 0],\n", " 'SamplePart23': ['SamplePart', ['x'], -1, [-1, 0]],\n", " 'Select25': ['Select', ['Linear24'], 3, 0],\n", " 'Select27': ['Select', ['Linear24'], 3, 1],\n", " 'Select30': ['Select', ['Linear24'], 3, 2],\n", " 'Sin28': ['Sin', ['Select27']],\n", " 'Tan26': ['Tan', ['Select25']]}}\u001b[0m\n", "\u001b[1;32m--------------------------- out {'dim': 1, 'sw': 1} ----------------------------\u001b[0m" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = Input('x')\n", "input_layer = Linear(output_dimension=3)\n", "output_layer = Linear(output_dimension=1)\n", "equation_learner = EquationLearner(functions=[Tan, Sin, Cos], linear_in=input_layer, linear_out=output_layer)\n", "Output('out', equation_learner(x.last()))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Multiple inputs\n", "\n", "Create an simple equation learner block using nnodely trigonometric functions and passing multiple inputs when calling the equation layer block.\n", "\n", "All the given inputs will be concatenated before going through the linear input layer. The input and output dimensions of the input layer are 2, 3 respectively." ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "ExecuteTime": { "end_time": "2025-05-23T15:05:19.717319Z", "start_time": "2025-05-23T15:05:19.707535Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[33m[check_names] The name 'x' is already in defined as NeuObj but it is overwritten.\u001b[0m\n", "\u001b[33m[check_names] The name 'out' is already in defined as NeuObj but it is overwritten.\u001b[0m\n" ] }, { "data": { "text/plain": [ "\u001b[1;32m==================================== Output ====================================\u001b[0m\n", "\u001b[32m{'Constants': {},\n", " 'Functions': {},\n", " 'Info': {},\n", " 'Inputs': {'F': {'dim': 1, 'sw': [-1, 0]}, 'x': {'dim': 1, 'sw': [-1, 0]}},\n", " 'Outputs': {'out': 'Concatenate47'},\n", " 'Parameters': {'PLinear23W': {'dim': [2, 3]}, 'PLinear23b': {'dim': 3}},\n", " 'Relations': {'Concatenate38': ['Concatenate',\n", " ['SamplePart35', 'SamplePart37']],\n", " 'Concatenate44': ['Concatenate', ['Tan41', 'Sin43']],\n", " 'Concatenate47': ['Concatenate', ['Concatenate44', 'Cos46']],\n", " 'Cos46': ['Cos', ['Select45']],\n", " 'Linear39': ['Linear',\n", " ['Concatenate38'],\n", " 'PLinear23W',\n", " 'PLinear23b',\n", " 0],\n", " 'SamplePart35': ['SamplePart', ['x'], -1, [-1, 0]],\n", " 'SamplePart37': ['SamplePart', ['F'], -1, [-1, 0]],\n", " 'Select40': ['Select', ['Linear39'], 3, 0],\n", " 'Select42': ['Select', ['Linear39'], 3, 1],\n", " 'Select45': ['Select', ['Linear39'], 3, 2],\n", " 'Sin43': ['Sin', ['Select42']],\n", " 'Tan41': ['Tan', ['Select40']]}}\u001b[0m\n", "\u001b[1;32m--------------------------- out {'dim': 3, 'sw': 1} ----------------------------\u001b[0m" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = Input('x')\n", "F = Input('F')\n", "equation_learner = EquationLearner(functions=[Tan, Sin, Cos])\n", "Output('out',equation_learner(inputs=(x.last(),F.last())))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Multi-parameter functions\n", "Create an equation learner block with functions that take 2 parameters (add, sub, mul ...). \n", "\n", "Be __careful__ to the output dimension that the linear input layer should have to connect correctly all the activation functions. \n", "\n", "In the example below, both the Add and Mul relations take 2 parameters so the total number of output dimension is 7 instead of 5." ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "ExecuteTime": { "end_time": "2025-05-23T15:07:50.438527Z", "start_time": "2025-05-23T15:07:50.423945Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[33m[check_names] The name 'x' is already in defined as NeuObj but it is overwritten.\u001b[0m\n", "\u001b[33m[check_names] The name 'F' is already in defined as NeuObj but it is overwritten.\u001b[0m\n", "\u001b[33m[check_names] The name 'out' is already in defined as NeuObj but it is overwritten.\u001b[0m\n" ] }, { "data": { "text/plain": [ "\u001b[1;32m==================================== Output ====================================\u001b[0m\n", "\u001b[32m{'Constants': {},\n", " 'Functions': {},\n", " 'Info': {},\n", " 'Inputs': {'x': {'dim': 1, 'sw': [-1, 0]}},\n", " 'Outputs': {'out': 'Concatenate66'},\n", " 'Parameters': {'PLinear29W': {'dim': [1, 7]}},\n", " 'Relations': {'Add55': ['Add', ['Select53', 'Select54']],\n", " 'Concatenate56': ['Concatenate', ['Tan52', 'Add55']],\n", " 'Concatenate59': ['Concatenate', ['Concatenate56', 'Sin58']],\n", " 'Concatenate63': ['Concatenate', ['Concatenate59', 'Mul62']],\n", " 'Concatenate66': ['Concatenate',\n", " ['Concatenate63', 'Identity65']],\n", " 'Identity65': ['Identity', ['Select64']],\n", " 'Linear50': ['Linear', ['SamplePart49'], 'PLinear29W', None, 0],\n", " 'Mul62': ['Mul', ['Select60', 'Select61']],\n", " 'SamplePart49': ['SamplePart', ['x'], -1, [-1, 0]],\n", " 'Select51': ['Select', ['Linear50'], 7, 0],\n", " 'Select53': ['Select', ['Linear50'], 7, 1],\n", " 'Select54': ['Select', ['Linear50'], 7, 2],\n", " 'Select57': ['Select', ['Linear50'], 7, 3],\n", " 'Select60': ['Select', ['Linear50'], 7, 4],\n", " 'Select61': ['Select', ['Linear50'], 7, 5],\n", " 'Select64': ['Select', ['Linear50'], 7, 6],\n", " 'Sin58': ['Sin', ['Select57']],\n", " 'Tan52': ['Tan', ['Select51']]}}\u001b[0m\n", "\u001b[1;32m--------------------------- out {'dim': 5, 'sw': 1} ----------------------------\u001b[0m" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = Input('x')\n", "F = Input('F')\n", "\n", "linear_layer_in_1 = Linear(output_dimension=7)\n", "equation_learner_1 = EquationLearner(functions=[Tan, Add, Sin, Mul, Identity], linear_in=linear_layer_in_1)\n", "Output('out',equation_learner_1(x.last()))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Using custom parametric functions\n", "Create an equation learner block with simple parametric functions" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "ExecuteTime": { "end_time": "2025-05-23T15:08:02.972854Z", "start_time": "2025-05-23T15:08:02.958664Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[33m[check_names] The name 'x' is already in defined as NeuObj but it is overwritten.\u001b[0m\n", "\u001b[33m[check_names] The name 'out' is already in defined as NeuObj but it is overwritten.\u001b[0m\n" ] }, { "data": { "text/plain": [ "\u001b[1;32m==================================== Output ====================================\u001b[0m\n", "\u001b[32m{'Constants': {},\n", " 'Functions': {'FParamFun34': {'code': 'def func1(K1):\\n'\n", " ' return torch.sin(K1)\\n',\n", " 'in_dim': [{'dim': 1, 'sw': 1}],\n", " 'map_over_dim': False,\n", " 'n_input': 1,\n", " 'name': 'func1',\n", " 'params_and_consts': []},\n", " 'FParamFun35': {'code': 'def func2(K2):\\n'\n", " ' return torch.cos(K2)\\n',\n", " 'in_dim': [{'dim': 1, 'sw': 1}],\n", " 'map_over_dim': False,\n", " 'n_input': 1,\n", " 'name': 'func2',\n", " 'params_and_consts': []}},\n", " 'Info': {},\n", " 'Inputs': {'x': {'dim': 1, 'sw': [-1, 0]}},\n", " 'Outputs': {'out': 'Concatenate74'},\n", " 'Parameters': {'PLinear37W': {'dim': [1, 2]}, 'PLinear37b': {'dim': 2}},\n", " 'Relations': {'Concatenate74': ['Concatenate', ['ParamFun71', 'ParamFun73']],\n", " 'Linear69': ['Linear',\n", " ['SamplePart68'],\n", " 'PLinear37W',\n", " 'PLinear37b',\n", " 0],\n", " 'ParamFun71': ['ParamFun', ['Select70'], 'FParamFun34'],\n", " 'ParamFun73': ['ParamFun', ['Select72'], 'FParamFun35'],\n", " 'SamplePart68': ['SamplePart', ['x'], -1, [-1, 0]],\n", " 'Select70': ['Select', ['Linear69'], 2, 0],\n", " 'Select72': ['Select', ['Linear69'], 2, 1]}}\u001b[0m\n", "\u001b[1;32m--------------------------- out {'dim': 2, 'sw': 1} ----------------------------\u001b[0m" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import torch\n", "\n", "def func1(K1):\n", " return torch.sin(K1)\n", "\n", "def func2(K2):\n", " return torch.cos(K2)\n", "\n", "x = Input('x')\n", "parfun1 = ParamFun(func1)\n", "parfun2 = ParamFun(func2)\n", "equation_learner = EquationLearner([parfun1, parfun2])\n", "Output('out',equation_learner(x.last()))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Using parametric functions with parameters\n", "Create an equation learner block with simple parametric functions" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "ExecuteTime": { "end_time": "2025-05-23T15:08:46.604225Z", "start_time": "2025-05-23T15:08:46.585376Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[33m[check_names] The name 'x' is already in defined as NeuObj but it is overwritten.\u001b[0m\n", "\u001b[33m[check_names] The name 'F' is already in defined as NeuObj but it is overwritten.\u001b[0m\n", "\u001b[33m[check_names] The name 'out' is already in defined as NeuObj but it is overwritten.\u001b[0m\n" ] }, { "data": { "text/plain": [ "\u001b[1;32m==================================== Output ====================================\u001b[0m\n", "\u001b[32m{'Constants': {},\n", " 'Functions': {'FParamFun45': {'code': 'def myFun(K1,K2,p1,p2):\\n'\n", " ' return K1*p1+K2*p2\\n',\n", " 'in_dim': [{'dim': 1, 'sw': 1},\n", " {'dim': 1, 'sw': 1}],\n", " 'map_over_dim': False,\n", " 'n_input': 2,\n", " 'name': 'myFun',\n", " 'params_and_consts': ['k1', 'k2']}},\n", " 'Info': {},\n", " 'Inputs': {'F': {'dim': 1, 'sw': [-1, 0]}, 'x': {'dim': 1, 'sw': [-1, 0]}},\n", " 'Outputs': {'out': 'Concatenate90'},\n", " 'Parameters': {'PLinear47W': {'dim': [2, 5]},\n", " 'PLinear47b': {'dim': 5},\n", " 'k1': {'dim': 1,\n", " 'init_values': [[2.0]],\n", " 'sw': 1,\n", " 'values': [[2.0]]},\n", " 'k2': {'dim': 1,\n", " 'init_values': [[3.0]],\n", " 'sw': 1,\n", " 'values': [[3.0]]}},\n", " 'Relations': {'Add89': ['Add', ['Select87', 'Select88']],\n", " 'Concatenate79': ['Concatenate',\n", " ['SamplePart76', 'SamplePart78']],\n", " 'Concatenate86': ['Concatenate', ['ParamFun83', 'Sin85']],\n", " 'Concatenate90': ['Concatenate', ['Concatenate86', 'Add89']],\n", " 'Linear80': ['Linear',\n", " ['Concatenate79'],\n", " 'PLinear47W',\n", " 'PLinear47b',\n", " 0],\n", " 'ParamFun83': ['ParamFun',\n", " ['Select81', 'Select82'],\n", " 'FParamFun45'],\n", " 'SamplePart76': ['SamplePart', ['x'], -1, [-1, 0]],\n", " 'SamplePart78': ['SamplePart', ['F'], -1, [-1, 0]],\n", " 'Select81': ['Select', ['Linear80'], 5, 0],\n", " 'Select82': ['Select', ['Linear80'], 5, 1],\n", " 'Select84': ['Select', ['Linear80'], 5, 2],\n", " 'Select87': ['Select', ['Linear80'], 5, 3],\n", " 'Select88': ['Select', ['Linear80'], 5, 4],\n", " 'Sin85': ['Sin', ['Select84']]}}\u001b[0m\n", "\u001b[1;32m--------------------------- out {'dim': 3, 'sw': 1} ----------------------------\u001b[0m" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def myFun(K1,K2,p1,p2):\n", " return K1*p1+K2*p2\n", "\n", "x = Input('x')\n", "F = Input('F')\n", "\n", "K1 = Parameter('k1', dimensions = 1, sw = 1, values=[[2.0]])\n", "K2 = Parameter('k2', dimensions = 1, sw = 1, values=[[3.0]])\n", "parfun = ParamFun(myFun, parameters_and_constants=[K1,K2])\n", "\n", "equation_learner = EquationLearner([parfun, Sin, Add])\n", "Output('out',equation_learner((x.last(),F.last())))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Parametric functions and fuzzy layers\n", "Create an equation learner block with parametric functions and fuzzy layers" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "ExecuteTime": { "end_time": "2025-05-23T15:08:56.623080Z", "start_time": "2025-05-23T15:08:56.608222Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[33m[check_names] The name 'x' is already in defined as NeuObj but it is overwritten.\u001b[0m\n", "\u001b[33m[check_names] The name 'F' is already in defined as NeuObj but it is overwritten.\u001b[0m\n", "\u001b[33m[check_names] The name 'out' is already in defined as NeuObj but it is overwritten.\u001b[0m\n" ] }, { "data": { "text/plain": [ "\u001b[1;32m==================================== Output ====================================\u001b[0m\n", "\u001b[32m{'Constants': {},\n", " 'Functions': {'FFuzzify55': {'centers': [0, 1, 2, 3],\n", " 'dim_out': {'dim': 4},\n", " 'functions': 'Triangular',\n", " 'names': 'Triangular'},\n", " 'FParamFun54': {'code': 'def myFun(K1,p1):\\n return K1*p1\\n',\n", " 'in_dim': [{'dim': 1, 'sw': 1}],\n", " 'map_over_dim': False,\n", " 'n_input': 1,\n", " 'name': 'myFun',\n", " 'params_and_consts': ['k']}},\n", " 'Info': {},\n", " 'Inputs': {'F': {'dim': 1, 'sw': [-1, 0]}, 'x': {'dim': 1, 'sw': [-1, 0]}},\n", " 'Outputs': {'out': 'Concatenate101'},\n", " 'Parameters': {'PLinear57W': {'dim': [2, 2]},\n", " 'PLinear57b': {'dim': 2},\n", " 'k': {'dim': 1,\n", " 'init_values': [[2.0]],\n", " 'sw': 1,\n", " 'values': [[2.0]]}},\n", " 'Relations': {'Concatenate101': ['Concatenate', ['ParamFun98', 'Fuzzify100']],\n", " 'Concatenate95': ['Concatenate',\n", " ['SamplePart92', 'SamplePart94']],\n", " 'Fuzzify100': ['Fuzzify', ['Select99'], 'FFuzzify55'],\n", " 'Linear96': ['Linear',\n", " ['Concatenate95'],\n", " 'PLinear57W',\n", " 'PLinear57b',\n", " 0],\n", " 'ParamFun98': ['ParamFun', ['Select97'], 'FParamFun54'],\n", " 'SamplePart92': ['SamplePart', ['x'], -1, [-1, 0]],\n", " 'SamplePart94': ['SamplePart', ['F'], -1, [-1, 0]],\n", " 'Select97': ['Select', ['Linear96'], 2, 0],\n", " 'Select99': ['Select', ['Linear96'], 2, 1]}}\u001b[0m\n", "\u001b[1;32m--------------------------- out {'dim': 5, 'sw': 1} ----------------------------\u001b[0m" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def myFun(K1,p1):\n", " return K1*p1\n", "\n", "x = Input('x')\n", "F = Input('F')\n", "\n", "K = Parameter('k', dimensions = 1, sw = 1,values=[[2.0]])\n", "parfun = ParamFun(myFun, parameters_and_constants = [K])\n", "\n", "fuzzi = Fuzzify(centers=[0,1,2,3])\n", "equation_learner = EquationLearner([parfun, fuzzi])\n", "Output('out',equation_learner((x.last(),F.last())))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Cascade Equation Learner Blocks\n", "Create a cascade of equation learner blocks with various functions and temporal window inputs" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "ExecuteTime": { "end_time": "2025-05-23T15:10:15.274054Z", "start_time": "2025-05-23T15:10:15.228793Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[33m[check_names] The name 'x' is already in defined as NeuObj but it is overwritten.\u001b[0m\n", "\u001b[33m[check_names] The name 'F' is already in defined as NeuObj but it is overwritten.\u001b[0m\n", "\u001b[33m[check_names] The name 'k1' is already in defined as NeuObj but it is overwritten.\u001b[0m\n", "\u001b[33m[check_names] The name 'k2' is already in defined as NeuObj but it is overwritten.\u001b[0m\n", "\u001b[33m[check_names] The name 'out' is already in defined as NeuObj but it is overwritten.\u001b[0m\n" ] }, { "data": { "text/plain": [ "\u001b[1;32m==================================== Output ====================================\u001b[0m\n", "\u001b[32m{'Constants': {},\n", " 'Functions': {'FParamFun65': {'code': 'def myFun(K1,K2,p1,p2):\\n'\n", " ' return K1*p1+K2*p2\\n',\n", " 'in_dim': [{'dim': 1, 'sw': 1},\n", " {'dim': 1, 'sw': 1}],\n", " 'map_over_dim': False,\n", " 'n_input': 2,\n", " 'name': 'myFun',\n", " 'params_and_consts': ['k1', 'k2']}},\n", " 'Info': {},\n", " 'Inputs': {'F': {'dim': 1, 'sw': [-1, 0]}, 'x': {'dim': 1, 'sw': [-1, 0]}},\n", " 'Outputs': {'out': 'Linear135'},\n", " 'Parameters': {'PLinear66W': {'dim': [2, 5],\n", " 'init_fun': {'name': 'init_constant',\n", " 'params': {'value': 1}}},\n", " 'PLinear68W': {'dim': [3, 7],\n", " 'init_fun': {'name': 'init_constant',\n", " 'params': {'value': 1}}},\n", " 'PLinear70W': {'dim': [5, 1],\n", " 'init_fun': {'name': 'init_constant',\n", " 'params': {'value': 1}}},\n", " 'PLinear70b': {'dim': 1},\n", " 'k1': {'dim': 1,\n", " 'init_values': [[2.0]],\n", " 'sw': 1,\n", " 'values': [[2.0]]},\n", " 'k2': {'dim': 1,\n", " 'init_values': [[3.0]],\n", " 'sw': 1,\n", " 'values': [[3.0]]}},\n", " 'Relations': {'Add116': ['Add', ['Select114', 'Select115']],\n", " 'Add123': ['Add', ['Select121', 'Select122']],\n", " 'Concatenate106': ['Concatenate',\n", " ['SamplePart103', 'SamplePart105']],\n", " 'Concatenate113': ['Concatenate', ['ParamFun110', 'Sin112']],\n", " 'Concatenate117': ['Concatenate', ['Concatenate113', 'Add116']],\n", " 'Concatenate124': ['Concatenate', ['Tan120', 'Add123']],\n", " 'Concatenate127': ['Concatenate', ['Concatenate124', 'Sin126']],\n", " 'Concatenate131': ['Concatenate', ['Concatenate127', 'Mul130']],\n", " 'Concatenate134': ['Concatenate',\n", " ['Concatenate131', 'Identity133']],\n", " 'Identity133': ['Identity', ['Select132']],\n", " 'Linear107': ['Linear',\n", " ['Concatenate106'],\n", " 'PLinear66W',\n", " None,\n", " 0],\n", " 'Linear118': ['Linear',\n", " ['Concatenate117'],\n", " 'PLinear68W',\n", " None,\n", " 0],\n", " 'Linear135': ['Linear',\n", " ['Concatenate134'],\n", " 'PLinear70W',\n", " 'PLinear70b',\n", " 0],\n", " 'Mul130': ['Mul', ['Select128', 'Select129']],\n", " 'ParamFun110': ['ParamFun',\n", " ['Select108', 'Select109'],\n", " 'FParamFun65'],\n", " 'SamplePart103': ['SamplePart', ['x'], -1, [-1, 0]],\n", " 'SamplePart105': ['SamplePart', ['F'], -1, [-1, 0]],\n", " 'Select108': ['Select', ['Linear107'], 5, 0],\n", " 'Select109': ['Select', ['Linear107'], 5, 1],\n", " 'Select111': ['Select', ['Linear107'], 5, 2],\n", " 'Select114': ['Select', ['Linear107'], 5, 3],\n", " 'Select115': ['Select', ['Linear107'], 5, 4],\n", " 'Select119': ['Select', ['Linear118'], 7, 0],\n", " 'Select121': ['Select', ['Linear118'], 7, 1],\n", " 'Select122': ['Select', ['Linear118'], 7, 2],\n", " 'Select125': ['Select', ['Linear118'], 7, 3],\n", " 'Select128': ['Select', ['Linear118'], 7, 4],\n", " 'Select129': ['Select', ['Linear118'], 7, 5],\n", " 'Select132': ['Select', ['Linear118'], 7, 6],\n", " 'Sin112': ['Sin', ['Select111']],\n", " 'Sin126': ['Sin', ['Select125']],\n", " 'Tan120': ['Tan', ['Select119']]}}\u001b[0m\n", "\u001b[1;32m--------------------------- out {'dim': 1, 'sw': 1} ----------------------------\u001b[0m" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = Input('x')\n", "F = Input('F')\n", "\n", "def myFun(K1,K2,p1,p2):\n", " return K1*p1+K2*p2\n", "\n", "K1 = Parameter('k1', dimensions = 1, sw = 1, values=[[2.0]])\n", "K2 = Parameter('k2', dimensions = 1, sw = 1, values=[[3.0]])\n", "parfun = ParamFun(myFun, parameters_and_constants = [K1,K2])\n", "\n", "input_layer_1 = Linear(output_dimension=5, W_init='init_constant', W_init_params={'value':1}, b_init='init_constant', b_init_params={'value':0})\n", "input_layer_2 = Linear(output_dimension=7, W_init='init_constant', W_init_params={'value':1}, b_init='init_constant', b_init_params={'value':0})\n", "output_layer = Linear(output_dimension=1, W_init='init_constant', W_init_params={'value':1}, b=True)\n", "equation_learner = EquationLearner([parfun, Sin, Add], linear_in=input_layer_1)\n", "equation_learner_2 = EquationLearner(functions=[Tan, Add, Sin, Mul, Identity], linear_in=input_layer_2, linear_out=output_layer)\n", "\n", "Output('out',equation_learner_2(equation_learner((x.sw(1),F.sw(1)))))" ] } ], "metadata": { "kernelspec": { "display_name": "venv (3.11.4)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.4" } }, "nbformat": 4, "nbformat_minor": 2 }