【问题标题】:Implementing a Generative RNN with continuous input and discrete output实现具有连续输入和离散输出的生成式 RNN
【发布时间】:2017-12-18 00:21:01
【问题描述】:

我目前正在使用生成式 RNN 对序列中的索引进行分类(有点说某事物是噪声还是非噪声)。

我的输入是连续的(即 0 到 1 之间的实数值),我的输出是(0 或 1)。

例如,如果模型将大于 0.5 的数字标记为 1,否则标记为 0,

[.21, .35, .78, .56, ..., .21] => [0, 0, 1, 1, ..., 0]:

   0     0     1     1          0
   ^     ^     ^     ^          ^
   |     |     |     |          |
o->L1  ->L2  ->L3  ->L4 ->... ->L10
   ^     ^     ^     ^          ^
   |     |     |     |          |
   .21  .35   .78   .56   ...  .21

使用

n_steps = 10
n_inputs = 1
n_neurons = 7
X = tf.placeholder(tf.float32, [None, n_steps, n_inputs])
y = tf.placeholder(tf.float32, [None, n_steps, n_outputs])

cell = tf.contrib.rnn.BasicRNNCell(num_units=n_neurons, activation=tf.nn.relu)
rnn_outputs, states = tf.nn.dynamic_rnn(cell, X, dtype=tf.float32)

rnn_outputs 变为 (?, 10, 7) 形状张量,假定每 10 个时间步长有 7 个输出。

以前,我在输出投影上运行以下 sn-p 包装 rnn_outputs 以获得每个序列的分类标签。

xentropy = tf.nn.sparse_softmax_cross_entropy_with_logits(labels=y,logits=logits)

loss = tf.reduce_mean(xentropy)

我如何在 rnn_outputs 上运行类似的东西来获得序列?

具体来说,

1.我可以从每个步骤中获取 rnn_output 并将其输入到 softmax 中吗?

curr_state = rnn_outputs[:,i,:]
logits = tf.layers.dense(states, n_outputs)
xentropy = tf.nn.sparse_softmax_cross_entropy_with_logits(labels=y, logits=logits)

2。我应该使用什么损失函数?应该将它应用于每个序列的每个值?(对于序列 i 和步骤 jloss = y_{ij} (true) - y_{ij}(predicted))?

我的损失应该是loss = tf.reduce_mean(np.sum(xentropy))吗?

编辑 看来我正在尝试实现类似于 TensorFlow 中 https://machinelearningmastery.com/develop-bidirectional-lstm-sequence-classification-python-keras/ 中的类似内容。

在 Keras 中,有一个 TimeDistributed 函数:

然后您可以使用 TimeDistributed 将 Dense 层应用于每个 10 个时间步,独立

我将如何在 Tensorflow 中实现类似的东西?

【问题讨论】:

    标签: machine-learning tensorflow neural-network lstm recurrent-neural-network


    【解决方案1】:

    首先,您似乎在进行 seq-to-seq 建模。在这类问题中,使用编码器-解码器架构通常是一个好主意,而不是从同一个 RNN 预测序列。 Tensorflow 有一个名为 "Neural Machine Translation (seq2seq) Tutorial" 的大型教程,我建议您查看。

    但是,如果n_steps 是已知的静态(尽管使用dynamic_rnn),您询问的架构也是可能的。在这种情况下,可以计算每个单元输出的交叉熵,然后将所有损失相加。如果 RNN 长度也是动态的,那么它是可能的,但会更毛茸茸。代码如下:

    n_steps = 2
    n_inputs = 3
    n_neurons = 5
    
    X = tf.placeholder(dtype=tf.float32, shape=[None, n_steps, n_inputs], name='x')
    y = tf.placeholder(dtype=tf.int32, shape=[None, n_steps], name='y')
    basic_cell = tf.nn.rnn_cell.BasicRNNCell(num_units=n_neurons)
    outputs, states = tf.nn.dynamic_rnn(basic_cell, X, dtype=tf.float32)
    
    # Reshape to make `time` a 0-axis
    time_based_outputs = tf.transpose(outputs, [1, 0, 2])
    time_based_labels = tf.transpose(y, [1, 0])
    losses = []
    for i in range(n_steps):
      cell_output = time_based_outputs[i]   # get the output, can do apply further dense layers if needed
      labels = time_based_labels[i]         # get the label (sparse)
      loss = tf.nn.sparse_softmax_cross_entropy_with_logits(labels=labels, logits=cell_output)
      losses.append(loss)                   # collect all losses
    total_loss = tf.reduce_sum(losses)      # compute the total loss
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-19
      • 2019-03-26
      • 2010-10-24
      • 2016-09-07
      • 1970-01-01
      • 2014-10-05
      相关资源
      最近更新 更多