本篇以经典的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())