我是靠谱客的博主 野性小鸽子,最近开发中收集的这篇文章主要介绍1.穷举线性回归1 Prepare the train set.2 Define the model3 Define the loss function4 come to train5 come to plot,觉得挺不错的,现在分享给大家,希望可以做个参考。
概述
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
1 Prepare the train set.
# 数据集
x_data = [1.0, 2.0, 3.0]
y_data = [2.0, 4.0, 6.0]
2 Define the model
# 正向传播
def forward(x):
return x * w + b
3 Define the loss function
# 计算损失
def loss(x, y):
y_pred = forward(x)
return (y - y_pred) * (y - y_pred)
4 come to train
# 构造二维网络(使用穷举法)
W = np.arange(0.0, 4.1, 0.1)
B = np.arange(-1.0, 1.1, 0.1)
# 利用W和B穷举生成——网格矩阵
(w, b) = np.meshgrid(W, B)
mse_list = []
# 开始训练
l_sum = 0
for x_val, y_val in zip(x_data, y_data):
y_pred_val = forward(x_val)
# print(y_pred_val)
loss_val = loss(x_val, y_val)
# print(loss_val)
l_sum += loss_val
print(l_sum)
[[9.00000000e+00 8.41000000e+00 7.84000000e+00 7.29000000e+00
6.76000000e+00 6.25000000e+00 5.76000000e+00 5.29000000e+00
4.84000000e+00 4.41000000e+00 4.00000000e+00 3.61000000e+00
3.24000000e+00 2.89000000e+00 2.56000000e+00 2.25000000e+00
1.96000000e+00 1.69000000e+00 1.44000000e+00 1.21000000e+00
1.00000000e+00 8.10000000e-01 6.40000000e-01 4.90000000e-01
3.60000000e-01 2.50000000e-01 1.60000000e-01 9.00000000e-02
4.00000000e-02 1.00000000e-02 0.00000000e+00 1.00000000e-02
4.00000000e-02 9.00000000e-02 1.60000000e-01 2.50000000e-01
3.60000000e-01 4.90000000e-01 6.40000000e-01 8.10000000e-01
1.00000000e+00]
[8.41000000e+00 7.84000000e+00 7.29000000e+00 6.76000000e+00
6.25000000e+00 5.76000000e+00 5.29000000e+00 4.84000000e+00
4.41000000e+00 4.00000000e+00 3.61000000e+00 3.24000000e+00
2.89000000e+00 2.56000000e+00 2.25000000e+00 1.96000000e+00
1.69000000e+00 1.44000000e+00 1.21000000e+00 1.00000000e+00
8.10000000e-01 6.40000000e-01 4.90000000e-01 3.60000000e-01
2.50000000e-01 1.60000000e-01 9.00000000e-02 4.00000000e-02
1.00000000e-02 1.97215226e-31 1.00000000e-02 4.00000000e-02
9.00000000e-02 1.60000000e-01 2.50000000e-01 3.60000000e-01
4.90000000e-01 6.40000000e-01 8.10000000e-01 1.00000000e+00
1.21000000e+00]
[7.84000000e+00 7.29000000e+00 6.76000000e+00 6.25000000e+00
5.76000000e+00 5.29000000e+00 4.84000000e+00 4.41000000e+00
4.00000000e+00 3.61000000e+00 3.24000000e+00 2.89000000e+00
2.56000000e+00 2.25000000e+00 1.96000000e+00 1.69000000e+00
1.44000000e+00 1.21000000e+00 1.00000000e+00 8.10000000e-01
6.40000000e-01 4.90000000e-01 3.60000000e-01 2.50000000e-01
1.60000000e-01 9.00000000e-02 4.00000000e-02 1.00000000e-02
0.00000000e+00 1.00000000e-02 4.00000000e-02 9.00000000e-02
1.60000000e-01 2.50000000e-01 3.60000000e-01 4.90000000e-01
6.40000000e-01 8.10000000e-01 1.00000000e+00 1.21000000e+00
1.44000000e+00]
[7.29000000e+00 6.76000000e+00 6.25000000e+00 5.76000000e+00
5.29000000e+00 4.84000000e+00 4.41000000e+00 4.00000000e+00
3.61000000e+00 3.24000000e+00 2.89000000e+00 2.56000000e+00
2.25000000e+00 1.96000000e+00 1.69000000e+00 1.44000000e+00
1.21000000e+00 1.00000000e+00 8.10000000e-01 6.40000000e-01
4.90000000e-01 3.60000000e-01 2.50000000e-01 1.60000000e-01
9.00000000e-02 4.00000000e-02 1.00000000e-02 0.00000000e+00
1.00000000e-02 4.00000000e-02 9.00000000e-02 1.60000000e-01
2.50000000e-01 3.60000000e-01 4.90000000e-01 6.40000000e-01
8.10000000e-01 1.00000000e+00 1.21000000e+00 1.44000000e+00
1.69000000e+00]
[6.76000000e+00 6.25000000e+00 5.76000000e+00 5.29000000e+00
4.84000000e+00 4.41000000e+00 4.00000000e+00 3.61000000e+00
3.24000000e+00 2.89000000e+00 2.56000000e+00 2.25000000e+00
1.96000000e+00 1.69000000e+00 1.44000000e+00 1.21000000e+00
1.00000000e+00 8.10000000e-01 6.40000000e-01 4.90000000e-01
3.60000000e-01 2.50000000e-01 1.60000000e-01 9.00000000e-02
4.00000000e-02 1.00000000e-02 0.00000000e+00 1.00000000e-02
4.00000000e-02 9.00000000e-02 1.60000000e-01 2.50000000e-01
3.60000000e-01 4.90000000e-01 6.40000000e-01 8.10000000e-01
1.00000000e+00 1.21000000e+00 1.44000000e+00 1.69000000e+00
1.96000000e+00]
[6.25000000e+00 5.76000000e+00 5.29000000e+00 4.84000000e+00
4.41000000e+00 4.00000000e+00 3.61000000e+00 3.24000000e+00
2.89000000e+00 2.56000000e+00 2.25000000e+00 1.96000000e+00
1.69000000e+00 1.44000000e+00 1.21000000e+00 1.00000000e+00
8.10000000e-01 6.40000000e-01 4.90000000e-01 3.60000000e-01
2.50000000e-01 1.60000000e-01 9.00000000e-02 4.00000000e-02
1.00000000e-02 0.00000000e+00 1.00000000e-02 4.00000000e-02
9.00000000e-02 1.60000000e-01 2.50000000e-01 3.60000000e-01
4.90000000e-01 6.40000000e-01 8.10000000e-01 1.00000000e+00
1.21000000e+00 1.44000000e+00 1.69000000e+00 1.96000000e+00
2.25000000e+00]
[5.76000000e+00 5.29000000e+00 4.84000000e+00 4.41000000e+00
4.00000000e+00 3.61000000e+00 3.24000000e+00 2.89000000e+00
2.56000000e+00 2.25000000e+00 1.96000000e+00 1.69000000e+00
1.44000000e+00 1.21000000e+00 1.00000000e+00 8.10000000e-01
6.40000000e-01 4.90000000e-01 3.60000000e-01 2.50000000e-01
1.60000000e-01 9.00000000e-02 4.00000000e-02 1.00000000e-02
0.00000000e+00 1.00000000e-02 4.00000000e-02 9.00000000e-02
1.60000000e-01 2.50000000e-01 3.60000000e-01 4.90000000e-01
6.40000000e-01 8.10000000e-01 1.00000000e+00 1.21000000e+00
1.44000000e+00 1.69000000e+00 1.96000000e+00 2.25000000e+00
2.56000000e+00]
[5.29000000e+00 4.84000000e+00 4.41000000e+00 4.00000000e+00
3.61000000e+00 3.24000000e+00 2.89000000e+00 2.56000000e+00
2.25000000e+00 1.96000000e+00 1.69000000e+00 1.44000000e+00
1.21000000e+00 1.00000000e+00 8.10000000e-01 6.40000000e-01
4.90000000e-01 3.60000000e-01 2.50000000e-01 1.60000000e-01
9.00000000e-02 4.00000000e-02 1.00000000e-02 0.00000000e+00
1.00000000e-02 4.00000000e-02 9.00000000e-02 1.60000000e-01
2.50000000e-01 3.60000000e-01 4.90000000e-01 6.40000000e-01
8.10000000e-01 1.00000000e+00 1.21000000e+00 1.44000000e+00
1.69000000e+00 1.96000000e+00 2.25000000e+00 2.56000000e+00
2.89000000e+00]
[4.84000000e+00 4.41000000e+00 4.00000000e+00 3.61000000e+00
3.24000000e+00 2.89000000e+00 2.56000000e+00 2.25000000e+00
1.96000000e+00 1.69000000e+00 1.44000000e+00 1.21000000e+00
1.00000000e+00 8.10000000e-01 6.40000000e-01 4.90000000e-01
3.60000000e-01 2.50000000e-01 1.60000000e-01 9.00000000e-02
4.00000000e-02 1.00000000e-02 0.00000000e+00 1.00000000e-02
4.00000000e-02 9.00000000e-02 1.60000000e-01 2.50000000e-01
3.60000000e-01 4.90000000e-01 6.40000000e-01 8.10000000e-01
1.00000000e+00 1.21000000e+00 1.44000000e+00 1.69000000e+00
1.96000000e+00 2.25000000e+00 2.56000000e+00 2.89000000e+00
3.24000000e+00]
[4.41000000e+00 4.00000000e+00 3.61000000e+00 3.24000000e+00
2.89000000e+00 2.56000000e+00 2.25000000e+00 1.96000000e+00
1.69000000e+00 1.44000000e+00 1.21000000e+00 1.00000000e+00
8.10000000e-01 6.40000000e-01 4.90000000e-01 3.60000000e-01
2.50000000e-01 1.60000000e-01 9.00000000e-02 4.00000000e-02
1.00000000e-02 0.00000000e+00 1.00000000e-02 4.00000000e-02
9.00000000e-02 1.60000000e-01 2.50000000e-01 3.60000000e-01
4.90000000e-01 6.40000000e-01 8.10000000e-01 1.00000000e+00
1.21000000e+00 1.44000000e+00 1.69000000e+00 1.96000000e+00
2.25000000e+00 2.56000000e+00 2.89000000e+00 3.24000000e+00
3.61000000e+00]
[4.00000000e+00 3.61000000e+00 3.24000000e+00 2.89000000e+00
2.56000000e+00 2.25000000e+00 1.96000000e+00 1.69000000e+00
1.44000000e+00 1.21000000e+00 1.00000000e+00 8.10000000e-01
6.40000000e-01 4.90000000e-01 3.60000000e-01 2.50000000e-01
1.60000000e-01 9.00000000e-02 4.00000000e-02 1.00000000e-02
4.93038066e-32 1.00000000e-02 4.00000000e-02 9.00000000e-02
1.60000000e-01 2.50000000e-01 3.60000000e-01 4.90000000e-01
6.40000000e-01 8.10000000e-01 1.00000000e+00 1.21000000e+00
1.44000000e+00 1.69000000e+00 1.96000000e+00 2.25000000e+00
2.56000000e+00 2.89000000e+00 3.24000000e+00 3.61000000e+00
4.00000000e+00]
[3.61000000e+00 3.24000000e+00 2.89000000e+00 2.56000000e+00
2.25000000e+00 1.96000000e+00 1.69000000e+00 1.44000000e+00
1.21000000e+00 1.00000000e+00 8.10000000e-01 6.40000000e-01
4.90000000e-01 3.60000000e-01 2.50000000e-01 1.60000000e-01
9.00000000e-02 4.00000000e-02 1.00000000e-02 4.93038066e-32
1.00000000e-02 4.00000000e-02 9.00000000e-02 1.60000000e-01
2.50000000e-01 3.60000000e-01 4.90000000e-01 6.40000000e-01
8.10000000e-01 1.00000000e+00 1.21000000e+00 1.44000000e+00
1.69000000e+00 1.96000000e+00 2.25000000e+00 2.56000000e+00
2.89000000e+00 3.24000000e+00 3.61000000e+00 4.00000000e+00
4.41000000e+00]
[3.24000000e+00 2.89000000e+00 2.56000000e+00 2.25000000e+00
1.96000000e+00 1.69000000e+00 1.44000000e+00 1.21000000e+00
1.00000000e+00 8.10000000e-01 6.40000000e-01 4.90000000e-01
3.60000000e-01 2.50000000e-01 1.60000000e-01 9.00000000e-02
4.00000000e-02 1.00000000e-02 4.93038066e-32 1.00000000e-02
4.00000000e-02 9.00000000e-02 1.60000000e-01 2.50000000e-01
3.60000000e-01 4.90000000e-01 6.40000000e-01 8.10000000e-01
1.00000000e+00 1.21000000e+00 1.44000000e+00 1.69000000e+00
1.96000000e+00 2.25000000e+00 2.56000000e+00 2.89000000e+00
3.24000000e+00 3.61000000e+00 4.00000000e+00 4.41000000e+00
4.84000000e+00]
[2.89000000e+00 2.56000000e+00 2.25000000e+00 1.96000000e+00
1.69000000e+00 1.44000000e+00 1.21000000e+00 1.00000000e+00
8.10000000e-01 6.40000000e-01 4.90000000e-01 3.60000000e-01
2.50000000e-01 1.60000000e-01 9.00000000e-02 4.00000000e-02
1.00000000e-02 0.00000000e+00 1.00000000e-02 4.00000000e-02
9.00000000e-02 1.60000000e-01 2.50000000e-01 3.60000000e-01
4.90000000e-01 6.40000000e-01 8.10000000e-01 1.00000000e+00
1.21000000e+00 1.44000000e+00 1.69000000e+00 1.96000000e+00
2.25000000e+00 2.56000000e+00 2.89000000e+00 3.24000000e+00
3.61000000e+00 4.00000000e+00 4.41000000e+00 4.84000000e+00
5.29000000e+00]
[2.56000000e+00 2.25000000e+00 1.96000000e+00 1.69000000e+00
1.44000000e+00 1.21000000e+00 1.00000000e+00 8.10000000e-01
6.40000000e-01 4.90000000e-01 3.60000000e-01 2.50000000e-01
1.60000000e-01 9.00000000e-02 4.00000000e-02 1.00000000e-02
4.93038066e-32 1.00000000e-02 4.00000000e-02 9.00000000e-02
1.60000000e-01 2.50000000e-01 3.60000000e-01 4.90000000e-01
6.40000000e-01 8.10000000e-01 1.00000000e+00 1.21000000e+00
1.44000000e+00 1.69000000e+00 1.96000000e+00 2.25000000e+00
2.56000000e+00 2.89000000e+00 3.24000000e+00 3.61000000e+00
4.00000000e+00 4.41000000e+00 4.84000000e+00 5.29000000e+00
5.76000000e+00]
[2.25000000e+00 1.96000000e+00 1.69000000e+00 1.44000000e+00
1.21000000e+00 1.00000000e+00 8.10000000e-01 6.40000000e-01
4.90000000e-01 3.60000000e-01 2.50000000e-01 1.60000000e-01
9.00000000e-02 4.00000000e-02 1.00000000e-02 1.97215226e-31
1.00000000e-02 4.00000000e-02 9.00000000e-02 1.60000000e-01
2.50000000e-01 3.60000000e-01 4.90000000e-01 6.40000000e-01
8.10000000e-01 1.00000000e+00 1.21000000e+00 1.44000000e+00
1.69000000e+00 1.96000000e+00 2.25000000e+00 2.56000000e+00
2.89000000e+00 3.24000000e+00 3.61000000e+00 4.00000000e+00
4.41000000e+00 4.84000000e+00 5.29000000e+00 5.76000000e+00
6.25000000e+00]
[1.96000000e+00 1.69000000e+00 1.44000000e+00 1.21000000e+00
1.00000000e+00 8.10000000e-01 6.40000000e-01 4.90000000e-01
3.60000000e-01 2.50000000e-01 1.60000000e-01 9.00000000e-02
4.00000000e-02 1.00000000e-02 4.93038066e-32 1.00000000e-02
4.00000000e-02 9.00000000e-02 1.60000000e-01 2.50000000e-01
3.60000000e-01 4.90000000e-01 6.40000000e-01 8.10000000e-01
1.00000000e+00 1.21000000e+00 1.44000000e+00 1.69000000e+00
1.96000000e+00 2.25000000e+00 2.56000000e+00 2.89000000e+00
3.24000000e+00 3.61000000e+00 4.00000000e+00 4.41000000e+00
4.84000000e+00 5.29000000e+00 5.76000000e+00 6.25000000e+00
6.76000000e+00]
[1.69000000e+00 1.44000000e+00 1.21000000e+00 1.00000000e+00
8.10000000e-01 6.40000000e-01 4.90000000e-01 3.60000000e-01
2.50000000e-01 1.60000000e-01 9.00000000e-02 4.00000000e-02
1.00000000e-02 4.93038066e-32 1.00000000e-02 4.00000000e-02
9.00000000e-02 1.60000000e-01 2.50000000e-01 3.60000000e-01
4.90000000e-01 6.40000000e-01 8.10000000e-01 1.00000000e+00
1.21000000e+00 1.44000000e+00 1.69000000e+00 1.96000000e+00
2.25000000e+00 2.56000000e+00 2.89000000e+00 3.24000000e+00
3.61000000e+00 4.00000000e+00 4.41000000e+00 4.84000000e+00
5.29000000e+00 5.76000000e+00 6.25000000e+00 6.76000000e+00
7.29000000e+00]
[1.44000000e+00 1.21000000e+00 1.00000000e+00 8.10000000e-01
6.40000000e-01 4.90000000e-01 3.60000000e-01 2.50000000e-01
1.60000000e-01 9.00000000e-02 4.00000000e-02 1.00000000e-02
4.93038066e-32 1.00000000e-02 4.00000000e-02 9.00000000e-02
1.60000000e-01 2.50000000e-01 3.60000000e-01 4.90000000e-01
6.40000000e-01 8.10000000e-01 1.00000000e+00 1.21000000e+00
1.44000000e+00 1.69000000e+00 1.96000000e+00 2.25000000e+00
2.56000000e+00 2.89000000e+00 3.24000000e+00 3.61000000e+00
4.00000000e+00 4.41000000e+00 4.84000000e+00 5.29000000e+00
5.76000000e+00 6.25000000e+00 6.76000000e+00 7.29000000e+00
7.84000000e+00]
[1.21000000e+00 1.00000000e+00 8.10000000e-01 6.40000000e-01
4.90000000e-01 3.60000000e-01 2.50000000e-01 1.60000000e-01
9.00000000e-02 4.00000000e-02 1.00000000e-02 1.97215226e-31
1.00000000e-02 4.00000000e-02 9.00000000e-02 1.60000000e-01
2.50000000e-01 3.60000000e-01 4.90000000e-01 6.40000000e-01
8.10000000e-01 1.00000000e+00 1.21000000e+00 1.44000000e+00
1.69000000e+00 1.96000000e+00 2.25000000e+00 2.56000000e+00
2.89000000e+00 3.24000000e+00 3.61000000e+00 4.00000000e+00
4.41000000e+00 4.84000000e+00 5.29000000e+00 5.76000000e+00
6.25000000e+00 6.76000000e+00 7.29000000e+00 7.84000000e+00
8.41000000e+00]
[1.00000000e+00 8.10000000e-01 6.40000000e-01 4.90000000e-01
3.60000000e-01 2.50000000e-01 1.60000000e-01 9.00000000e-02
4.00000000e-02 1.00000000e-02 1.97215226e-31 1.00000000e-02
4.00000000e-02 9.00000000e-02 1.60000000e-01 2.50000000e-01
3.60000000e-01 4.90000000e-01 6.40000000e-01 8.10000000e-01
1.00000000e+00 1.21000000e+00 1.44000000e+00 1.69000000e+00
1.96000000e+00 2.25000000e+00 2.56000000e+00 2.89000000e+00
3.24000000e+00 3.61000000e+00 4.00000000e+00 4.41000000e+00
4.84000000e+00 5.29000000e+00 5.76000000e+00 6.25000000e+00
6.76000000e+00 7.29000000e+00 7.84000000e+00 8.41000000e+00
9.00000000e+00]]
5 come to plot
# 绘制3D曲面图
fig = plt.figure()
ax = Axes3D(fig)
ax.plot_surface(w, b, l_sum/3)
plt.show()
最后
以上就是野性小鸽子为你收集整理的1.穷举线性回归1 Prepare the train set.2 Define the model3 Define the loss function4 come to train5 come to plot的全部内容,希望文章能够帮你解决1.穷举线性回归1 Prepare the train set.2 Define the model3 Define the loss function4 come to train5 come to plot所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复