我实际上是在使用TF keras开发基本GAN,在这里我使用 train_on_batch 方法来训练生成器和鉴别器,或者没有 callbacks 参数用于将张量板日志写为例如keras模型的 fit 方法。现在,我想在训练期间编写模型日志,以监控张量板上的权重和渐变。
培训代码部分如下,
def train(g_model,d_model,gan_model,dataset,latent_dim,seed,n_epochs=100,n_batch=128):
bat_per_epo = int(dataset.shape[0] / n_batch)
half_batch = int(n_batch / 2)
for i in range(n_epochs):
for j in range(bat_per_epo):
# Training discriminator with real images
X_real,y_real = generate_real_samples(dataset,half_batch)
d_loss1,_ = d_model.train_on_batch(X_real,y_real * .9) # Label Smoothing
# Training discriminator with fake images
X_fake,y_fake = generate_fake_samples(g_model,half_batch)
d_loss2,_ = d_model.train_on_batch(X_fake,y_fake + .1) # Label Smoothing
# Training generator with latent points
X_gan = generate_latent_points(latent_dim,n_batch)
y_gan = ones((n_batch,1))
g_loss = gan_model.train_on_batch(X_gan,y_gan)
if not j%10:
print('>%d,%d/%d,d1=%.3f,d2=%.3f g=%.3f' %(i+1,j+1,bat_per_epo,d_loss1,d_loss2,g_loss))
display.clear_output(True)
print('>%d,g_loss))
summarize_performance(i,g_model,seed)
display.clear_output(True)
print('>%d,g_loss))
summarize_performance(i,seed)
我发现了一些计算梯度的方法,
但是我对此以及如何记录不带回调选项的渐变感到困惑。有人可以帮我吗?