如何使用RNNCells将压差添加到我的Tensorflow神经网络中?

时间:2019-01-03 18:47:32

标签: tensorflow recurrent-neural-network dropout

我有一些神经网络(张量流)

    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缺失添加到该神经网络中?

感谢您的建议!

1 个答案:

答案 0 :(得分:1)

您的代码只是对输入X进行了删除,您应该使用tf.contrib.rnn.DropoutWrapperlink)。

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))