Learn Before
Code
GoogLeNet Model Code Implementation
The full GoogLeNet model is assembled by sequentially connecting its five constituent modules ( through ) and appending a fully connected output layer. The complete, multi-framework code implementations for all modules and their assembly are provided below.
PyTorch Implementation:
class GoogleNet(d2l.Classifier): def b1(self): return nn.Sequential( nn.LazyConv2d(64, kernel_size=7, stride=2, padding=3), nn.ReLU(), nn.MaxPool2d(kernel_size=3, stride=2, padding=1)) @d2l.add_to_class(GoogleNet) def b2(self): return nn.Sequential( nn.LazyConv2d(64, kernel_size=1), nn.ReLU(), nn.LazyConv2d(192, kernel_size=3, padding=1), nn.ReLU(), nn.MaxPool2d(kernel_size=3, stride=2, padding=1)) @d2l.add_to_class(GoogleNet) def b3(self): return nn.Sequential(Inception(64, (96, 128), (16, 32), 32), Inception(128, (128, 192), (32, 96), 64), nn.MaxPool2d(kernel_size=3, stride=2, padding=1)) @d2l.add_to_class(GoogleNet) def b4(self): return nn.Sequential(Inception(192, (96, 208), (16, 48), 64), Inception(160, (112, 224), (24, 64), 64), Inception(128, (128, 256), (24, 64), 64), Inception(112, (144, 288), (32, 64), 64), Inception(256, (160, 320), (32, 128), 128), nn.MaxPool2d(kernel_size=3, stride=2, padding=1)) @d2l.add_to_class(GoogleNet) def b5(self): return nn.Sequential(Inception(256, (160, 320), (32, 128), 128), Inception(384, (192, 384), (48, 128), 128), nn.AdaptiveAvgPool2d((1,1)), nn.Flatten()) @d2l.add_to_class(GoogleNet) def __init__(self, lr=0.1, num_classes=10): super(GoogleNet, self).__init__() self.save_hyperparameters() self.net = nn.Sequential(self.b1(), self.b2(), self.b3(), self.b4(), self.b5(), nn.LazyLinear(num_classes)) self.net.apply(d2l.init_cnn)
MXNet Implementation:
class GoogleNet(d2l.Classifier): def b1(self): net = nn.Sequential() net.add(nn.Conv2D(64, kernel_size=7, strides=2, padding=3, activation='relu'), nn.MaxPool2D(pool_size=3, strides=2, padding=1)) return net @d2l.add_to_class(GoogleNet) def b2(self): net = nn.Sequential() net.add(nn.Conv2D(64, kernel_size=1, activation='relu'), nn.Conv2D(192, kernel_size=3, padding=1, activation='relu'), nn.MaxPool2D(pool_size=3, strides=2, padding=1)) return net @d2l.add_to_class(GoogleNet) def b3(self): net = nn.Sequential() net.add(Inception(64, (96, 128), (16, 32), 32), Inception(128, (128, 192), (32, 96), 64), nn.MaxPool2D(pool_size=3, strides=2, padding=1)) return net @d2l.add_to_class(GoogleNet) def b4(self): net = nn.Sequential() net.add(Inception(192, (96, 208), (16, 48), 64), Inception(160, (112, 224), (24, 64), 64), Inception(128, (128, 256), (24, 64), 64), Inception(112, (144, 288), (32, 64), 64), Inception(256, (160, 320), (32, 128), 128), nn.MaxPool2D(pool_size=3, strides=2, padding=1)) return net @d2l.add_to_class(GoogleNet) def b5(self): net = nn.Sequential() net.add(Inception(256, (160, 320), (32, 128), 128), Inception(384, (192, 384), (48, 128), 128), nn.GlobalAvgPool2D()) return net @d2l.add_to_class(GoogleNet) def __init__(self, lr=0.1, num_classes=10): super(GoogleNet, self).__init__() self.save_hyperparameters() self.net = nn.Sequential() self.net.add(self.b1(), self.b2(), self.b3(), self.b4(), self.b5(), nn.Dense(num_classes)) self.net.initialize(init.Xavier())
JAX Implementation:
class GoogleNet(d2l.Classifier): lr: float = 0.1 num_classes: int = 10 def setup(self): self.net = nn.Sequential([self.b1(), self.b2(), self.b3(), self.b4(), self.b5(), nn.Dense(self.num_classes)]) def b1(self): return nn.Sequential([ nn.Conv(64, kernel_size=(7, 7), strides=(2, 2), padding='same'), nn.relu, lambda x: nn.max_pool(x, window_shape=(3, 3), strides=(2, 2), padding='same')]) @d2l.add_to_class(GoogleNet) def b2(self): return nn.Sequential([nn.Conv(64, kernel_size=(1, 1)), nn.relu, nn.Conv(192, kernel_size=(3, 3), padding='same'), nn.relu, lambda x: nn.max_pool(x, window_shape=(3, 3), strides=(2, 2), padding='same')]) @d2l.add_to_class(GoogleNet) def b3(self): return nn.Sequential([Inception(64, (96, 128), (16, 32), 32), Inception(128, (128, 192), (32, 96), 64), lambda x: nn.max_pool(x, window_shape=(3, 3), strides=(2, 2), padding='same')]) @d2l.add_to_class(GoogleNet) def b4(self): return nn.Sequential([Inception(192, (96, 208), (16, 48), 64), Inception(160, (112, 224), (24, 64), 64), Inception(128, (128, 256), (24, 64), 64), Inception(112, (144, 288), (32, 64), 64), Inception(256, (160, 320), (32, 128), 128), lambda x: nn.max_pool(x, window_shape=(3, 3), strides=(2, 2), padding='same')]) @d2l.add_to_class(GoogleNet) def b5(self): return nn.Sequential([Inception(256, (160, 320), (32, 128), 128), Inception(384, (192, 384), (48, 128), 128), # Flax does not provide a GlobalAvgPool2D layer lambda x: nn.avg_pool(x, window_shape=x.shape[1:3], strides=x.shape[1:3], padding='valid'), lambda x: x.reshape((x.shape[0], -1))])
TensorFlow Implementation:
class GoogleNet(d2l.Classifier): def b1(self): return tf.keras.models.Sequential([ tf.keras.layers.Conv2D(64, 7, strides=2, padding='same', activation='relu'), tf.keras.layers.MaxPool2D(pool_size=3, strides=2, padding='same')]) @d2l.add_to_class(GoogleNet) def b2(self): return tf.keras.Sequential([ tf.keras.layers.Conv2D(64, 1, activation='relu'), tf.keras.layers.Conv2D(192, 3, padding='same', activation='relu'), tf.keras.layers.MaxPool2D(pool_size=3, strides=2, padding='same')]) @d2l.add_to_class(GoogleNet) def b3(self): return tf.keras.models.Sequential([ Inception(64, (96, 128), (16, 32), 32), Inception(128, (128, 192), (32, 96), 64), tf.keras.layers.MaxPool2D(pool_size=3, strides=2, padding='same')]) @d2l.add_to_class(GoogleNet) def b4(self): return tf.keras.Sequential([ Inception(192, (96, 208), (16, 48), 64), Inception(160, (112, 224), (24, 64), 64), Inception(128, (128, 256), (24, 64), 64), Inception(112, (144, 288), (32, 64), 64), Inception(256, (160, 320), (32, 128), 128), tf.keras.layers.MaxPool2D(pool_size=3, strides=2, padding='same')]) @d2l.add_to_class(GoogleNet) def b5(self): return tf.keras.Sequential([ Inception(256, (160, 320), (32, 128), 128), Inception(384, (192, 384), (48, 128), 128), tf.keras.layers.GlobalAvgPool2D(), tf.keras.layers.Flatten()]) @d2l.add_to_class(GoogleNet) def __init__(self, lr=0.1, num_classes=10): super(GoogleNet, self).__init__() self.save_hyperparameters() self.net = tf.keras.Sequential([ self.b1(), self.b2(), self.b3(), self.b4(), self.b5(), tf.keras.layers.Dense(num_classes)])
0
1
Updated 2026-05-13
Tags
D2L
Dive into Deep Learning @ D2L