Md. Asif Uddin
    Problem I.12.B05

    Count the code before calling it a bottleneck

    shape▲▲△

    Exact integers.

    STATEMENT

    Track shapes and parameters through a dense and a convolutional autoencoder for the same image, and find the two places where the convolutional one goes wrong.

    GIVEN

    One input is a 1×28×281\times28\times28 image, 784784 numbers.

    The dense autoencoder has layer widths 784784, 128128, 3232, 128128, 784784, with a bias on every layer.

    The convolutional encoder is two layers with 3×33\times3 kernels, stride 22 and padding 11, taking 11 channel to 88 and then 88 to 1616. The decoder is two transposed convolutions with the same kernel, stride and padding, taking 1616 channels to 88 and then 88 to 11. All four layers have biases.

    FIND

    (a) The dense parameter count, untied and with tied weights. (b) The spatial sizes through the convolutional encoder, and how many numbers its code holds. (c) The decoder’s output size with output padding 00 and with 11. (d) The convolutional parameter count. (e) The code channel counts for which the convolutional code is narrower than its input.

    STRATEGY

    Count each dense layer as inputs times outputs plus outputs. Use (I.11.4) for the encoder’s sizes and (I.11.5) for the decoder’s.

    SOLUTION

    (a) The four dense layers hold

    784⋅128+128=100,480,128⋅32+32=4,128,32⋅128+128=4,224,128⋅784+784=101,136,\begin{aligned} 784\cdot128+128&=100{,}480,\\ 128\cdot32+32&=4{,}128,\\ 32\cdot128+128&=4{,}224,\\ 128\cdot784+784&=101{,}136, \end{aligned}

    for a total of 209,968209{,}968. With tied weights the two decoder matrices are transposes of the encoder’s. The weights are then 100,352+4,096=104,448100{,}352+4{,}096=104{,}448 and the four bias vectors add 1,0721{,}072, for 105,520105{,}520. The code is 3232 numbers, 24.524.5 times fewer than the input.

    (b) By (I.11.4), with no dilation,

    ⌊28+2−32⌋+1=14,⌊14+2−32⌋+1=7.\begin{aligned} \left\lfloor\frac{28+2-3}{2}\right\rfloor+1&=14,\\[2pt] \left\lfloor\frac{14+2-3}{2}\right\rfloor+1&=7. \end{aligned}

    The code has shape 16×7×716\times7\times7 and holds 784784 numbers, exactly as many as the input. This is the first fault. Each layer halved the height and the width and the picture looks like an hourglass, but the channel count grew as fast as the grid shrank. By count, nothing forces this code to discard anything.

    (c) By (I.11.5) a transposed layer returns (n−1)⋅2−2+2+o+1=2n−1+o(n-1)\cdot2-2+2+o+1=2n-1+o for output padding oo. With o=0o=0 the sizes run 7→13→257\to13\to25. The reconstruction is 25×2525\times25, with 625625 numbers, and cannot be compared with a 28×2828\times28 target. This is the second fault. With o=1o=1 they run 7→14→287\to14\to28.

    The ambiguity is real. Under the encoder’s stride-two layer an input of size 2727 and one of size 2828 both give 1414. The transposed layer cannot know which it is undoing, and output padding tells it.

    (d) A transposed convolution stores its weight with shape (Cin,Cout,kh,kw)(C_{\mathrm{in}},C_{\mathrm{out}},k_h,k_w) and one bias per output channel. The four layers hold

    8⋅1⋅9+8=80,16⋅8⋅9+16=1,168,16⋅8⋅9+8=1,160,8⋅1⋅9+1=73,\begin{aligned} 8\cdot1\cdot9+8&=80,\\ 16\cdot8\cdot9+16&=1{,}168,\\ 16\cdot8\cdot9+8&=1{,}160,\\ 8\cdot1\cdot9+1&=73, \end{aligned}

    a total of 2,4812{,}481, against 209,968209{,}968 for the dense stack.

    (e) A code of CC channels on the 7×77\times7 grid holds 49C49C numbers. It is narrower than the input when 49C<78449C<784, that is, C<16C<16. With C=4C=4 the code holds 196196 numbers, a quarter of the input.

    Answer

    (a) 209,968209{,}968 untied and 105,520105{,}520 tied. (b) 28→14→728\to14\to7; the code holds 784784 numbers. (c) 7→13→257\to13\to25 without output padding and 7→14→287\to14\to28 with it. (d) 2,4812{,}481. (e) Fewer than 1616 code channels.

    Check — sanity

    The tied count is half the untied weights plus all the biases: 208,896/2+1,072=105,520208{,}896/2+1{,}072=105{,}520. The transposed sizes 1414 and 2828 are the encoder’s sizes read backwards.

    Where this breaks

    A count of numbers is a necessary check and not a sufficient one. A code with fewer numbers than the input can still memorise (I.12.B04), and shared, local weights restrict a convolutional autoencoder in ways the count does not show. The count catches only the case where no restriction was imposed at all.

    Variation

    With 4×44\times4 kernels, stride 22 and padding 11, the encoder still gives ⌊(28+2−4)/2⌋+1=14\lfloor(28+2-4)/2\rfloor+1=14, and the transposed layer returns (7−1)⋅2−2+3+0+1=14(7-1)\cdot2-2+3+0+1=14 with no output padding. An even input size is then recovered without the extra argument.

    Draws on