Exemplo n.º 1
0
def namer_gru_c2v():
    # np.random.seed(1)
    # X_train, y_train, char_to_index, index_to_char = load_training_data()
    X_train, y_train, char_to_index, index_to_char = load_sample_training_data(
        1)
    char_num = len(char_to_index.keys())
    print char_num

    name_num = 30
    name_len = 3
    # first_name = u"宋"
    # first_name = u"董"
    first_name = u"陈"
    if first_name not in char_to_index:
        print "暂时不支持这个姓,Sorry!!!"
    else:
        print "支持这个姓,请稍等 ... ..."
        model = gru.GRUTheano(char_num)
        gru.load_model_parameters_theano(
            'model/gru_c2v/GRU-2017-01-22-12-09-5264-48-128.dat.npz', model)
        ignore_num = 0
        i = 0
        while i <= name_num:
            name = [first_name]
            try:
                name = generate_name(model, name_len, first_name, X_train,
                                     y_train, char_to_index, index_to_char)
            except ValueError as ve:
                ignore_num += 1
                continue
            print " ".join(name)
            i += 1
        print "ignored samples: ", ignore_num
Exemplo n.º 2
0
def namer_rnn_one_hot():
    # np.random.seed(1)
    # X_train, y_train, char_to_index, index_to_char = load_training_data()
    X_train, y_train, char_to_index, index_to_char = load_sample_training_data(
        0)
    char_num = len(char_to_index.keys())
    print char_num

    name_num = 10
    name_len = 3
    # first_name = u"宋"
    # first_name = u"董"
    first_name = u"陈"
    if first_name not in char_to_index:
        print "暂时不支持这个姓,Sorry!!!"
    else:
        print "支持这个姓,请稍等 ... ..."
        model = rnn.RNNTheano(char_num)
        # losses = train_with_sgd(model, X_train, y_train, nepoch=50)
        # save_model_parameters_theano('./data/trained-model-theano.npz', model)
        rnn.load_model_parameters_theano(
            'model/rnn_one_hot/model-80-3296-2017-01-14-21-30-39.npz', model)
        ignore_num = 0
        i = 0
        while i <= name_num:
            name = [first_name]
            try:
                name = generate_name(model, name_len, first_name, X_train,
                                     y_train, char_to_index, index_to_char)
            except ValueError as ve:
                ignore_num += 1
                continue
            print " ".join(name)
            i += 1
        print "ignored samples: ", ignore_num
Exemplo n.º 3
0
def namer_lstm_c2v():
    # np.random.seed(1)
    # X_train, y_train, char_to_index, index_to_char = load_training_data()
    X_train, y_train, char_to_index, index_to_char = load_sample_training_data(
        1)
    char_num = len(char_to_index.keys())
    print char_num

    name_num = 10
    name_len = 3
    # first_name = u"宋"
    # first_name = u"董"
    first_name = u"陈"
    if first_name not in char_to_index:
        print "暂时不支持这个姓,Sorry!!!"
    else:
        print "支持这个姓,请稍等 ... ..."
        model = lstm.LSTM()
        model.sample_name(
            first_name=first_name,
            ckpt_file="model/lstm_c2v/LSTM-2017-01-29-20-00-6569-400-128.ckpt")
Exemplo n.º 4
0
    print "Saved model parameters to %s." % outfile


def load_model_parameters_theano(path, model):
    npzfile = np.load(path)
    U, V, W = npzfile["U"], npzfile["V"], npzfile["W"]
    model.hidden_dim = U.shape[0]
    model.word_dim = U.shape[1]
    model.U.set_value(U)
    model.V.set_value(V)
    model.W.set_value(W)
    print "Loaded model parameters from %s. hidden_dim=%d word_dim=%d" % (path, U.shape[0], U.shape[1])


if __name__ == '__main__':
    X_train, y_train, char_to_index, index_to_char = load_sample_training_data(1)
    # X_train, y_train, char_to_index, index_to_char = load_training_data()
    char_num = len(char_to_index.keys())

    vocabulary_size = char_num
    model = RNNTheano(vocabulary_size, hidden_dim=HIDDEN_DIM)
    t1 = time.time()
    model.sgd_step(X_train[10], y_train[10], LEARNING_RATE)
    t2 = time.time()
    print "SGD Step time: %f milliseconds" % ((t2 - t1) * 1000.)

    if MODEL_FILE:
        load_model_parameters_theano(MODEL_FILE, model)

    train_with_sgd(model, X_train, y_train, nepoch=NEPOCH, learning_rate=LEARNING_RATE)