def test_memory_block_with_glove_embedder_params(self):
     payload = WordEmbedGlove()
     original_embedding = payload.predict([6])
     memb = MemoryBlock(payload, np.asarray([1, 2, 6]), outdim=payload.outdim)
     memory_embedding = memb.predict([2])
     self.assertTrue(np.allclose(original_embedding, memory_embedding))
     print memb.output.allparams
 def test_memory_block_with_glove_embedder(self):
     payload = WordEmbedGlove()
     original_embedding = payload.predict([6])
     memb = MemoryBlock(payload,
                        np.asarray([1, 2, 6]),
                        outdim=payload.outdim)
     memory_embedding = memb.predict([2])
     self.assertTrue(np.allclose(original_embedding, memory_embedding))
 def test_memory_block_with_seq_encoder_dynamic_fail(self):
     invocabsize = 5
     encdim = 13
     gru = GRU(dim=invocabsize, innerdim=encdim)
     payload = SeqEncoder(IdxToOneHot(vocsize=invocabsize), gru)
     dynmemb = MemoryBlock(payload, outdim=encdim)
     idxs = [0, 2, 5]
     #dynmemb.predict(idxs)
     self.assertRaises(AssertionError, lambda: dynmemb.predict(idxs))
 def test_memory_block_with_seq_encoder_static_fail(self):
     invocabsize = 5
     memsize = 10
     seqlen = 3
     encdim = 13
     data = np.random.randint(0, invocabsize, (memsize, seqlen))
     gru = GRU(dim=invocabsize, innerdim=encdim)
     payload = SeqEncoder(IdxToOneHot(vocsize=invocabsize), gru)
     memb = MemoryBlock(payload, data, outdim=encdim)
     idxs = [0, 2, 5]
     self.assertRaises(AssertionError, lambda: memb.predict(idxs, data))
 def test_memory_block_with_seq_encoder_dynamic_fail(self):
     invocabsize = 5
     encdim = 13
     gru = GRU(dim=invocabsize, innerdim=encdim)
     payload = SeqEncoder(
         IdxToOneHot(vocsize=invocabsize),
         gru
     )
     dynmemb = MemoryBlock(payload, outdim=encdim)
     idxs = [0, 2, 5]
     #dynmemb.predict(idxs)
     self.assertRaises(AssertionError, lambda: dynmemb.predict(idxs))
 def test_memory_block_with_seq_encoder_static_fail(self):
     invocabsize = 5
     memsize = 10
     seqlen = 3
     encdim = 13
     data = np.random.randint(0, invocabsize, (memsize, seqlen))
     gru = GRU(dim=invocabsize, innerdim=encdim)
     payload = SeqEncoder(
         IdxToOneHot(vocsize=invocabsize),
         gru
     )
     memb = MemoryBlock(payload, data, outdim=encdim)
     idxs = [0, 2, 5]
     self.assertRaises(AssertionError, lambda: memb.predict(idxs, data))
 def test_memory_block_with_seq_encoder(self):
     invocabsize = 5
     memsize = 10
     seqlen = 3
     encdim = 13
     data = np.random.randint(0, invocabsize, (memsize, seqlen))
     gru = GRU(dim=invocabsize, innerdim=encdim)
     payload = SeqEncoder(IdxToOneHot(vocsize=invocabsize), gru)
     memb = MemoryBlock(payload, data, indim=invocabsize, outdim=encdim)
     idxs = [0, 2, 5]
     memory_element = memb.predict(idxs)
     self.assertEqual(memory_element.shape, (len(idxs), encdim))
     gruparams = set([getattr(gru, pname) for pname in gru.paramnames])
     allparams = set(memb.output.allparams)
     self.assertEqual(gruparams.intersection(allparams), allparams)
