{ "cells": [ { "cell_type": "raw", "metadata": { "vscode": { "languageId": "raw" } }, "source": [ "\n", " \"Open\n", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Partitioning\n", "\n", "Here are listed all the relation blocks to slice or select a subvector inside the neural architecture." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "ExecuteTime": { "end_time": "2025-04-04T12:48:52.033102Z", "start_time": "2025-04-04T12:48:52.026679Z" } }, "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": [ "## Part\n", "\n", "Represents a sub-window from a relation in the neural network model. The index of the first and last element must be provided.\n", "\n", "The Part relation works along the object dimension (third dimension) of the input." ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "ExecuteTime": { "end_time": "2025-04-04T12:48:55.514599Z", "start_time": "2025-04-04T12:48:55.501131Z" } }, "outputs": [ { "data": { "text/plain": [ "{'out': [[[1.0, 2.0]]]}" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = Input('x', dimensions=10).last()\n", "sub = Part(x, 0, 2)\n", "out = Output('out', sub)\n", "test = Modely(visualizer=None)\n", "test.addModel('test', [out])\n", "test.neuralizeModel()\n", "test({'x': [[1,2,3,4,5,6,7,8,9,10]]})" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Select\n", "\n", "Represents a single element from a relation in the neural network model, using 0-index convention. The index of the element must be provided.\n", "\n", "The Select relation works along the object dimension (third dimension) of the input." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "ExecuteTime": { "end_time": "2025-04-04T12:49:05.161679Z", "start_time": "2025-04-04T12:49:05.153939Z" } }, "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": [ "{'out': [2.0]}" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = Input('x', dimensions=3).last()\n", "sub = Select(x, 1)\n", "out = Output('out', sub)\n", "test = Modely(visualizer=None)\n", "test.addModel('test', [out])\n", "test.neuralizeModel()\n", "test({'x': [[1,2,3]]})" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Concatenate\n", "\n", "Represents the concatenate function between two relations.\n", "\n", "The Concatenate relation works along the object dimension (third dimension) of the input." ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "ExecuteTime": { "end_time": "2025-04-04T12:49:15.472955Z", "start_time": "2025-04-04T12:49:15.464211Z" } }, "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": [ "{'out': [[[1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0]]]}" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = Input('x', dimensions=3).last()\n", "y = Input('y', dimensions=5).last()\n", "cat = Concatenate(x, y)\n", "out = Output('out', cat)\n", "test = Modely(visualizer=None)\n", "test.addModel('test', [out])\n", "test.neuralizeModel()\n", "test({'x': [[1,2,3]],'y': [[4,5,6,7,8]]})" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## SamplePart\n", "\n", "Represents a sub-window from a relation in the neural network model. The index of the first and last element must be provided. \n", "\n", "The SamplePart relation works along the time dimension (second dimension) of the input." ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "ExecuteTime": { "end_time": "2025-04-04T12:49:17.634867Z", "start_time": "2025-04-04T12:49:17.626277Z" } }, "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": [ "{'out2': [10.0], 'out': [[1.0, 2.0, 3.0]]}" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = Input('x')\n", "x_sw10 = x.sw(10)\n", "relation = SamplePart(x_sw10, 0, 3)\n", "out = Output('out', relation)\n", "out2 = Output('out2', x.last())\n", "test = Modely(visualizer=None)\n", "test.addModel('test', [out,out2])\n", "test.neuralizeModel()\n", "# Test 1 input in time\n", "test({'x': [1,2,3,4,5,6,7,8,9,10]})" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "ExecuteTime": { "end_time": "2025-04-04T12:49:19.205201Z", "start_time": "2025-04-04T12:49:19.200756Z" } }, "outputs": [ { "data": { "text/plain": [ "{'out2': [10.0, 11.0], 'out': [[1.0, 2.0, 3.0], [2.0, 3.0, 4.0]]}" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Test 2 input in time\n", "test({'x': [1,2,3,4,5,6,7,8,9,10,11]})" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## SampleSelect\n", "\n", "Represents a single element from a relation in the neural network model, using 0-index convention. The index of the element must be provided.\n", "\n", "The SampleSelect relation works along the time dimension (second dimension) of the input." ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "ExecuteTime": { "end_time": "2025-04-04T12:49:27.547326Z", "start_time": "2025-04-04T12:49:27.539908Z" } }, "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": [ "{'out': [6.0]}" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = Input('x').sw(10)\n", "relation = SampleSelect(x, 5)\n", "out = Output('out', relation)\n", "test = Modely(visualizer=None)\n", "test.addModel('test', [out])\n", "test.neuralizeModel()\n", "test({'x': [1,2,3,4,5,6,7,8,9,10]})" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "ExecuteTime": { "end_time": "2025-04-04T12:49:28.934121Z", "start_time": "2025-04-04T12:49:28.924125Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[33m[check_names] The name 'out2' is already in defined as NeuObj but it is overwritten.\u001b[0m\n" ] }, { "data": { "text/plain": [ "{'out2': [[0.0, 6.0], [6.0, 7.0]], 'out': [6.0, 7.0]}" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "test = Modely(visualizer=None)\n", "test.addModel('test', [out, Output('out2', relation.sw(2))])\n", "test.neuralizeModel()\n", "test({'x': [1,2,3,4,5,6,7,8,9,10,11]})" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## TimeConcatenate\n", "\n", "Represents the concatenate function between two relations but the TimeConcatenate relation works along the time dimension (second dimension)." ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "ExecuteTime": { "end_time": "2025-04-04T12:49:30.539654Z", "start_time": "2025-04-04T12:49:30.528292Z" } }, "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 'y' 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", "\u001b[33m[check_names] The name 'out2' is already in defined as NeuObj but it is overwritten.\u001b[0m\n" ] }, { "data": { "text/plain": [ "{'out2': [[4.684988021850586, 1.0, 2.0, 3.0]],\n", " 'out': [[1.0, 2.0, 3.0, 4.0, 5.0, 1.0, 2.0, 3.0]]}" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = Input('x').sw(5)\n", "y = Input('y').sw(3)\n", "cat = TimeConcatenate(x, y)\n", "cat2 = TimeConcatenate(Fir(x), y)\n", "out = Output('out', cat)\n", "out2 = Output('out2', cat2)\n", "test = Modely(visualizer=None)\n", "test.addModel('test', [out,out2])\n", "test.neuralizeModel()\n", "test({'x': [1,2,3,4,5],'y': [1,2,3]})" ] } ], "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 }