def symbols_to_logits_fn(ids, dec_state): dec = [] dec_c, dec_h = [], [] # (batch x beam_size x decoded_seq) ids = tf.reshape(ids, [Hp.batch_size, beam_size, -1]) print("dec_state ", dec_state[0].get_shape().as_list()) for ind in range(beam_size): with tf.variable_scope('dec_lstm', reuse=ind > 0 or reuse_vars): w_input = ids[:, ind, -1].sg_lookup(emb=emb_word) dec_state0 = tf.contrib.rnn.LSTMStateTuple( c=dec_state.c[:, ind, :], h=dec_state.h[:, ind, :]) dec_out, dec_state_i = dec_cell(w_input, dec_state0) dec_out = tf.expand_dims(dec_out, 1) dec_i = dec_out.sg_conv1d_gpus(size=1, dim=Hp.word_vs, name="out_conv", act="linear", dev=dev, reuse=ind > 0 or reuse_vars) dec.append(tf.squeeze(dec_i, 1)) dec_c.append(dec_state_i[0]) dec_h.append(dec_state_i[1]) return tf.stack(dec, 1), tf.contrib.rnn.LSTMStateTuple( tf.stack(dec_c, 1), tf.stack(dec_h, 1))
def read_and_decode(filename_queue, batch_size): reader = tf.TFRecordReader() _, serialized_example = reader.read(filename_queue) features = tf.parse_single_example( serialized_example, # Defaults are not specified since both keys are required. features={ 'height': tf.FixedLenFeature([], tf.int64), 'word_opinion': tf.FixedLenFeature([Hp.w_maxlen * 6], dtype=tf.int64, default_value=[-1] * Hp.w_maxlen * 6), 'char_opinion': tf.FixedLenFeature([], tf.string) }) char_opinion = tf.decode_raw(features['char_opinion'], tf.uint8) height = tf.cast(features['height'], tf.int32) word_opinion = tf.cast(features['word_opinion'], tf.int32) char_opinion = tf.reshape(char_opinion, tf.stack([6, Hp.c_maxlen])) word_opinion = tf.reshape(word_opinion, tf.stack([6, Hp.w_maxlen])) words, chars = tf.train.batch([word_opinion, char_opinion], batch_size=batch_size, capacity=3 * batch_size, num_threads=1) return (words, chars)
def sg_upconv(tensor, opt): r"""Applies a up convolution (or convolution transpose). Args: tensor: A 4-D `Tensor` (automatically passed by decorator). opt: size: A tuple/list of integers of length 2 representing `[kernel height, kernel width]`. Can be an integer if both values are the same. If not specified, (4, 4) is set implicitly. Default value is [1, 2, 2, 1]. stride: A tuple/list of integers of length 2 or 4 representing stride dimensions. If the length is 2, i.e., (a, b), the stride is `[1, a, b, 1]`. If the length is 4, i.e., (a, b, c, d), the stride is `[a, b, c, d]`. Can be an integer. If the length is a, the stride is `[1, a, a, 1]`. in_dim: A positive `integer`. The size of input dimension. dim: A positive `integer`. The size of output dimension. pad: Either `SAME` (Default) or `VALID`. bias: Boolean. If True, biases are added. Returns: A `Tensor` with the same type as `tensor`. """ # default options opt += tf.sg_opt(size=(4, 4), stride=(1, 2, 2, 1), pad='SAME') opt.size = opt.size if isinstance(opt.size, (tuple, list)) else [opt.size, opt.size] opt.stride = opt.stride if isinstance( opt.stride, (tuple, list)) else [1, opt.stride, opt.stride, 1] opt.stride = [1, opt.stride[0], opt.stride[1], 1] if len( opt.stride) == 2 else opt.stride # parameter tf.sg_initializer w = tf.sg_initializer.he_uniform( 'W', (opt.size[0], opt.size[1], opt.dim, opt.in_dim)) b = tf.sg_initializer.constant('b', opt.dim) if opt.bias else 0 # tedious shape handling for conv2d_transpose shape = tensor.get_shape().as_list() out_shape = [ tf.shape(tensor)[0], shape[1] * opt.stride[1], shape[2] * opt.stride[2], opt.dim ] # apply convolution out = tf.nn.conv2d_transpose(tensor, w, output_shape=tf.stack(out_shape), strides=opt.stride, padding=opt.pad) + b # reset shape is needed because conv2d_transpose() erase all shape information. # noinspection PyUnresolvedReferences out.set_shape([None, out_shape[1], out_shape[2], opt.dim]) return out
def iou(self, boxes1, boxes2): """ Note: Modified from https://github.com/nilboy/tensorflow-yolo/blob/python2.7/yolo/net/yolo_net.py calculate ious Args: boxes1: 4-D tensor [CELL_SIZE, CELL_SIZE, BOXES_PER_CELL, 4] ====> (x_center, y_center, w, h) boxes2: 1-D tensor [4] ===> (x_center, y_center, w, h) Return: iou: 3-D tensor [CELL_SIZE, CELL_SIZE, BOXES_PER_CELL] """ boxes1 = tf.stack([ boxes1[:, 0] - boxes1[:, 2] / 2, boxes1[:, 1] - boxes1[:, 3] / 2, boxes1[:, 0] + boxes1[:, 2] / 2, boxes1[:, 1] + boxes1[:, 3] / 2 ]) boxes2 = tf.stack([ boxes2[:, 0] - boxes2[:, 2] / 2, boxes2[:, 1] - boxes2[:, 3] / 2, boxes2[:, 0] + boxes2[:, 2] / 2, boxes2[:, 1] + boxes2[:, 3] / 2 ]) #calculate the left up point lu = tf.maximum(boxes1[0:2], boxes2[0:2]) rd = tf.minimum(boxes1[2:], boxes2[2:]) #intersection intersection = rd - lu inter_square = tf.multiply(intersection[0], intersection[1]) mask = tf.cast(intersection[0] > 0, tf.float32) * tf.cast( intersection[1] > 0, tf.float32) inter_square = tf.multiply(mask, inter_square) #calculate the boxs1 square and boxs2 square square1 = tf.multiply((boxes1[2] - boxes1[0]), (boxes1[3] - boxes1[1])) square2 = tf.multiply((boxes2[2] - boxes2[0]), (boxes2[3] - boxes2[1])) return inter_square / (square1 + square2 - inter_square + 1e-6), inter_square
def sg_upconv1d(tensor, opt): r"""Applies 1-D a up convolution (or convolution transpose). Args: tensor: A 3-D `Tensor` (automatically passed by decorator). opt: size: A positive `integer` representing `[kernel width]`. As a default it is set to 4 stride: A positive `integer` representing stride dimension. As a default it is set to 2 in_dim: A positive `integer`. The size of input dimension. dim: A positive `integer`. The size of output dimension. pad: Either `SAME` (Default) or `VALID`. bias: Boolean. If True, biases are added. regularizer: A (Tensor -> Tensor or None) function; the result of applying it on a newly created variable will be added to the collection tf.GraphKeys.REGULARIZATION_LOSSES and can be used for regularization summary: If True, summaries are added. The default is True. Returns: A `Tensor` with the same type as `tensor`. """ # default options opt += tf.sg_opt(size=4, stride=2, pad='SAME') opt.size = [opt.size, 1] opt.stride = [1, opt.stride, 1, 1] # parameter tf.sg_initializer w = tf.sg_initializer.he_uniform('W', (opt.size[0], opt.size[1], opt.dim, opt.in_dim), regularizer=opt.regularizer, summary=opt.summary) b = tf.sg_initializer.constant('b', opt.dim, summary=opt.summary) if opt.bias else 0 # make 4-D tensor tensor = tensor.sg_expand_dims(axis=2) # tedious shape handling for conv2d_transpose shape = tensor.get_shape().as_list() if opt.pad == "SAME": out_shape = [tf.shape(tensor)[0], shape[1] * opt.stride[1], shape[2] * opt.stride[2], opt.dim] else: out_shape = [tf.shape(tensor)[0], (shape[1] - 1) * opt.stride[1] + opt.size[0], (shape[2] - 1) * opt.stride[2] + opt.size[1], opt.dim] # apply convolution out = tf.nn.conv2d_transpose(tensor, w, output_shape=tf.stack(out_shape), strides=opt.stride, padding=opt.pad) + b # reset shape is needed because conv2d_transpose() erase all shape information. # noinspection PyUnresolvedReferences out.set_shape([None, out_shape[1], out_shape[2], opt.dim]) # squeeze out = out.sq_squeeze(axis=2) return out
def sg_upconv1d(tensor, opt): r"""Applies 1-D a up convolution (or convolution transpose). Args: tensor: A 3-D `Tensor` (automatically passed by decorator). opt: size: A positive `integer` representing `[kernel width]`. As a default it is set to 4 stride: A positive `integer` representing stride dimension. As a default it is set to 2 in_dim: A positive `integer`. The size of input dimension. dim: A positive `integer`. The size of output dimension. pad: Either `SAME` (Default) or `VALID`. bias: Boolean. If True, biases are added. Returns: A `Tensor` with the same type as `tensor`. """ # default options opt += tf.sg_opt(size=4, stride=2, pad='SAME') opt.size = [opt.size, 1] opt.stride = [1, opt.stride, 1, 1] # parameter tf.sg_initializer w = tf.sg_initializer.he_uniform( 'W', (opt.size[0], opt.size[1], opt.dim, opt.in_dim)) b = tf.sg_initializer.constant('b', opt.dim) if opt.bias else 0 # make 4-D tensor tensor = tensor.sg_expand_dims(dim=2) # tedious shape handling for conv2d_transpose shape = tensor.get_shape().as_list() out_shape = [ tf.shape(tensor)[0], shape[1] * opt.stride[1], shape[2] * opt.stride[2], opt.dim ] # apply convolution out = tf.nn.conv2d_transpose(tensor, w, output_shape=tf.stack(out_shape), strides=opt.stride, padding=opt.pad) + b # reset shape is needed because conv2d_transpose() erase all shape information. # noinspection PyUnresolvedReferences out.set_shape([None, out_shape[1], out_shape[2], opt.dim]) # squeeze out = out.sq_squeeze(dim=2) return out
def q_process(t1, t2): ''' Processes each training sample so that it fits in the queue. ''' # Lstrip zeros zeros = tf.equal(t1, tf.zeros_like(t1)).sg_int().sg_sum() t1 = t1[zeros:] t2 = t2[zeros:] # zero-PrePadding t1 = tf.concat([tf.zeros([Hyperparams.seqlen-1], tf.int32), t1], 0)# 49 zero-prepadding t2 = tf.concat([tf.zeros([Hyperparams.seqlen-1], tf.int32), t2], 0)# 49 zero-prepadding # radom crop stacked = tf.stack((t1, t2)) cropped = tf.random_crop(stacked, [2, Hyperparams.seqlen]) t1, t2 = cropped[0], cropped[1] t2 = t2[-1] return t1, t2
def tower_infer_dec(chars, scope, rnn_cell, dec_cell, word_emb, rnn_state, out_reuse_vars=False, dev='/cpu:0'): with tf.device(dev): with tf.variable_scope('embatch_size', reuse=True): # (vocab_size, latent_dim) emb_char = tf.sg_emb(name='emb_char', voca_size=Hp.char_vs, dim=Hp.hd, dev=dev) emb_word = tf.sg_emb(name='emb_word', emb=word_emb, voca_size=Hp.word_vs, dim=300, dev=dev) print(chars) ch = chars ch = tf.reverse_sequence(input=ch, seq_lengths=[Hp.c_maxlen] * Hp.batch_size, seq_dim=1) reuse_vars = reuse_vars_enc = True # -------------------------- BYTENET ENCODER -------------------------- with tf.variable_scope('encoder'): # embed table lookup enc = ch.sg_lookup(emb=emb_char) #(batch, sentlen, latentdim) # loop dilated conv block for i in range(Hp.num_blocks): enc = (enc.sg_res_block(size=5, rate=1, name="enc1_%d" % (i), is_first=True, reuse_vars=reuse_vars, dev=dev).sg_res_block( size=5, rate=2, name="enc2_%d" % (i), reuse_vars=reuse_vars, dev=dev).sg_res_block( size=5, rate=4, name="enc4_%d" % (i), reuse_vars=reuse_vars, dev=dev).sg_res_block( size=5, rate=8, name="enc8_%d" % (i), reuse_vars=reuse_vars, dev=dev).sg_res_block( size=5, rate=16, name="enc16_%d" % (i), reuse_vars=reuse_vars, dev=dev)) byte_enc = enc # -------------------------- QCNN + QPOOL ENCODER #1 -------------------------- with tf.variable_scope('quazi'): #quasi cnn layer ZFO [batch * 3, seqlen, dim2 ] conv = byte_enc.sg_quasi_conv1d(is_enc=True, size=4, name="qconv_1", dev=dev, reuse_vars=reuse_vars) # c = f * c + (1 - f) * z, h = o*c [batch * 4, seqlen, hd] pool0 = conv.sg_quasi_rnn(is_enc=False, att=False, name="qrnn_1", reuse_vars=reuse_vars, dev=dev) qpool_last = pool0[:, -1, :] # -------------------------- MAXPOOL along time dimension -------------------------- inpt_maxpl = tf.expand_dims(byte_enc, 1) # [batch, 1, seqlen, channels] maxpool = tf.nn.max_pool(inpt_maxpl, [1, 1, Hp.c_maxlen, 1], [1, 1, 1, 1], 'VALID') maxpool = tf.squeeze(maxpool, [1, 2]) # -------------------------- HIGHWAY -------------------------- concat = qpool_last + maxpool with tf.variable_scope('highway', reuse=reuse_vars): input_lstm = highway(concat, concat.get_shape()[-1], num_layers=1) # -------------------------- CONTEXT LSTM -------------------------- input_lstm = tf.nn.dropout(input_lstm, Hp.keep_prob) with tf.variable_scope('contx_lstm', reuse=reuse_vars): output, rnn_state = rnn_cell(input_lstm, rnn_state) beam_size = 8 reuse_vars = out_reuse_vars greedy = False if greedy: dec_state = rnn_state dec_out = [] d_out = tf.constant([1] * Hp.batch_size) for idx in range(Hp.w_maxlen): w_input = d_out.sg_lookup(emb=emb_word) dec_state = tf.contrib.rnn.LSTMStateTuple(c=dec_state.c, h=dec_state.h) with tf.variable_scope('dec_lstm', reuse=idx > 0 or reuse_vars): d_out, dec_state = dec_cell(w_input, dec_state) dec_out.append(d_out) d_out = tf.expand_dims(d_out, 1).sg_conv1d_gpus(size=1, dim=Hp.word_vs, name="out_conv", act="linear", dev=dev, reuse=idx > 0 or reuse_vars) d_out = tf.squeeze(d_out).sg_argmax() dec_out = tf.stack(dec_out, 1) dec = dec_out.sg_conv1d_gpus(size=1, dim=Hp.word_vs, name="out_conv", act="linear", dev=dev, reuse=True) return dec.sg_argmax(), rnn_state else: # ------------------ BEAM SEARCH -------------------- dec_state = tf.contrib.rnn.LSTMStateTuple( tf.tile(tf.expand_dims(rnn_state[0], 1), [1, beam_size, 1]), tf.tile(tf.expand_dims(rnn_state[1], 1), [1, beam_size, 1])) initial_ids = tf.constant([1] * Hp.batch_size) def symbols_to_logits_fn(ids, dec_state): dec = [] dec_c, dec_h = [], [] # (batch x beam_size x decoded_seq) ids = tf.reshape(ids, [Hp.batch_size, beam_size, -1]) print("dec_state ", dec_state[0].get_shape().as_list()) for ind in range(beam_size): with tf.variable_scope('dec_lstm', reuse=ind > 0 or reuse_vars): w_input = ids[:, ind, -1].sg_lookup(emb=emb_word) dec_state0 = tf.contrib.rnn.LSTMStateTuple( c=dec_state.c[:, ind, :], h=dec_state.h[:, ind, :]) dec_out, dec_state_i = dec_cell(w_input, dec_state0) dec_out = tf.expand_dims(dec_out, 1) dec_i = dec_out.sg_conv1d_gpus(size=1, dim=Hp.word_vs, name="out_conv", act="linear", dev=dev, reuse=ind > 0 or reuse_vars) dec.append(tf.squeeze(dec_i, 1)) dec_c.append(dec_state_i[0]) dec_h.append(dec_state_i[1]) return tf.stack(dec, 1), tf.contrib.rnn.LSTMStateTuple( tf.stack(dec_c, 1), tf.stack(dec_h, 1)) final_ids, final_probs = beam_search.beam_search(symbols_to_logits_fn, dec_state, initial_ids, beam_size, Hp.w_maxlen - 1, Hp.word_vs, 3.5, eos_id=2) return final_ids[:, 0, :], rnn_state