【发布时间】:2020-04-22 02:36:01
【问题描述】:
我一直在关注卷积 VAE 的 TF 2.0 教程,位于 here。
因为它很急切,所以梯度是手动计算的,然后使用 tf.GradientTape() 手动应用。
for epoch in epochs:
for x in x_train:
with tf.GradientTape() as tape:
loss = compute_loss(model, x)
apply_gradients(tape.gradient(loss, model.trainable_variables))
该代码的问题在于它非常慢,每个 epoch 大约需要 40-50 秒。 如果我将批量大小增加很多(到 2048 左右),那么最终每个 epoch 需要大约 8 秒,但模型的性能会下降很多。
另一方面,如果我做一个更传统的模型(即使用基于惰性图的模型而不是 Eagerness),例如 here,那么每个 epoch 需要 8 秒,即使是很小的批量大小。
model.add_loss(lazy_graph_loss)
model.fit(x_train epochs=epochs)
根据这些信息,我的猜测是 TF2.0 代码的问题在于手动计算损失和梯度。
有没有什么办法可以加快TF2.0代码的速度,使其更接近正常的代码?
【问题讨论】:
-
几乎没有人会从您的链接中读取大量代码。阅读this article 以改进您的问题。
标签: python tensorflow keras