Esempio n. 1
0
    def init_model(cls, ninput, nhidden, codec):
        """
        Initialize a new neural network.

        Args:
            ninput (int): Dimension of input vector
            nhidden (int): Number of nodes in hidden layer
            codec (list): List mapping n-th entry in output matrix to glyph
        """
        self = cls()
        self.rnn = clstm.make_net_init(
            'bidi',
            'ninput={}:nhidden={}:noutput={}'.format(ninput, nhidden,
                                                     len(codec)))
        self.rnn.initialize()
Esempio n. 2
0
    def init_model(cls, ninput, nhidden, codec):
        """
        Initialize a new neural network.

        Args:
            ninput (int): Dimension of input vector
            nhidden (int): Number of nodes in hidden layer
            codec (list): List mapping n-th entry in output matrix to glyph
        """
        self = cls()
        self.rnn = clstm.make_net_init('bidi',
                                       'ninput={}:nhidden={}:noutput={}'.format(ninput,
                                                                                nhidden, 
                                                                                len(codec)))
        self.rnn.initialize()
Esempio n. 3
0
    def learn(self, episodes):
        state_size = len(episodes[0].states[0])

        # Create the model of this action if it does not exist yet
        if self._model is None:
            self._model = clstm.make_net_init(
                "lstm1",
                "ninput=%i:nhidden=%i:noutput=%i:output_type=LinearLayer" % \
                    (state_size, self.hidden_neurons, self.nb_actions)
            )

        # Create an (timestep, variable, batch) array containing the states
        max_timestep = max([len(episode.states) for episode in episodes])
        data = zeros(shape=(max_timestep, state_size, len(episodes)), dtype=float32)
        values = zeros(shape=(max_timestep, self.nb_actions, len(episodes)), dtype=float32)

        for e, episode in enumerate(episodes):
            # Number of timesteps of this episode
            timesteps = len(episode.states)

            # episode.states has a shape of (timestep, variable), which corresponds
            # to what we want to put in the data array.
            data[0:timesteps, :, e] = episode.states

            # episode.states has a shape of (timestep, action), which also corresponds
            # to the desired shape
            values[0:timesteps, :, e] = episode.values

        # Train the model
        print('training')
        self._model.inputs.aset(data)
        self._model.forward()

        errors = values - self._model.outputs.array()
        self._model.d_outputs.aset(errors)
        self._model.backward()
        self._model.update()
        print('done')
Esempio n. 4
0
#!/usr/bin/python
from numpy import *
from pylab import *
from numpy.random import rand
import clstm
net = clstm.make_net_init("lstm1", "ninput=1:nhidden=4:noutput=2")
net.setLearningRate(1e-4, 0.9)
N = 20
ntrain = 30000
ntest = 1000
print "training 1:4:2 network to learn delay"
for i in range(ntrain):
    xs = array(rand(N) < 0.3, 'f')
    ys = roll(xs, 1)
    ys[0] = 0
    ys = array([1 - ys, ys], 'f').T.copy()
    net.inputs.aset(xs.reshape(N, 1, 1))
    net.forward()
    net.outputs.dset(ys.reshape(N, 2, 1) - net.outputs.array())
    net.backward()
    clstm.sgd_update(net)
print "testing", ntest, "random instances"
maxerr = 0.0
for i in range(ntest):
    xs = array(rand(N) < 0.3, 'f')
    ys = roll(xs, 1)
    ys[0] = 0
    net.inputs.aset(xs.reshape(N, 1, 1))
    net.forward()
    preds = net.outputs.array()[:, 1, 0]
    err = abs(amax(abs(ys - preds)))
Esempio n. 5
0
#!/usr/bin/python
from numpy import *
from pylab import *
from numpy.random import rand
import clstm
net = clstm.make_net_init("lstm1", "ninput=1:nhidden=4:noutput=2")
net.setLearningRate(1e-4, 0.9)
N = 20
ntrain = 30000
ntest = 1000
print "training 1:4:2 network to learn delay"
for i in range(ntrain):
    xs = array(rand(N) < 0.3, 'f')
    ys = roll(xs, 1)
    ys[0] = 0
    ys = array([1 - ys, ys], 'f').T.copy()
    net.inputs.aset(xs.reshape(N, 1, 1))
    net.forward()
    net.outputs.dset(ys.reshape(N, 2, 1) - net.outputs.array())
    net.backward()
    clstm.sgd_update(net)
print "testing", ntest, "random instances"
maxerr = 0.0
for i in range(ntest):
    xs = array(rand(N) < 0.3, 'f')
    ys = roll(xs, 1)
    ys[0] = 0
    net.inputs.aset(xs.reshape(N, 1, 1))
    net.forward()
    preds = net.outputs.array()[:, 1, 0]
    err = abs(amax(abs(ys - preds)))