def nn_routine_ds(ds):
  feasize = ds['input'].shape[1]
  trainDS, testDS = ds.splitWithProportion(0.8)
  net = _FeedForwardNetwork()

  inLayer = LinearLayer(feasize) 
  net.addInputModule(inLayer)
  net, hl = add_fullyC_layer(net, inLayer, TanhLayer(feasize*8))
  net, hl = add_fullyC_layer(net, hl, TanhLayer(feasize*4))
  net, hl = add_fullyC_layer(net, hl, TanhLayer(feasize*2))
  net, hl = add_fullyC_layer(net, hl, TanhLayer(feasize))
  net, hl = add_fullyC_layer(net, hl, TanhLayer(feasize/2))
  net, hl = add_fullyC_layer(net, hl, TanhLayer(feasize/4))
  net, hl = add_fullyC_layer(net, hl, TanhLayer(feasize/8))
  outLayer = SigmoidLayer(1)
  net.addOutputModule(outLayer)
  hidden_last_to_out = FullConnection(hl, outLayer)
  net.addConnection(hidden_last_to_out)

  net.sortModules()
  trainer = BackpropTrainer(net, trainDS, learningrate=0.001, momentum=.1, verbose=True)
  epochs = 10000
  for zzz in range(epochs):
    if zzz % 100 == 0:
      print "%2.3f percent complete" % (100.*zzz/epochs)
    trainer.train()
  res = np.append(testDS['target'], net.activateOnDataset(testDS), axis=1)
  return (net, trainer, trainDS, testDS, res)
Esempio n. 2
0
    def __init__(self):
        self.width = 28
        self.height = 28
        self.testds, self.trainds = \
            makeMnistDataSets('/Users/bayerj/Desktop/MNIST/')
        
        # Initialize MDRNN
        self.net = _FeedForwardNetwork()
        inlayer = LinearLayer(self.width * self.height)
        hiddenlayer = MdrnnLayer(timedim=2, 
                                 shape=(self.width, self.height), 
                                 blockshape=(1, 1), 
                                 hiddendim=4,
                                 outsize=10,
                                 name='mdrnn')
        outlayer = SigmoidLayer(self.width * self.height * 10)
        con1 = IdentityConnection(inlayer, hiddenlayer)
        con2 = IdentityConnection(hiddenlayer, outlayer)
        self.net.addInputModule(inlayer)
        self.net.addModule(hiddenlayer)
        self.net.addOutputModule(outlayer)
        self.net.addConnection(con1)
        self.net.addConnection(con2)

        self.net.sortModules()
Esempio n. 3
0
    def testParametersDerivatives(self):
        rnet = pybrainbridge._RecurrentNetwork()
        self.lstm_network(rnet)
        rnet.sortModules()
        self.assert_(getattr(rnet, '_derivs', None) is not None)

        fnet = pybrainbridge._FeedForwardNetwork()
        self.two_layer_network(fnet)
        fnet.sortModules()
        self.assert_(getattr(fnet, '_derivs', None) is not None)
Esempio n. 4
0
    def testParametersDerivatives(self):
        rnet = pybrainbridge._RecurrentNetwork()
        self.lstm_network(rnet)
        rnet.sortModules()
        self.assert_(getattr(rnet, '_derivs', None) is not None)

        fnet = pybrainbridge._FeedForwardNetwork()
        self.two_layer_network(fnet)
        fnet.sortModules()
        self.assert_(getattr(fnet, '_derivs', None) is not None)
Esempio n. 5
0
    def convertToFastNetwork(self):
        """ Attempt to transform the network into a fast network. If fast networks are not available,
        or the network cannot be converted, it returns None. """

        from pybrain.structure.networks import FeedForwardNetwork, RecurrentNetwork

        try:
            from arac.pybrainbridge import _RecurrentNetwork, _FeedForwardNetwork  # @UnresolvedImport
        except:
            print "No fast networks available."
            return None

        net = self.copy()
        if isinstance(net, FeedForwardNetwork):
            cnet = _FeedForwardNetwork()
        elif isinstance(net, RecurrentNetwork):
            cnet = _RecurrentNetwork()

        for m in net.inmodules:
            cnet.addInputModule(m)
        for m in net.outmodules:
            cnet.addOutputModule(m)
        for m in net.modules:
            cnet.addModule(m)

        for clist in net.connections.values():
            for c in clist:
                cnet.addConnection(c)
        if isinstance(net, RecurrentNetwork):
            for c in net.recurrentConns:
                cnet.addRecurrentConnection(c)

        try:
            cnet.sortModules()
        except ValueError:
            print "Network cannot be converted."
            return None

        cnet.owner = cnet
        return cnet
Esempio n. 6
0
    def convertToFastNetwork(self):
        """ Attempt to transform the network into a fast network. If fast networks are not available,
        or the network cannot be converted, it returns None. """

        from pybrain.structure.networks import FeedForwardNetwork, RecurrentNetwork
        try:
            from arac.pybrainbridge import _RecurrentNetwork, _FeedForwardNetwork #@UnresolvedImport
        except:
            print("No fast networks available.")
            return None

        net = self.copy()
        if isinstance(net, FeedForwardNetwork):
            cnet = _FeedForwardNetwork()
        elif isinstance(net, RecurrentNetwork):
            cnet = _RecurrentNetwork()

        for m in net.inmodules:
            cnet.addInputModule(m)
        for m in net.outmodules:
            cnet.addOutputModule(m)
        for m in net.modules:
            cnet.addModule(m)

        for clist in list(net.connections.values()):
            for c in clist:
                cnet.addConnection(c)
        if isinstance(net, RecurrentNetwork):
            for c in net.recurrentConns:
                cnet.addRecurrentConnection(c)

        try:
            cnet.sortModules()
        except ValueError:
            print("Network cannot be converted.")
            return None

        cnet.owner = cnet
        return cnet
Esempio n. 7
0
 def equivalence_feed_forward(self, builder):
     _net = pybrainbridge._FeedForwardNetwork()
     builder(_net)
     net = FeedForwardNetwork()
     builder(net)
     super(TestNetworkEquivalence, self).equivalence_feed_forward(net, _net)
Esempio n. 8
0
 def equivalence_feed_forward(self, builder):
     _net = pybrainbridge._FeedForwardNetwork()
     builder(_net)
     net = FeedForwardNetwork()
     builder(net)
     super(TestNetworkEquivalence, self).equivalence_feed_forward(net, _net)