{ "cells": [ { "cell_type": "raw", "metadata": { "vscode": { "languageId": "raw" } }, "source": [ "\n", " \"Open\n", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Constant and Parameter\n", "\n", "Create custom Constant parameters. \n", "\n", "NNodely constants are static and not modified during the training process.\n", "\n", "Create custom Parameter vectors that can also be used in various NNodely blocks. \n", "\n", "NNodely parameters can be updated during the training process." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "ExecuteTime": { "end_time": "2025-04-04T14:02:36.658925Z", "start_time": "2025-04-04T14:02:36.656030Z" } }, "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 Constant 'g' and a Parameter 'k'" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "ExecuteTime": { "end_time": "2025-04-04T14:02:36.675590Z", "start_time": "2025-04-04T14:02:36.672632Z" } }, "outputs": [], "source": [ "g = Constant('g', values=[9.81])\n", "k = Parameter('k', tw=4)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Using a Parameter inside another block\n", "\n", "Create a parameter k of dimension 3 and use this parameter to initialize the weigths of two Fir Layers.\n", "\n", "!! Note that the two Fir share now the same weights because they have been initialized with the same Parameter" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "ExecuteTime": { "end_time": "2025-04-04T14:02:36.685487Z", "start_time": "2025-04-04T14:02:36.680672Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[33m[check_names] The name 'k' is already in defined as NeuObj but it is overwritten.\u001b[0m\n" ] } ], "source": [ "x = Input('x')\n", "\n", "k = Parameter('k', dimensions=3, tw=4)\n", "\n", "fir1 = Fir(W=k)\n", "fir2 = Fir(3, W=k)\n", "\n", "out = Output('out', fir1(x.tw(4))+fir2(x.tw(4)))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Parametric functions\n", "\n", "Create two parameters and use them inside a parametric function. The parameters are inizialized with custom values" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "ExecuteTime": { "end_time": "2025-04-04T14:02:36.701580Z", "start_time": "2025-04-04T14:02:36.696368Z" } }, "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 'g' 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": [ "x= Input('x')\n", "\n", "g = Parameter('g', dimensions=3, values=[[4,5,6]])\n", "t = Parameter('t', dimensions=3, values=[[1,2,3]])\n", "\n", "def fun(x, k, t):\n", " return x+(k+t)\n", "\n", "p = ParamFun(fun, parameters_and_constants=[g,t])\n", "\n", "out = Output('out', p(x.tw(1)))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Arithmetic functions with Parameters and Constants\n", "\n", "Constant and Parameter work with all the arithmetic functions. Here are summed together along with the input variable" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "ExecuteTime": { "end_time": "2025-04-04T14:02:36.714901Z", "start_time": "2025-04-04T14:02:36.711096Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[33m[check_names] The name 'g' 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": [ "g = Parameter('g', sw=1, values=[[1,2,3,4]])\n", "o = Constant('o', sw=1, values=[[1,2,3,4]])\n", "x = Input('x', dimensions=4)\n", "out = Output('out', x.last()+g+o)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## SampleTime\n", "\n", "Create a constant equal to the sample time" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "ExecuteTime": { "end_time": "2025-04-04T14:02:36.727191Z", "start_time": "2025-04-04T14:02:36.724269Z" } }, "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" ] } ], "source": [ "dt = SampleTime()\n", "x = Input('x')\n", "out = Output('out', x.last() + dt)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Initialization functions\n", "\n", "Initialize a Parameter of dimension 4 using a constant initialization function" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "ExecuteTime": { "end_time": "2025-04-04T14:02:36.737888Z", "start_time": "2025-04-04T14:02:36.734799Z" } }, "outputs": [], "source": [ "p = Parameter('p', dimensions=4, init=init_constant, init_params={'value':4})" ] } ], "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 }