Beispiel #8
0
    def init(self):
        #memory
        wencpg = WordEncoderPlusGlove(numchars=self.numchars, numwords=self.numwords, encdim=self.wordencdim, embdim=self.wordembdim, embtrainfrac=0.0, glovepath=self.glovepath)
        self.memenco = SeqEncoder(
            wencpg,
            GRU(dim=self.wordembdim + self.wordencdim, innerdim=self.encinnerdim)
        )

        entemb = VectorEmbed(indim=self.outdim, dim=self.entembdim)
        self.mempayload = ConcatBlock(entemb, self.memenco)
        self.memblock = MemoryBlock(self.mempayload, self.memdata, indim=self.outdim, outdim=self.encinnerdim+self.entembdim)

        #encoder
        wencpg2 = WordEncoderPlusGlove(numchars=self.numchars, numwords=self.numwords, encdim=self.wordencdim, embdim=self.wordembdim, embtrainfrac=0.0, glovepath=self.glovepath)
        self.enc = SeqEncoder(
            wencpg2,
            GRU(dim=self.wordembdim + self.wordencdim, innerdim=self.encinnerdim)
        )

        #decoder
        entemb2 = VectorEmbed(indim=self.outdim, dim=self.entembdim)
        self.softmaxoutblock = stack(self.memaddr(self.memblock, indim=self.decinnerdim, memdim=self.memblock.outdim, attdim=self.attdim), Softmax())
        self.dec = SeqDecoder(
            [entemb2,  #self.memblock,
             GRU(dim=entemb.outdim + self.encinnerdim, innerdim=self.decinnerdim),             # GRU(dim=self.memblock.outdim + self.encinnerdim, innerdim=self.decinnerdim),
             ],
            inconcat=True,
            innerdim=self.decinnerdim,
            softmaxoutblock=self.softmaxoutblock
        )
 def test_memory_block_with_seq_encoder(self):
     invocabsize = 5
     memsize = 10
     seqlen = 3
     encdim = 13
     data = np.random.randint(0, invocabsize, (memsize, seqlen))
     gru = GRU(dim=invocabsize, innerdim=encdim)
     payload = SeqEncoder(
         IdxToOneHot(vocsize=invocabsize),
         gru
     )
     memb = MemoryBlock(payload, data, indim=invocabsize, outdim=encdim)
     idxs = [0, 2, 5]
     memory_element = memb.predict(idxs)
     self.assertEqual(memory_element.shape, (len(idxs), encdim))
     gruparams = set([getattr(gru, pname) for pname in gru.paramnames])
     allparams = set(memb.output.allparams)
     self.assertEqual(gruparams.intersection(allparams), allparams)
Beispiel #10
0
    def __init__(
            self,
            entembdim=50,
            wordembdim=50,
            wordencdim=100,
            memdata=None,
            attdim=100,
            numchars=128,  # number of different chars
            numwords=4e5,  # number of different words
            glovepath=None,
            innerdim=100,  # dim of memory payload encoder output
            outdim=1e4,  # number of entities
            memaddr=DotMemAddr,
            **kw):
        super(FBMemMatch, self).__init__(**kw)
        self.wordembdim = wordembdim
        self.wordencdim = wordencdim
        self.entembdim = entembdim
        self.attdim = attdim
        self.encinnerdim = innerdim
        self.outdim = outdim

        memaddr = TransDotMemAddr

        # memory encoder per word
        #wencpg = WordEmbed(indim=numwords, outdim=self.wordembdim, trainfrac=1.0)
        wordencoder = WordEncoderPlusGlove(numchars=numchars,
                                           numwords=numwords,
                                           encdim=self.wordencdim,
                                           embdim=self.wordembdim,
                                           embtrainfrac=0.0,
                                           glovepath=glovepath)

        # memory encoder for one cell
        self.phraseencoder = SeqEncoder(
            wordencoder,
            GRU(dim=self.wordembdim + self.wordencdim,
                innerdim=self.encinnerdim))
        # entity embedder
        entemb = VectorEmbed(indim=self.outdim, dim=self.entembdim)
        self.entembs = entemb(
            memdata[0])  #Val(np.arange(0, self.outdim, dtype="int32")))
        # memory block
        self.mempayload = self.phraseencoder  #ConcatBlock(entemb, self.phraseencoder)
        self.memblock = MemoryBlock(
            self.mempayload,
            memdata[1],
            indim=self.outdim,
            outdim=self.encinnerdim)  # + self.entembdim)
        # memory addressing
        self.mema = memaddr(self.memblock,
                            memdim=self.memblock.outdim,
                            attdim=attdim,
                            indim=self.encinnerdim)
