Beispiel #1
0
def gen_text():
    mpath = model_path+"gru"

    model = Model.load(mpath)
    print("loadding model finished")
    outshape = (4, 7)

    print("vocab size: ", vocab.size())

    def do_gen(txt):
        #编码
        #pdb.set_trace()
        res = vocab.encode(sentence=txt)

        m, n = outshape

        for i in range(m*n - 1):
            in_batch = np.array(res).reshape((1, -1))
            preds = model.predict(in_batch)
            #取最后一维的预测结果
            preds = preds[:, -1]
            outs = dlmath.categories_sample(preds, 1)
            res.append(outs[0,0])

        #pdb.set_trace()
        txt = ""
        for i in range(m):
            txt = txt + ''.join(vocab.decode(res[i*n:(i+1)*n])) + "\n"

        return txt


    starts = ['云', '故', '画', '花']
    for txt in starts:
        model.reset()
        res = do_gen(txt)
        print(res)
Beispiel #2
0
    def test_save_load(self):
        print("test model save and load")
        fpath = "./models/testmodel"
        self.model.save(fpath)

        model = Model.load(fpath)