梯度下降、反向传播

日萌社

人工智能AI:Keras PyTorch MXNet TensorFlow PaddlePaddle 深度学习实战(不定时更新)


nltk 处理文本

注意力机制 SENet、CBAM

卷积神经网络 处理文本:word2vec、TF-IDF、TextRank、字符卷积、词卷积、卷积神经网络文本分类模型的实现(Conv1D一维卷积、Conv2D二维卷积)

反向传播、链式求导

梯度下降

最小二乘法(LS算法):实际为L2范数的一个具体应用(计算残差平方和)

线性回归 例子


【程序3-2】
x = [(2, 0, 3), (1, 0, 3), (1, 1, 3), (1,4, 2), (1, 2, 4)]
y = [5, 6, 8, 10, 11]
epsilon = 0.002 #噪音值
alpha = 0.02 #学习率
diff = [0, 0]
max_itor = 1000
error0 = 0
error1 = 0
cnt = 0
m = len(x)
theta0 = 0 #θ权重参数
theta1 = 0
theta2 = 0

while True:
    cnt += 1
    for i in range(m):
        diff[0] = (theta0 * x[i][0] + theta1 * x[i][1] + theta2 * x[i][2]) - y[i]
        theta0 -= alpha * diff[0] * x[i][0]
        theta1 -= alpha * diff[0] * x[i][1]
        theta2 -= alpha * diff[0] * x[i][2]
    error1 = 0
    for lp in range(len(x)):
        error1 += (y[lp] - (theta0 * x[lp][0] + theta1 * x[lp][1] + theta2 * x[lp][2])) ** 2 / 2
    if abs(error1 - error0) < epsilon: 
        break
    else:
        error0 = error1

#theta0 : -0.510496, theta1 : 1.542565, theta2 : 2.140431, error0 : 0.246543, error1 : 0.244565
print('theta0 : %f, theta1 : %f, theta2 : %f, error0 : %f, error1 : %f' % (theta0, theta1, theta2, error0, error1))
#Done: theta0 : -0.510496, theta1 : 1.542565, theta2 : 2.140431
print('Done: theta0 : %f, theta1 : %f, theta2 : %f' % (theta0, theta1, theta2)) 
print('迭代次数: %d' % cnt) #迭代次数: 77

#最终比较计算的值[5.400301,5.910797,7.453362,9.940628,11.136359] 和 y的值[5, 6, 8, 10, 11] 之间的相近性
for i in range(m):
    result = theta0 * x[i][0] + theta1 * x[i][1] + theta2 * x[i][2]
    print('%f' % result)
#5.400301
#5.910797
#7.453362
#9.940628
#11.136359

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

あずにゃん

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值