![](/img/trans.png)
[英]Tensorflow: How to determine the value of clip_norm when using clip_by_norm or clip_by_global_norm?
[英]How to weight clip in tensorflow?
我正在 mnist 数据集上的 tensorflow 中编码 wgan,它运行良好,但我发现很难在 tensorflow 中裁剪鉴别器模型[-0.01,0.01]
权重。 在keras 中,我们可以使用权重裁剪。
for l in self.discriminator.layers:
weights = l.get_weights()
weights = [np.clip(w, -self.clip_value, self.clip_value) for w in weights]
l.set_weights(weights)
我找到了一个用于权重裁剪鉴别器的 tensorflow 文档
tf.contrib.gan.features.clip_discriminator_weights(
optimizer,
model,
weight_clip
)
除此之外,没有太多说明如何使用此功能。
#my tf code
def generator(z):
h=tf.nn.relu(layer_mlp(z,"g1",[10,128]))
prob=tf.nn.sigmoid(layer_mlp(h,"g2",[128,784]))
return prob
def discriminator(x):
h=tf.nn.relu(layer_mlp(x,"d1",[784,128]))
logit=layer_mlp(h,"d2",[128,1])
prob=tf.nn.sigmoid(logit)
return prob
G_sample=generator(z)
D_real= discriminator(x)
D_fake= discriminator(G_sample)
D_loss = tf.reduce_mean(D_real) - tf.reduce_mean(D_fake)
G_loss = -tf.reduce_mean(D_fake)
for epoch in epochs:
#training the model
您可以使用以下函数在 tensorflow 中实现裁剪。
tf.clip_by_value(
t,
clip_value_min,
clip_value_max,
name=None
)
请参阅以下链接,了解如何在您的代码中实现它。
添加到 Yaakov 的答案中,您可以使用 tf.clip_by_value 和 trainable_variables,如此回购https://github.com/hcnoh/WGAN-tensorflow2所示
for w in model.discriminator.trainable_variables:
w.assign(tf.clip_by_value(w, -clip_const, clip_const))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.