{ "cells": [ { "cell_type": "raw", "metadata": { "vscode": { "languageId": "raw" } }, "source": [ "\n", " \"Open\n", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Local Model\n", "\n", "Represents a Local Model relation in the neural network model. \n", "\n", "The LocalModel structure block is a way to handle non-linear relationships using the fuzzy logic inside the neural network architecture." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "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", "\n", "Create a local model with a triangular activation function that represent the one-hot encoding of the discrete variable 'c' and a Fir function for the input 'x'" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "c = Input('c')\n", "x = Input('x')\n", "activation = Fuzzify(2,[0,1],functions='Triangular')(c.last())\n", "loc = LocalModel(input_function=Fir())\n", "out = Output('out', loc(x.tw(1), activation))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Passing an output function\n", "\n", "We can also define an output function where only the 'active' nodes will be updated.\n", "\n", "(Note: if we want a different function (with different weights) for each activation input we have to use the built-in 'lambda' keyword)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[33m[check_names] The name 'c' is already in defined as NeuObj but it is overwritten.\u001b[0m\n", "\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" ] } ], "source": [ "c = Input('c')\n", "x = Input('x')\n", "activation = Fuzzify(2,[0,1],functions='Triangular')(c.last())\n", "loc = LocalModel(input_function = lambda:Fir, output_function = lambda:Fir)(x.last(), activation)\n", "out = Output('out', loc)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Passing a custom function\n", "\n", "The LocalModel also accept Parametric functions created using custom user-defined functions." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[33m[check_names] The name 'c' is already in defined as NeuObj but it is overwritten.\u001b[0m\n", "\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" ] } ], "source": [ "def myFun(in1,p1,p2):\n", " return p1*in1+p2\n", "\n", "c = Input('c')\n", "x = Input('x')\n", "activation = Fuzzify(2,[0,1],functions='Triangular')(c.last())\n", "loc = LocalModel(input_function = lambda:ParamFun(myFun), output_function = lambda:Fir)(x.last(), activation)\n", "out = Output('out', loc)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Custom function with multiple activations\n", "\n", "We can use multiple activation functions to define the fuzzy logic of the local model. If this is the case, just include all the activations inside a tuple to pass to the local model." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[33m[check_names] The name 'c' is already in defined as NeuObj but it is overwritten.\u001b[0m\n", "\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" ] } ], "source": [ "c = Input('c')\n", "d = Input('d')\n", "activationA = Fuzzify(2,[0,1],functions='Triangular')(c.tw(1))\n", "activationB = Fuzzify(2,[0,1],functions='Triangular')(d.tw(1))\n", "\n", "def myFun(in1,p1,p2):\n", " return p1*in1+p2\n", "\n", "x = Input('x')\n", "loc = LocalModel(input_function = lambda:ParamFun(myFun), output_function = Fir(3))(x.tw(1),(activationA,activationB))\n", "out = Output('out', loc)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Passing indexes\n", "\n", "By setting the 'pass_indexes' attribute to True we are indicating whether to pass indexes to the functions" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[33m[check_names] The name 'c' is already in defined as NeuObj but it is overwritten.\u001b[0m\n", "\u001b[33m[check_names] The name 'd' is already in defined as NeuObj but it is overwritten.\u001b[0m\n", "\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 'p1_0' is already in defined as NeuObj but it is overwritten.\u001b[0m\n", "\u001b[33m[check_names] The name 'p2_0' is already in defined as NeuObj but it is overwritten.\u001b[0m\n", "\u001b[33m[check_names] The name 'p1_1' is already in defined as NeuObj but it is overwritten.\u001b[0m\n", "\u001b[33m[check_names] The name 'p2_1' 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" ] } ], "source": [ "c = Input('c')\n", "d = Input('d')\n", "activationA = Fuzzify(2,[0,1],functions='Triangular')(c.tw(1))\n", "activationB = Fuzzify(2,[0,1],functions='Triangular')(d.tw(1))\n", "\n", "def myFun(in1,p1,p2):\n", " return p1*in1+p2\n", "\n", "def input_function_gen(idx_list):\n", " if idx_list == [0,0]:\n", " p1, p2 = Parameter('p1_0',values=[[1]]), Parameter('p2_0',values=[[2]])\n", " if idx_list == [0,1]:\n", " p1, p2 = Parameter('p1_0',values=[[1]]), Parameter('p2_1',values=[[3]])\n", " if idx_list == [1,0]:\n", " p1, p2 = Parameter('p1_1',values=[[2]]), Parameter('p2_0',values=[[2]])\n", " if idx_list == [1, 1]:\n", " p1, p2 = Parameter('p1_1',values=[[2]]), Parameter('p2_1',values=[[3]])\n", " return ParamFun(myFun,parameters_and_constants=[p1,p2])\n", "\n", "def output_function_gen(idx_list):\n", " pfir = Parameter('pfir_'+str(idx_list),tw=1,dimensions=2,values=[[1+idx_list[0],2+idx_list[1]],[3+idx_list[0],4+idx_list[1]]])\n", " return Fir(2,W=pfir)\n", "\n", "x = Input('x')\n", "loc = LocalModel(input_function=input_function_gen, output_function= output_function_gen, pass_indexes = True)(x.tw(1),(activationA,activationB))\n", "out = Output('out', loc)" ] } ], "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 }