Qual é a complexidade de tempo para treinar uma rede neural usando retropropagação?

17

Suponha que um NN contenha n camadas ocultas, m exemplos de treinamento, x recursos e ni nós em cada camada. Qual é a complexidade de tempo para treinar esse NN usando a propagação traseira?

Eu tenho uma idéia básica sobre como eles encontram a complexidade do tempo dos algoritmos, mas aqui há quatro fatores diferentes a serem considerados aqui, como iterações, camadas, nós em cada camada, exemplos de treinamento e talvez mais fatores. Encontrei uma resposta aqui, mas não estava clara o suficiente.

Existem outros fatores, além dos mencionados acima, que influenciam a complexidade de tempo do algoritmo de treinamento de uma NN?

DuttaA
fonte
Veja também https://qr.ae/TWttzq .
N

Respostas:

11

Não vi uma resposta de uma fonte confiável, mas tentarei responder a mim mesmo, com um exemplo simples (com meu conhecimento atual).

Em geral, observe que o treinamento de um MLP usando a propagação traseira é geralmente implementado com matrizes.

Complexidade temporal da multiplicação da matriz

A complexidade temporal da multiplicação de matrizes para MijMjk é simplesmente O(ijk) .

Observe que estamos assumindo o algoritmo de multiplicação mais simples aqui: existem outros algoritmos com uma complexidade de tempo um pouco melhor.

Algoritmo de avanço de feedforward

O algoritmo de propagação de feedforward é o seguinte.

Primeiro, para passar da camada i para j , você precisa

Sj=WjiZi

Então você aplica a função de ativação

Zj=f(Sj)

Se tivermos N camadas (incluindo as camadas de entrada e saída), isso será executado N1 vezes.

Exemplo

Como exemplo, vamos calcular a complexidade do tempo para o algoritmo de encaminhamento para um MLP com 4 camadas, em que i indica o número de nós da camada de entrada, j o número de nós na segunda camada, k o número de nós na terceira camada e l o número de nós na camada de saída.

Como existem 4 camadas, você precisa de 3 matrizes para representar pesos entre essas camadas. Vamos denotá-los por Wji , Wkj e Wlk , onde Wji é uma matriz com j linhas e colunas i ( Wji contém os pesos que vão da camada i à camada j ).

Suponha que você tenha t exemplos de treinamento. Para propagação da camada i a j , temos primeiro

Sjt=WjiZit

e esta operação (isto é, multiplicação de matrizes) tem complexidade de tempo O(jit) . Então aplicamos a função de ativação

Zjt=f(Sjt)

e este tem O(jt) complexidade de tempo, porque é uma operação de elemento a elemento.

Então, no total, temos

O(jit+jt)=O(jt(t+1))=O(jit)

Usando a mesma lógica, para ir jk , temos O(kjt) e, para kl , temos O(lkt) .

No total, a complexidade do tempo para propagação de feedforward será

O(jit+kjt+lkt)=O(t(ij+jk+kl))

Não tenho certeza se isso pode ser simplificado ainda mais ou não. Talvez seja apenas O(tijkl) , mas eu não tenho certeza.

Algoritmo de propagação traseira

O algoritmo de retropropagação prossegue da seguinte maneira. Partindo da camada de saída lk , calculamos o sinal de erro, Elt , uma matriz que contém os sinais de erro para nós na camada l

Elt=f(Slt)(ZltOlt)

onde significa multiplicação por elementos. Note-se que Elt tem l linhas e t colunas: ele simplesmente significa que cada coluna é o sinal de erro para treinar exemplo t .

We then compute the "delta weights", DlkRl×k (between layer l and layer k)

Dlk=EltZtk

where Ztk is the transpose of Zkt.

We then adjust the weights

Wlk=WlkDlk

Para lk , temos, assim, a complexidade do tempo O(lt+lt+ltk+lk)=O(ltk) .

Agora, voltando de kj . Primeiro temos

Ekt=f(Skt)(WklElt)

Então

Dkj=EktZtj

And then

Wkj=WkjDkj

where Wkl is the transpose of Wlk. For kj, we have the time complexity O(kt+klt+ktj+kj)=O(kt(l+j)).

