python - 没有 fit() 使用 keras 和 tf 的 Tensorboard

标签 python tensorflow machine-learning keras tensorboard

我之前和每次都只是在 model.fit() 函数上使用回调,将 tensorboard 与一些相当简单的神经网络一起使用。我试图了解更多关于 GAN 的知识,并试图理解像这样的一些代码

class ACGAN():
    def __init__(self):
        # Input shape
        self.img_rows = 28
        self.img_cols = 28
        self.channels = 1
        self.img_shape = (self.img_rows, self.img_cols, self.channels)
        self.num_classes = 10
        self.latent_dim = 100

        optimizer = Adam(0.0002, 0.5)
        losses = ['binary_crossentropy', 'sparse_categorical_crossentropy']

        # Build and compile the discriminator
        self.discriminator = self.build_discriminator()
        self.discriminator.compile(loss=losses,
            optimizer=optimizer,
            metrics=['accuracy'])

        # Build the generator
        self.generator = self.build_generator()

        # The generator takes noise and the target label as input
        # and generates the corresponding digit of that label
        noise = Input(shape=(self.latent_dim,))
        label = Input(shape=(1,))
        img = self.generator([noise, label])

        # For the combined model we will only train the generator
        self.discriminator.trainable = False

        # The discriminator takes generated image as input and determines validity
        # and the label of that image
        valid, target_label = self.discriminator(img)

        # The combined model  (stacked generator and discriminator)
        # Trains the generator to fool the discriminator
        self.combined = Model([noise, label], [valid, target_label])
        self.combined.compile(loss=losses,
            optimizer=optimizer)

    def build_generator(self):
.......

    def build_discriminator(self):
.........

    def train(self, epochs, batch_size=128, sample_interval=50):

        # Load the dataset
        (X_train, y_train), (_, _) = mnist.load_data()

        # Configure inputs
        X_train = (X_train.astype(np.float32) - 127.5) / 127.5
        X_train = np.expand_dims(X_train, axis=3)
        y_train = y_train.reshape(-1, 1)

        # Adversarial ground truths
        valid = np.ones((batch_size, 1))
        fake = np.zeros((batch_size, 1))

        for epoch in range(epochs):

            # ---------------------
            #  Train Discriminator
            # ---------------------

            # Select a random batch of images
            idx = np.random.randint(0, X_train.shape[0], batch_size)
            imgs = X_train[idx]

            # Sample noise as generator input
            noise = np.random.normal(0, 1, (batch_size, 100))

            # The labels of the digits that the generator tries to create an
            # image representation of
            sampled_labels = np.random.randint(0, 10, (batch_size, 1))

            # Generate a half batch of new images
            gen_imgs = self.generator.predict([noise, sampled_labels])

            # Image labels. 0-9 if image is valid or 10 if it is generated (fake)
            img_labels = y_train[idx]
            fake_labels = 10 * np.ones(img_labels.shape)

            # Train the discriminator
            d_loss_real = self.discriminator.train_on_batch(imgs, [valid, img_labels])
            d_loss_fake = self.discriminator.train_on_batch(gen_imgs, [fake, fake_labels])
            d_loss = 0.5 * np.add(d_loss_real, d_loss_fake)

            # ---------------------
            #  Train Generator
            # ---------------------

            # Train the generator
            g_loss = self.combined.train_on_batch([noise, sampled_labels], [valid, sampled_labels])

            # Plot the progress
            print ("%d [D loss: %f, acc.: %.2f%%, op_acc: %.2f%%] [G loss: %f]" % (epoch, d_loss[0], 100*d_loss[3], 100*d_loss[4], g_loss[0]))

            # If at save interval => save generated image samples
            if epoch % sample_interval == 0:
                self.save_model()
                self.sample_images(epoch)

    def sample_images(self, epoch):
        r, c = 10, 10
        noise = np.random.normal(0, 1, (r * c, 100))
        sampled_labels = np.array([num for _ in range(r) for num in range(c)])
        gen_imgs = self.generator.predict([noise, sampled_labels])
        # Rescale images 0 - 1
        gen_imgs = 0.5 * gen_imgs + 0.5

        fig, axs = plt.subplots(r, c)
        cnt = 0
        for i in range(r):
            for j in range(c):
                axs[i,j].imshow(gen_imgs[cnt,:,:,0], cmap='gray')
                axs[i,j].axis('off')
                cnt += 1
        fig.savefig("images/%d.png" % epoch)
        plt.close()



if __name__ == '__main__':
    acgan = ACGAN()
    acgan.train(epochs=14000, batch_size=32, sample_interval=200)

由于这段代码中没有 fit() 函数,我不确定应该在哪里导入 tensorboard 回调以及如何可视化模型? 我删除了构建生成器和构建鉴别器函数,因为我认为它们不会在其中,但如果我错了请纠正我。 我无法发布整个代码,所以 here you go如果你想要更多的细节

最佳答案

我正在使用 TF2,以下代码对我有用:

log_dir = os.path.join("logs", datetime.datetime.now().strftime("%Y%m%d-%H%M%S"))
summary_writer = tf.summary.create_file_writer(logdir=log_dir)
for epoch in range(num_epochs):
  epoch_loss_avg = tf.keras.metrics.Mean()
  epoch_accuracy = tf.keras.metrics.SparseCategoricalAccuracy()

  for x, y in train_dataset:
    loss_value, grads = grad(model, x, y)
    optimizer.apply_gradients(zip(grads, model.trainable_variables))
    epoch_loss_avg(loss_value)
    epoch_accuracy(y, model(x))

  train_loss_results.append(epoch_loss_avg.result()) 
  train_accuracy_results.append(epoch_accuracy.result())

  with summary_writer.as_default():
    tf.summary.scalar('epoch_loss_avg', epoch_loss_avg.result(), step=optimizer.iterations)
    tf.summary.scalar('epoch_accuracy', epoch_accuracy.result(), step=optimizer.iterations)

您可以找到完整的代码 here因为我删除了我的代码中的一些评论以保持答案准确。我不明白这是如何工作的,因为我没有找到 TF2 的文档,我的代码只是根据我在其他人的代码中找到的内容进行的反复试验。

关于python - 没有 fit() 使用 keras 和 tf 的 Tensorboard,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52471464/

相关文章:

python - 如何自动加载图像文件夹?

python - 实时对象检测没有名为 'tensorflow.compat.v1' 的模块

machine-learning - 使用 gensim 构建的 doc2vec 模型的 GridSearch

machine-learning - LibSVM 中的特征值缩放是否有必要?

machine-learning - 发布具有特征哈希的 Azure 机器学习服务

python - 多页 tiff 调整 python 大小

python - PyODBC 从简单的 SELECT 子句获取结果

python - 瓶内IP过滤

Tensorflow 将 op 应用于 2d 张量的每个元素

python - 一定范围内的自定义准确度指标,用于回归