Learn Before
Code
NiN Block Code Implementation
The Network in Network (NiN) block can be implemented programmatically across deep learning frameworks as a sequential container of convolutional layers. It consists of an initial spatial convolution followed by two convolutions that act as per-pixel fully connected layers. Each convolutional layer in the block is immediately followed by a ReLU activation function.
PyTorch Implementation:
def nin_block(out_channels, kernel_size, strides, padding): return nn.Sequential( nn.LazyConv2d(out_channels, kernel_size, strides, padding), nn.ReLU(), nn.LazyConv2d(out_channels, kernel_size=1), nn.ReLU(), nn.LazyConv2d(out_channels, kernel_size=1), nn.ReLU())
MXNet Implementation:
def nin_block(num_channels, kernel_size, strides, padding): blk = nn.Sequential() blk.add(nn.Conv2D(num_channels, kernel_size, strides, padding, activation='relu'), nn.Conv2D(num_channels, kernel_size=1, activation='relu'), nn.Conv2D(num_channels, kernel_size=1, activation='relu')) return blk
JAX Implementation:
def nin_block(out_channels, kernel_size, strides, padding): return nn.Sequential([ nn.Conv(out_channels, kernel_size, strides, padding), nn.relu, nn.Conv(out_channels, kernel_size=(1, 1)), nn.relu, nn.Conv(out_channels, kernel_size=(1, 1)), nn.relu])
TensorFlow Implementation:
def nin_block(out_channels, kernel_size, strides, padding): return tf.keras.models.Sequential([ tf.keras.layers.Conv2D(out_channels, kernel_size, strides=strides, padding=padding), tf.keras.layers.Activation('relu'), tf.keras.layers.Conv2D(out_channels, 1), tf.keras.layers.Activation('relu'), tf.keras.layers.Conv2D(out_channels, 1), tf.keras.layers.Activation('relu')])
0
1
Updated 2026-05-13
Tags
D2L
Dive into Deep Learning @ D2L