Keras中GAN的断开连接图

时间:2017-12-14 15:21:53

标签: python-2.7 graph neural-network keras

对于此代码:

     # Initialize generator - feed noise and profile images
noise = random_normal(shape = (-1, 8, 8, z_dim), mean = 0.0, stddev = 1.0, dtype = None, seed = None)

noise      = Input(tensor = noise)
input_data = Input(shape = (128, 128, 3))

generated_img = generator_network(input_data, noise)

# Initialize discriminator - feed frontal faces as ground truth and the generated images as fake
generated_img = Input(tensor = generated_img)

true_score = discriminator_network(input_data)
fake_score = discriminator_network(generated_img)

# Optimizer
Adam_optimizer = Adam(lr = learning_rate, beta_1 = 0.9, beta_2 = 0.999, epsilon = 1e-08, decay = decay_rate)

# Losses
discrim_loss = discriminator_loss(true_score, fake_score)
var_loss     = variation_loss(input_data, generated_img)
pix_loss     = pixel_loss(input_data, generated_img)
cross_loss   = cross_entropy_loss(true_score, fake_score)
gen_loss     = generator_loss(discrim_loss, var_loss, pix_loss, cross_loss)

# Models
discriminator = Model(inputs = generated_img      , outputs = fake_score)
generator     = Model(inputs = [input_data, noise], outputs = generated_img)

# Compilers
discriminator.compile(optimizer = Adam_optimizer, loss = discriminator_loss)
generator.compile(    optimizer = Adam_optimizer, loss = generator_loss)

我收到此错误:

  

追踪(最近一次通话):     文件“main.py”,第74行,in       generator = Model(inputs = [input_data,noise],outputs = generated_img)     在包装器中输入文件“/home/diana/Documents/VirtualNN/local/lib/python2.7/site-packages/keras/legacy/interfaces.py”,第87行       return func(* args,** kwargs)     在 init 中输入文件“/home/diana/Documents/VirtualNN/local/lib/python2.7/site-packages/keras/engine/topology.py”,第1793行       STR(layers_with_complete_input))   RuntimeError:Graph disconnected:无法在层“input_3”获取张量Tensor(“conv2d_35 / Relu:0”,shape =(?,?,?,3),dtype = float32)的值。访问以下先前的图层时没有问题:[]

有谁知道为什么它说我的模型生成器不是连接图?根据我的理解,它是相互联系的。 但也许还有其他一些我看不到的东西。

1 个答案:

答案 0 :(得分:0)

如果您的目的是构建GAN模型,则应将生成器网络和鉴别器网络包装为另一个网络中的两个连续层。例如:

from keras.models import Sequential

# generator_network() and generator_network() should each have an Input layer 
#   that defines their input shapes respectively
g_network = generator_network()
d_network = discriminator_network()

gan_network = Sequential()
gan_network.add(g_network)
d_network.trainable = False
gan_network.add(d_network)

# Compilers
d_network.compile(optimizer = Adam_optimizer, loss = discriminator_loss)
gan_network.compile(optimizer = Adam_optimizer, loss = generator_loss)