[NNabla] How to remove the middle tier of a pre-built network

Introduction

This is the second post to qiita. (article2) Continuing from last time, while I was using nnabla, I managed to feel like "I wish I had this kind of information in qiita" Summary of what I found in nnabla reference and dir () (a standard python function that returns member variables and functions of arguments) I will.

1. Requirements

· OS: macOS Catalina (version 10.15.1) ・ Python: 3.5.4 ・ Nnabla: 1.3.0

2. Network construction

The sample network is defined below. (Same as Last time so far)

article2_rewire_on.py


import nnabla as nn
import nnabla.functions as F

# [define network]
x = nn.Variable()
y = F.add_scalar(x, 0.5)  # <-- (1)far
y = F.mul_scalar(y, 2.0)

It is simply in the form of $ y = (x + 0.5) \ times2 $. At this time, the variable y is a variable that has the result of the above formula, and the result of F.add_scalar (x, 0.5), which is an intermediate calculation, is called (1).

3. Remove the middle layer

I will explain how to delete (1) above and simply set $ y = x \ times2 $. It uses the member variable rewire_on of nnabla.Variable in the nnabla reference. This is also (relatively) easy to understand in reference. I practiced it below.

article2_rewire_on.py


h1 = y.parent.inputs[0]   # = (1)
x.rewire_on(h1)

The operation check was done below.

article2_rewire_on.py


# [check func for visit]
def get_func_name(f):
    print(f.name)
print('--- before ---')
y.visit(get_func_name)
print('')

# [rewire_on]
h1 = y.parent.inputs[0]   # = (1)
x.rewire_on(h1)

print('--- after ---')
y.visit(get_func_name)
print('')

output

--- before ---
AddScalar
MulScalar

--- after ---
MulScalar

Commentary

4. Next notice (?)

You can use rewire_on not only to remove the middle layer, but also to insert a new layer. I managed to do this myself, but I thought "I wanted someone to write something like qiita", so I'll write it next time.

Recommended Posts

[NNabla] How to remove the middle tier of a pre-built network
[NNabla] How to get the output (variable) of the middle layer of a pre-built network
[NNabla] How to add a new layer between the middle layers of a pre-built network
[NNabla] How to add a quantization layer to the middle layer of a trained model
Basics of PyTorch (2) -How to make a neural network-
[Ubuntu] How to delete the entire contents of a directory
How to find the scaling factor of a biorthogonal wavelet
How to connect the contents of a list into a string
How to easily draw the structure of a neural network on Google Colaboratory using "convnet-drawer"
How to determine the existence of a selenium element in Python
How to check the memory size of a dictionary in Python
How to find the memory address of a Pandas dataframe value
A command to easily check the speed of the network on the console
How to take a screenshot of the Chrome screen (prevent it from cutting off in the middle)
How to check the version of Django
I tried how to improve the accuracy of my own Neural Network
[Introduction to Python] How to sort the contents of a list efficiently with list sort
How to put a line number at the beginning of a CSV file
How to play a video while watching the number of frames (Mac)
A simple example of how to use ArgumentParser
How to find the area of the Voronoi diagram
How to pass the execution result of a shell command in a list in Python
How to mention a user group in slack notification, how to check the id of the user group
[Circuit x Python] How to find the transfer function of a circuit using Lcapy
To output a value even in the middle of a cell with Jupyter Notebook
How to count the number of elements in Django and output to a template
How to access the contents of a Linux disk on a Mac (but read-only)
[python] How to sort by the Nth Mth element of a multidimensional array
A memorandum of how to execute the! Sudo magic command in Jupyter Notebook
[Numpy, scipy] How to calculate the square root of a semi-fixed definite matrix
How to make a Raspberry Pi that speaks the tweets of the specified user
How to get a list of files in the same directory with python
[Introduction to Python] How to get the index of data with a for statement
[Python] How to remove duplicate values from the list
How to know the port number of the xinetd service
How to write a GUI using the maya command
How to display the modification date of a file in C language up to nanoseconds
How to get the number of digits in Python
A memo of how to use AIST supercomputer ABCI
A memo to visually understand the axis of pandas.Panel
The story of making a music generation neural network
Steps to calculate the likelihood of a normal distribution
[Ruby] How to replace only a part of the string matched by the regular expression?
[Blender] How to dynamically set the selection of EnumProperty
How to check in Python if one of the elements of a list is in another list
How to hit the document of Magic Function (Line Magic)
How to access the global variable of the imported module
How to post a ticket from the Shogun API
How to change the generated image of GAN to a high quality one to your liking
[Selenium] How to specify the relative path of chromedriver?
Python Note: The mystery of assigning a variable to a variable
[Linux] [C / C ++] How to get the return address value of a function and the function name of the caller
[Deep learning] Investigating how to use each function of the convolutional neural network [DW day 3]
How to display the regional mesh of the official statistics window (eStat) in a web browser
How to quickly count the frequency of appearance of characters from a character string in Python?
How to find the average amount of information (entropy) of the original probability distribution from a sample
How to plot the distribution of bacterial composition from Qiime2 analysis data in a box plot
How to plot a lot of legends by changing the color of the graph continuously with matplotlib
How to insert a specific process at the start and end of spider with scrapy
How to pass the execution result of a shell command in a list in Python (non-blocking version)
A story about porting the code of "Try and understand how Linux works" to Rust