Mengoptimumkan Rangkaian Neural Anda

DDD
Lepaskan: 2024-10-13 06:15:02
asal
810 orang telah melayarinya

Minggu lepas saya menyiarkan artikel tentang cara membina rangkaian saraf mudah, khususnya perceptron berbilang lapisan. Artikel ini akan menyelam lebih dalam ke dalam spesifik rangkaian saraf untuk membincangkan cara kita boleh memaksimumkan prestasi rangkaian saraf dengan mengubah konfigurasinya.

Berapa Lama Untuk Melatih Model Anda

Apabila melatih model, anda mungkin berfikir bahawa jika anda melatih model anda dengan cukup, model itu akan menjadi sempurna. Ini mungkin benar, tetapi itu hanya berlaku untuk set data yang dilatih. Malah, jika anda memberikan satu lagi set data yang nilainya berbeza, model itu boleh mengeluarkan ramalan yang salah sepenuhnya.

Untuk memahami perkara ini dengan lebih lanjut, katakan anda berlatih setiap hari untuk peperiksaan pemandu anda dengan memandu dalam garisan lurus tanpa menggerakkan roda. (Sila jangan lakukan ini.) Walaupun anda mungkin akan beraksi dengan sangat baik pada jalur seret, jika anda diberitahu untuk membuat belok kiri pada peperiksaan sebenar, anda mungkin bertukar menjadi tanda BERHENTI sebaliknya.

Fenomena ini dipanggil overfitting. Model anda boleh mempelajari semua aspek dan corak data yang dilatih tetapi jika model anda mempelajari corak yang mematuhi set data latihan terlalu rapat, maka apabila diberikan set data baharu, model anda akan berprestasi buruk. Pada masa yang sama, jika anda tidak melatih model anda dengan cukup, maka model anda tidak akan dapat mengecam corak dalam set data lain dengan betul. Dalam kes ini, anda akan menjadi kurang sesuai.

Optimizing Your Neural Networks


Contoh overfitting. Kehilangan pengesahan, yang diwakili oleh garis oren semakin meningkat secara beransur-ansur manakala kerugian latihan, yang diwakili oleh garisan biru semakin berkurangan.

Dalam contoh di atas, kedudukan yang baik untuk menghentikan latihan model anda adalah tepat apabila kehilangan pengesahan mencapai tahap minimum. Anda boleh melakukan ini dengan berhenti awal, yang menghentikan latihan apabila tiada peningkatan dalam kehilangan pengesahan selepas bilangan kitaran latihan (zaman) yang sewenang-wenangnya.

Melatih model anda adalah tentang mencari keseimbangan antara pemasangan terlebih dan kurang semasa menggunakan berhenti awal jika perlu. Itulah sebabnya set data latihan anda harus mewakili keseluruhan populasi anda supaya model anda boleh membuat ramalan dengan lebih tepat pada data yang belum dilihatnya.

Fungsi Kehilangan

Mungkin salah satu konfigurasi latihan paling penting yang boleh diubah suai ialah fungsi kehilangan, iaitu "ketidaktepatan" antara ramalan model anda dan nilai sebenar mereka. "Ketidaktepatan" boleh diwakili secara matematik dalam pelbagai cara, salah satu yang paling biasa ialah ralat kuasa dua min (MSE):

MSE=i=1n(y나는ˉy나는)2ntext{MSE} = frac{sum_{i=1}^n (바{y_i} - y_i)^2}{n} MSE= ni=1 n(yiˉyi)2

wo yiˉbar{y_i} yichˉ ist die Vorhersage des Modells und yiy_i yi ist der wahre Wert. Es gibt eine ähnliche Variante namens mittlerer absoluter Fehler (MAE)

MAE=i=1ny나는ˉy나는ntext{MAE} = frac{sum_{i=1}^n |bar{y_i} - y_i|}{n} MAE= ni=1 nyiˉyi

Quelle est la différence entre ces deux-là et lequel est le meilleur ? La vraie réponse est que cela dépend de divers facteurs. Considérons un exemple simple de régression linéaire bidimensionnelle.

Dans de nombreux cas, il peut y avoir des points de données qui constituent des valeurs aberrantes, des points éloignés des autres points de données. En termes de régression linéaire, cela signifie qu'il y a quelques points sur le xyxy xy -avions qui sont loin des autres. Si vous vous souvenez de vos cours de statistiques, ce sont des points comme ceux-ci qui peuvent affecter de manière significative la droite de régression linéaire calculée.

