[PYTHON] [NNabla] How to add a new layer between the middle layers of a pre-built network

Introduction

This is the third post to qiita. (article3) 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. (Up to this point, previous same)

article3_add_layer.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)

It is simply in the form of $ y = (x + 0.5) \ times2 $.

3. Add a new layer between existing intermediate layers

Last time $ y = (x + 0.5) \ times2 $ above $ y = (x + 0.5) ^ 2 \ times2 using the contents explained above I will show you how to change it to $. The code is below.

article3_add_layer.py


# [get middle variable]
h1 = y.parent.inputs[0]
additional_layer = F.pow_scalar(h1, 2.0)
redefine_layer = F.mul_scalar(additional_layer, **y.parent.info.args)

# [rewire_on]
y.rewire_on(redefine_layer)

The operation check was done below just before rewire_on above, with print in between.

article3_add_layer.py


def print_func(f):
    print('{} output = {}'.format(f.name, f.outputs[0].d))

# [print & forward]
x.d.fill(0)
y.forward()
print('--- before ---')
y.visit(print_func)
print('y.d = {}'.format(y.d))
print('')

# [rewire_on]
y.rewire_on(redefine_layer)

# [print & forward]
y.forward()
print('--- after ---')
y.visit(print_func)
print('y.d = {}'.format(y.d))
print('')

output

--- before ---
AddScalar output = 0.5
MulScalar output = -1.0
y.d = -1.0

--- after ---
AddScalar output = 0.5
PowScalar output = 0.25
MulScalar output = -0.5
y.d = -0.5

Commentary

4. Summary

I introduced how to insert a new layer. You can also use it to insert a quantization layer into the output of each activation of an existing trained model, or convolve Convolution + Batch Normalization into a single Convolution. Next time, I will touch on this area.

Recommended Posts

[NNabla] How to add a new layer between the middle layers of a pre-built network
[NNabla] How to get the output (variable) of the middle layer of a pre-built network
[NNabla] How to remove the middle tier of a pre-built network
How to calculate the volatility of a brand
Visualize the inner layer of a neural network
Basics of PyTorch (2) -How to make a neural network-
[Morphological analysis] How to add a new dictionary to Mecab
[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 take a screenshot of the Chrome screen (prevent it from cutting off in the middle)
How to determine the existence of a selenium element in Python
How to check the memory size of a variable in Python
How to check the memory size of a dictionary in Python
How to find the memory address of a Pandas dataframe value
How to output the output result of the Linux man command to a file
How to get the vertex coordinates of a feature in ArcPy
A command to easily check the speed of the network on the console
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 create a wrapper that preserves the signature of the function to wrap
How to play a video while watching the number of frames (Mac)
How to add a package with PyCharm
How to check the version of Django
Get the value of the middle layer of NN
Add a layer using the Keras backend
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
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
I want to add silence to the beginning of a wav file for 1 second
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
A new form of app that works with GitHub: How to make GitHub Apps
A simple example of how to use ArgumentParser
How to find the area of the Voronoi diagram
How to add a Python module search path
How to Implement a new CPUFreq Processor Driver
[Java] How to switch between multiple versions of Java
[Wagtail] Add a login page to the Wagtail project
How to display the modification date of a file in C language up to nanoseconds
How to identify the element with the smallest number of characters in a Python list?
[Ruby] How to replace only a part of the string matched by the regular expression?
How to check in Python if one of the elements of a list is in another list
The world's most easy-to-understand explanation of how to make a LINE BOT (1) [Account preparation]
A memo on how to overcome the difficult problem of capturing FX with AI
A story about trying to introduce Linter in the middle of a Python (Flask) project
How to change the generated image of GAN to a high quality one to your liking
Add a function to tell the weather of today to slack bot (made by python)
Read the Python-Markdown source: How to create a parser
How to know the port number of the xinetd service
How to write a GUI using the maya command
How to get the number of digits in Python
Add information to the bottom of the figure with Matplotlib
How to create a submenu with the [Blender] plugin