我对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个时期更新一次,在约150个时期之后发生器也不会改善
示例输出如下: Epoch 500
我还想指出鉴别器确实可以正常工作。我开始使用鉴别器作为mnist的分类CNN,然后我基本上将所有输出合并到一个节点中,以便与GAN一起使用。
其他输出也遵循类似的平铺模式,我不知道为什么。如果有人能提供帮助就会很棒!如果您需要更多信息,请告诉我。