如何使用 RNNCells 将 dropout 添加到我的 tensorflow 神经网络中?

How add dropout into my tensorflow neural network with RNNCells?

我有一些神经网络(tensorflow)

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

    layers = []
    for i in range(n_layers):
        layers.append(tf.contrib.rnn.BasicRNNCell(num_units=n_neurons, activation=tf.nn.relu))


    multi_layer_cell = tf.contrib.rnn.MultiRNNCell(layers)

    rnn_outputs, states = tf.nn.dynamic_rnn(multi_layer_cell, X, dtype=tf.float32)

这样(下)是正确的吗?它正在工作,但我不确定 ;)

training = tf.placeholder_with_default(True,shape=())
X_dropout = tf.layers.dropout(X,dropout_rate,training=training)
rnn_outputs, states = tf.nn.dynamic_rnn(multi_layer_cell, X_dropout, dtype=tf.float32)

如何在这个神经网络中加入tensorflow dropout?

感谢您的任何建议!

您的代码只是对输入 X 进行 dropout,您应该使用 tf.contrib.rnn.DropoutWrapper(link).

layers = []
for i in range(n_layers):
    layers.append(tf.contrib.rnn.DropoutWrapper(tf.contrib.rnn.BasicRNNCell(num_units=n_neurons
                                                                            , activation=tf.nn.relu)
                                                ,output_keep_prob=1-dropout_rate))