Twitter proof: neural networks and the linear activation function


< change language

In this post we will see why it is not helpful to have two consecutive layers of neurons with linear activation functions in neural networks. With just a bit of maths we can conclude that $n$ consecutive linear layers compute the exact same functions as $1$ single layer.

Claim: having two fully connected layers of neurons using linear activation functions is the same as having just one layer with a linear activation function.

We just have to lay down some notation in order for the maths to be doable. Assume the two consecutive layers of linear neurons are preceded by a layer with $n_0$ neurons, whose outpus are $o_1, o_2, \cdots, o_{n_0}$.
Let us say that after that layer, there is a layer of $n_1$ neurons with linear activation functions $f_i(x) = a_ix + b_i$; the weight from neuron $t$ of the previous layer to the neuron $i$ of this layer is $w_{t,i}$.
The second layer of neurons has $n_2$ neurons, with linear activation functions $f_i'(x) = a_i'x+b_i'$ and the weight from neuron $i$ of the previous layer to neuron $j$ of this layer is $w'_{i,j}$.

Twitter proof: we will compute the outputs produced by the second layer, in terms of the values $o_1, \cdots, o_{n_0}$ of the $0$-th layer and conclude we obtain a linear function of those. The output of the $j$-th neuron will be:
$$\begin{align} f'_j\left[\sum_{i=1}^{n_1}w'_{i,j}f_i\left(\sum_{t=1}^{n_0} w_{t,i}o_t\right)\right] &= a'_j\times\left[\sum_{i=1}^{n_1}w'_{i,j}f_i\left(\sum_{t=1}^{n_0} w_{t,i}o_t\right) \right] + b'_j\\ &=a'_j \left[\sum_{i=1}^{n_1}w'_{i,j}\left(a_i\times\left(\sum_{t=1}^{n_0} w_{t,i}o_t\right) + b_i\right) \right] + b'_j\\ &=a'_j \left[\sum_{i=1}^{n_1}w'_{i,j}a_i\left(\sum_{t=1}^{n_0} w_{t,i}o_t\right) + \sum_{i=1}^{n_1}w'_{i,j}b_i \right] + b'_j\\ &=a'_j \left[\sum_{i=1}^{n_1}w'_{i,j}a_i\left(\sum_{t=1}^{n_0} w_{t,i}o_t\right)\right] + a'_j\sum_{i=1}^{n_1}w'_{i,j}b_i + b'_j\\ &=a'_j \left[\sum_{t=1}^{n_0} \sum_{i=1}^{n_1}w'_{i,j}a_i w_{t,i}o_t\right] + a'_j\sum_{i=1}^{n_1}w'_{i,j}b_i + b'_j\\ \end{align}$$ now defining $a''_j = a'_j$, $$b''_j = b'_j + a'_j \sum_{i=1}^{n_1}w'_{i,j}b_i$$ and $$w''_{t,j} = \sum_{i=1}^{n_1} w'_{i,j} a_i w_{t,i}$$ we get that the output of the $j$-th neuron really is $$f''_j\left(\sum_{t=1}^{n_0} w''_{t,j} o_t\right)$$ where $f''_j(x) = a''_jx + b''_j$, a linear function. This concludes our proof!

I wrote this post today because of the #100DaysofMLcode initiative I started (yesterday!)!
Neste post vou mostrar porque é que não é nada útil, em redes neuronais, ter duas camadas consecutivas de neurónios com função de ativação linear. De facto, $n$ camadas consecutivas de neurónios com função de ativação linear calculam exatamente as mesmas funções que apenas $1$ camada!

Proposição: ter duas camadas consecutivas, totalmente ligadas, cuja função de ativação é linear, é exatamente o mesmo que ter apenas uma camada com função de ativação linear.

Para eu poder fazer as contas preciso apenas de estabelecer alguma notação. Vamos assumir que as duas camadas consecutivas de neurónios são precedidas por uma camada com $n_0$ neurónios, cujos outputs são $o_1, o_2, \cdots, o_{n_0}$.
Digamos que a camada seguinte é uma camada de $n_1$ neurónios que usam uma função de ativação linear $f_i(x) = a_ix + b_i$; o peso que vai do neurónio $t$ da camada anterior para o neurónio $i$ desta camada é $w_{t,i}$.
A segunda camada tem $n_2$ neurónios, com funções lineares de ativação $f_i'(x) = a'_ix + b'_i$; o peso que vai do neurónio $i$ da camada anterior para o neurónio $j$ desta camada é $w'_{i,j}$.

Prova num tweet: vamos calcular o output do $j$-ésimo neurónio desta segunda camada, em função dos outputs $o_1, \cdots, o_{n_0}$ da $0$-ésima camada, e verificar que obtemos uma expressão que depende linearmente desses valores. De facto, o output do $j$-ésimo neurónio da segunda camada vai ser:
$$\begin{align} f'_j\left[\sum_{i=1}^{n_1}w'_{i,j}f_i\left(\sum_{t=1}^{n_0} w_{t,i}o_t\right)\right] &= a'_j\times\left[\sum_{i=1}^{n_1}w'_{i,j}f_i\left(\sum_{t=1}^{n_0} w_{t,i}o_t\right) \right] + b'_j\\ &=a'_j \left[\sum_{i=1}^{n_1}w'_{i,j}\left(a_i\times\left(\sum_{t=1}^{n_0} w_{t,i}o_t\right) + b_i\right) \right] + b'_j\\ &=a'_j \left[\sum_{i=1}^{n_1}w'_{i,j}a_i\left(\sum_{t=1}^{n_0} w_{t,i}o_t\right) + \sum_{i=1}^{n_1}w'_{i,j}b_i \right] + b'_j\\ &=a'_j \left[\sum_{i=1}^{n_1}w'_{i,j}a_i\left(\sum_{t=1}^{n_0} w_{t,i}o_t\right)\right] + a'_j\sum_{i=1}^{n_1}w'_{i,j}b_i + b'_j\\ &=a'_j \left[\sum_{t=1}^{n_0} \sum_{i=1}^{n_1}w'_{i,j}a_i w_{t,i}o_t\right] + a'_j\sum_{i=1}^{n_1}w'_{i,j}b_i + b'_j\\ \end{align}$$ definindo agora $a''_j = a'_j$, $$b''_j = b'_j + a'_j \sum_{i=1}^{n_1}w'_{i,j}b_i$$ e $$w''_{t,j} = \sum_{i=1}^{n_1} w'_{i,j} a_i w_{t,i}$$ verificamos que o output do $j$-ésimo neurónio é $$f''_j\left(\sum_{t=1}^{n_0} w''_{t,j} o_t\right)$$ onde $f''_j(x) = a''_jx + b''_j$ é uma função linear. Isto conclui a nossa prova!

Escrevi este post hoje por causa da iniciativa #100DaysofMLcode que comecei (ontem!)!

  - RGS

Popular posts from this blog

Tutorial on programming a memory card game

Markov Decision Processes 01: the basics

The hairy ball theorem and why there is no wind (somewhere) on Earth