Optimizing Your Neural Networks
Un graphique simple avec des points sur (1, 1), (2, 2), (3, 3) et (4, 4)

Si vous vouliez penser à une ligne qui pourrait traverser les quatre points, alors y=xy = x y=x serait un excellent choix car cette ligne passerait par tous les points.

Optimizing Your Neural Networks
Un graphique simple avec des points sur (1, 1), (2, 2), (3, 3) et (4, 4) et la droite y=xy = x y=x en passant par ça

Cependant, disons que je décide d'ajouter un autre point à (5,1) (5, 1) (5,1) . Maintenant, quelle devrait être la droite de régression ? Eh bien, il s'avère que c'est complètement différent : y=0,2x 1,6y = 0,2x 1,6 y=0,2x 1,6

Optimizing Your Neural Networks


Un graphique simple avec des points sur (1, 1), (2, 2), (3, 3), (4, 4) et (5,1 ) avec une ligne de régression linéaire qui le traverse.

Compte tenu des points de données précédents, la ligne s'attendrait à ce que la valeur de yy y quand x=5x = 5 x=5 est 5, mais en raison de la valeur aberrante et de son MSE, la ligne de régression est « tirée vers le bas » de manière significative.

Ce n'est qu'un exemple simple, mais cela pose une question à laquelle vous, en tant que développeur d'apprentissage automatique, devez vous arrêter et réfléchir : Dans quelle mesure mon modèle doit-il être sensible aux valeurs aberrantes ? Si vous voulez que votre modèle pour être plus sensible aux valeurs aberrantes, vous choisirez alors une métrique comme MSE, car dans ce cas, les erreurs impliquant des valeurs aberrantes sont plus prononcées en raison de la mise au carré et votre modèle s'ajustera pour minimiser cela. Sinon, vous choisiriez une métrique comme MAE, qui ne se soucie pas autant des valeurs aberrantes.

Optimizers

In my previous post, I also discussed the concept of backpropagation, gradient descent, and how they work to minimize the loss of the model. The gradient is a vector that points towards the direction of greatest change. A gradient descent algorithm will calculate this vector and move in the exact opposite direction so that it eventually reaches a minimum.

Most optimizers have a specific learning rate, commonly denoted as α\alpha α that they adhere to. Essentially, this represents how much the algorithm will move towards the minimum each time it calculates the gradient. Be careful of setting your learning rate to be too large! Your algorithm may never reach the minimum due to the large steps it takes that could repeatedly skip over the minimum.

Optimizing Your Neural Networks
[Tensorflow's neural network playground](https://playground.tensorflow.org) showing what can happen if you set the learning rate to be too large. Notice how the testing and training loss are both `NaN`.

Going back to gradient descent, while it is effective in minimizing loss, this might significantly slow down the training process as the loss function is calculated on the entire dataset. There are several alternatives to gradient descent that are more efficient but have their respective downsides.

Stochastic Gradient Descent

One of the most popular alternatives to standard gradient descent is a variant called stochastic gradient descent (SGD). As with gradient descent, SGD has a fixed learning rate. But rather than running through the entire dataset like gradient descent, SGD takes a small sample is randomly selected and the weights of your neural network are updated based on the sample instead. Eventually, the parameter values converge to a point that approximately (but not exactly) minimizes the loss function. This is one of the downsides of SGD, as it doesn't always reach the exact minimum. Additionally, similar to gradient descent, it remains sensitive to the learning rate that you set.

The Adam Optimizer

The name, Adam, is derived from adaptive moment estimation. It essentially combines two variants of SGD to adjust the learning rate for each input parameter based on how often it gets updated during each training iteration (adaptive learning rate). At the same time, it also keeps track of past gradient calculations as a moving average to smooth out updates (momentum). However, because of its momentum characteristic, it can sometimes take longer to converge than other algorithms.

Putting it All Together

Now for an example!

I've created an example walkthrough on Google Colab that uses PyTorch to create a neural network that learns a simple linear relationship.

If you're a bit new to Python, don't worry! I've included some explanations that discuss what's going on in each section.

Reflection

While this obviously doesn't cover everything about optimizing neural networks, I wanted to at least cover a few of the most important concepts that you can take advantage of while training your own models. Hopefully you've learned something this week and thanks for reading!

Atas ialah kandungan terperinci Mengoptimumkan Rangkaian Neural Anda. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

sumber:dev.to
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan
Tentang kita Penafian Sitemap
Laman web PHP Cina:Latihan PHP dalam talian kebajikan awam,Bantu pelajar PHP berkembang dengan cepat!