|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "metadata": {}, |
| 5 | + "cell_type": "markdown", |
| 6 | + "source": [ |
| 7 | + "# API - Networks\n", |
| 8 | + "\n", |
| 9 | + "This notebook illustrates the main features of pyNeVer for the creation of a neural network" |
| 10 | + ], |
| 11 | + "id": "7386e4bcb1b4c027" |
| 12 | + }, |
| 13 | + { |
| 14 | + "metadata": {}, |
| 15 | + "cell_type": "markdown", |
| 16 | + "source": [ |
| 17 | + "## The _networks_ module\n", |
| 18 | + "\n", |
| 19 | + "The module *networks* contains the classes __SequentialNetwork__ and __AcyclicNetwork__ to represent feed-forward and residual neural networks, respectively. Both subclass the abstract class __NeuralNetwork__ that provides base methods and utilities." |
| 20 | + ], |
| 21 | + "id": "180d25f1124192a2" |
| 22 | + }, |
| 23 | + { |
| 24 | + "metadata": {}, |
| 25 | + "cell_type": "code", |
| 26 | + "source": [ |
| 27 | + "from pynever.networks import NeuralNetwork, SequentialNetwork, AcyclicNetwork\n", |
| 28 | + "\n", |
| 29 | + "# Create an empty FF network with identifier 'my_net' and input identifier 'X'\n", |
| 30 | + "my_ff_net = SequentialNetwork('my_awesome_ff_net', 'X')\n", |
| 31 | + "\n", |
| 32 | + "# Create an empty ResNet. Notice that the input identifiers are in a list to allow multiple inputs.\n", |
| 33 | + "my_res_net = AcyclicNetwork('my_awesome_res_net', ['X_b', 'X_b'])\n", |
| 34 | + "\n", |
| 35 | + "print(my_ff_net)\n", |
| 36 | + "print(my_res_net)\n", |
| 37 | + "\n", |
| 38 | + "print(isinstance(my_ff_net, NeuralNetwork))\n", |
| 39 | + "print(isinstance(my_res_net, NeuralNetwork))" |
| 40 | + ], |
| 41 | + "id": "b63800010ce3b95a", |
| 42 | + "outputs": [], |
| 43 | + "execution_count": null |
| 44 | + }, |
| 45 | + { |
| 46 | + "metadata": {}, |
| 47 | + "cell_type": "markdown", |
| 48 | + "source": [ |
| 49 | + "## The _nodes_ module\n", |
| 50 | + "\n", |
| 51 | + "The module *nodes* contains the definition of NN layers as nodes in the computational graph. For the list of all supported layers, see [the documentation](http://www.neuralverification.org/pynever/API/1_Nodes.html). All nodes require a string identifier and the definition of the input dimension: the neural network object contains no information about this." |
| 52 | + ], |
| 53 | + "id": "2c88535153a53d36" |
| 54 | + }, |
| 55 | + { |
| 56 | + "metadata": {}, |
| 57 | + "cell_type": "code", |
| 58 | + "source": [ |
| 59 | + "from pynever import nodes\n", |
| 60 | + "import torch\n", |
| 61 | + "\n", |
| 62 | + "w = torch.Tensor([[1, 1], [-1, 1]])\n", |
| 63 | + "b = torch.zeros(2)\n", |
| 64 | + "\n", |
| 65 | + "# Create a fully connected layer with 2 inputs and 2 neurons.\n", |
| 66 | + "# The input dimension in_dim is always a tuple\n", |
| 67 | + "fc = nodes.FullyConnectedNode('fc', (2,), 2, weight=w, bias=b)\n", |
| 68 | + "\n", |
| 69 | + "# Add it to the ff network\n", |
| 70 | + "my_ff_net.append_node(fc)\n", |
| 71 | + "# Let's add a ReLU layer now\n", |
| 72 | + "my_ff_net.append_node(nodes.ReLUNode('relu', (2,)))\n", |
| 73 | + "\n", |
| 74 | + "print(my_ff_net)" |
| 75 | + ], |
| 76 | + "id": "2667008980c520b5", |
| 77 | + "outputs": [], |
| 78 | + "execution_count": null |
| 79 | + }, |
| 80 | + { |
| 81 | + "metadata": {}, |
| 82 | + "cell_type": "markdown", |
| 83 | + "source": [ |
| 84 | + "## Residual networks\n", |
| 85 | + "\n", |
| 86 | + "For ResNets we provide a different method to add layers: *add_node* allows to specify the layer parents and, possibly, children" |
| 87 | + ], |
| 88 | + "id": "a8b5ddea7d01fad7" |
| 89 | + }, |
| 90 | + { |
| 91 | + "metadata": {}, |
| 92 | + "cell_type": "code", |
| 93 | + "source": [ |
| 94 | + "rl = nodes.ReLUNode('relu', (2,))\n", |
| 95 | + "fc_2 = nodes.FullyConnectedNode('fc_2', (2,), 2, weight=w, bias=b)\n", |
| 96 | + "rl_2 = nodes.ReLUNode('relu_2', (2,))\n", |
| 97 | + "\n", |
| 98 | + "my_res_net.add_node(fc) # This is the first layer\n", |
| 99 | + "my_res_net.add_node(rl, [fc]) # Layer rl follows fc\n", |
| 100 | + "my_res_net.add_node(fc_2, [rl]) # Layer fc_2 follows rl\n", |
| 101 | + "my_res_net.add_node(rl_2, [fc, fc_2]) # Layer rl_2 has a skip connection and has as parents both fc and fc_2\n", |
| 102 | + "\n", |
| 103 | + "print(my_res_net)\n", |
| 104 | + "\n", |
| 105 | + "# A few utility methods\n", |
| 106 | + "print(f'Topological sort: {my_res_net.get_topological_order()}')\n", |
| 107 | + "print(f'Parents of rl_2: {my_res_net.get_parents(rl_2)}')\n", |
| 108 | + "print(f'Children of fc: {my_res_net.get_children(fc)}')\n", |
| 109 | + "print(f'Leaves of the nn: {my_res_net.get_leaves()}')" |
| 110 | + ], |
| 111 | + "id": "fedaca77cdea61f", |
| 112 | + "outputs": [], |
| 113 | + "execution_count": null |
| 114 | + }, |
| 115 | + { |
| 116 | + "metadata": {}, |
| 117 | + "cell_type": "markdown", |
| 118 | + "source": "", |
| 119 | + "id": "bada530cf2b5aa93" |
| 120 | + } |
| 121 | + ], |
| 122 | + "metadata": { |
| 123 | + "kernelspec": { |
| 124 | + "display_name": "Python 3", |
| 125 | + "language": "python", |
| 126 | + "name": "python3" |
| 127 | + }, |
| 128 | + "language_info": { |
| 129 | + "codemirror_mode": { |
| 130 | + "name": "ipython", |
| 131 | + "version": 2 |
| 132 | + }, |
| 133 | + "file_extension": ".py", |
| 134 | + "mimetype": "text/x-python", |
| 135 | + "name": "python", |
| 136 | + "nbconvert_exporter": "python", |
| 137 | + "pygments_lexer": "ipython2", |
| 138 | + "version": "2.7.6" |
| 139 | + } |
| 140 | + }, |
| 141 | + "nbformat": 4, |
| 142 | + "nbformat_minor": 5 |
| 143 | +} |
0 commit comments