pycnn xor实例
2016-03-24 13:09
274 查看
本篇以经典的xor为例,解释使用pycnn的完整流程。
pycnn-examples-xor
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())
参考资料
pycnn-apipycnn-examples-xor
相关文章推荐
- cookie的使用
- (转)git相关的基本操作都在此篇
- Opencv imread 不能正常读取
- Matlab的BwLabel实现
- Java学习之Iterator(迭代器)的一般用法
- items 排序 0
- 关于Activity启动模式的理解
- 丘比龙的女神
- makefile函数
- Fusion 360 获取装配部件的三角面片顶点在装配坐标系中的数值
- .net attribute 设置获取方法
- sqlite variable-length integers 实现方式
- Physical and Logical Block Corruptions. All you wanted to know about it. (Doc ID 840978.1)
- think python学习心得-(4)有返回函数
- css create 多边形 polygon
- myeclipse 注册机
- android studio 中创建java项目的服务器端并运行
- 时间格式转换大全
- iOS 常用的宏定义
- Named Return Value Optimization