Memuat
Memuat
Watch every step above performed live, with English narration.
0 plays
Class book
Page 1 / 5
Cover: Class Deep Learning CNN-LSTM Pneumonia Detection #5: Training CNN-LSTM: Class Weight, Scheduler & Early Stopping
Click a page or use ◀ ▶ to turn · swipe on mobile · to enlarge
Want to go deeper?
Join one of our courses or ask us directly on WhatsApp.
Picked from the software, topic, and level of “Class Deep Learning CNN-LSTM Pneumonia Detection #5: Training CNN-LSTM: Class Weights, Scheduler & Early Stopping”.
Other ways we can help with Python.
Learn with a mentor
Scheduled AI & Machine Learning courses with live guidance and a certificate.

Multi-Objective Optimization Course: NSGA-II, MOPSO & Hybrid Algorithms (Python)

ECG Arrhythmia Classification Course with Wavelet & Deep Learning (PyTorch)

ANFIS + PSO/GA Course for Non-Linear Time Series Forecasting (Python)
Have it built for you
No time to learn Python? Our team can do the project for you.

Python Machine Learning — Prediction Models, Customer Segmentation, Fraud Detection & AI Apps

AI Image Creation with Stable Diffusion — Unlimited Custom Visual Production
★ 4.9

AI Image & Video Creation with Leonardo AI — All-in-One Visuals for Business
★ 4.9
Get notified about new Python classes
At most one email a week. Unfollow anytime with one click.
The full narration of this video. The current sentence is highlighted while the video plays; click any sentence to jump there.
Here is the final result, the training curve of the Hybrid CNN-LSTM model, with training and validation loss at each epoch. The best model achieves a validation accuracy of about 97%, and training stops automatically when the model starts to plateau, thanks to early stopping. Let's get started. Hello, welcome to episode five of the Hybrid CNN-LSTM model series for early detection of lung diseases. The architecture is ready, now we will train it using techniques commonly used in research. Class weights for imbalanced data, learning rate scheduler, early stopping to prevent overfitting, and saving the best model checkpoint.
Create a file named train_underscore_hybrid.py We import the loader and the CNN-LSTM model from the previous episode. Remember, the training data is 74% pneumonia. Without handling, the model tends to favor the majority class. The solution is class weights, the number of data is divided by twice the number of each class. The result is normal getting a weight of about 1.94, pneumonia 0.67, so errors in the normal class are penalized almost three times more.
These weights are then added to the cross entropy loss. The optimizer is Adam, plus a scheduler reduceron plata or, which automatically halves the learning rate if the validation loss does not improve. We also create a function to calculate the validation loss and accuracy.
Now the training loop with early stopping. Every time the validation loss reaches a new best, the model is saved to hybrid_underscore_best.pt and the patience counter is reset. If there is no improvement for 4 consecutive epochs, training is stopped. The maximum limit is 20 epochs. The history is saved to JSON and we plot the curve.
Check the terminal. At the beginning, the validation loss fluctuates. This is normal because LSTM takes time to adjust. Around epoch 9, the validation loss reaches its lowest point with a validation accuracy of about 97%. After that, there is no improvement for 4 epochs, so early stopping stops training at epoch 13. The left graph shows the training loss continuously decreasing, while the validation loss starts to rise again, indicating the early signs of overfitting that have been successfully prevented. Done. We have handled the imbalanced data using class weights, employing a learning rate scheduler, early stopping, and saving the best model checkpoint. In the final episode, we will evaluate the model clinically, including sensitivity, specificity, ROC-AUC, and observe where the model looks with grade cam. Currently working on a thesis, research, or AI project in the field of medical imaging? Cerdas Inovasi Digital helps implement deep learning, from CNN, LSTM, hybrid models, up to grade cam interpretation and result writing. as well as offering deep learning courses with PyTorch. Want to master the entire series, from the Röntgen dataset to evaluation and grade cam? All 6 episodes, complete with PDF step-by-step guides and source code, are available for premium tutorial subscribers. Subscribe starting at Rp69.000 per month, save 28% with the 12-month package, or pay once Rp999.000 for lifetime access. Want just one class? Buy a single class with lifetime access. Subscribe now at the premium page cerdasinovasidigital.com Contact us via WhatsApp, or visit cerdasinovasidigital.com Thank you for watching, see you in the next episode. Thank you for watching, see you in the next episode.
Type a question or keyword; we find the part of this episode that explains it and jump the video there.
Notes are saved with the current video time and are only visible to you.
Stuck on a step? Ask here; the instructor and other learners can answer.
No questions yet.

Cerdas Inovasi Digital Team
Software developers, BIM & 3D engineers, and AI practitioners
We build software, BIM models, games and AI automations for clients every day, and turn that hands-on work into step-by-step classes: every step is shown on screen and comes with a PDF guide you can keep.
See our clients →