Làm chủ Deep Learning với Python & Machine Learning - The LazyProgrammer

Tài liệu hướng dẫn học Deep Learning với Python, làm chủ Khoa học dữ liệu và Máy học. Xây dựng mạng nơ-ron hiện đại bằng Theano và TensorFlow.

Chuyên ngành

Khoa học Dữ liệu

Người đăng

Ẩn danh

Thể loại

Sách

2016

104
2
0

Phí lưu trữ

35 Point

Tóm tắt

I. Giới Thiệu Về Deep Learning và Neural Networks

Deep Learning là một lĩnh vực của Machine Learning đang tạo nên cuộc cách mạng trong thế giới công nghệ nhân tạo. Từ khi AlphaGo của Google đánh bại Lee Sedol tại trò chơi Go vào năm 2016, Deep Learning đã chứng minh khả năng vượt trội của mình. Neural Networks hay mạng nơ-ron nhân tạo được lấy cảm hứng từ cấu trúc của bộ não con người, nơi các nơ-ron giao tiếp với nhau thông qua các tín hiệu điện và hóa học. Tuy nhiên, các neural networks nhân tạo có cấu trúc được xác định trước một cách chặt chẽ và khác biệt so với bộ não thực. Deep Learning không chỉ phức tạp mà còn được thiết kế để tự động học các đặc trưng từ dữ liệu, giúp tiết kiệm thời gian so với các thuật toán máy học truyền thống.

1.1. Khái Niệm Mạng Nơ Ron Nhân Tạo

Mạng nơ-ron nhân tạo (Artificial Neural Networks) mô phỏng hoạt động của bộ não con người trong máy tính. Các nơ-ron trong mạng giao tiếp qua các kết nối có độ mạnh khác nhau, tuân theo nguyên tắc "neurons that fire together, wire together". Mỗi nơ-ron có thể ở trạng thái "bật" hoặc "tắt", tương tự như các bit 0 và 1 trong máy tính kỹ thuật số. Cấu trúc này làm cho neural networks trở thành công cụ hoàn hảo cho các bài toán phân loại nhị phân như logistic regression.

1.2. Tại Sao Deep Learning Khác Biệt

Deep Learning khác biệt vì khả năng tự động học các đặc trưng (features) mà không cần can thiệp của con người. Thay vì phải tự tạo ra các "kernels" hoặc "interaction effects", Deep Learning cho phép mạng nơ-ron tự khám phá các mô hình trong dữ liệu. Mỗi lớp (layer) trong mạng học một trừu tượng khác nhau - từ những chi tiết đơn giản đến các biểu diễn phức tạp hơn. Ví dụ, trong phân loại hình ảnh, lớp đầu tiên học các nét vẽ, sau đó học hình dạng, rồi đặc trưng khuôn mặt, cuối cùng là biểu diễn bậc cao của khuôn mặt.

II. Nền Tảng Toán Học Của Neural Networks

Neural Networks hoạt động dựa trên các nguyên tắc toán học và sinh lý học. Tín hiệu được truyền từ nơ-ron này sang nơ-ron khác thông qua "action potential", một sự tăng đột ngột của điện thế dọc theo màng tế bào. Điểm thú vị là nguyên tắc "all or nothing" - tín hiệu hoặc xảy ra hoặc không, không có trạng thái trung gian. Mức độ kết nối giữa hai nơ-ron (trọng số kết nối) quyết định liệu tín hiệu từ nơ-ron này có kích hoạt nơ-ron kia hay không. Những kiến thức này tạo thành cơ sở toán học cho việc huấn luyện neural networks một cách hiệu quả.

2.1. Action Potential và Nguyên Tắc All or Nothing

Action potential là một sự tăng đột ngột của điện thế dọc theo màng tế bào của nơ-ron. Theo nguyên tắc "all or nothing", action potential hoặc xảy ra hoặc không xảy ra, không có trạng thái trung gian nào. Điều này phản ánh trong các neural networks nhân tạo, nơi một nơ-ron có thể ở trạng thái "bật" (1) hoặc "tắt" (0). Khả năng này làm cho neural networks trở thành mô hình hoàn hảo để xử lý các vấn đề quyết định nhị phân và phân loại.

2.2. Trọng Số Kết Nối và Hebb s Learning Rule

