pycnn xor实例

本篇以经典的xor为例,解释使用pycnn的完整流程。

from pycnn import *

hidden_size = 8
iterations = 500

m = Model()
sgd = SimpleSGDTrainer(m)

m.add_parameters('W', (hidden_size, 2))
m.add_parameters('b', hidden_size)
m.add_parameters('V', (1, hidden_size))
m.add_parameters('a', 1)

renew_cg() # new computation graph. not strictly needed here, but good practice.

W = parameter(m['W'])
b = parameter(m['b'])
V = parameter(m['V'])
a = parameter(m['a'])

x = vecInput(2)
y = scalarInput(0)

h = tanh((W*x)+b)
y_pred = logistic((V*h)+a)

loss = binary_log_loss(y_pred, y)

x_examples = [[0, 0], [0, 1], [1, 0], [1, 1]]
y_examples = [0, 1, 1, 0]

for i in xrange(iterations):
    mloss = 0.0
    for j in xrange(len(x_examples)):
        x.set(x_examples[j])        # 为模型参数赋值
        y.set(y_examples[j])
        mloss += loss.scalar_value()    # 该步会执行正向传播forward
        loss.backward()                 # 执行反向传播,计算参数的梯度
        sgd.update(1.0)                 # 更新模型参数,Here 1.0 is the scaling factor that allows us to control the size of the update.
    # sgd.update_epoch()
    mloss /= 4
    if i % (iterations/10) == 0 or i == (iterations-1):
        print 'iter %d, loss: %f' % (i, mloss)
for i in xrange(len(x_examples)):
    x.set(x_examples[i])
    print '[%d, %d]: %f' % (x_examples[i][0], x_examples[i][1], y_pred.scalar_value())

参考资料

  1. pycnn-api
  2. pycnn-examples-xor
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值