Exemplo n.º 1
0
def build_graph(config):
    
    input_x = tf.placeholder(tf.int32, [None, None], name='input_x')
    input_n = tf.placeholder(tf.int32, [None], name='input_n')
    input_y = tf.placeholder(tf.int64, [None], name='input_y')

    with tf.device('/cpu:0'):
        emb_mat = tf.get_variable('embedding',
                                  [config.vocab.size(), config.vocab.emb_dim],
                                  initializer=tf.constant_initializer(config.vocab.embeddings),
                                  trainable = config.emb_tune)
        seq_emb = tf.nn.embedding_lookup(emb_mat, input_x)
        
        seq_mask = tf.cast(tf.cast(input_x, dtype = tf.bool), dtype = tf.int32)
        seq_len = tf.reduce_sum(seq_mask, 1)

    with tf.name_scope("rnn"):
        
        seq_e = rnn_layer(seq_emb, seq_len, config.hidden_units, config.keep_prob,
                          activation = tf.nn.relu, concat = True, scope = 'bi-lstm-1')        
        feat_s = seq_e[:,-1,:]
        
        #
        feat_g, mask_s = gather_and_pad_layer(feat_s, input_n)
        
        #
        seq_e = rnn_layer(feat_g, input_n, config.hidden_units, config.keep_prob,
                          activation = tf.nn.relu, concat = True, scope = 'bi-lstm-2')        
        feat = seq_e[:,-1,:]
        

    with tf.name_scope("score"):
        #
        fc = tf.contrib.layers.dropout(feat, config.keep_prob)
        fc = tf.layers.dense(fc, config.hidden_units, name='fc1')            
        fc = tf.nn.relu(fc)
        
        fc = tf.contrib.layers.dropout(fc, config.keep_prob)
        logits = tf.layers.dense(fc, config.num_classes, name='fc2')
        # logits = tf.nn.sigmoid(fc)
        
        normed_logits = tf.nn.softmax(logits, name='logits')          
        y_pred_cls = tf.argmax(logits, 1, name='pred_cls')
        
    with tf.name_scope("loss"):
        #
        cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(logits = logits,
                                                                       labels = input_y)
        loss = tf.reduce_mean(cross_entropy, name = 'loss')

    with tf.name_scope("accuracy"):
        #
        correct_pred = tf.equal(input_y, y_pred_cls)
        acc = tf.reduce_mean(tf.cast(correct_pred, tf.float32), name = 'metric')
    
    #
    print(normed_logits)
    print(acc)
    print(loss)
    print()
Exemplo n.º 2
0
def rnn_detect_layers(conv_feat, sequence_length, num_anchors):
    #
    # Input features is [batchSize paddedSeqLen numFeatures]
    #
    #
    rnn_size = 256  # 256, 512
    fc_size = 512  # 256, 384, 512
    #
    #
    # Transpose to time-major order for efficiency
    #  --> [paddedSeqLen batchSize numFeatures]
    #
    rnn_sequence = tf.transpose(conv_feat, perm=[1, 0, 2], name='time_major')
    #
    rnn1 = layers.rnn_layer(rnn_sequence, sequence_length, rnn_size, 'bdrnn1')
    rnn2 = layers.rnn_layer(rnn1, sequence_length, rnn_size, 'bdrnn2')
    #rnn3 = rnn_layer(rnn2, sequence_length, rnn_size, 'bdrnn3')
    #
    weight_initializer = tf.contrib.layers.variance_scaling_initializer()
    bias_initializer = tf.constant_initializer(value=0.0)
    #
    rnn_feat = tf.layers.dense(rnn2,
                               fc_size,
                               activation=tf.nn.relu,
                               kernel_initializer=weight_initializer,
                               bias_initializer=bias_initializer,
                               name='rnn_feat')
    #
    # out
    #
    rnn_cls = tf.layers.dense(rnn_feat,
                              num_anchors * 2,
                              activation=tf.nn.sigmoid,
                              kernel_initializer=weight_initializer,
                              bias_initializer=bias_initializer,
                              name='text_cls')
    #
    rnn_ver = tf.layers.dense(rnn_feat,
                              num_anchors * 2,
                              activation=tf.nn.tanh,
                              kernel_initializer=weight_initializer,
                              bias_initializer=bias_initializer,
                              name='text_ver')
    #
    rnn_hor = tf.layers.dense(rnn_feat,
                              num_anchors * 2,
                              activation=tf.nn.tanh,
                              kernel_initializer=weight_initializer,
                              bias_initializer=bias_initializer,
                              name='text_hor')
    #
    # dense operates on last dim
    #

    #
    rnn_cls = tf.transpose(rnn_cls, perm=[1, 0, 2], name='rnn_cls')
    rnn_ver = tf.transpose(rnn_ver, perm=[1, 0, 2], name='rnn_ver')
    rnn_hor = tf.transpose(rnn_hor, perm=[1, 0, 2], name='rnn_hor')
    #
    return rnn_cls, rnn_ver, rnn_hor
