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()
def rec_two_layer_network(self, net): inlayer = LinearLayer(2, 'in') outlayer = LinearLayer(2, 'out') con = IdentityConnection(inlayer, outlayer) rcon = IdentityConnection(inlayer, outlayer) net.addInputModule(inlayer) net.addOutputModule(outlayer) net.addConnection(con) net.addRecurrentConnection(rcon)
def buildNonGravityNet(recurrent=False): if recurrent: net = RecurrentNetwork() else: net = FeedForwardNetwork() l1 = LinearLayer(2) l2 = LinearLayer(3) s1 = SigmoidLayer(2) l3 = LinearLayer(1) net.addInputModule(l1) net.addModule(l2) net.addModule(s1) net.addOutputModule(l3) net.addConnection(IdentityConnection(l1, l2, outSliceFrom=1)) net.addConnection(IdentityConnection(l1, l2, outSliceTo=2)) net.addConnection(IdentityConnection(l2, l3, inSliceFrom=2)) net.addConnection(IdentityConnection(l2, l3, inSliceTo=1)) net.addConnection(IdentityConnection(l1, s1)) net.addConnection(IdentityConnection(l2, s1, inSliceFrom=1)) net.addConnection(IdentityConnection(s1, l3, inSliceFrom=1)) if recurrent: net.addRecurrentConnection(IdentityConnection(s1, l1)) net.addRecurrentConnection( IdentityConnection(l2, l2, inSliceFrom=1, outSliceTo=2)) net.sortModules() return net
def buildMinimalLSTMNetwork(): N = RecurrentNetwork('simpleLstmNet') i = LinearLayer(4, name='i') h = LSTMLayer(1, peepholes=True, name='lstm') o = LinearLayer(1, name='o') N.addInputModule(i) N.addModule(h) N.addOutputModule(o) N.addConnection(IdentityConnection(i, h)) N.addConnection(IdentityConnection(h, o)) N.sortModules() return N
def buildMinimalMDLSTMNetwork(): N = RecurrentNetwork('simpleMdLstmNet') i = LinearLayer(4, name = 'i') h = MDLSTMLayer(1, peepholes = True, name = 'mdlstm') o = LinearLayer(1, name = 'o') N.addInputModule(i) N.addModule(h) N.addOutputModule(o) N.addConnection(IdentityConnection(i, h, outSliceTo = 4)) N.addRecurrentConnection(IdentityConnection(h, h, outSliceFrom = 4, inSliceFrom = 1)) N.addConnection(IdentityConnection(h, o, inSliceTo = 1)) N.sortModules() return N
def sliced_connection_network(self, net): inlayer = LinearLayer(2, 'in') outlayer = LinearLayer(2, 'out') con = IdentityConnection(inlayer, outlayer, inSliceFrom=0, inSliceTo=1, outSliceFrom=1, outSliceTo=2, ) con = IdentityConnection(inlayer, outlayer, inSliceFrom=1, inSliceTo=2, outSliceFrom=0, outSliceTo=1, ) net.addInputModule(inlayer) net.addOutputModule(outlayer) net.addConnection(con)
def __init__(self, module, learner = None): LearningAgent.__init__(self, module, learner) # exploration module (linear flat network) self.explorationmodule = buildNetwork(self.indim, self.outdim, bias=False) # state dependent exploration layer self.explorationlayer = StateDependentLayer(self.outdim, self.explorationmodule, 'explore') # add exploration layer to top of network through identity connection out = self.module.outmodules.pop() self.module.addOutputModule(self.explorationlayer) self.module.addConnection(IdentityConnection(out, self.module['explore'], self.module)) self.module.sortModules() # tell learner the new module self.learner.setModule(self.module) # add the log likelihood (loglh) to the dataset and link it to the others self.history.addField('loglh', self.module.paramdim) self.history.link.append('loglh') self.loglh = None # if this flag is set to True, random weights are drawn after each reward, # effectively acting like the vanilla policy gradient alg. self.actaspg = False
def lstm_cell(self, net): inpt = LinearLayer(4, 'inpt') forgetgate = GateLayer(1, 'forgetgate') ingate = GateLayer(1, 'ingate') outgate = GateLayer(1, 'outgate') state = LinearLayer(1, 'state') in_to_fg = IdentityConnection(inpt, forgetgate, inSliceFrom=0, inSliceTo=1, outSliceFrom=0, outSliceTo=1, name='in_to_fg') in_to_og = IdentityConnection(inpt, outgate, inSliceFrom=1, inSliceTo=2, outSliceFrom=1, outSliceTo=2, name='in_to_og') in_to_ig = IdentityConnection(inpt, ingate, inSliceFrom=2, inSliceTo=4, outSliceFrom=0, outSliceTo=2, name='in_to_ig') fg_to_st = IdentityConnection(forgetgate, state, name='fg_to_st') st_to_fg = IdentityConnection(state, forgetgate, outSliceFrom=1, outSliceTo=2, name='st_to_fg') st_to_og = IdentityConnection(state, outgate, outSliceFrom=1, outSliceTo=2, name='st_to_og') ig_to_st = IdentityConnection(ingate, state, name='ig_to_st') net.addInputModule(inpt) net.addModule(forgetgate) net.addModule(ingate) net.addModule(state) net.addOutputModule(outgate) net.addConnection(in_to_fg) net.addConnection(in_to_og) net.addConnection(in_to_ig) net.addConnection(fg_to_st) net.addRecurrentConnection(st_to_fg) net.addConnection(st_to_og) net.addConnection(ig_to_st)
def __init__(self, module, learner=None): assert isinstance(module, FeedForwardNetwork) assert len(module.outmodules) == 1 LearningAgent.__init__(self, module, learner) # create gaussian layer self.explorationlayer = GaussianLayer(self.outdim, name='gauss') self.explorationlayer.setSigma([-2] * self.outdim) # add gaussian layer to top of network through identity connection out = self.module.outmodules.pop() self.module.addOutputModule(self.explorationlayer) self.module.addConnection(IdentityConnection(out, self.module['gauss'])) self.module.sortModules() # tell learner the new module self.learner.setModule(self.module) # add the log likelihood (loglh) to the dataset and link it to the others self.history.addField('loglh', self.module.paramdim) self.history.link.append('loglh') self.loglh = None
def __init__(self, *args, **kwargs): IdentityConnection.__init__(self, *args, **kwargs) ParameterContainer.__init__(self, self.indim)