NLP分類常用模型(二):rnn多層 tf.nn.dynamic_rnn()+ 全連接tf.layers.dense()

"""文本分類,RNN模型"""
    def __init__(self):
        # 三個待輸入的數據
        self.x = tf.placeholder(tf.int32, [None, config.max_seq_length], name='x')
        self.y = tf.placeholder(tf.float32, [None, config.lables], name='y')
        self.keep_prob = tf.placeholder(tf.float32, name='keep_prob')
        self.rnn()

    def rnn(self):
        """rnn模型"""
        def lstm_cell():  # lstm核
            return tf.contrib.rnn.BasicLSTMCell(config.lstm_hidden_size, state_is_tuple=True)

        def dropout():  # 爲每一個rnn核後面加一個dropout層
            cell = lstm_cell()
            return tf.contrib.rnn.DropoutWrapper(cell, config.keep_prob)

        # 詞向量映射
        with tf.name_scope("embedding"):
            embedding = tf.get_variable('embedding', [config.vocab_size, config.embedding_dim])
            embedding_inputs = tf.nn.embedding_lookup(embedding, self.x)

        with tf.name_scope("rnn"):
            # 多層rnn網絡
            cells = [dropout() for _ in range(3)]
            rnn_cell = tf.contrib.rnn.MultiRNNCell(cells, state_is_tuple=True)

            _outputs, _ = tf.nn.dynamic_rnn(cell=rnn_cell, inputs=embedding_inputs, dtype=tf.float32)
            # _outputs [batch_size, n_step, lstm_hidden_size]
            last = _outputs[:, -1, :]  # 取最後一個時序輸出作爲結果

        with tf.name_scope("score"):
            # 全連接層,後面接dropout以及relu激活
            fc = tf.layers.dense(last, config.lstm_hidden_size, name='fc1')
            fc = tf.contrib.layers.dropout(fc, config.keep_prob)
            fc = tf.nn.relu(fc)
            # 分類器
            self.logits = tf.layers.dense(fc, config.lables, name='fc2')
            self.y_pred_cls = tf.argmax(tf.nn.softmax(self.logits), 1)  # 預測類別

        with tf.name_scope("optimize"):
            # 損失函數,交叉熵
            cross_entropy = tf.nn.softmax_cross_entropy_with_logits(logits=self.logits, labels=self.y)
            self.loss = tf.reduce_mean(cross_entropy)
            # 優化器
            self.train_op = tf.train.AdamOptimizer(learning_rate=config.learning_rate).minimize(self.loss)

        with tf.name_scope("accuracy"):
            # 準確率
            correct_pred = tf.equal(tf.argmax(self.y, 1), self.y_pred_cls)
            self.accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章