Beispiel #11
0
    def init(self):
        #MEMORY: encodes how entity is written + custom entity embeddings
        wencpg = WordEncoderPlusGlove(numchars=self.numchars,
                                      numwords=self.numwords,
                                      encdim=self.wordencdim,
                                      embdim=self.wordembdim,
                                      embtrainfrac=0.0,
                                      glovepath=self.glovepath)
        self.memenco = SeqEncoder(
            wencpg,
            GRU(dim=self.wordembdim + self.wordencdim,
                innerdim=self.encinnerdim))

        entemb = VectorEmbed(indim=self.outdim, dim=self.entembdim)
        self.mempayload = ConcatBlock(entemb, self.memenco)
        self.memblock = MemoryBlock(self.mempayload,
                                    self.memdata,
                                    indim=self.outdim,
                                    outdim=self.encinnerdim + self.entembdim)

        #ENCODER: uses the same language encoder as memory
        #wencpg2 = WordEncoderPlusGlove(numchars=self.numchars, numwords=self.numwords, encdim=self.wordencdim, embdim=self.wordembdim, embtrainfrac=0.0, glovepath=glovepath)
        self.enc = RecStack(
            wencpg,
            GRU(dim=self.wordembdim + self.wordencdim,
                innerdim=self.encinnerdim))

        #ATTENTION
        attgen = LinearGateAttentionGenerator(indim=self.encinnerdim +
                                              self.decinnerdim,
                                              innerdim=self.attdim)
        attcon = WeightedSumAttCon()

        #DECODER
        #entemb2 = VectorEmbed(indim=self.outdim, dim=self.entembdim)
        self.softmaxoutblock = stack(
            self.memaddr(self.memblock,
                         indim=self.decinnerdim + self.encinnerdim,
                         memdim=self.memblock.outdim,
                         attdim=self.attdim), Softmax())

        self.dec = SeqDecoder([
            self.memblock,
            GRU(dim=self.entembdim + self.encinnerdim,
                innerdim=self.decinnerdim)
        ],
                              outconcat=True,
                              inconcat=False,
                              attention=Attention(attgen, attcon),
                              innerdim=self.decinnerdim + self.encinnerdim,
                              softmaxoutblock=self.softmaxoutblock)
Beispiel #12
0
 def __init__(self,
              memenc,
              memdata,
              memaddr=DotMemAddr,
              memdim=None,
              memattdim=100,
              **kw):
     assert (memenc is not None)
     memblock = MemoryBlock(memenc,
                            memdata,
                            indim=memdata.shape[0],
                            outdim=memdim)
     memstack = MemoryStack(memblock, memaddr, memattdim=memattdim)
     super(MemVec2Idx, self).__init__(memstack, **kw)
Beispiel #13
0
    def test_memory_block_with_seq_encoder_dynamic(self):
        invocabsize = 5
        memsize = 10
        seqlen = 3
        encdim = 13
        data = np.random.randint(0, invocabsize, (memsize, seqlen))
        gru = GRU(dim=invocabsize, innerdim=encdim)
        payload = SeqEncoder(IdxToOneHot(vocsize=invocabsize), gru)
        dynmemb = MemoryBlock(payload, outdim=encdim)
        idxs = [0, 2, 5]
        p = dynmemb.predict
        memory_element = p(idxs, data)
        self.assertEqual(memory_element.shape, (len(idxs), encdim))
        gruparams = set([
            getattr(gru, pname)
            for pname in "u w b uhf whf bhf um wm bm".split()
        ])
        allparams = set(p.outs[0].allparams)
        self.assertEqual(gruparams.intersection(allparams), allparams)

        statmemb = MemoryBlock(payload, data, outdim=encdim)
        statpred = statmemb.predict(idxs)
        self.assertTrue(np.allclose(statpred, memory_element))