原理:
通过梯度下降算法,求解loss的最小值,设置好初始w权重值,不断进行迭代,最后得到最小的loss值
代码:
# Training Data
x_data = [1.0, 2.0, 3.0]
y_data = [2.0, 4.0, 6.0]
w = 1.0 # a random guess: random value
# our model forward pass
def forward(x):
return x * w
# Loss function
def loss(x, y):
y_pred = forward(x)
return (y_pred - y) * (y_pred - y)
# compute gradient
def gradient(x, y): # d_loss/d_w
return 2 * x * (x * w - y)
# Before training
print("Prediction (before training)", 4, forward(4))
# Training loop
for epoch in range(10):
for x_val, y_val in zip(x_data, y_data):
# Compute derivative w.r.t to the learned weights
# Update the weights
# Compute the loss and print progress
grad = gradient(x_val, y_val)
w = w - 0.01 * grad
print("\tgrad: ", x_val, y_val, round(grad, 2))
l = loss(x_val, y_val)
print("progress:", epoch, "w=", round(w, 2), "loss=", round(l, 2))
# After training
print("Predicted score (after training)", "4 hours of studying: ", forward(4))
知识点:
a为学习率,设置为0.01
迭代过程:
运行结果:
大概比起生活的艰辛,我更怕的是,好不容易来这世间走一趟,却把唯一一次的生命,按照别人的意愿,活成了别人的人生。