예제 #1
0
import pickle
import tsp_data as tsp
import numpy as np
from tensorflow import keras
import tensorflow as tf


def scheduler(epoch):
    if epoch < nb_epochs/4:
        return learning_rates
    elif epoch < nb_epochs/2:
        return learning_rate*0.5
    return learning_rate*0.1
if __name__=='__main__':
        print("preparing dataset...")
        t = tsp.Tsp()
        X, Y = t.next_batch(3)#10000
        #X.shape
        #(3, 10, 2)  
        #Y.shape
        #(3, 10)        
        x_test, y_test = t.next_batch(2)

        YY = []
        for y in Y:
            YY.append(to_categorical(y))
        YY = np.asarray(YY)

        hidden_size = 128
        seq_len = 10
        nb_epochs = 100
예제 #2
0
파일: run.py 프로젝트: xwang186/NPL_pn
def runIt(hidden_size=128, learning_r=0.1, batch_s=30000, sameLen=True):
    print("preparing dataset...")
    t = tsp.Tsp()
    if sameLen:
        X, Y = t.next_batch(batch_s)
    else:
        X, Y = t.next_small_batch(batch_s)
    x_test, y_test = t.next_batch(1)
    learning_rate = learning_r
    YY = []
    for y in Y:
        YY.append(to_categorical(y))
    YY = np.asarray(YY)
    X_val, Y_val = t.next_batch(1000)
    YY_val = []
    for y in Y_val:
        YY_val.append(to_categorical(y))
    YY_val = np.asarray(YY_val)

    seq_len = 6

    print("building model...")
    main_input = Input(shape=(seq_len, 3), name='main_input')

    encoder = LSTM(output_dim=hidden_size,
                   return_sequences=True,
                   name="encoder")(main_input)
    decoder = PointerLSTM(hidden_size, output_dim=hidden_size,
                          name="decoder")(encoder)

    model = Model(input=main_input, output=decoder)
    model.compile(optimizer='adadelta',
                  loss='categorical_crossentropy',
                  metrics=['accuracy'])
    history = LossHistory()

    #X_val, Y_val = t.next_small_batch(1000)

    model.fit(X,
              YY,
              nb_epoch=nb_epochs,
              batch_size=64,
              callbacks=[LearningRateScheduler(scheduler), history],
              validation_data=(X_val, YY_val))

    score_val = model.evaluate(X_val, YY_val, verbose=0)
    print("Let's do one test!")
    print("The Data I want to predict:")
    print(x_test)
    print("The result should be", y_test)
    print(model.predict(x_test))
    print("------")
    print(to_categorical(y_test))
    model.save_weights('model_weight_100.hdf5')

    #score_train=model.evaluate(X, YY, verbose=0)

    #print('Train score:', score_train[0])
    #print('Train accuracy:', score_train[1])
    print('Test score:', score_val[0])
    print('Test accuracy:', score_val[1])
    history.loss_plot('epoch')