def init_test(): mode_train, mode_test = 'TR', 'TE' dict_obj = set_dict.Dictionary() # train object params_train = set_params.ParamsClass(mode=mode_train) dir_train = set_dir.Directory(mode_train) params_train.num_classes = len(dict_obj.label_dict) # test object params_test = set_params.ParamsClass(mode=mode_test) dir_test = set_dir.Directory(mode_test) params_test.num_instances, params_test.indices = get_length(dir_test.data_filename) params_test.batch_size = 1 params_test.num_classes = len(dict_obj.label_dict) word_emb_path = dir_train.word_embedding word_emb_matrix = np.float32(np.genfromtxt(word_emb_path, delimiter=' ')) params_train.vocab_size = params_test.vocab_size = len(word_emb_matrix) print('***** INITIALIZING TF GRAPH *****') session = tf.Session() with tf.variable_scope("classifier", reuse=None): test_obj = model.DeepAttentionClassifier(params_test, dir_test) model_saver = tf.train.Saver() print('Loading model ...') model_saver.restore(session, set_dir.Directory('TE').test_model) print('**** MODEL LOADED ****\n') return session, test_obj
def __init__(self): self.dataDir = set_dir.Directory('TR').data_path self.vocabDir = set_dir.Directory('TR').vocab_path self.gloveDict = set_dir.Directory('TR').glove_path self.config = set_params.ParamsClass('TR')
def run_train(dict_obj): mode_train, mode_valid, mode_all = 'TR', 'VA', 'ALL' # train object params_train = set_params.ParamsClass(mode=mode_train) dir_train = set_dir.Directory(mode_train) params_train.num_instances, params_train.indices = get_length( dir_train.data_filename) # valid object params_valid = set_params.ParamsClass(mode=mode_valid) dir_valid = set_dir.Directory(mode_valid) params_valid.num_instances, params_valid.indices = get_length( dir_valid.data_filename) params_train.num_classes = params_valid.num_classes = len( dict_obj.label_dict) if params_train.enable_shuffle: random.shuffle(params_train.indices) random.shuffle(params_valid.indices) min_loss = sys.float_info.max word_emb_path = dir_train.word_embedding word_emb_matrix = np.float32(np.genfromtxt(word_emb_path, delimiter=' ')) params_train.vocab_size = params_valid.vocab_size = len(word_emb_matrix) print('***** INITIALIZING TF GRAPH *****') timestamp = str(int(time.time())) train_out_dir = os.path.abspath( os.path.join(dir_train.log_path, "train", timestamp)) valid_out_dir = os.path.abspath( os.path.join(dir_train.log_path, "valid", timestamp)) print("Writing to {}\n".format(train_out_dir)) with tf.Graph().as_default(), tf.Session() as session: # random_normal_initializer = tf.random_normal_initializer() # random_uniform_initializer = tf.random_uniform_initializer(-params_train.init_scale, params_train.init_scale) xavier_initializer = tf.contrib.layers.xavier_initializer( uniform=True, seed=None, dtype=tf.float32) with tf.variable_scope("classifier", reuse=None, initializer=xavier_initializer): train_obj = model.DeepAttentionClassifier(params_train, dir_train) train_writer = tf.summary.FileWriter(train_out_dir, session.graph) valid_writer = tf.summary.FileWriter(valid_out_dir) if not params_train.enable_checkpoint: session.run(tf.global_variables_initializer()) if params_train.enable_checkpoint: ckpt = tf.train.get_checkpoint_state(dir_train.model_path) if ckpt and ckpt.model_checkpoint_path: print("Loading model from: %s" % ckpt.model_checkpoint_path) tf.train.Saver().restore(session, ckpt.model_checkpoint_path) elif not params_train.use_random_initializer: session.run( tf.assign(train_obj.word_emb_matrix, word_emb_matrix, name="word_embedding_matrix")) with tf.variable_scope("classifier", reuse=True, initializer=xavier_initializer): valid_obj = model.DeepAttentionClassifier(params_valid, dir_valid) print('**** TF GRAPH INITIALIZED ****') start_time = time.time() for i in range(params_train.max_max_epoch): lr_decay = params_train.lr_decay**max(i - params_train.max_epoch, 0.0) train_obj.assign_lr(session, params_train.learning_rate * lr_decay) # print(params_train.learning_rate * lr_decay) print('\n++++++++=========+++++++\n') print("Epoch: %d Learning rate: %.5f" % (i + 1, session.run(train_obj.lr))) train_loss, _ = run_epoch(session, train_writer, train_obj.train_op, min_loss, train_obj, dict_obj, i, verbose=True) print("Epoch: %d Train loss: %.3f" % (i + 1, train_loss)) valid_loss, curr_loss = run_epoch(session, valid_writer, tf.no_op(), min_loss, valid_obj, dict_obj, i) if curr_loss < min_loss: min_loss = curr_loss print("Epoch: %d Valid loss: %.3f" % (i + 1, valid_loss)) curr_time = time.time() print('1 epoch run takes ' + str(((curr_time - start_time) / (i + 1)) / 60) + ' minutes.') train_writer.close() valid_writer.close()
def util(): raw_training_file = set_dir.Directory('TR').raw_train_path training_file = set_dir.Directory('TR').data_filename sample_train_file(raw_training_file, training_file, set_params.ParamsClass().sampling_threshold)
import cPickle import re from global_module.settings_module import set_dir, set_params glove_dict = cPickle.load(open(set_dir.Directory('TR').glove_path, 'rb')) config = set_params.ParamsClass('TR') def sample_train_file(raw_training_file, training_file, threshold): raw_training_file_pointer = open(raw_training_file, 'r') training_file_pointer = open(training_file, 'w') word_dict = {} print('\nReading raw training file .... ') for line in raw_training_file_pointer: line = line.rstrip() # line = line.lower() string = re.split(r'\t', line) size = len(string) tokenized_training_string = '' for j in range(size): # string[j] = re.sub(r'#[0-9]+', r'', string[j].strip()) tokenized_sent = string[j].split(" ") tokenized_string = ' '.join(tokenized_sent) tokenized_training_string += tokenized_string + '\t' for token in tokenized_sent: if token not in word_dict:
def main(): params = set_params.ParamsClass(mode='TR') dir_obj = set_dir.Directory('TR') classifier_obj = DeepAttentionClassifier(params, dir_obj)
def __init__(self): self.glove_dict = cPickle.load( open(set_dir.Directory('TR').glove_path, 'rb')) self.config = set_params.ParamsClass('TR')