Exemplo n.º 3
0
def rnn_feat_layers(conv_feat, sequence_length):
    #
    # Input features is [batchSize paddedSeqLen numFeatures]
    #
    #
    rnn_size = 256  # 256, 512
    fc_size = 512  # 256, 384, 512
    #
    with tf.variable_scope("rnn_comm"):
        #
        # Transpose to time-major order for efficiency
        #  --> [paddedSeqLen batchSize numFeatures]
        #
        rnn_sequence = tf.transpose(conv_feat,
                                    perm=[1, 0, 2],
                                    name='time_major')
        #
        rnn1 = layers.rnn_layer(rnn_sequence, sequence_length, rnn_size,
                                'bdrnn1')
        rnn2 = layers.rnn_layer(rnn1, sequence_length, rnn_size, 'bdrnn2')
        #rnn3 = rnn_layer(rnn2, sequence_length, rnn_size, 'bdrnn3')
        #
        weight_initializer = tf.contrib.layers.variance_scaling_initializer()
        bias_initializer = tf.constant_initializer(value=0.0)
        #
        rnn_feat = tf.layers.dense(rnn2,
                                   fc_size,
                                   activation=tf.nn.relu,
                                   kernel_initializer=weight_initializer,
                                   bias_initializer=bias_initializer,
                                   name='rnn_feat')
        #

    return rnn_feat
Exemplo n.º 4
0
def do_encoding(sett, emb_q, emb_p, len_q, len_p, keep_prob):
    """
    """
    with tf.variable_scope('encoding'):
        encoded_q = rnn_layer(emb_q,
                              len_q,
                              sett.encd_size,
                              keep_prob,
                              scope="q")
        output_q = dense(encoded_q, sett.encd_size, scope="q")
        #
        encoded_p = rnn_layer(emb_p,
                              len_p,
                              sett.encd_size,
                              keep_prob,
                              scope="p")
        output_p = dense(encoded_p, sett.encd_size, scope="p")

    return output_q, output_p
Exemplo n.º 5
0
def do_featuring(sett, matched_p, len_p, keep_prob):
    """
    """
    with tf.variable_scope('featuring'):
        feat_s = rnn_layer(matched_p,
                           len_p,
                           sett.feat_size,
                           keep_prob,
                           scope="s")
        feat_s = dense(feat_s, sett.feat_size, scope="s")
        #
        input_e = tf.concat([feat_s, matched_p], -1)
        #
        feat_e = rnn_layer(input_e,
                           len_p,
                           sett.feat_size,
                           keep_prob,
                           scope="e")
        feat_e = dense(feat_e, sett.feat_size, scope="e")

    return feat_s, feat_e
    def build_inference(self, input_tensors):
        """
        """
        settings = self.settings
        input_x = input_tensors["input_x"]
        #
        keep_prob = tf.get_variable("keep_prob",
                                    shape=[],
                                    dtype=tf.float32,
                                    trainable=False)
        #
        with tf.device('/cpu:0'):
            emb_mat = tf.get_variable(
                'embedding', [settings.vocab.size(), settings.vocab.emb_dim],
                initializer=tf.constant_initializer(settings.vocab.embeddings),
                trainable=settings.emb_tune,
                dtype=tf.float32)
            seq_emb = tf.nn.embedding_lookup(emb_mat, input_x)

            seq_mask = tf.cast(tf.cast(input_x, dtype=tf.bool), dtype=tf.int32)
            seq_len = tf.reduce_sum(seq_mask, 1)

        with tf.name_scope("rnn"):

            seq_e = rnn_layer(seq_emb,
                              seq_len,
                              128,
                              keep_prob,
                              activation=tf.nn.relu,
                              concat=True,
                              scope='bi-lstm-1')

            # attention-pooling, 注意力加权采提
            #
            B = tf.shape(seq_e)[0]
            query = tf.get_variable("query", [settings.att_dim],
                                    initializer=tf.ones_initializer())
            query = tf.tile(tf.expand_dims(query, 0), [B, 1])

            feat = att_pool_layer(query,
                                  seq_e,
                                  seq_mask,
                                  settings.att_dim,
                                  keep_prob,
                                  scope="att_pooling")

            #feat = seq_e[:,-1,:]

        with tf.name_scope("score"):
            #
            fc = tf.nn.dropout(feat, keep_prob)
            fc = tf.layers.dense(fc, 128, name='fc1')
            fc = tf.nn.relu(fc)

            fc = tf.nn.dropout(fc, keep_prob)
            logits = tf.layers.dense(fc, settings.num_classes, name='fc2')

            normed_logits = tf.nn.softmax(logits, name='logits')

        #
        print(normed_logits)
        #
        output_tensors = {"normed_logits": normed_logits, "logits": logits}
        #
        return output_tensors