And finally, for ji, we have O(jt(k+i)). In total, we have

O(ltk+tk(l+j)+tj(k+i))=O(t(lk+kj+ji))

which is same as feedforward pass algorithm. Since they are same, the total time complexity for one epoch will be

O(t(ij+jk+kl)).

This time complexity is then multiplied by number of iterations (epochs). So, we have

O(nt(ij+jk+kl)),
where n is number of iterations.

Notes

Note that these matrix operations can greatly be paralelized by GPUs.

Conclusion

We tried to find the time complexity for training a neural network that has 4 layers with respectively i, j, k and l nodes, with t training examples and n epochs. The result was O(nt(ij+jk+kl)).

We assumed the simplest form of matrix multiplication that has cubic time complexity. We used batch gradient descent algorithm. The results for stochastic and mini-batch gradient descent should be same. (Let me know if you think the otherwise: note that batch gradient descent is the general form, with little modification, it becomes stochastic or mini-batch)

Also, if you use momentum optimization, you will have same time complexity, because the extra matrix operations required are all element-wise operations, hence they will not affect the time complexity of the algorithm.

I'm not sure what the results would be using other optimizers such as RMSprop.

Sources

The following article http://briandolhansky.com/blog/2014/10/30/artificial-neural-networks-matrix-form-part-5 describes an implementation using matrices. Although this implementation is using "row major", the time complexity is not affected by this.

If you're not familiar with back-propagation, check this article:

http://briandolhansky.com/blog/2013/9/27/artificial-neural-networks-backpropagation-part-4

M.kazem Akhgary
fonte
Your answer is great..I could not find any ambiguity till now, but you forgot the no. of iterations part, just add it...and if no one answers in 5 days i'll surely accept your answer
DuttaA
@DuttaA I tried to put every thing I knew. it may not be 100% correct so feel free to leave this unaccepted :) I'm also waiting for other answers to see what other points I missed.
M.kazem Akhgary
4

For the evaluation of a single pattern, you need to process all weights and all neurons. Given that every neuron has at least one weight, we can ignore them, and have O(w) where w is the number of weights, i.e., nni, assuming full connectivity between your layers.

The back-propagation has the same complexity as the forward evaluation (just look at the formula).

So, the complexity for learning m examples, where each gets repeated e times, is O(wme).

The bad news is that there's no formula telling you what number of epochs e you need.

maaartinus
fonte
From the above answer don't you think itdepends on more factors?
DuttaA
1
@DuttaA No. There's a constant amount of work per weight, which gets repeated e times for each of m examples. I didn't bother to compute the number of weights, I guess, that's the difference.
maaartinus
1
I think the answers are same. in my answer I can assume number of weights w = ij + jk + kl. basically sum of n * n_i between layers as you noted.
M.kazem Akhgary
1

A potential disadvantage of gradient-based methods is that they head for the nearest minimum, which is usually not the global minimum.

This means that the only difference between these search methods is the speed with which solutions are obtained, and not the nature of those solutions.

An important consideration is time complexity, which is the rate at which the time required to find a solution increases with the number of parameters (weights). In short, the time complexities of a range of different gradient-based methods (including second-order methods) seem to be similar.

Six different error functions exhibit a median run-time order of approximately O(N to the power 4) on the N-2-N encoder in this paper:

Lister, R and Stone J "An Empirical Study of the Time Complexity of Various Error Functions with Conjugate Gradient Back Propagation" , IEEE International Conference on Artificial Neural Networks (ICNN95), Perth, Australia, Nov 27-Dec 1, 1995.

Summarised from my book: Artificial Intelligence Engines: A Tutorial Introduction to the Mathematics of Deep Learning.

James V Stone
fonte
Hi J. Stone. Thanks for trying to contribute to the site. However, please, note that this is not a place for advertising yourself. Anyway, you can surely provide a link to your own books if they are useful for answering the questions and provided you're not just trying to advertise yourself.
nbro
@nbro If James Stone can provide an insightful answer - and it seems so - then i'm fine with him also mentioning some of his work. Having experts on this network is a solid contribution to the quality and level.
javadba