def generate_main(start_seed=None): # if no seed was found create a default empty sequence if start_seed is None: start_seed = [SONG_BEGIN] print("Initializing...") # prepare environment if not os.path.isdir('./checkpoints/'): print("LSTM model must be trained first.") exit(1) if not os.path.isdir('./data/'): print("LSTM model must be trained first.") exit(1) print("Restoring vocabulary and model...") vocabulary_info = io.read_vocabulary_info_json(config.vocabulary_path) data_transformer = DatasetTransformer(vocabulary_info) # create model instance (vocabulary length + 3 is for synthetic words like SONG_BEGIN) lyrics_model = LSTMModel(len(vocabulary_info) + 3) lyrics_model.set_dataset_transformer(data_transformer) print("Generating...") text = lyrics_model.generate_text(data_transformer, start_seed) print("Generated Song:\n", text, "\n") return 0
def save(artist, model_path, num_save): sample_save_dir = c.get_dir('../save/samples/') sess = tf.Session() print artist data_reader = DataReader(artist) vocab = data_reader.get_vocab() print 'Init model...' model = LSTMModel(sess, vocab, c.BATCH_SIZE, c.SEQ_LEN, c.CELL_SIZE, c.NUM_LAYERS, test=True) saver = tf.train.Saver() sess.run(tf.initialize_all_variables()) saver.restore(sess, model_path) print 'Model restored from ' + model_path artist_save_dir = c.get_dir(join(sample_save_dir, artist)) for i in xrange(num_save): print i path = join(artist_save_dir, str(i) + '.txt') sample = model.generate() processed_sample = process_sample(sample) with open(path, 'w') as f: f.write(processed_sample)
def __init__(self, model_load_path, artist_name, test, prime_text): self.sess = tf.Session() self.artist_name = artist_name print 'Process data...' self.data_reader = DataReader(self.artist_name) self.vocab = self.data_reader.get_vocab() print 'Init model...' self.model = LSTMModel(self.sess, self.vocab, c.BATCH_SIZE, c.SEQ_LEN, c.CELL_SIZE, c.NUM_LAYERS, test=test) print 'Init variables...' self.saver = tf.train.Saver(max_to_keep=None) self.sess.run(tf.global_variables_initializer()) # if load path specified, load a saved model if model_load_path is not None: self.saver.restore(self.sess, model_load_path) print 'Model restored from ' + model_load_path if test: self.test(prime_text) else: self.train()
class LyricGenRunner: def __init__(self, model_load_path, artist_name, test, prime_text): self.sess = tf.Session() self.artist_name = artist_name print 'Process data...' self.data_reader = DataReader(self.artist_name) self.vocab = self.data_reader.get_vocab() print 'Init model...' self.model = LSTMModel(self.sess, self.vocab, c.BATCH_SIZE, c.SEQ_LEN, c.CELL_SIZE, c.NUM_LAYERS, test=test) print 'Init variables...' self.saver = tf.train.Saver(max_to_keep=None) self.sess.run(tf.initialize_all_variables()) if model_load_path is not None: self.saver.restore(self.sess, model_load_path) print 'Model restored from ' + model_load_path if test: self.test(prime_text) else: self.train() def train(self): while True: inputs, targets = self.data_reader.get_train_batch( c.BATCH_SIZE, c.SEQ_LEN) print 'Training model...' feed_dict = { self.model.inputs: inputs, self.model.targets: targets } global_step, loss, _ = self.sess.run( [self.model.global_step, self.model.loss, self.model.train_op], feed_dict=feed_dict) print 'Step: %d | loss: %f' % (global_step, loss) if global_step % c.MODEL_SAVE_FREQ == 0: print 'Saving model...' self.saver.save(self.sess, join(c.MODEL_SAVE_DIR, self.artist_name + '.ckpt'), global_step=global_step) def test(self, prime_text): sample = self.model.generate(prime=prime_text) print sample
def __init__(self, model_load_path, artist_name, test, prime_text): """ Initializes the Lyric Generation Runner. @param model_load_path: The path from which to load a previously-saved model. Default = None. @param artist_name: The name of the artist on which to train. (Used to grab data). Default = 'kanye_west' @param test: Whether to test or train the model. Testing generates a sequence from the provided model and artist. Default = False. @param prime_text: The text with which to start the test sequence. """ self.sess = tf.Session() self.artist_name = artist_name print 'Process data...' self.data_reader = DataReader(self.artist_name) self.vocab = self.data_reader.get_vocab() print 'Init model...' self.model = LSTMModel(self.sess, self.vocab, c.BATCH_SIZE, c.SEQ_LEN, c.CELL_SIZE, c.NUM_LAYERS, test=test) print 'Init variables...' self.saver = tf.train.Saver(max_to_keep=None) self.sess.run(tf.global_variables_initializer()) # if load path specified, load a saved model if model_load_path is not None: self.saver.restore(self.sess, tf.train.latest_checkpoint(model_load_path)) print 'Model restored from ' + model_load_path if test: self.test(prime_text) else: self.train()
def run_lstm(data, test): model = LSTMModel() model.build_model(configs) steps_per_epoch = math.ceil( (data.len_train - configs['data']['sequence_length']) / configs['training']['batch_size']) model.train_generator( data_gen=data.generate_train_batch( seq_len=configs['data']['sequence_length'], batch_size=configs['training']['batch_size'], normalise=configs['data']['normalise'] ), epochs=configs['training']['epochs'], batch_size=configs['training']['batch_size'], steps_per_epoch=steps_per_epoch, ) predictions = model.predict_point_by_point(test) print(predictions.shape) return predictions.tolist()
def __init__(self, filepath): super().__init__() # here we make a computational graph suited optimal RNN model # then we load the .pt file and we are all set # create model INPUT_DIM, OUTPUT_DIM = 8, 3 HID_DIM, N_LAYERS, DROPOUT = 16, 2, 0 DEVICE = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") model = LSTMModel(INPUT_DIM, HID_DIM, OUTPUT_DIM, N_LAYERS, DROPOUT, DEVICE) model = model.to(DEVICE) # load file model.load_state_dict(torch.load(filepath)) model.eval() # set sttribute self.model = model # self.criterion = nn.MSELoss().to(DEVICE) #nn.SmoothL1Loss().to(DEVICE)
def train_main(): print("Initializing...") # prepare environment if not os.path.isdir('./checkpoints/'): os.makedirs('./checkpoints/') if not os.path.isdir('./data/'): os.makedirs('./data/') print("Creating model...") # load dataset lyrics_dataset = LyricsDataset() # create word <-> id encoder/decoder data_transformer = DatasetTransformer(lyrics_dataset.vocabulary_info) # create model instance lyrics_model = LSTMModel(lyrics_dataset.vocabulary_size()) lyrics_model.set_dataset_transformer(data_transformer) print("Preparing dataset...") # encode words to ids dataset = data_transformer.transform_encode(lyrics_dataset.dataset) # split dataset to train and test sequences (not used) #train_data, test_data = split_train_test(dataset, test_data_ratio=0.3) print("Training...") # train the model on ALL the sequences and evaluate # on the same data to force the RNN to learn all the # sequences, this is a generation effort after all lyrics_model.fit(dataset, dataset) # train the model on the train sequences # after the first few epochs train on train sequences #lyrics_model.fit(train_data, test_data) return 0
#-*- coding: UTF-8 -*- import sys from Dataset import * from LSTMModel import LSTMModel dataname = sys.argv[1] classes = sys.argv[2] voc = Wordlist('../data/' + dataname + '/wordlist.txt') trainset = Dataset('../data/' + dataname + '/train.txt', voc) devset = Dataset('../data/' + dataname + '/dev.txt', voc) print('data loaded.') model = LSTMModel(voc.size, trainset, devset, dataname, classes, None) model.train(100) print( '****************************************************************************' ) print('test 1') result = model.test() print( '****************************************************************************' ) print('\n') for i in range(1, 400): model.train(100) print( '****************************************************************************' ) print('test', i + 1) newresult = model.test()
def train(self,x_trData, y_trData, x_tsData, y_tsData): tAV = list(); tPV = list(); tL = list(); indexValue = 0; lossIndex = 0 outW = np.random.randn(1, 20) / np.sqrt(1) lstmRnnObject = LSTMModel(1, 20) inValues = np.zeros((19, 1)) tValues = np.zeros((19, 1)) changedOWeights = np.zeros_like(outW) for runs in range(2000): if indexValue + 19 + 1 >= len(x_trData): indexValue = 0 lstmRnnObject.resetValues() inValues[:, 0] = x_trData[indexValue, :] tValues[:, 0] = y_trData[indexValue] tAV.append(np.mean(np.square(tValues))) lstmRnnObject.forwardPropogation(inValues) hLOutput = lstmRnnObject.getHiddenLayerValues() output = hLOutput.dot(outW.T) differenceValue = output - tValues tPV.append(np.mean(output)) changedBackWeights = (differenceValue).T.dot(hLOutput) loss = np.mean(np.square(output - tValues)) tL.append(loss) backPropAnswer = (differenceValue).dot(outW) lstmRnnObject.backwardPropogation(backPropAnswer) lstmRnnObject.trainModel(0.005) for i, j, memory in zip([outW], [changedBackWeights], [changedOWeights]): memory += j * j i += -0.005 * j / np.sqrt(memory + 1e-8) print(lossIndex,loss) indexValue = indexValue+1; lossIndex = lossIndex+1 print("Total Training Loss: ", str(np.sum(tL))) print("Training Accuracy for our system: ", 100 - (abs(statistics.mean([tPV_i - tAV_i for tPV_i, tAV_i in zip(tPV, tAV)]))) * 100) self.test(x_tsData,y_tsData,outW,lstmRnnObject)
class LyricGenRunner: def __init__(self, model_load_path, artist_name, test, prime_text): """ Initializes the Lyric Generation Runner. @param model_load_path: The path from which to load a previously-saved model. Default = None. @param artist_name: The name of the artist on which to train. (Used to grab data). Default = 'kanye_west' @param test: Whether to test or train the model. Testing generates a sequence from the provided model and artist. Default = False. @param prime_text: The text with which to start the test sequence. """ self.sess = tf.Session() self.artist_name = artist_name print('Process data...') #Py3Upgrade self.data_reader = DataReader(self.artist_name) self.vocab = self.data_reader.get_vocab() print('Init model...') #Py3Upgrade self.model = LSTMModel(self.sess, self.vocab, c.BATCH_SIZE, c.SEQ_LEN, c.CELL_SIZE, c.NUM_LAYERS, test=test) print('Init variables...') #Py3Upgrade self.saver = tf.train.Saver(max_to_keep=None) self.sess.run(tf.global_variables_initializer()) # if load path specified, load a saved model if model_load_path is not None: self.saver.restore(self.sess, model_load_path) print('Model restored from ', model_load_path) #Py3Upgrade if test: self.test(prime_text) else: self.train() def train(self): """ Runs a training loop on the model. """ for i in range( 30000): #changed from True while loop to 30,000 for loop inputs, targets = self.data_reader.get_train_batch( c.BATCH_SIZE, c.SEQ_LEN) print('Training model...') #Py3Upgrade feed_dict = { self.model.inputs: inputs, self.model.targets: targets } global_step, loss, _ = self.sess.run( [self.model.global_step, self.model.loss, self.model.train_op], feed_dict=feed_dict) print('Step: %d | loss: %f' % (global_step, loss)) #Py3Upgrade if global_step % c.MODEL_SAVE_FREQ == 0: print('Saving model...') #Py3Upgrade self.saver.save(self.sess, join(c.MODEL_SAVE_DIR, self.artist_name + '.ckpt'), global_step=global_step) def test(self, prime_text): """ Generates a text sequence. """ # generate and save sample sequence sample = self.model.generate(prime=prime_text) print(sample) #Py3Upgrade # save sample as txt file f = open("../save/outputs/dirty/" + self.artist_name + ".txt", "w+") f.write(sample) f.close()
#-*- coding: UTF-8 -*- import sys from Dataset import * from LSTMModel import LSTMModel classes=19 voc = Wordlist('file//voc.pkl') trainset=None #trainset = Dataset('data\\'+dataname+'\\train.txt', voc ) testset = Dataset('file//testset.pkl', voc) print 'data loaded.' model = LSTMModel(voc.size, trainset, devset,classes,'model') print 'model loaded.' preds, label,_=model.test()
#-*- coding: UTF-8 -*- import pandas as pd from Dataset import * from LSTMModel import LSTMModel classes = 19 voc = Wordlist('file/voc.pkl') devset = Dataset('file/devset.pkl', voc) trainset = Dataset('file/trainset.pkl', voc) print 'data loaded.' print '****************************************************************************' model = LSTMModel(voc.size, trainset, devset, classes, 'model') print '****************************************************************************' model.train() print '****************************************************************************' #print 'test 1' _, _, f1 = model.test() model.save('model') print '****************************************************************************' print '\n' for i in xrange(1, 98): model.train() print '****************************************************************************' print 'test', i + 1 _, _, newf1 = model.test() print 'f1:', f1, 'newf1:', newf1 print '****************************************************************************'
print("~~~~~~~~~~~~~~BASE MARKOV MODEL~~~~~~~~~~~~~~") BaseModel = BaseMarkovModel(file_path, taggedTokens) final_rap = "" for i in range(3): verse = BaseModel.createVerse(9, 6) final_rap += verse + "\n" print(verse) with open(str(artist) + "_base_markov_rap.txt", 'w', encoding='utf8') as f: [f.write(final_rap)] print("\nFinished writing to " + str(artist) + "_base_markov_rap.txt!") # LSTM Model print("~~~~~~~~~~~~~~LSTM MODEL~~~~~~~~~~~~~~") neural_network_model = LSTMModel(artist, file_path, batch_size, epochs, max_syllables, training=False) num_lines = 24 # Number of lines after the seed line (number of lines to generate = num_lines + 1) if neural_network_model.training: lyrics = neural_network_model.get_artist_lyrics(file_path) else: lyrics = neural_network_model.generate_lyrics() rhyming_endings = neural_network_model.get_rhyming_endings(lyrics) rap_analytics = neural_network_model.get_lyric_analytics( lyrics, rhyming_endings)
def main(): with open(config.word2id_vocab_file, 'r', encoding='utf-8') as fd: word2id = json.load(fd) with open(config.id2word_vocab_file, 'r', encoding='utf-8') as fd: id2word = json.load(fd) if model_type == 'bilstm': model = LSTMModel() params = list(model.encoder.parameters()) + list(model.decoder.parameters()) + \ list(model.reduce_state.parameters()) data_loader_train = get_loader(config.train_data_file, word2id, max_enc_steps=config.max_enc_steps, max_dec_steps=config.max_dec_steps, batch_size=config.batch_size) data_loader_val = get_loader(config.val_data_file, word2id, max_enc_steps=config.max_enc_steps, max_dec_steps=config.max_dec_steps, batch_size=config.batch_size) data_loader_test = get_loader(config.test_data_file, word2id, max_enc_steps=config.max_enc_steps, max_dec_steps=config.max_dec_steps, batch_size=1, shuffle=False) elif model_type == 'gcn': model = GCNModel() params = list(model.encoder.parameters()) + list( model.decoder.parameters()) + list(model.reduce_state.parameters()) data_loader_train = get_gcn_loader(config.train_data_file, word2id, max_enc_steps=config.max_enc_steps, max_dec_steps=config.max_dec_steps, batch_size=config.batch_size) data_loader_val = get_gcn_loader(config.val_data_file, word2id, max_enc_steps=config.max_enc_steps, max_dec_steps=config.max_dec_steps, batch_size=config.batch_size) data_loader_test = get_gcn_loader(config.test_data_file, word2id, max_enc_steps=config.max_enc_steps, max_dec_steps=config.max_dec_steps, batch_size=1, shuffle=False) elif model_type == 'gtr' or model_type == 'gtr2': model = GTRModel2() # model = GTRModel() params = list(model.encoder.parameters()) + list( model.decoder.parameters()) + list(model.reduce_state.parameters()) data_loader_train = get_gtr_loader(config.train_data_file, word2id, max_enc_steps=config.max_enc_steps, max_dec_steps=config.max_dec_steps, batch_size=config.batch_size) data_loader_val = get_gtr_loader(config.val_data_file, word2id, max_enc_steps=config.max_enc_steps, max_dec_steps=config.max_dec_steps, batch_size=config.batch_size) data_loader_test = get_gtr_loader(config.test_data_file, word2id, max_enc_steps=config.max_enc_steps, max_dec_steps=config.max_dec_steps, batch_size=1, shuffle=False) elif model_type == 'gcn_gtr2': model = GCNGTR2Model() params = list(model.encoder_gcn.parameters()) + list(model.encoder_gtr.parameters()) + \ list(model.decoder.parameters()) + list(model.reduce_state.parameters()) data_loader_train = get_gcn_gtr_loader( config.train_data_file, word2id, max_enc_steps=config.max_enc_steps, max_dec_steps=config.max_dec_steps, batch_size=config.batch_size) data_loader_val = get_gcn_gtr_loader( config.val_data_file, word2id, max_enc_steps=config.max_enc_steps, max_dec_steps=config.max_dec_steps, batch_size=config.batch_size) data_loader_test = get_gcn_gtr_loader( config.test_data_file, word2id, max_enc_steps=config.max_enc_steps, max_dec_steps=config.max_dec_steps, batch_size=1, shuffle=False) num_nodes_batch = [] for i, (src_inputs, trg_seqs, trg_lengths) in enumerate(data_loader_train): src_gcn_inputs = src_inputs['src_gcn_inputs'] src_gcn_inputs = list( DataLoader(src_gcn_inputs, len(src_gcn_inputs)))[0].to(device) num_nodes_batch.append(src_gcn_inputs.num_nodes) initial_lr = config.lr_coverage if config.is_coverage else config.lr optimizer = optim.Adam(params, lr=initial_lr) # optimizer = optim.Adam(params, lr=initial_lr, initial_accumulator_value=config.adagrad_init_acc) # best_valid_loss = 1.19 best_valid_loss = float('inf') # if load_model: # if model_type=='gcn_lstm': # model.encoder_gcn.load_state_dict(torch.load(config.save_model_path + '_encoder_gcn', map_location={'cuda:1':'cuda:3'})) # model.encoder_lstm.load_state_dict(torch.load(config.save_model_path + '_encoder_lstm', map_location={'cuda:1':'cuda:3'})) # else: # model.encoder.load_state_dict(torch.load(config.save_model_path + '_encoder')) # # model.decoder.load_state_dict(torch.load(config.save_model_path + '_decoder', map_location={'cuda:1':'cuda:3'})) # model.reduce_state.load_state_dict(torch.load(config.save_model_path + '_reduce_state', map_location={'cuda:1':'cuda:3'})) if config.load_model: if model_type == 'gcn_gtr2': # model.encoder_gcn.load_state_dict(torch.load(config.save_model_path + '_encoder_gcn')) model.encoder_gcn.load_state_dict( torch.load(config.save_model_path + '_encoder_gcn', map_location=torch.device('cpu'))) # model.encoder_gtr.load_state_dict(torch.load(config.save_model_path + '_encoder_gtr')) model.encoder_gtr.load_state_dict( torch.load(config.save_model_path + '_encoder_gtr', map_location=torch.device('cpu'))) else: model.encoder.load_state_dict( torch.load(config.save_model_path + '_encoder', map_location='cpu')) # model.decoder.load_state_dict(torch.load(config.save_model_path + '_decoder')) model.decoder.load_state_dict( torch.load(config.save_model_path + '_decoder', map_location=torch.device('cpu'))) # model.reduce_state.load_state_dict(torch.load(config.save_model_path + '_reduce_state')) model.reduce_state.load_state_dict( torch.load(config.save_model_path + '_reduce_state', map_location=torch.device('cpu'))) if config.mode == 'train': for epoch in range(config.n_epoch): train_loss = train(model, data_loader_train, optimizer, epoch=epoch, id2word=id2word) valid_loss = evaluate(model, data_loader_val) print('Train Loss: {} | Train PPL: {}'.format( train_loss, math.exp(train_loss))) print('Val. Loss: {} | Val. PPL: {}'.format( valid_loss, math.exp(valid_loss))) if valid_loss < best_valid_loss: best_valid_loss = valid_loss if model_type == 'gcn_gtr2': torch.save(model.encoder_gcn.state_dict(), config.save_model_path + '_encoder_gcn') torch.save(model.encoder_gtr.state_dict(), config.save_model_path + '_encoder_gtr') else: torch.save(model.encoder.state_dict(), config.save_model_path + '_encoder') torch.save(model.decoder.state_dict(), config.save_model_path + '_decoder') torch.save(model.reduce_state.state_dict(), config.save_model_path + '_reduce_state') elif config.mode == 'translate': # beam_search(model, data_loader_test, word2id, id2word, rplc_dict) beam_search(model, data_loader_test, word2id, id2word)
usrdict = Usrlist('../../../data/' + dataname + '/usrlist.txt') prddict = Prdlist('../../../data/' + dataname + '/prdlist.txt') trainset = Dataset('../../../data/' + dataname + '/train.txt', voc, usrdict, prddict, maxbatch=16) devset = Dataset('../../../data/' + dataname + '/test.txt', voc, usrdict, prddict, maxbatch=16) print 'data loaded.' model = LSTMModel(voc.size, usrdict.size, prddict.size, trainset, devset, dataname, classes, None) model.train(100) print '****************************************************************************' print 'test 1' result = model.test() print '****************************************************************************' print '\n' for i in xrange(1, 400): model.train(100) print '****************************************************************************' print 'test', i + 1 newresult = model.test() print '****************************************************************************' print '\n' if newresult[0] > result[0]: result = newresult
class LyricGenRunner: def __init__(self, model_load_path, artist_name, test, prime_text): """ Initializes the Lyric Generation Runner. @param model_load_path: The path from which to load a previously-saved model. Default = None. @param artist_name: The name of the artist on which to train. (Used to grab data). Default = 'kanye_west' @param test: Whether to test or train the model. Testing generates a sequence from the provided model and artist. Default = False. @param prime_text: The text with which to start the test sequence. """ self.sess = tf.Session() self.artist_name = artist_name print 'Process data...' self.data_reader = DataReader(self.artist_name) self.vocab = self.data_reader.get_vocab() print 'Init model...' self.model = LSTMModel(self.sess, self.vocab, c.BATCH_SIZE, c.SEQ_LEN, c.CELL_SIZE, c.NUM_LAYERS, test=test) print 'Init variables...' self.saver = tf.train.Saver(max_to_keep=None) self.sess.run(tf.global_variables_initializer()) # if load path specified, load a saved model if model_load_path is not None: self.saver.restore(self.sess, model_load_path) print 'Model restored from ' + model_load_path if test: self.test(prime_text) else: self.train() def train(self): """ Runs a training loop on the model. """ keep_training = True while keep_training: inputs, targets = self.data_reader.get_train_batch( c.BATCH_SIZE, c.SEQ_LEN) print 'Training model...' feed_dict = { self.model.inputs: inputs, self.model.targets: targets } global_step, loss, _ = self.sess.run( [self.model.global_step, self.model.loss, self.model.train_op], feed_dict=feed_dict) if global_step == c.MAX_STEPS: keep_training = False print 'Step: %d | loss: %f' % (global_step, loss) if global_step % c.MODEL_SAVE_FREQ == 0 or not keep_training: print 'Saving model...' self.saver.save(self.sess, join(c.MODEL_SAVE_DIR, self.artist_name + '.ckpt'), global_step=global_step) def test(self, prime_text): """ Generates a text sequence. """ # generate and save sample sequence sample = self.model.generate(prime=prime_text, num_out=200).replace( '.', '.\n').replace(',', ',\n ') print sample
print("*** PRE-PROCESSING TOOK --- %s seconds ---" % (time.time() - start_time)) # we will always train on all dirs, but each run of # this program is tasked (via this variable) to create the features for just the pairwise Mentions in this dir print "# unique types: " + str(len(l.uniqueTypes)) print "*** size of corpus: " + str(len(l.corpusTokenIDs)) with tf.Graph().as_default(), tf.Session() as session: initializer = tf.random_uniform_initializer(-0.10, 0.10) with tf.variable_scope("model", reuse = None, initializer = initializer): mtrain = LSTMModel(dev_name, len(l.uniqueTypes), batch_size, l.num_steps, l.hidden_size, l.num_layers, l.max_grad_norm, l.decay_rate, is_training = True) with tf.variable_scope("model", reuse = True, initializer = initializer): mtest = LSTMModel(dev_name, len(l.uniqueTypes), 1, 1, l.hidden_size, l.num_layers, l.max_grad_norm, l.decay_rate, is_training = False) tf.initialize_all_variables().run() # Add ops to save and restore all the variables. saver = tf.train.Saver() #print "hiddenStateIndices: " + str(l.hiddenStateIndices) #print "corpusTokenIDs" + str(l.corpusTokenIDs) start_time = time.time() for i in range(l.num_epochs): sys.stdout.flush() lr_decay = l.decay_rate ** max(i-l.num_init_epoch, 0.0) session.run(tf.assign(mtrain._lr, l.learning_rate*lr_decay)) train_perplexity = run_epoch(session, mtrain, l.corpusTokenIDs, l.corpusTokens, l.hiddenStateIndices, mtrain._train_op, write = False)
#-*- coding: UTF-8 -*- import sys from Dataset import * from LSTMModel import LSTMModel dataname = sys.argv[1] classes = sys.argv[2] voc = Wordlist('../data/'+dataname+'/wordlist.txt') usrdict = Usrlist('../data/'+dataname+'/usrlist.txt') prddict = Prdlist('../data/'+dataname+'/prdlist.txt') testset = Dataset('../data/'+dataname+'/test.txt', voc, usrdict, prddict) trainset = [] print 'data loaded.' model = LSTMModel(voc.size, usrdict.size, prddict.size, trainset, testset, dataname, classes, '../model/'+dataname+'/bestmodel') print 'model loaded.' model.test()
# -*- coding: utf-8 -*- from flask import Flask, request, render_template from runner import LyricGenRunner from LSTMModel import LSTMModel from data_reader import DataReader import tensorflow as tf import constants as c app = Flask(__name__) sess = tf.Session() artist_name = 'all_lyrics' data_reader = DataReader(artist_name) vocab = data_reader.get_vocab() model = LSTMModel(sess, vocab, c.BATCH_SIZE, c.SEQ_LEN, c.CELL_SIZE, c.NUM_LAYERS, test=True) saver = tf.train.Saver(max_to_keep=None) sess.run(tf.global_variables_initializer()) load_path = '/home/ubuntu/eight/encore.ai/save/models/test/all_lyrics.ckpt-25000' saver.restore(sess, load_path) @app.route('/') def eight(): with open('/home/ubuntu/eight/templates/valentine2.htm', 'r', encoding='cp1252') as f: return f.read() #return render_template('valentine2.htm') #return render_template('predict.html') @app.route('/', methods=['POST']) def eight_post():