Trọng số kết nối giữa các nơ-ron xác định cách tín hiệu được truyền đạt. Nguyên tắc Hebb nói rằng "neurons that fire together, wire together" - các nơ-ron kích hoạt cùng lúc sẽ có kết nối mạnh. Nếu kết nối mạnh, tín hiệu từ nơ-ron này sẽ dễ dàng kích hoạt nơ-ron khác. Ngược lại, kết nối yếu chỉ tạo ra tăng điện thế nhỏ không đủ để gây ra action potential. Đây là cơ sở cho việc điều chỉnh trọng số trong quá trình training mạng.

III. Khởi Động Với Python Công Cụ và Thư Viện Cần Thiết

Để bắt đầu học Deep Learning với Python, bạn chỉ cần những công cụ miễn phí và dễ cài đặt. Python là ngôn ngữ lập trình được chọn lựa vì tính đơn giản và sức mạnh của nó. NumPy là thư viện tính toán số cấp độ cao giúp xử lý các mảng và ma trận một cách hiệu quả. Sau đó, bạn có thể nâng cấp lên Theano hoặc TensorFlow - các thư viện được thiết kế đặc biệt cho Deep Learning, cho phép tăng tốc độ tính toán bằng cách sử dụng GPU. Không cần phải là một thiên tài toán học; chỉ cần kiến thức toán học ở cấp độ đại học là đủ.

3.1. Python và NumPy Nền Tảng Cơ Bản

Python là lựa chọn hoàn hảo cho Deep Learning vì cú pháp đơn giản và cộng đồng hỗ trợ rộng lớn. NumPy cung cấp công cụ mạnh mẽ để làm việc với mảng và ma trận, việc cần thiết cho các tính toán trong neural networks. Với NumPy, bạn có thể thực hiện các phép toán số phức tạp một cách nhanh chóng và hiệu quả. Điều này tạo nên nền tảng vững chắc cho việc học Deep Learning từ những cơ bản nhất.

3.2. Theano và TensorFlow Thư Viện Chuyên Dụng

TheanoTensorFlow là các thư viện được phát triển riêng cho Deep Learning. Chúng cho phép bạn xây dựng các mạng nơ-ron phức tạp và tăng tốc độ bằng cách sử dụng GPU (Graphics Processing Unit). TensorFlow, được phát triển bởi Google, hiện tại là lựa chọn phổ biến nhất. Các thư viện này giúp bạn từ cơ bản đến nâng cao, cho phép xây dựng các ứng dụng Deep Learning thực tế trong các lĩnh vực như nhận dạng hình ảnh và xử lý ngôn ngữ tự nhiên.

IV. Từ Cơ Bản Đến Nâng Cao Lộ Trình Học Tập

Hành trình học Deep Learning nên được bắt đầu từ những khái niệm cơ bản nhất. Bước đầu tiên là hiểu cấu trúc của neural networks và cách output được tạo ra từ input. Tiếp theo, học về backpropagation - thuật toán huấn luyện cơ bản cho neural networks. Sau đó, bạn có thể khám phá các kỹ thuật hiện đại như momentum, adaptive learning rate, và regularization để cải thiện quá trình huấn luyện. Cuối cùng, tiến đến các kiến trúc nâng cao như convolutional neural networks (CNN), recurrent neural networks (RNN), và autoencoders. Bạn sẽ ngạc nhiên nhận ra rằng bạn biết nhiều hơn những gì bạn tưởng tượng.

4.1. Backpropagation và Training Neural Networks

Backpropagation là thuật toán cốt lõi để huấn luyện neural networks. Nó hoạt động bằng cách tính toán đạo hàm của hàm mất mát (loss function) đối với mỗi trọng số, sau đó cập nhật các trọng số để giảm thiểu loss. Quá trình này được lặp lại nhiều lần cho đến khi mạng hội tụ (convergence). Backpropagation là nền tảng cho tất cả các kỹ thuật training hiện đại. Hiểu rõ backpropagation sẽ giúp bạn nắm bắt được bản chất của Deep Learning.

4.2. Kiến Trúc Nâng Cao CNN RNN và Autoencoders

Sau khi nắm vững backpropagation, bạn có thể khám phá các kiến trúc nâng cao. Convolutional Neural Networks (CNN) xuất sắc trong xử lý hình ảnh, Recurrent Neural Networks (RNN)LSTMs tuyệt vời cho dữ liệu tuần tự, còn autoencoders được dùng cho học không giám sát (unsupervised learning). Restricted Boltzmann Machines là một khái niệm quan trọng khác. Mỗi kiến trúc này có ứng dụng riêng và việc hiểu chúng sẽ mở rộng khả năng giải quyết các vấn đề thực tế.

18/12/2025

Trích đoạn nội dung tài liệu

