在tensorflow中添加d​​ropout导致错误的结果?

时间:2017-04-25 11:50:32

标签: machine-learning tensorflow deep-learning

我使用tensorflow和python来预测演示中的股票价格。但是当我向代码添加dropout时,生成的数字似乎不正确。请告知错误的地方。

lightData: LightData[];
public lineChartData = Array<any>();
public lineChartLabels = Array<any>();

ngOnInit() {
    this.lightData = []; // Array is empty
    this.loadAll(); // Load all the datas
    this.setChartData() // Set datas of my chart
}

loadAll() {
    this.lightDataService.query().subscribe(
        (res: Response) => this.onSuccess(res.json(), res.headers),
        (res: Response) => this.onError(res.json())
    );
}

public setChartData(){
    for(let i =0; i< this.lightData.length; i++)
    {
        this.lineChartLabels.push(this.lightData[i].id);
        this.lineChartData.push(this.lightData[i].value);
    }
}

private onSuccess(data, headers) {
    for (let i = 0; i < data.length; i++) {
        this.lightData.push(data[i]);
    }
}

private onError(error) {
    this.alertService.error(error.message, null, null);
}

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:1)

您应该只在训练中应用辍学,但不应在推理中应用。

您可以通过占位符传递辍学概率来实现此目的。

然后在推理时将保持概率设置为1。

作为你的例子:

input_keep_prob = tf.placeholder(tf.float32)
output_keep_prob = tf.placeholder(tf.float32)
with tf.variable_scope(scope_name):
    cell = tf.nn.rnn_cell.BasicLSTMCell(num_units=n_inputs)
    lstm_dropout = tf.nn.rnn_cell.DropoutWrapper(cell,input_keep_prob=input_keep_prob, 
        output_keep_prob=output_keep_prob)
    cell = tf.nn.rnn_cell.MultiRNNCell([lstm_dropout]*num_layers)
    output, state = tf.nn.rnn(cell, input, dtype=tf.float32) 
#setup your loss and training optimizer
#y_pred = .....
#loss = .....
#train_op = .....

with tf.Session() as sess:
    sess.run(train_op, feed_dict={input_keep_prob=0.7, output_keep_prob=0.7}) #set dropout when training
    y = sess.run(y_pred, feed_dict={input_keep_prob=1.0, output_keep_prob=1.0}) #retrieve the prediction without dropout when inference