深度学习实践指南(五)—— 求解异或问题

  • (1)本文的实现为仅有一个隐层的输入层-隐层-输出层的三层简单神经网络

  • (2)因为模型较为简单,参数较少,权值矩阵的更新我们采用的数值分析的方式,而不是反向传播。

import numpy as np
import operator
import random
import matplotlib.pyplot as plt

# S 型函数
def sigmoid(z):
    return 1./(1.+np.exp(-z))

# 定义 list 的内积运算
def inner_prod(w, x):
    return reduce(operator.add, map(operator.mul, w, x))

# 单个神经元
def neuron(w, x):
    return sigmoid(w[0] + inner_prod(w[1:], x))

# 判别函数
def h(ws, x):
    hidden1 = neuron(ws[:3], x)
    hidden2 = neuron(ws[3:6], x)
    return neuron(ws[6:], [hidden1, hidden2])

INPUTS = [[0, 0], [0, 1], [1, 0], [1, 1]]
OUTPUTS = [0, 1, 1, 0]

# 代价函数(全部样本)
def cost(ws):
    return 1./2*sum((h(ws, x)-y)**2 for x, y in zip(INPUTS, OUTPUTS))

# 数值分析的方式计算偏导
def partial(f, k, ws):
    ws_plus, ws_minus = ws[:], ws[:]
    epsilon = 0.01
    ws_plus[k] += epsilon
    ws_minus[k] -= epsilon
    return (f(ws_plus)-f(ws_minus))/2/epsilon

# 梯度下降求解最优化(最小化)问题
def gradient_descent(cost, eta, n):
    costs = []
    ws = [random.random()*2-1 for _ in xrange(9)]
    for iter in xrange(n):
        c = cost(ws)
        print 'iter: ', iter, ', cost: ', c
        costs.append(c)
        grad = [partial(cost, k, ws) for k in xrange(9)]
        ws = [ws[k]-eta*grad[k] for k in xrange(9)]
    return ws, costs

def main():
    ws, costs = gradient_descent(cost, 0.1, 10000)
    print '\nFinal cost:', cost(ws)
    print '\nFinal weights:', ws
    print '\nPrediction: ', [h(ws, x) for x in INPUTS]
    plt.plot(np.array(costs))
    plt.xlabel('iteration')
    plt.ylabel('cost')
    plt.show()
if __name__ == '__main__':
    main()

注:因为数据量较小,所以会对权值矩阵的初始化以及学习率 η 的设置较为敏感,如果学习速率较低,可适当增加学习率。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

五道口纳什

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

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

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

打赏作者

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

抵扣说明:

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

余额充值