ageron / ageron/handson-ml3

[BUG] Chapter 14: `DepthPool` class does not work when the number of channels is not divisible by the `pool_size`

Open
#83 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Jupyter Notebook
Stars
14.1k
Forks
5.3k
PR merge metrics
No merged PRs in 30d

Description

In chapter 14, the following class is introduced:
```python
class DepthPool(tf.keras.layers.Layer):
def __init__(self, pool_size=2, **kwargs):
super().__init__(**kwargs)
self.pool_size = pool_size

def call(self, inputs):
shape = tf.shape(inputs) # shape[-1] is the number of channels
groups = shape[-1] // self.pool_size # number of channel groups
new_shape = tf.concat([shape[:-1], [groups, self.pool_size]], axis=0)
return tf.reduce_max(tf.reshape(inputs, new_shape), axis=-1)
```

The `tf.reshape` operation breaks down the last dimension to multiple groups so that then `tf.reduce_max` can be applied per group. However, the `tf.reshape` operation fails if `shape[-1]` (the number of channels) is not divisible by `self.pool_size`.

I suggest mentioning this limitation in the text. Alternatively, consider adding 3 lines to the `call` method to appropriately pad the tensor on the last dimension:
```python
class DepthPool(tf.keras.layers.Layer):
def __init__(self, pool_size=2, **kwargs):
super().__init__(**kwargs)
self.pool_size = pool_size

def call(self, inputs):
trailing_zeros = (self.pool_size - inputs.shape[-1] % self.pool_size) % self.pool_size
paddings = tf.constant([[0, 0], [0, 0], [0, 0], [0, trailing_zeros]])
inputs = tf.pad(inputs, paddings)

shape = tf.shape(inputs) # shape[-1] is the number of channels
groups = shape[-1] // self.pool_size # number of channel groups
new_shape = tf.concat([shape[:-1], [groups, self.pool_size]], axis=0)
return tf.reduce_max(tf.reshape(inputs, new_shape), axis=-1)
```

Contributor guide

No contributing guide indexed for this repository

Research direction

Open the Chapter 14 notebook and locate the DepthPool class shown in the issue. Check how it behaves when the channel count is not divisible by pool_size, then decide whether the chapter should document the limitation or support the case with padding. Done means the chosen behavior is explained or verified for non-divisible channel counts.

Written by the indexing model from the issue text.

Assessment

Tech stack
keras, python, tensorflow
Domain
machine-learning
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.