前向传播慢 - 训练时间正常

时间:2018-04-16 20:11:50

标签: python python-3.x performance tensorflow tensorboard

我无法弄清楚为什么当我执行向前传播时,我的代码非常慢。有问题的代码可以在这里找到:https://github.com/rekkit/lazy_programmer_ml_course/blob/develop/05_unsupervised_deep_learning/poetry_generator_rnn.py

我将代码的性能与此代码的性能进行比较:https://github.com/lazyprogrammer/machine_learning_examples/blob/master/rnn_class/srn_language_tf.py

不同之处在于我跑

self.session.run(self.predict(x_batch), feed_dict={...})

或我跑步时

self.returnPrediction(x_batch)

运行大约需要0.14秒。现在这可能听起来不像是灾难,但每句话的时间为0.14秒(我正在制作一个RNN来预测句子中的下一个单词)。由于有1436个句子,我们每个时期大约需要3分20秒。如果我想训练10个时代,那就是半个小时。比其他代码更多的方式。

有没有人知道问题可能是什么?我能看到的唯一区别是我已经模块化了代码。

感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

我已经弄清楚了。每当我调用预测方法时,我都在重建图形。相反,在fit方法中我定义了一个变量:

preds = self.predict(self.tfX)

然后每次我需要预测时,而不是使用:

predictions = self.session.run(self.predict(x_batch), feed_dict={...})

我用:

predictions = self.session.run(self.preds, feed_dict={...})