Javascript is my "native language", use it to learn artificial intelligence, and remember it more firmly.
Is the threshold for learning artificial intelligence high? It took me 2 months to get started. I learned python and calculus, and then I roughly understood the basic principles of deep learning. It was really daunting, but now I look back and think about it, in fact, the basic principles are not so difficult.
1. Basic principles
This section does not require any foundation, and every engineer should be able to understand it.
After my digestion, the expressed artificial intelligence process is: there is some data, assuming that these data follow a certain law, but the specific parameters in this law are unknown, let the computer guess the value of these parameters many times, and from the Find the set of parameters with the smallest error, and you can make predictions on new data .
For example, there is a set of data of height x and age y:
arr = [
{x: 10, y: 131},
{x: 11, y: 135},
{x: 12, y: 139},
{x: 13, y: 145},
]
The older the age, the higher the height, so let's assume that the rules of height and age are:
y = kx + b
How much is k and b? Computers are scumbags. After reading the formulas and data, they are confused, so let's make a guess.
k = 10
b = 15
则
10*10 + 15 = 115
10*11 + 15 = 125
10*12 + 15 = 135
10*13 + 15 = 145
Look at the difference between this guess and the actual value. We learned an evaluation method in middle school called variance. Here we use a similar method to evaluate loss:
let yArr = [131, 135, 139, 145]
let zArr = [115, 125, 135, 145]
let loss = 0
yArr.forEach((y, index) => {
let z = zArr[index]
loss += Math.pow(z - y, 2) // (z - y)²
})
loss = loss / data.length
// loss = 93
The loss is estimated to be 93, whether it is too big or too small, and the computer is not very clear, so the computer needs to make more guesses, and take the k and b with the smallest loss from all the guesses as the final value.
The complete implementation in code is:
model = {
xArr: [10, 11, 12, 13],
yArr: [131, 135, 139, 145],
k: 10, // 参数1
b: 15, // 参数2
// 损失函数:评估误差用的
getLoss (k, b) {
let zArr = this.xArr.map(x => this.getY(x, k, b))
let loss = 0
this.yArr.forEach((y, index) => {
let z = zArr[index]
loss += Math.pow(y - z, 2) // (y - z)²
})
loss = loss / this.yArr.length
return loss
},
// 规律的公式
getY(x, k, b) {
return k * x + b
},
// 训练:让电脑瞎猜,实际开发中不是像以下代码这样猜的,但是虽然手段不同,原理却是一样的
learn () {
// 让k和b分别取1到100
let kArr = Array.from(Array(100), (v,i) => i + 1)
let bArr = Array.from(Array(100), (v,i) => i + 1)
let lossArr = []
kArr.forEach(k => {
bArr.forEach(b => {
lossArr.push({
loss: this.getLoss(k, b), // 通通计算一遍,算出loss
k: k,
b: b
})
})
})
let result = {loss: Infinity}
lossArr.forEach(d => {
if (d.loss < result.loss) {
result = d // 找出最小的loss
}
})
console.log(result.loss)
this.k = result.k
this.b = result.b
}
}
model.learn()
console.log(model.k, model.b)
console.log(model.yArr)
console.log(model.xArr.map(x => model.getY(x, model.k, model.b)))
Finally, get
k = 5
b = 80
yArr = [131, 135, 139, 145]
zArr = [130, 135, 140, 145]
Let's try to predict the height of a 14-year-old
y = kx + b
y = 5 * 14 + 80
y = 150
150cm is very reasonable, this is artificial intelligence, with the knowledge learned from here, let's develop face recognition, hehe🤪
I've seen it all here. Would you like to give me a like? ? ?
2. Optimize the guessing algorithm
In this section, you need to understand mathematics in advance, such as derivatives, partial derivatives, product of vectors, etc.
In the previous section, we guessed violently and guessed k and b one by one in the range of 1~100, but it is possible that the optimal solution is not in the range of 1~100, so let's optimize the guessing algorithm.
First, review the formula:
\(y = kx + b\)
\( L = \sum_{i=1}^N (y_{i} - z_{i})^2 \)
\( y_{i} \) is the actual value, \( z_{i} \) is the value guessed by the computer, N is the length of yArr, here we don't use the code to express the loss, but use the mathematical formula to express, L = loss , Students who don't understand it go to study first and then come back and read it again.
To see the essence of the problem clearly, we can better optimize the algorithm. We need to understand the relationship between k, b, and L. We draw the values of k, b and L into a graph:
As can be seen from the figure, L is a surface, and we require the lowest point of this surface. If a glass bead is placed on the surface at this time, then the glass bead will roll back toward the lowest point. Using the principle of this phenomenon to optimize the algorithm, You can let the computer find the minimum value of L in the shortest time.
The algorithm for the glass beads to automatically move to the lowest point is called the gradient descent method . At a certain point on the surface of L, find the partial derivatives of k and b. The product of the partial derivatives can calculate the direction of gradient descent. The derivation process is as follows.
\( \frac{\mathrm{d} L}{\mathrm{d} k} = \frac{\mathrm{d} L}{\mathrm{d} z} \frac{\mathrm{d} z}{ \mathrm{d} k} \)
First find \( \frac{\mathrm{d} L}{\mathrm{d} z} \)
\( L = \sum_{i=1}^N (y_{i} - z_{i})^2 \)
\( \frac{\mathrm{d} L}{\mathrm{d} z} = \sum_{i=1}^N (2y_{i} - 2z_{i}) \)
\( \frac{\mathrm{d} L}{\mathrm{d} z} = \sum_{i=1}^N (2y_{i} - 2kx_{i} - 2b) \)
Then find \( \frac{\mathrm{d} z}{\mathrm{d} k} \)
\( z_{i} = kx_{i} + b \)
\( \frac{\mathrm{d} z}{\mathrm{d} k} = x_{i} \)
To be continued , click on the collection to see later
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。