pycnn以多项式为例的动态网络结构

在加法的基础上,修改为目标值是三个输入的任意多项式,并且由原来静态网络结构改为动态网络结构。动态网络和静态结构很相似,只是对于每个训练实例都创建一个新的网络。
以多项式为例,可能与静态网络并无差别,但是动态网络非常适用于结果不固定的模型,例如循环神经网络和递归神经网络。

from pycnn import *
import random

hidden_size = 8
iterations = 500

m = Model()
sgd = SimpleSGDTrainer(m)

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


def equation(a, b, c):  # 定义一个返回多项式值得函数
    result = a**3 + 2*b**2 + 3*c + 4*1
    return result

def get_examples(num):
    extent = 1
    x_examples = []
    y_examples = []
    for i in xrange(num):
        a = random.random()*extent
        b = random.random()*extent
        c = random.random()*extent
        x_examples.append([a, b, c])
        y_examples.append(equation(a, b, c))    # a,b,c的多项式值
    return x_examples, y_examples

def create_add_network(m, inputs, label):   #对于每个输入实例都创建新的网络
    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(len(inputs))
    x.set(inputs)
    y = scalarInput(0)
    y.set(label)
    h = tanh((W*x)+b)

    y_pred = (V*h) + a
    loss = squared_distance(y_pred, y)
    return loss

x_examples, y_examples = get_examples(100)

for i in xrange(iterations):
    mloss = 0.0
    for j in xrange(len(x_examples)):
        loss = create_add_network(m, x_examples[j], y_examples[j]) # 创建新的网络
        mloss += loss.scalar_value()
        loss.backward()
        sgd.update()
    # sgd.update_epoch()
    mloss /= len(x_examples)
    if i % (iterations/10) == 0 or i == (iterations-1):
        print 'iter %d, loss: %f' % (i, mloss)

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(3)
h = tanh((W*x)+b)
y_pred = (V*h) + a

print b.value()     # 查看参数b的最终结果

x_test, y_test = get_examples(10)
for i in xrange(len(x_test)):
    x.set(x_test[i])
    y_gold = equation(x_test[i][0], x_test[i][1], x_test[i][2])
    print '[%f, %f, %f]: %f, %f' % (x_test[i][0], x_test[i][1], x_test[i][2], y_pred.scalar_value(), y_gold)

参考资料

  1. pycnn-api
  2. Working with the pyCNN package

你可能感兴趣的:(神经网络,pycnn,动态网络结构)