TensorFlow:如何使用 RNN 对二进制序列进行分类?

Posted

技术标签:

【中文标题】TensorFlow:如何使用 RNN 对二进制序列进行分类?【英文标题】:TensorFlow: how to classify a binary sequence using RNN? 【发布时间】:2017-11-07 15:32:54 【问题描述】:

我得到以下 TensorFlow 序列分类的代码(参见本题末尾的代码):

https://gist.github.com/danijar/c7ec9a30052127c7a1ad169eeb83f159

我需要修改代码来解决我的二进制序列分类问题。

我的训练输入是一个csv文件(非常大),csv文件的格式是:

binary_sequence(fixed sequence length of 100),result
1>0>1>1>0>0>0>0>...>0,1
0>1>0>1>0>0>0>0>...>0,0
...
0>1>0>1>0>0>0>0>...>0,0

我需要训练一个分类器来分类新的 binary_sequences。

我的测试输入也是一个类似格式的 csv 文件。

非常感谢任何可以就我的问题修改以下代码提出建议的人!

# Example for my blog post at:
# https://danijar.com/introduction-to-recurrent-networks-in-tensorflow/
import functools
import sets
import tensorflow as tf


def lazy_property(function):
    attribute = '_' + function.__name__

    @property
    @functools.wraps(function)
    def wrapper(self):
        if not hasattr(self, attribute):
            setattr(self, attribute, function(self))
        return getattr(self, attribute)
    return wrapper


class SequenceClassification:

    def __init__(self, data, target, dropout, num_hidden=200, num_layers=3):
        self.data = data
        self.target = target
        self.dropout = dropout
        self._num_hidden = num_hidden
        self._num_layers = num_layers
        self.prediction
        self.error
        self.optimize

    @lazy_property
    def prediction(self):
        # Recurrent network.
        network = tf.contrib.rnn.GRUCell(self._num_hidden)
        network = tf.contrib.rnn.DropoutWrapper(
            network, output_keep_prob=self.dropout)
        network = tf.contrib.rnn.MultiRNNCell([network] * self._num_layers)
        output, _ = tf.nn.dynamic_rnn(network, self.data, dtype=tf.float32)
        # Select last output.
        output = tf.transpose(output, [1, 0, 2])
        last = tf.gather(output, int(output.get_shape()[0]) - 1)
        # Softmax layer.
        weight, bias = self._weight_and_bias(
            self._num_hidden, int(self.target.get_shape()[1]))
        prediction = tf.nn.softmax(tf.matmul(last, weight) + bias)
        return prediction

    @lazy_property
    def cost(self):
        cross_entropy = -tf.reduce_sum(self.target * tf.log(self.prediction))
        return cross_entropy

    @lazy_property
    def optimize(self):
        learning_rate = 0.003
        optimizer = tf.train.RMSPropOptimizer(learning_rate)
        return optimizer.minimize(self.cost)

    @lazy_property
    def error(self):
        mistakes = tf.not_equal(
            tf.argmax(self.target, 1), tf.argmax(self.prediction, 1))
        return tf.reduce_mean(tf.cast(mistakes, tf.float32))

    @staticmethod
    def _weight_and_bias(in_size, out_size):
        weight = tf.truncated_normal([in_size, out_size], stddev=0.01)
        bias = tf.constant(0.1, shape=[out_size])
        return tf.Variable(weight), tf.Variable(bias)


def main():
    # We treat images as sequences of pixel rows.
    train, test = sets.Mnist()
    _, rows, row_size = train.data.shape
    num_classes = train.target.shape[1]
    data = tf.placeholder(tf.float32, [None, rows, row_size])
    target = tf.placeholder(tf.float32, [None, num_classes])
    dropout = tf.placeholder(tf.float32)
    model = SequenceClassification(data, target, dropout)
    sess = tf.Session()
    sess.run(tf.global_variables_initializer())
    for epoch in range(10):
        for _ in range(100):
            batch = train.sample(10)
            sess.run(model.optimize, 
                data: batch.data, target: batch.target, dropout: 0.5)
        error = sess.run(model.error, 
            data: test.data, target: test.target, dropout: 1)
        print('Epoch :2d error :3.1f%'.format(epoch + 1, 100 * error))


if __name__ == '__main__':
    main()

【问题讨论】:

【参考方案1】:

大部分代码都可以直接重用。您唯一需要做的就是修改其中的一部分以适合您的数据格式。例如,

num_classes = 2
data = tf.placeholder(tf.float32, [None, your_seq_length, your_feature_length])
batch = make_your_own_batch

查看您的代码教程。它是here。如果你不熟悉 RNN,here 是一个很好的介绍。

【讨论】:

以上是关于TensorFlow:如何使用 RNN 对二进制序列进行分类?的主要内容,如果未能解决你的问题,请参考以下文章

对 Tensorflow 中的多层双向 RNN 感到困惑

TensorFlow:如何使用具有可变输入长度的 CudnnLSTM(如 dynamic_rnn)?

Tensorflow.keras:RNN 对 Mnist 进行分类

如何用TensorFlow构建RNN

如何用TensorFlow构建RNN

学习Tensorflow的LSTM的RNN例子