Deep Learning in Python Master Data Science and Machine Learning with Modern Neural Networks written in Python, Theano, and TensorFlow By: The LazyProgrammer (http://lazyprogrammer.me) Introduction Chapter 1: What is a neural network? Chapter 2: Biological analogies Chapter 3: Getting output from a neural network Chapter 4: Training a neural network with backpropagation Chapter 5: Theano Chapter 6: TensorFlow Chapter 7: Improving backpropagation with modern techniques - momentum, adaptive learning rate, and regularization Chapter 8: Unsupervised learning, autoencoders, restricted Boltzmann machines, convolutional neural networks, and LSTMs Chapter 9: You know more than you think you know Conclusion Introduction Deep learning is making waves. At the time of this writing (March 2016), Google’s AlghaGo program just beat 9-dan professional Go player Lee Sedol at the game of Go, a Chinese board game. Experts in the field of Artificial Intelligence thought we were 10 years away from achieving a victory against a top professional Go player, but progress seems to have accelerated! While deep learning is a complex subject, it is not any more difficult to learn than any other machine learning algorithm. I wrote this book to introduce you to the basics of neural networks.

You will get along fine with undergraduate-level math and programming skill. All the materials in this book can be downloaded and installed for free. We will use the Python programming language, along with the numerical computing library Numpy. I will also show you in the later chapters how to build a deep network using Theano and TensorFlow, which are libraries built specifically for deep learning and can accelerate computation by taking advantage of the GPU.

Unlike other machine learning algorithms, deep learning is particularly powerful because it automatically learns features. That means you don’t need to spend your time trying to come up with and test “kernels” or “interaction effects” - something only statisticians love to do. Instead, we will let the neural network learn these things for us. Each layer of the neural network learns a different abstraction than the previous layers.

For example, in image classification, the first layer might learn different strokes, and in the next layer put the strokes together to learn shapes, and in the next layer put the shapes together to form facial features, and in the next layer have a high level representation of faces. Do you want a gentle introduction to this “dark art”, with practical code examples that you can try right away and apply to your own data? Then this book is for you. Chapter 1: What is a neural network? A neural network is called such because at some point in history, computer scientists were trying to model the brain in computer code. The eventual goal is to create an “artificial general intelligence”, which to me means a program that can learn anything you or I can learn.

We are not there yet, so no need to get scared about the machines taking over humanity. Currently neural networks are very good at performing singular tasks, like classifying images and speech. Unlike the brain, these artificial neural networks have a very strict predefined structure. The brain is made up of neurons that talk to each other via electrical and chemical signals (hence the term, neural network).

We do not differentiate between these 2 types of signals in artificial neural networks, so from now on we will just say “a” signal is being passed from one neuron to another. Signals are passed from one neuron to another via what is called an “action potential”. It is a spike in electricity along the cell membrane of a neuron. The interesting thing about action potentials is that either they happen, or they don’t.

There is no “in between”. This is called the “all or nothing” principle. Below is a plot of the action potential vs. time, with real, physical units.

These connections between neurons have strengths. You may have heard the phrase, “neurons that fire together, wire together”, which is attributed to the Canadian neuropsychologist Donald Hebb. Neurons with strong connections will be turned “on” by each other. So if one neuron sends a signal (action potential) to another neuron, and their connection is strong, then the next neuron will also have an action potential, would could then be passed on to other neurons, etc.

If the connection between 2 neurons is weak, then one neuron sending a signal to another neuron might cause a small increase in electrical potential at the 2nd neuron, but not enough to cause another action potential. Thus we can think of a neuron being “on” or “off”. it has an action potential, or it doesn’t) What does this remind you of? If you said “digital computers”, then you would be right! Specifically, neurons are the perfect model for a yes no, true false, 0 / 1 type of problem. We call this “binary classification” and the machine learning analogy would be the “logistic regression” algorithm.

The above image is a pictorial representation of the logistic regression model. It takes as inputs x1, x2, and x3, which you can imagine as the outputs of other neurons or some other input signal (i. the visual receptors in your eyes or the mechanical receptors in your fingertips), and outputs another signal which is a combination of these inputs, weighted by the strength of those input neurons to this output neuron. Because we’re going to have to eventually deal with actual numbers and formulas, let’s look at how we can calculate y from x.

y = sigmoid(w1*x1 + w2*x2 + w3*x3) Note that in this book, we will ignore the bias term, since it can easily be included in the given formula by adding an extra dimension x0 which is always equal to 1. So each input neuron gets multiplied by its corresponding weight (synaptic strength) and added to all the others. We then apply a “sigmoid” function on top of that to get the output y. The sigmoid is defined as: sigmoid(x) = 1 / (1 + exp(-x)) If you were to plot the sigmoid, you would get this: You can see that the output of a sigmoid is always between 0 and 1.

