首页 > 解决方案 > 如何将亲笔签名的好处与训练循环结合起来?

问题描述

如何使用autograph“魔术”提高以下代码的执行速度?

从这里给定代码:https ://www.tensorflow.org/alpha/tutorials/generation/dcgan

特别是这部分https://www.tensorflow.org/alpha/tutorials/generation/dcgan#train_the_model

def train(dataset, epochs):
  for epoch in range(epochs):
    start = time.time()

    for image_batch in dataset:
      train_step(image_batch)

    # Produce images for the GIF as we go
    display.clear_output(wait=True)
    generate_and_save_images(generator,
                             epoch + 1,
                             seed)

    # Save the model every 15 epochs
    if (epoch + 1) % 15 == 0:
      checkpoint.save(file_prefix = checkpoint_prefix)

    print ('Time for epoch {} is {} sec'.format(epoch + 1, time.time()-start))

  # Generate after the final epoch
  display.clear_output(wait=True)
  generate_and_save_images(generator,
                           epochs,
                           seed)

tf.data.Dataset并考虑将 a 包装到@tf.function通话中的好处。如此处所示:将 tf.data.Dataset 包装到 tf.function 中会提高性能吗?.

我试过的:

由于. train()_ @tf.function_ train但是:此代码通常需要用于日志记录、检查点等。

问题:

  1. 这段代码如何利用两全其美?
  2. 或者甚至有可能吗?

标签: pythontensorflowtensorflow2.0

解决方案


推荐阅读