我对 tensorflow 有点陌生,所以如果问题很明显,请原谅我。基本上,我正在制作一个生成对抗网络,而我的生成器会产生非常重复的瓦片状模式。目前我的发电机模型看起来像这样
def generator(input):
net = dense_block(input, 1024*4*4, "dense-1")
net = tf.reshape(net, [-1,4,4,1024])
net = tf.nn.relu(tf.layers.batch_normalization(net, name="bn1"))
print ("Dense 1", net.get_shape())
net = deconv_block(net, 512, [5,5], [1,2,2,1], "SAME", "deconv1")
net = tf.layers.batch_normalization(net, name="bn2")
net = tf.nn.relu(net)
print ("deconv 1", net.get_shape())
net = deconv_block(net, 256, [5,5], [1,2,2,1], "SAME", "deconv2")
net = tf.layers.batch_normalization(net, name="bn3")
net = tf.nn.relu(net)
print ("deconv 2", net.get_shape())
net = deconv_block(net, 128, [5,5], [1,2,2,1], "SAME", "deconv3")
net = tf.layers.batch_normalization(net, name="bn4")
net = tf.nn.relu(net)
print ("deconv 3", net.get_shape())
net = deconv_block(net, channels, [5,5], [1,2,2,1], "SAME", "deconv4")
net = tf.layers.batch_normalization(net, name="bn5")
net = tf.nn.tanh(net)
return net
其中密集块由以下组成:
def dense_block(net, out_dim, name):
w = init_weights([net.get_shape().as_list()[-1], out_dim], name=name+"-weights")
b = init_biases(out_dim, name=name+"-biases")
dense = tf.matmul(net, w) + b
return dense
deconv 块如下所示:
def deconv_block(net, filter_num, kernel_size, stride_size, padding, name):
shape = [kernel_size[1], kernel_size[0], filter_num, net.get_shape().as_list()[-1]]
in_shape = net.get_shape().as_list()
w = init_weights(shape, name)
b = init_biases(filter_num, name)
out_shape=[in_shape[0], in_shape[2]*2, in_shape[1]*2, filter_num]
deconv = tf.nn.conv2d_transpose(value=net, filter=w, strides=stride_size, output_shape=out_shape, padding=padding, name=name)
deconv = tf.reshape(tf.nn.bias_add(deconv, b), deconv.get_shape())
return deconv
权重和偏差创建如下:
def init_weights(shape, name):
return tf.get_variable(initializer=tf.truncated_normal(shape, stddev=0.05), name=name+"-weights")
def init_biases(length, name):
return tf.get_variable(initializer=tf.constant(0.05, shape=[length]), name=name+"-biases")
损失函数和优化器是:
g_loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=D_net2, labels=tf.ones_like(D_net2)))
g_opt = tf.train.AdamOptimizer().minimize(loss=g_loss, var_list=g_params)
我正在训练图“G”中的所有参数,定义如下:
g_params = [v for v in vars if v.name.startswith('G/')]
这似乎确实返回了正确数量的变量。网络被输入一个大小为 (batch_size, 100) 的张量,它应该开始类似于一些放大的 mnist 数据(缩放到 64x64)。我从错误中注意到的是,生成器仅在几个时期的损失方面有所改善,然后是平坦的。即使判别器仅每 50 个 epoch 更新一次,生成器在大约 150 个 epoch 后也没有改进
示例输出如下所示: Epoch 500
我还想指出,鉴别器确实可以正常工作。我开始将鉴别器作为 mnist 的分类 CNN,然后我基本上将所有输出合并到一个节点中,以便与 GAN 一起使用。
其他输出也遵循类似的平铺模式,我不知道为什么。如果有人可以提供帮助,那就太好了!如果您需要更多信息,请告诉我。