Exemplo n.º 7
0
def rnn_detect_layers(conv_feat, sequence_length, num_anchors):
    #
    # one-picture features
    print("conv feat before sequeeze shape: ", conv_feat.shape)
    conv_feat = tf.squeeze(conv_feat, axis=0)  # squeeze
    print("conv feat after sequeeze shape: ", conv_feat.shape)
    #
    #
    # Transpose to time-major order for efficiency
    #  --> [paddedSeqLen batchSize numFeatures]
    #
    rnn_sequence = tf.transpose(conv_feat, perm=[1, 0, 2], name='time_major')
    print("conv feat after transpose shape: ", rnn_sequence.shape)
    #

    #
    rnn_size = 256  # 256, 512
    fc_size = 512  # 256, 384, 512
    #
    #

    rnn1 = layers.rnn_layer(rnn_sequence, sequence_length, rnn_size, 'bdrnn1')
    print("RNN1 feat shape: ", rnn1.shape)
    rnn2 = layers.rnn_layer(rnn1, sequence_length, rnn_size, 'bdrnn2')
    print("RNN2 feat shape: ", rnn2.shape)
    #rnn3 = rnn_layer(rnn2, sequence_length, rnn_size, 'bdrnn3')
    #
    weight_initializer = tf.contrib.layers.variance_scaling_initializer()
    bias_initializer = tf.constant_initializer(value=0.0)
    #
    rnn_feat = tf.layers.dense(rnn2,
                               fc_size,
                               activation=tf.nn.relu,
                               kernel_initializer=weight_initializer,
                               bias_initializer=bias_initializer,
                               name='rnn_feat')
    print("RNN feat(after dense) shape: ", rnn_feat.shape)
    #
    # out
    #
    rnn_cls = tf.layers.dense(rnn_feat,
                              num_anchors * 2,
                              activation=tf.nn.sigmoid,
                              kernel_initializer=weight_initializer,
                              bias_initializer=bias_initializer,
                              name='text_cls')
    #
    rnn_ver = tf.layers.dense(rnn_feat,
                              num_anchors * 2,
                              activation=tf.nn.tanh,
                              kernel_initializer=weight_initializer,
                              bias_initializer=bias_initializer,
                              name='text_ver')
    #
    rnn_hor = tf.layers.dense(rnn_feat,
                              num_anchors * 2,
                              activation=tf.nn.tanh,
                              kernel_initializer=weight_initializer,
                              bias_initializer=bias_initializer,
                              name='text_hor')
    print("rnn_cls  shape: ", rnn_cls.shape)
    print("rnn_ver shape: ", rnn_ver.shape)
    print("rnn_hor shape: ", rnn_hor.shape)
    #
    # dense operates on last dim
    #

    #
    rnn_cls = tf.transpose(rnn_cls, perm=[1, 0, 2], name='rnn_cls')
    rnn_ver = tf.transpose(rnn_ver, perm=[1, 0, 2], name='rnn_ver')
    rnn_hor = tf.transpose(rnn_hor, perm=[1, 0, 2], name='rnn_hor')
    print("rnn_cls transpose shape: ", rnn_cls.shape)
    print("rnn_ver transpose shape: ", rnn_ver.shape)
    print("rnn_hor transpose shape: ", rnn_hor.shape)
    #
    return rnn_cls, rnn_ver, rnn_hor