浅说遍历学习和批次学习(含代码)

浅说遍历学习和批次学习(含代码)_第1张图片

 代码:

import matplotlib.pyplot as plt
import numpy as np
# 这两行代码解决 plt 中文显示的问题
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
#数据加载
x = np.array([0.0,1.0,2.0,3.0,4.0]);
t = [0.0,0.35,0.76,1.15,1.6];
#参数设定
w_0 = 0.5; #初始权值
beta = 0.1; #学习率
w = w_0;
Eb_2 = [0,0,0,0,0];
Ep_2 = [0,0,0,0,0];
#遍历学习
print("遍历学习")
for i in range(5):
    x_b = x[i];
    t_b = t[i];
    y = w * x_b;
    E = t_b - y;
    Eb_2[i] = E*E;
    w = w + beta * E * x_b;
    print("w"+str(i)+"="+"{}".format(w))
    plt.figure(1)
    m = [231, 232, 233, 234, 235, 236]
    plt.subplot(m[i])
    plt.plot(x,t,'ro') #离散的点
    hanshu = w * x;
    plt.plot(x,hanshu)
MSE = sum(Eb_2) / 2 / 4;
print("MSE=",MSE)
#批次学习
print("批次学习")
k = [0,0,0,0,0];
for h in range(5):
    for j in range(5):
        x_p = x[j];
        t_p = t[j];
        y = w * x_p;
        E = t_p - y;
        Ep_2 = E *E;
        k[j] = E * x_p;
    sum_k = sum(k)
    w_p = beta * sum_k / 4
    w_p = w +w_p
    print("w"+str(h)+"="+"{}".format(w_p))
    w = w_p
    hanshu = w * x;
    plt.figure(2)
    m = [231, 232, 233, 234, 235, 236]
    plt.subplot(m[h])
    plt.plot(x, t, 'ro')
    plt.plot(x, hanshu)
MSE = Ep_2 / 2 / 4;
print("MSE=",MSE)
plt.show()

 结果:

遍历学习
w0=0.5
w1=0.485
w2=0.443
w3=0.3893
w4=0.40642000000000006
MSE= 0.012559105000000003
批次学习
w0=0.394605
w1=0.39165125
w2=0.3909128125
w3=0.390728203125
w4=0.39068205078125
MSE= 0.0001719324345825201

遍历学习:

浅说遍历学习和批次学习(含代码)_第2张图片

批次学习:

浅说遍历学习和批次学习(含代码)_第3张图片

总结:

总而言之,批次更好。。。。

你可能感兴趣的:(python,机器学习)