It has 2 asymptotes, so that the output is exactly 1 when the input is + infinity, and the output is exactly 0 when the input is - infinity. The output is 0.5 when the input is 0. You can interpret the output as a probability. In particular, we interpret it as the probability: P(Y=1 | X) Which can be read as “the probability that Y is equal to 1 given X”.

We usually just use this and “y” by itself interchangeably. They are both “the output” of the neuron. To get a neural network, we simply combine neurons together. The way we do this with artificial neural networks is very specific.

We connect them in a feedforward fashion. I have highlighted in red one logistic unit. Its inputs are (x1, x2) and its output is z1. See if you can find the other 2 logistic units in this picture.

We call the layer of z’s the “hidden layer”. Neural networks have one or more hidden layers. A neural network with more hidden layers would be called “deeper”. “Deep learning” is somewhat of a buzzword.

I have googled around about this topic, and it seems that the general consensus is that any neural network with one or more hidden layers is considered “deep”. Exercise Using the logistic unit as a building block, how would you calculate the output of a neural network Y? If you can’t get it now, don’t worry, we’ll cover it in Chapter 3. Chapter 2: Biological analogies I described in the previous chapter how an artificial neural network is analogous to a brain physically, but what about with respect to learning and other “high level” attributes? Excitability Threshold The output of a logistic unit must be between 0 and 1. In a classifier, we must choose which class to predict (say, is this is a picture of a cat or a dog?) If 1 = cat and 0 = dog, and the output is 0.7, what do we say? Cat! Why? Because our model is saying, “the probability that this is an image of a cat is 70%”.

The 50% line acts as the “excitability threshold” of a neuron, i. the threshold at which an action potential would be generated. Excitatory and Inhibitory Connections Neurons have the ability when sending signals to other neurons, to send an “excitatory” or “inhibitory” signal. As you might have guessed, excitatory connections produce action potentials, while inhibitory connections inhibit action potentials.

These are like the weights of a logistic regression unit. A very positive weight would be a very excitatory connection. A very negative weight would be a very inhibitory connection. Repetition and Familiarity “Practice makes perfect” people often say.

When you practice something over and over again, you become better at it. Neural networks are the same way. If you train a neural network on the same or similar examples again and again, it gets better at classifying those examples. Your mind, by practicing a task, is lowering its internal error curve for that particular task.

You will see how this is implemented in code when we talk about backpropagation, the training algorithm for a neural network. Essentially what we are going to do is do a for-loop a number of times, looking at the same samples again and again, doing backpropagation on them each time. Exercise In preparation for the next chapter, you’ll need to make sure you have the following installed on your machine: Python, Numpy, and optionally Pandas. Chapter 3: Getting output from a neural network Get some data to work with Assuming you don’t yet have any data to work with, you’ll need some to do the examples in this book.com is a great resource for this.

I would recommend the MNIST dataset. If you want to do binary classification you’ll have to choose another dataset. The data you’ll use for any machine learning problem often has the same format. We have some inputs X and some labels or targets Y.

Each sample (pair of x and y) is represented as a vector of real numbers for x and a categorical variable (often just 0, 1, 2, …) for y. You put all the sample inputs together to form a matrix X. Each input vector is a row. So that means each column is a different input feature.

Thus X is an N x D matrix, where N = number of samples and D = the dimensionality of each input. For MNIST, D = 784 = 28 x 28, because the original images, which are 28 x 28 matrices, are “flattened” into 1 x 784 vectors. If y is not a binary variable (0 or 1), you can turn it into a matrix of indicator variables, which will be needed later when we are doing softmax. So for the MNIST example you would transform Y into an indicator matrix (a matrix of 0s and 1s) where Y_indicator is an N x K matrix, where again N = number of samples and K = number of classes in the output.

For MNIST of course K = 10. Here is an example of how you could do this in Numpy: def y2indicator(y): N = len(y) ind = np.zeros((N, 10)) for i in xrange(N): ind[i, y[i]] = 1 return ind In this book, I will assume you already know how to load a CSV into a Numpy array or Pandas dataframe and do basic operations like multiplying and adding Numpy arrays. Architecture of an artificial neural network Unlike biological neural networks, where any one neuron can be connected to any other neuron, artificial neural networks have a very specific structure. In particular, they are composed of layers.

Each layer feeds into the next layer. There are no “feedback” connections.

Nội dung được bảo vệ bản quyền — Tải xuống đầy đủ