pycnn xor实例
来源:互联网 发布:淘宝精品橱窗怎么设置 编辑:程序博客网 时间:2024/05/16 15:20
本篇以经典的xor为例,解释使用pycnn的完整流程。
from pycnn import *hidden_size = 8iterations = 500m = 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())
参考资料
- pycnn-api
- pycnn-examples-xor
0 0
- pycnn xor实例
- pycnn add实例
- pycnn api
- 实例:学习XOR
- xor
- XOR ^
- xor
- xor
- xor
- pycnn以多项式为例的动态网络结构
- XOR加密
- Cow XOR
- BZOJ2115 Xor
- HDU3949 XOR
- 趣味XOR
- hdu3949:XOR
- qduoj-xor
- XOR Groups
- Opencv imread 不能正常读取
- 丘比龙的女神
- Matlab的BwLabel实现
- Java学习之Iterator(迭代器)的一般用法
- 深入解读Quartz的原理
- pycnn xor实例
- Spring Cron Expression
- (转)探索 .git 目录,让你真正了理解git
- 类型后面加问号 int?
- MySQL增删改查
- Java学习之HashMap和Hashtable的区别
- 329. Longest Increasing Path in a Matrix
- Android界面布局大集合(Fragment+ViewPager)
- (转)73条日常Linux shell命令