def _build_model(self):
        # Model PlaceHolder for input
        # [batch, sent]
        self.in_words = tf.placeholder(tf.int32, [None, self.max_len_sent])
        self.in_y = tf.placeholder(tf.int32, [None])
        self.in_len = tf.placeholder(tf.int32, [None])

        seq_words = tf.nn.embedding_lookup(self.we, self.in_words)  # [B, T, D]

        with tf.name_scope('rcnn'):
            rcnn_out = tf_encoder.rcnn(seq_words, self.in_lens_word,
                                       self.n_rnn)
            dim_sent = 2 * self.n_rnn + self.dim_word

        with tf.name_scope('mlp'):
            # rcnn_out = tf.nn.dropout(rcnn_out, keep_prob=self.keep_prob)
            mlp = tf_ops.dense_layer(rcnn_out,
                                     dim_sent,
                                     self.n_mlp,
                                     activation=tf.nn.relu)

        with tf.name_scope('logits'):
            self.logits = tf_ops.dense_layer(mlp,
                                             self.n_mlp,
                                             self.n_class,
                                             scope='logits')
Example #2
0
    def _build_model(self):
        self.in_chars = tf.placeholder(tf.int32, [None, self.max_len])
        self.in_y = tf.placeholder(tf.int32, [None])

        seq_chars = tf.nn.embedding_lookup(self.ce, self.in_chars)
        x = tf.expand_dims(seq_chars, -1)  # [B, T, D, 1]

        with tf.name_scope("conv1-max-pooling"):
            # [filter_height, filter_width, in_channels, out_channels]
            filter_shape = [7, self.dim_char, 1, 256]
            w = tf.Variable(tf.truncated_normal(filter_shape, stddev=0.05),
                            name="w")
            b = tf.Variable(tf.constant(0.1, shape=[256]), name="b")
            conv = tf.nn.conv2d(x,
                                w,
                                strides=[1, 1, 1, 1],
                                padding="VALID",
                                name="conv1")
            h = tf.nn.relu(tf.nn.bias_add(conv, b), name="relu")
            x = tf.nn.max_pool(h,
                               ksize=[1, 3, 1, 1],
                               strides=[1, 3, 1, 1],
                               padding="VALID",
                               name="pool1")

        with tf.name_scope("conv2-max-pooling"):
            filter_shape = [3, 1, 256, 256]
            w = tf.Variable(tf.truncated_normal(filter_shape, stddev=0.05),
                            name="W")
            b = tf.Variable(tf.constant(0.1, shape=[256]), name="b")
            conv = tf.nn.conv2d(x,
                                w,
                                strides=[1, 1, 1, 1],
                                padding="VALID",
                                name="conv3")
            h = tf.nn.relu(tf.nn.bias_add(conv, b), name="relu")
            x = tf.nn.max_pool(h,
                               ksize=[1, 3, 1, 1],
                               strides=[1, 3, 1, 1],
                               padding="VALID",
                               name="pool2")

        num_features_total = 12 * 256
        x = tf.reshape(x, [-1, num_features_total])
        with tf.name_scope("dropout-1"):
            x = tf.nn.dropout(x, self.keep_prob)

        x = tf_ops.dense_layer(x,
                               num_features_total,
                               1024,
                               activation=tf.nn.relu,
                               scope="fc1")
        with tf.name_scope("dropout-1"):
            x = tf.nn.dropout(x, self.keep_prob)
        x = tf_ops.dense_layer(x,
                               1024,
                               256,
                               activation=tf.nn.relu,
                               scope="fc2")
        self.logits = tf_ops.dense_layer(x, 256, self.n_class, scope="logits")
    def _build_model(self):
        self.in_x = tf.placeholder(tf.int32, [None, self.max_len_sent])
        self.in_y = tf.placeholder(tf.int32, [None])
        self.in_len = tf.placeholder(tf.int32, [None])

        seq_words = tf.nn.embedding_lookup(self.we, self.in_x)  # [B, T, D]
        with tf.name_scope("rnn"):
            rnn_out = tf_encoder.bi_rnn_std(seq_words, self.in_len, self.n_rnn, return_hs=True)
        with tf.name_scope("att"):
            sent = tf_ops.self_attention(rnn_out, self.in_len, att_size=self.att_size)
            sent_dim = 2*self.n_rnn
        with tf.name_scope("mlp"):
            mlp = tf_ops.dense_layer(sent, sent_dim, self.n_mlp, activation=tf.nn.relu)
        with tf.name_scope("logits"):
            self.logits = tf_ops.dense_layer(mlp, self.n_mlp, self.n_class, scope='logits')
    def _build_model(self):
        self.in_words = tf.placeholder(tf.int32, [None, self.max_len_sent])
        self.in_chars = tf.placeholder(tf.int32, [None, self.max_len_sent, self.max_len_word])
        self.in_y = tf.placeholder(tf.int32, [None])
        self.in_lens_sent = tf.placeholder(tf.int32, [None])
        self.in_lens_word = tf.placeholder(
            tf.int32, [None, self.max_len_sent], name="in_lens_word")

        seqs_words_embed = tf.nn.embedding_lookup(self.we, self.in_words)
        # seqs_words_embed = tf.nn.dropout(seqs_words_embed, self.keep_prob)

        c_gate_w = tf_encoder.char2word_gate_word(
            self.in_chars, self.in_lens_word, self.ce, self.n_rnn_char, seqs_words_embed)

        with tf.name_scope("rnn"):
            rnn_out = tf_encoder.bi_rnn_std(c_gate_w, self.in_lens_sent, self.n_rnn_word, return_hs=True)
        with tf.name_scope("att"):
            sent = tf_ops.self_attention(rnn_out, self.in_lens_sent, att_size=self.n_rnn_word)
            sent_dim = 2*self.n_rnn_word

        # sent = tf_encoder.word2sent(c_gate_w, self.in_lens_sent, self.n_rnn_word)
        # dim_sent = 2*self.n_rnn_word

        with tf.name_scope("mlp"):
            # sent = tf.nn.dropout(sent, self.keep_prob)
            mlp = tf_ops.dense_layer(
                sent, sent_dim, self.n_mlp, activation=tf.nn.relu, scope="mlp")

        with tf.name_scope("logits"):
            # self.logits = tf_ops.dense_layer(mlp, self.n_mlp, self.n_class, scope="logits")
            self.logits = tf_ops.centroids_logits(mlp, self.emoji_w)
