Incorrect receptive field
d-01 opened this issue · 1 comments
d-01 commented
A description of what the bug is
There are two problems with a receptive field:
- An effective receptive field size (see tests below) doesn't match a calculated size (
receptive_field
attribute). - TCN layer with dilation=1 have holes (blind spots) in a receptive field.
A snippet
The test code demonstrates which positions in the input sequence affect the output.
Test code (Google Colab):
import numpy as np
from tcn import TCN
import tensorflow as tf # 2.8.0
def test_inference(seq, tcn):
'''Perform inference with TCN layer, print input and output.'''
batch = np.asarray(seq, dtype=np.float32)[None, ..., None]
output = tcn(batch).numpy().squeeze()
print('input: {}; output: {:.4f}'.format(' '.join(map(str, seq)), output))
tf.random.set_seed(333)
tcn = TCN(nb_filters=1, kernel_size=2, dilations=(1, 2), return_sequences=False)
print(f'k={tcn.kernel_size}, dilations={tcn.dilations}, receptive_field={tcn.receptive_field}')
TEST_SEQ_LEN = 10
for i in range(TEST_SEQ_LEN):
print(i, end='. ')
test_seq = list(range(TEST_SEQ_LEN))
test_seq[i] = 0
test_inference(test_seq[1:][::-1], tcn)
Actual result
Output (with comments):
k=2, dilations=(1, 2), receptive_field=7
0. input: 9 8 7 6 5 4 3 2 1; output: 8.5691 base output (unmodified)
1. input: 9 8 7 6 5 4 3 2 0; output: 9.537 changed
2. input: 9 8 7 6 5 4 3 0 1; output: 8.5691 does not reflect changes in input
3. input: 9 8 7 6 5 4 0 2 1; output: 7.0454 changed
4. input: 9 8 7 6 5 0 3 2 1; output: 8.5691 does not reflect changes in input
5. input: 9 8 7 6 0 4 3 2 1; output: 2.9412 changed
6. input: 9 8 7 0 5 4 3 2 1; output: 8.5691 does not reflect changes in input
7. input: 9 8 0 6 5 4 3 2 1; output: 8.5691 does not reflect changes in input
8. input: 9 0 7 6 5 4 3 2 1; output: 8.5691 outside of receptive field
9. input: 0 8 7 6 5 4 3 2 1; output: 8.5691 outside of receptive field
_ _ _ elements model "sees" in the input
* * * * * * * receptive field indicator
- Actual receptive field size is 5, not 7.
- 2nd and 4th positions in the receptive field do not affect the output.
Expected result
Every element in the input sequence covered by receptive field affects the output.
Dependencies
- environment – Google Colab
- python – 3.7.12 (default, Jan 15 2022, 18:48:18)
- tensorflow – 2.8.0
- keras-tcn – 3.4.0
d-01 commented
Sorry for bothering, it was my mistake.
While testing, the relu activation should be disabled (activation=None
). The output will be:
k=2, dilations=(1, 2), receptive_field=7
0. input: 9 8 7 6 5 4 3 2 1; output: 12.6189
1. input: 9 8 7 6 5 4 3 2 0; output: 13.6868 *
2. input: 9 8 7 6 5 4 3 0 1; output: 12.3591 *
3. input: 9 8 7 6 5 4 0 2 1; output: 15.2741 *
4. input: 9 8 7 6 5 0 3 2 1; output: 5.9221 *
5. input: 9 8 7 6 0 4 3 2 1; output: 22.6492 *
6. input: 9 8 7 0 5 4 3 2 1; output: -8.8503 *
7. input: 9 8 0 6 5 4 3 2 1; output: 14.6726 *
8. input: 9 0 7 6 5 4 3 2 1; output: 12.6189
9. input: 0 8 7 6 5 4 3 2 1; output: 12.6189
I realized it after implemented TCN myself and got the same "bug" ¯\(ツ)/¯.