def load_encoder_and_decoder(): ## Define encoder input = Input(shape=(None, ), name='inp') embedding = Embedding(util.VOCAB_SIZE, EMBEDDING_SIZE, input_length=None) embedded = embedding(input) encoder = LSTM(LSTM_CAPACITY) h = Bidirectional(encoder)(embedded) tozmean = Dense(HIDDEN_LAYER_SIZE) zmean = tozmean(h) tozlsigma = Dense(HIDDEN_LAYER_SIZE) zlsigma = tozlsigma(h) ## Define KL Loss and sampling kl = KLLayer(weight = K.variable(1.0)) # computes the KL loss and stores it for later zmean, zlsigma = kl([zmean, zlsigma]) eps = Input(shape=(HIDDEN_LAYER_SIZE,), name='inp-epsilon') sample = Sample() zsample = sample([zmean, zlsigma, eps]) ## Define decoder input_shifted = Input(shape=(None, ), name='inp-shifted') expandz_h = Dense(LSTM_CAPACITY, input_shape=(HIDDEN_LAYER_SIZE,)) z_exp_h = expandz_h(zsample) expandz_c = Dense(LSTM_CAPACITY, input_shape=(HIDDEN_LAYER_SIZE,)) z_exp_c = expandz_c(zsample) state = [z_exp_h, z_exp_c] seq = embedding(input_shifted) seq = SpatialDropout1D(rate=0.5)(seq) decoder_rnn = LSTM(LSTM_CAPACITY, return_sequences=True) h = decoder_rnn(seq, initial_state=state) towords = TimeDistributed(Dense(util.VOCAB_SIZE)) out = towords(h) auto = Model([input, input_shifted, eps], out) ## Extract the encoder and decoder models form the autoencoder encoder = Model(input, [zmean, zlsigma]) encoder.load_weights("./py/lstm_sentence_level_chatbot/encoder.h5") z_in = Input(shape=(HIDDEN_LAYER_SIZE,)) s_in = Input(shape=(None,)) seq = embedding(s_in) z_exp_h = expandz_h(z_in) z_exp_c = expandz_c(z_in) state = [z_exp_h, z_exp_c] h = decoder_rnn(seq, initial_state=state) out = towords(h) decoder = Model([s_in, z_in], out) decoder.load_weights("./py/lstm_sentence_level_chatbot/decoder.h5") return (encoder, auto, decoder)
def main(argv): global PATH_IN, PATH_SCRIPT, PATH_OUT PATH_IN, PATH_SCRIPT, PATH_OUT = get_path() if (len(argv) == 2): from keras.models import model_from_json fileX = argv[0] X_minmax, X_meanvar, t = load_(fileX) json_file = open("model/LSTM.json", 'r') loaded_model_json = json_file.read() json_file.close() LSTM = model_from_json(loaded_model_json) # load weights into new model LSTM.compile(loss='binary_crossentropy', optimizer='adamax', metrics=['accuracy']) LSTM.load_weights("model/LSTM.h5") # Report("Loaded LSTM from disk") res = pd.DataFrame( LSTM.predict_proba( np.reshape(X_minmax, (X_minmax.shape[0], 1, X_minmax.shape[1])))) res.to_csv(str(fileX.split('.')[0]) + '_temp_LSTM.csv', index=False) return res else: if (len(argv) == 0): argv = [0.35] THRESHOLD = float(argv[0]) ##### get files names ### names = pd.read_csv('files.csv') fileX_train = literal_eval(names['fileX_train'][0]) fileY_train = literal_eval(names['fileY_train'][0]) fileX_valid = literal_eval(names['fileX_valid'][0]) fileY_valid = literal_eval(names['fileY_valid'][0]) fileX_test = literal_eval(names['fileX_test'][0]) fileY_test = literal_eval(names['fileY_test'][0]) X_train, Y_train, _ = load(fileX_train, fileY_train) X_valid, Y_valid, _ = load(fileX_valid, fileY_valid) X_test, Y_test, t = load(fileX_test, fileY_test) model, hist = model_fit(X_train, Y_train, X_valid, Y_valid) import matplotlib.pyplot as plt plt.plot(hist.losses, 'b', hist.val_losses, 'r') plt.savefig('plot3.png') plt.close() plt.plot(hist.fbeta, 'b', hist.val_fbeta, 'r') plt.savefig('plot4.png') pred = model.predict_proba(X_test) pred = np.clip(pred, 0, 1) testPredict = list([1 if i[0] > THRESHOLD else 0 for i in pred]) # plot results plot_res(t, testPredict, Y_test) pred_valid = model.predict_proba(X_valid) res_valid = pd.DataFrame(pred_valid) res_valid.to_csv('LSTM_valid.csv', index=False) res = pd.DataFrame(pred) res.to_csv('LSTM.csv', index=False) model_json = model.to_json() with open("model/LSTM.json", "w") as json_file: json_file.write(model_json) # serialize weights to HDF5 model.save_weights("model/LSTM.h5") return res