|
| 1 | +# pylint: disable=g-bad-file-header |
| 2 | +# Copyright 2021 DeepMind Technologies Limited. All Rights Reserved. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +# ============================================================================ |
| 16 | +"""Network definitions for LeNet5.""" |
| 17 | +from typing import Sequence |
| 18 | + |
| 19 | +from absl import logging |
| 20 | +import chex |
| 21 | +from enn import base |
| 22 | +from enn.networks import ensembles |
| 23 | +import haiku as hk |
| 24 | +import jax |
| 25 | + |
| 26 | +_LeNet5_CHANNELS = (6, 16, 120) |
| 27 | + |
| 28 | + |
| 29 | +class LeNet5(hk.Module): |
| 30 | + """VGG Network with batchnorm and without maxpool.""" |
| 31 | + |
| 32 | + def __init__(self, |
| 33 | + num_output_classes: int, |
| 34 | + lenet_output_channels: Sequence[int] = _LeNet5_CHANNELS,): |
| 35 | + super().__init__() |
| 36 | + logging.info('Initializing a LeNet5.') |
| 37 | + self._output_channels = lenet_output_channels |
| 38 | + num_channels = len(self._output_channels) |
| 39 | + |
| 40 | + self._conv_modules = [ |
| 41 | + hk.Conv2D( # pylint: disable=g-complex-comprehension |
| 42 | + output_channels=self._output_channels[i], |
| 43 | + kernel_shape=5, |
| 44 | + padding='SAME', |
| 45 | + name=f'conv_2d_{i}') for i in range(num_channels) |
| 46 | + ] |
| 47 | + self._mp_modules = [ |
| 48 | + hk.MaxPool( # pylint: disable=g-complex-comprehension |
| 49 | + window_shape=2, strides=2, padding='SAME', |
| 50 | + name=f'max_pool_{i}') for i in range(num_channels) |
| 51 | + ] |
| 52 | + self._flatten_module = hk.Flatten() |
| 53 | + self._linear_module = hk.Linear(84, name='linear') |
| 54 | + self._logits_module = hk.Linear(num_output_classes, name='logits') |
| 55 | + |
| 56 | + def __call__(self, inputs: chex.Array) -> chex.Array: |
| 57 | + net = inputs |
| 58 | + for conv_layer, mp_layer in zip(self._conv_modules, self._mp_modules): |
| 59 | + net = conv_layer(net) |
| 60 | + net = jax.nn.relu(net) |
| 61 | + net = mp_layer(net) |
| 62 | + net = self._flatten_module(net) |
| 63 | + net = self._linear_module(net) |
| 64 | + net = jax.nn.relu(net) |
| 65 | + return self._logits_module(net) |
| 66 | + |
| 67 | + |
| 68 | +class EnsembleLeNet5ENN(base.EpistemicNetworkWithState): |
| 69 | + """Ensemble of LeNet5 Networks created using einsum ensemble.""" |
| 70 | + |
| 71 | + def __init__(self, |
| 72 | + num_output_classes: int, |
| 73 | + num_ensemble: int = 1,): |
| 74 | + def net_fn(x: chex.Array) -> chex.Array: |
| 75 | + return LeNet5(num_output_classes)(x) |
| 76 | + transformed = hk.without_apply_rng(hk.transform_with_state(net_fn)) |
| 77 | + enn = ensembles.EnsembleWithState(transformed, num_ensemble) |
| 78 | + super().__init__(enn.apply, enn.init, enn.indexer) |
0 commit comments