Memuat
Memuat
Want to go deeper?
Join one of our courses or ask us directly on WhatsApp.
Picked from the software, topic, and level of “Machine Learning Python Class #16: Neural Network & Deep Learning with PyTorch”.
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.
This is the final result, our own convolutional neural network or CNN built with Python that recognizes handwritten digits with about 98% accuracy. The left graph shows the loss decreasing during training, and on the right is the model's prediction for the test image. Let's get started. Hello, welcome to episode K16 of the Machine Learning with Python series. We're diving into deep learning, the technology behind face recognition, autonomous cars, up to AI assistants. The core is neural networks, which are stacks of neuron layers that learn using gradient descent, as in episode 3, only with thousands or even billions of parameters. We're using PyTorch, the most popular deep learning framework among researchers. First, install PyTorch.
The CPU version is sufficient for this tutorial, the command is on the pytorch.org website. Then create a file called basic.py. In PyTorch, data is stored in tensors, similar to arrays but can run on GPU and automatically compute derivatives. If a tensor is given requires_grad equal to true, then when we call backward, PyTorch computes the derivative itself. For y equals x squared plus 3x at x equals 2, the derivative is 7, exactly like manual calculation. This is autograd, the core of backpropagation.
A neuron calculates the sum of input multiplied by weight, plus bias, then passes it through an activation function. Without activation, a stack of layers is just equivalent to one linear regression. We'll draw three popular activation functions, sigmoid, tanh, and ReLU. ReLU is the most commonly used because it's simple and fast. Negative values become 0, positive values are kept. Now we're building a real neural network.
Create a file called mlp.py. The data is the digits dataset, images of 8x8 numbers. We convert it to a tensor, then use the data loader to split the data into mini-batches containing 32 images. The model is a multi-layer perceptron with 64 inputs, 2 hidden layers with ReLU, and 10 outputs for the digits 0-9. This is the PyTorch-specific training loop.
For each epoch and each batch, calculate predictions, compute loss with cross entropy, zero out the old gradients with 0 grad, calculate new gradients with backward, then update the weights with optimizer Adam via step. We train for 30 epochs while printing the loss, then measure accuracy on the test data in evaluation mode without computing gradients. For images, there is a more suitable architecture, convolutional neural network or CNN.
Create file cnn.py. The convolutional layer slides a small filter over the image to detect patterns like lines and curves, then Max Pauling reduces its size. Unlike MLP, CNN understands that nearby pixels are related to each other. We shape the data into 4 dimensions. Number of images, 1 channel, 8, and 8. We train the CNN with the same training loop, then plot the loss curve and 12 predictions on the test image.
Green title means correct, red means wrong. Its accuracy is around 98%. Finally, we save the model weights with Tor Safe so they can be used again without retraining.
Done. We have already learned about tensors and autograd, neurons and activation functions, building an MLP with our own training loop, and CNNs for images. Other architectures such as RNN for sequential data and transformer for language are built on the same principles. In the next episode, we'll learn about the third type of machine learning, reinforcement learning. Want to master machine learning and data science? Cerdas Inovasi Digital offers Python, machine learning, and AI courses from the basics, prediction model development services, data analysis, and AI applications for business and research, as well as selling the complete source code of this tutorial. Want to master the entire series, from machine learning basics to deploying models to the web? All 18 episodes, complete with step-by-step PDF guides, are available to 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. Thank you for watching, see you in the next episode. Thank you for watching!
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 →