Example #1
0
    def _build_encoder(self):
        source = self.encoder_input_data

        if (self.time_major):
            source = tf.transpose(source)
        self.source_test = source
        encoder_emb_inp = tf.nn.embedding_lookup(self.embedding_encoder,
                                                 source)

        num_bi_layers = int(self.num_layers / 2)
        _log_info('encoder_type:bi-lstm, num_bi_layers:%d.' % (num_bi_layers))

        encoder_outputs, bi_encoder_state = _mh.create_bidirectional_RNN(self.unit_type, self.num_units, num_bi_layers, self.dropout,\
                                                                             self.mode, self.time_major, self.forget_bias,\
                                                                             encoder_emb_inp, sequence_length=self.seq_length_encoder_input_data,\
                                                                             dtype=self.dtype)
        '''
        if num_bi_layers > 1:
            fw_c,fw_h = bi_encoder_state[0][-1]
            bw_c,bw_h = bi_encoder_state[1][-1]
        else:
            fw_c,fw_h = bi_encoder_state[0]
            bw_c,bw_h = bi_encoder_state[1]
        encoder_state = [fw_h,bw_h]
        '''
        encoder_state = []

        return encoder_outputs, encoder_state
Example #2
0
 def _build_graph(self):
     _log_info('Begin to build graph...')
     loss = tf.constant(0.0)
     encoder_outputs, encoder_state = self._build_encoder()
     logits = self._build_decoder(encoder_outputs)
     if self.mode == _hm.MODE_TRAIN:
         loss = self._compute_loss(logits)
     return logits, loss
Example #3
0
def create_or_load_model(model_dir, session, saver):
    ckpt = tf.train.get_checkpoint_state(model_dir)
    step_dir = None
    if ckpt and ckpt.model_checkpoint_path:
        load_model(saver, ckpt.model_checkpoint_path, session)
        global_init(session, True)
        step_dir = ckpt.model_checkpoint_path
    else:
        global_init(session)
        _log_info('Create new model...')
    return step_dir
Example #4
0
def global_init(sess=None, only_table=False):
    '''
    Execute tf[global_variables_initializer, local_variables_initializer, tables_initializer].

    param: only_table, Should init table variables only by loading model.
    '''
    if (sess is None):
        _log_error('Invalid TF Session.')
        return

    init = tf.global_variables_initializer()
    local_init = tf.local_variables_initializer()
    table_init = tf.tables_initializer()
    if (only_table):
        sess.run([table_init])
        _log_info('table variables have been inited...')
    else:
        sess.run([init, local_init, table_init])
        _log_info('global & local & table variables have been inited...')
Example #5
0
def compare_targets(file1, file2):
    if (file1 is None or file2 is None):
        _log_error('Invalid file name.')
        return

    lines1 = _co.open(file1, encoding='utf-8').readlines()
    lines2 = _co.open(file2, encoding='utf-8').readlines()

    length1 = len(lines1)
    length2 = len(lines2)

    if (length1 != length2):
        _log_error('Number of lines is not same between the two files.')
        return

    errors = 0.0
    for i in range(length1):
        if (lines1[i] != lines2[i]):
            errors += 1.0
    res = errors / length1

    _log_info('Precision: %.2f%%' % ((1.0 - res) * 100))
    return (1.0 - res)
tf.flags.DEFINE_integer(
    "test_every", 1000,
    "Test model on test_data set after this many steps(default:500)")

# Misc Parameters
tf.flags.DEFINE_boolean("allow_soft_placement", True,
                        "Allow device soft device placement")
tf.flags.DEFINE_boolean("log_device_placement", False,
                        "Log placement of ops on devices")
tf.flags.DEFINE_string("padding_token", '<PAD>', "uniform sentences")

FLAGS = tf.flags.FLAGS

_log_warning("\nParameters:")
for attr, value in sorted(FLAGS.__flags.items()):
    _log_info("{}={}".format(attr.upper(), value.value))
_log_info("")

# Output directory for models and summaries
out_dir = data_helper.mkdir_if_not_exist("./runs")

# Load data
_log_warning("Loading data...")

# 建立词表
vocab_tokens = [
    line.strip()
    for line in codecs.open('./runs/vocab', 'r', 'utf-8').readlines()
]
vocsize = len(vocab_tokens)
vocab = {}
Example #7
0
 def __sleep_cpu__(self):
     if (self.step > 0):
         if (self.step % self.cpu_sleep_step == 0):
             _log_info('CPU sleep for ' + str(self.cpu_sleep_time) + 's...')
             time.sleep(self.cpu_sleep_time)
Example #8
0
def load_model(saver, ckpt, session):
    start_time = time.time()
    saver.restore(session, ckpt)
    _log_info("loaded model parameters from %s, time %.2fs" %
              (ckpt, time.time() - start_time))