梯度下降:代码
来源:互联网 发布:nginx websocket 转发 编辑:程序博客网 时间:2024/06/04 17:55
梯度下降:代码
之前我们看到一个权重的计算是:
Δwi=ηδxi
这里 error term δ 是指
δ=(y−y^)f′(h)=(y−y^)f′(∑wixi)
记住,上面公式中 (y−y^) 是输出误差,f′(h) 是激活函数 f(h) 的导函数,我们把这个导函数称做输出的梯度。
现在假设只有一个输出,我来把这个写成代码。我们还是用 sigmoid 来作为激活函数 f(h)。
import numpy as npdef sigmoid(x): """ Calculate sigmoid """ return 1/(1+np.exp(-x))def sigmoid_prime(x): """ # Derivative of the sigmoid function """ return sigmoid(x) * (1 - sigmoid(x))learnrate = 0.5x = np.array([1, 2, 3, 4])y = np.array(0.5)# Initial weightsw = np.array([0.5, -0.5, 0.3, 0.1])### Calculate one gradient descent step for each weight### Note: Some steps have been consilated, so there are### fewer variable names than in the above sample code# TODO: Calculate the node's linear combination of inputs and weightsh = np.dot(x, w)# TODO: Calculate output of neural networknn_output = sigmoid(h)# TODO: Calculate error of neural networkerror = y - nn_output# TODO: Calculate the error term# Remember, this requires the output gradient, which we haven't# specifically added a variable for.error_term = error * sigmoid_prime(h)# TODO: Calculate change in weightsdel_w = learnrate * error_term * xprint('Neural Network output:')print(nn_output)print('Amount of Error:')print(error)print('Change in Weights:')print(del_w)
阅读全文
0 0
- 梯度下降代码
- 梯度下降+代码
- 梯度下降:代码
- 梯度下降法-python代码
- 1、梯度下降c代码
- 梯度下降法Python代码
- 梯度下降详解(包含代码例子)
- 梯度下降来龙去脉附matlab代码
- LR 逻辑回归代码 (梯度下降)
- 梯度下降求解逻辑回归(代码)
- 梯度下降
- 梯度下降
- 梯度下降
- 梯度下降
- 梯度下降
- 梯度下降
- 梯度下降
- 梯度下降
- Java中的引用变量详谈
- Elasticsearch集成中文分词器ik
- python机器学习day'6
- 编程模拟飞船加速变轨过程-物理基础篇(5) 摄动方程
- codevs 1961 躲避大龙 解题报告 SPFA
- 梯度下降:代码
- Android 异常:JPush推送与远程进程问题
- 分布式架构设计之基础软件系统架构
- POJ 2545 Hamming Problem 笔记
- JSP内置对象request的方法
- Python练习8---字典的运用
- JavaScript若干盲点总结
- FPM(FastCGI Process Manager)
- OSG学习:响应键盘鼠标示例