示例#1
0
def run(p="../../../data/atis/atis.pkl",
        wordembdim=70,
        lablembdim=70,
        innerdim=300,
        lr=0.05,
        numbats=100,
        epochs=20,
        validinter=1,
        wreg=0.0003,
        depth=1):
    train, test, dics = pickle.load(open(p))
    word2idx = dics["words2idx"]
    table2idx = dics["tables2idx"]
    label2idx = dics["labels2idx"]
    label2idxrev = {v: k for k, v in label2idx.items()}
    train = zip(*train)
    test = zip(*test)
    print "%d training examples, %d test examples" % (len(train), len(test))
    #tup2text(train[0], word2idx, table2idx, label2idx)
    maxlen = 0
    for tup in train + test:
        maxlen = max(len(tup[0]), maxlen)

    numwords = max(word2idx.values()) + 2
    numlabels = max(label2idx.values()) + 2

    # get training data
    traindata = getdatamatrix(train, maxlen, 0).astype("int32")
    traingold = getdatamatrix(train, maxlen, 2).astype("int32")
    trainmask = (traindata > 0).astype("float32")

    # test data
    testdata = getdatamatrix(test, maxlen, 0).astype("int32")
    testgold = getdatamatrix(test, maxlen, 2).astype("int32")
    testmask = (testdata > 0).astype("float32")

    res = atiseval(testgold - 1, testgold - 1, label2idxrev)
    print res  #; exit()

    # define model
    innerdim = [innerdim] * depth
    m = SimpleSeqTransDec(indim=numwords,
                          inpembdim=wordembdim,
                          outembdim=lablembdim,
                          innerdim=innerdim,
                          outdim=numlabels)

    # training
    m = m.train([traindata, shiftdata(traingold), trainmask], traingold).adagrad(lr=lr).grad_total_norm(5.0).seq_cross_entropy().l2(wreg)\
        .split_validate(splits=5, random=True).seq_cross_entropy().seq_accuracy().validinter(validinter).takebest()\
        .train(numbats, epochs)

    # predict after training
    s = GreedySearch(m, startsymbol=0)
    testpred, _ = s.decode(testdata)
    testpred = testpred * testmask

    evalres = atiseval(testpred - 1, testgold - 1, label2idxrev)
    print evalres
示例#2
0
def run(p="../../../data/atis/atis.pkl", wordembdim=70, lablembdim=70, innerdim=300, lr=0.05, numbats=100, epochs=20, validinter=1, wreg=0.0003, depth=1):
    train, test, dics = pickle.load(open(p))
    word2idx = dics["words2idx"]
    table2idx = dics["tables2idx"]
    label2idx = dics["labels2idx"]
    label2idxrev = {v: k for k, v in label2idx.items()}
    train = zip(*train)
    test = zip(*test)
    print "%d training examples, %d test examples" % (len(train), len(test))
    #tup2text(train[0], word2idx, table2idx, label2idx)
    maxlen = 0
    for tup in train + test:
        maxlen = max(len(tup[0]), maxlen)

    numwords = max(word2idx.values()) + 2
    numlabels = max(label2idx.values()) + 2

    # get training data
    traindata = getdatamatrix(train, maxlen, 0).astype("int32")
    traingold = getdatamatrix(train, maxlen, 2).astype("int32")
    trainmask = (traindata > 0).astype("float32")

    # test data
    testdata = getdatamatrix(test, maxlen, 0).astype("int32")
    testgold = getdatamatrix(test, maxlen, 2).astype("int32")
    testmask = (testdata > 0).astype("float32")

    res = atiseval(testgold-1, testgold-1, label2idxrev); print res#; exit()

    # define model
    innerdim = [innerdim] * depth
    m = SimpleSeqTransDec(indim=numwords, inpembdim=wordembdim, outembdim=lablembdim, innerdim=innerdim, outdim=numlabels)

    # training
    m = m.train([traindata, shiftdata(traingold), trainmask], traingold).adagrad(lr=lr).grad_total_norm(5.0).seq_cross_entropy().l2(wreg)\
        .split_validate(splits=5, random=True).seq_cross_entropy().seq_accuracy().validinter(validinter).takebest()\
        .train(numbats, epochs)

    # predict after training
    s = SeqTransDecSearch(m)
    testpred, _ = s.decode(testdata)
    testpred = testpred * testmask

    evalres = atiseval(testpred-1, testgold-1, label2idxrev); print evalres
    def test_output_shape(self):
        # settings
        batsize = 10
        seqlen = 5
        invocsize = 50
        inembdim = 50
        outembdim = 40
        innerdim = 11
        outvocsize = 17

        # data
        traindata = np.random.randint(0, invocsize, (batsize, seqlen))
        traingold = np.random.randint(0, outvocsize, (batsize, seqlen))

        # model
        m = SimpleSeqTransDec(indim=invocsize, inpembdim=inembdim, outpembdim=outembdim, innerdim=innerdim, outdim=outvocsize)

        pred = m.predict(traindata, shiftdata(traingold))
        self.assertEqual(pred.shape, (batsize, seqlen, outvocsize))
示例#4
0
    def test_output_shape(self):
        # settings
        batsize = 10
        seqlen = 5
        invocsize = 50
        inembdim = 50
        outembdim = 40
        innerdim = 11
        outvocsize = 17

        # data
        traindata = np.random.randint(0, invocsize, (batsize, seqlen))
        traingold = np.random.randint(0, outvocsize, (batsize, seqlen))

        # model
        m = SimpleSeqTransDec(
            indim=invocsize, inpembdim=inembdim, outpembdim=outembdim, innerdim=innerdim, outdim=outvocsize
        )

        pred = m.predict(traindata, shiftdata(traingold))
        self.assertEqual(pred.shape, (batsize, seqlen, outvocsize))
示例#5
0
    def test_stop_symbol(self):
        m = SimpleSeqTransDec(indim=20,
                              outdim=10,
                              inpembdim=8,
                              outembdim=9,
                              innerdim=11)
        inpseq = np.random.randint(0, 20, (5, 7)).astype("int32")

        searcher = GreedySearch(m, startsymbol=0, stopsymbol=0)
        searcher.init(5)
        out = searcher.search(inpseq)
        self.assertTrue(np.all(out[0] == np.zeros_like(out[0])))
示例#6
0
 def test_recappl_shapes_model_user(self):
     batsize = 100
     model = SimpleSeqTransDec(indim=200,
                               outdim=50,
                               inpembdim=20,
                               outembdim=20,
                               innerdim=[40, 30])
     mu = RecPredictor(model).init(batsize)
     inpval2 = np.random.randint(0, 50, (batsize, )).astype("int32")
     for i in range(5):
         inpval = np.random.randint(0, 200, (batsize, )).astype("int32")
         outpval = mu.feed(inpval, inpval2)
         inpval2 = np.argmax(outpval, axis=1).astype("int32")
         self.assertEqual(outpval.shape, (batsize, 50))