res = eval_sub(x) # we clear the memory of the recurrent layer between input # sequences because otherwise the network starts in an unknown # state. rn.clear() return res test_sub = theano.function([x, y], cost.output) def test(x, y): res = test_sub(x, y) # clear here too rn.clear() return res train_sub = theano.function([], cost.output, updates=get_updates(cost.params,cost.output,0.05), givens={x: trainx, y: trainy}) def train(): res = train_sub() rn.clear() return res # Since we didn't do any training (yet) the net has poor performance print "Test at start:", test(testx, testy) # Now to do some training for _ in range(100): train() print "Test after 100:", test(testx, testy)
#%% Build ANN. sx = theano.tensor.matrix('x') sy = theano.tensor.matrix('y') h = SimpleNode(sx, 2, 4) h2 = SimpleNode(h, 4, 2) out = SimpleNode(h2, 2, 1, nlin=pynnet.nlins.sigmoid) cost = errors.mse(out, sy) theano.config.blas.ldflags = '' eval = theano.function([sx], out.output) test = theano.function([sx, sy], cost.output) train = theano.function([sx, sy], cost.output, updates=get_updates(cost.params, cost.output, 0.01)) print("Error at start:", test(X, Y)) for i in range(200000): train(X, Y) print("Error after 200000:", test(X, Y)) def pfunc(x): return 1. - eval(x) clp = probaproxy(pfunc) plt.figure()
from pynnet import * from pynnet.nodes import errors from pynnet.training import get_updates sx = theano.tensor.matrix("x") sy = theano.tensor.matrix("y") # We initialize an MLP with one hidden layer of two units. h = SimpleNode(sx, 2, 2) out = SimpleNode(h, 2, 1) cost = errors.mse(out, sy) # We can build functions from expressions to use our network eval = theano.function([sx], out.output) test = theano.function([sx, sy], cost.output) train = theano.function([sx, sy], cost.output, updates=get_updates(cost.params, cost.output, 0.01)) x = [[0, 0], [0, 1], [1, 0], [1, 1]] y = [[0], [1], [1], [0]] # At the start the error is terrible print "Error at start:", test(x, y) # So we train a little for i in range(10): train(x, y) # Now the error should be a bit better print "Error after 10:", test(x, y)