Python 如何正确使用RNN中的Dropout

Python 如何正确使用RNN中的Dropout,python,tensorflow,Python,Tensorflow,我想使用tensorflow的退出函数来检查我是否可以改进我的递归神经网络的结果(TPR,FPR)。 然而,我通过遵循指南来实现它。所以我不确定我是否犯了错误。但是,如果我用例如10个时代来训练我的模型,我在验证后会得到几乎相同的结果。这就是为什么我不确定是否正确使用了退出功能。这是下面代码中正确的实现还是我做错了什么?如果我做的每件事都是对的,为什么我会得到几乎相同的结果呢 hm_epochs = 10 n_classes = 2 batch_size = 128 chunk_size = 3

我想使用tensorflow的退出函数来检查我是否可以改进我的递归神经网络的结果(TPR,FPR)。 然而,我通过遵循指南来实现它。所以我不确定我是否犯了错误。但是,如果我用例如10个时代来训练我的模型,我在验证后会得到几乎相同的结果。这就是为什么我不确定是否正确使用了退出功能。这是下面代码中正确的实现还是我做错了什么?如果我做的每件事都是对的,为什么我会得到几乎相同的结果呢

hm_epochs = 10
n_classes = 2
batch_size = 128
chunk_size = 341
n_chunks = 5
rnn_size = 32

dropout_prop = 0.5 # Dropout, probability to drop a unit

batch_size_validation = 65536
x = tf.placeholder('float', [None, n_chunks, chunk_size])
y = tf.placeholder('float')

def recurrent_neural_network(x):
     layer = {'weights':tf.Variable(tf.random_normal([rnn_size, n_classes])),
              'biases':tf.Variable(tf.random_normal([n_classes]))}

     x = tf.transpose(x, [1,0,2])
     x = tf.reshape(x, [-1, chunk_size])
     x = tf.split(x, n_chunks, 0)

     lstm_cell = rnn.BasicLSTMCell(rnn_size) 
     outputs, states = rnn.static_rnn(lstm_cell, x, dtype=tf.float32)

     output = tf.matmul(outputs[-1], layer['weights']) + layer['biases']

     #DROPOUT Implementation -> is this code really working?
     #The result is nearly the same after 20 epochs...
     output_layer = tf.layers.dropout(output, rate=dropout_prop)

     return output

def train_neural_network(x):
     prediction = recurrent_neural_network(x)
     cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=prediction,labels=y))
     optimizer = tf.train.AdamOptimizer().minimize(cost)

     with tf.Session() as sess:
         sess.run(tf.global_variables_initializer())

         for epoch in range(1,hm_epochs+1):
             epoch_loss = 0

             for i in range(0, training_data.shape[0], batch_size):
                 epoch_x = np.array(training_data[i:i+batch_size, :, :], dtype='float')
                 epoch_y = np.array(training_labels[i:i+batch_size, :], dtype='float')

                 if len(epoch_x) != batch_size:
                     epoch_x = epoch_x.reshape((len(epoch_x), n_chunks, chunk_size))
                 else:
                     epoch_x = epoch_x.reshape((batch_size, n_chunks, chunk_size))

                 _, c = sess.run([optimizer, cost], feed_dict={x: epoch_x, y: epoch_y}) 
                 epoch_loss += c

 train_neural_network(x)
 print("rnn - finished!")

在其最基本的形式中,退出应该发生在单元格内部并应用于权重。你只是在事后才申请的。通过一些很好的可视化和很少的变化,对它进行了很好的解释

要在代码中使用它,您可以

  • 实现您自己的RNN单元,其中keep probability是初始化单元的参数,或者是每次调用时传入的参数

  • 使用rnn退出包装器


  • 在其最基本的形式中,退出应该发生在单元格内部并应用于权重。你只是在事后才申请的。通过一些很好的可视化和很少的变化,对它进行了很好的解释

    要在代码中使用它,您可以

  • 实现您自己的RNN单元,其中keep probability是初始化单元的参数,或者是每次调用时传入的参数

  • 使用rnn退出包装器


  • 谢谢你的解释。我注意到了我的错误。我想试试这两个建议。谢谢你的解释。我注意到了我的错误。我想尝试一下这两个建议。