Example #5
0
    def _build_model(self):
        # Model PlaceHolder for input
        self.in_words = tf.placeholder(tf.int32, [None, self.max_len_sent])
        self.in_y = tf.placeholder(tf.int32, [None])
        self.in_lens_word = tf.placeholder(tf.int32, [None])

        embedded_seq = tf.nn.embedding_lookup(self.we, self.in_words)

        with tf.name_scope('nbow'):
            avg_out = tf_ops.avg_pool(embedded_seq, self.in_lens_word)

        with tf.name_scope("mlp"):
            mlp = tf_ops.dense_layer(avg_out,
                                     self.dim_word,
                                     self.n_mlp,
                                     activation=tf.nn.relu,
                                     scope="mlp")

        with tf.name_scope('logits'):
            self.logits = tf_ops.dense_layer(mlp,
                                             self.n_mlp,
                                             self.n_class,
                                             scope="logits")
Example #6
0
    def _build_model(self):
        # Model PlaceHolder for input
        # [batch, sent]
        self.in_words = tf.placeholder(tf.int32, [None, self.max_len_sent])
        self.in_lens_sent = tf.placeholder(tf.int32, [None])
        self.in_y = tf.placeholder(tf.int32, [None])

        # Embedding layer
        # [batch, sent, dim_word]
        embedded_seq = tf.nn.embedding_lookup(self.we, self.in_words)

        # with tf.name_scope("lstm"):
        #     rnn_h_out = tf_encoder.bi_rnn_std(
        #         embedded_seq, self.in_lens_word, self.n_rnn, return_hs=True)
        #     rnn_out = tf_ops.max2d_pooling(rnn_h_out, self.max_len_sent)
        #
        # with tf.name_scope("mlp"):
        #     mlp = tf_ops.dense_layer(
        #         rnn_out, 2*self.n_rnn, self.n_mlp, activation=tf.nn.relu, scope="mlp")

        with tf.name_scope("lstm"):
            rnn_out = tf_encoder.bi_rnn_std(embedded_seq, self.in_lens_word,
                                            self.n_rnn)

        with tf.name_scope("mlp"):
            mlp = tf_ops.dense_layer(rnn_out,
                                     2 * self.n_rnn,
                                     self.n_mlp,
                                     activation=tf.nn.relu,
                                     scope="mlp")

        with tf.name_scope("logits"):
            self.logits = tf_ops.dense_layer(mlp,
                                             self.n_mlp,
                                             self.n_class,
                                             scope="logits")
            self.logits = tf.layers.dense(mlp, self.n_class)