keras multi output: one loss depends on another

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



keras multi output: one loss depends on another



I am training a cGAN in Keras which has two outputs:



1: real/fake (1/0)



2: target label (a vector of size 10 (i am classifying the output to 10 classes))



For real/fake, I am using binary cross entropy as loss function



For target label I want to use categorical cross entropy when label for real/fake is 1 (real) and loss=0 when label for real/fake is 0 (fake).



So the loss function of one output depends on the ground truth of the other output. I have been trying different ways but none seem to work. Can anyone help me out?



Here's the network:


def Discriminator():
input_shape = (96,96,3)
inputs = Input(shape=(input_shape))

x1 = Convolution2D(32,(3,3), strides=(1,1), padding='same')(inputs)
x1 = BatchNormalization()(x1)
x1 = LeakyReLU(0.2)(x1)
x1 = Convolution2D(32,(3,3), strides=(2,2), padding='same')(x1)
x1 = BatchNormalization()(x1)
### More Conv,BN and Activation layers
x1 = Flatten()(x1)
x1 = Dropout(0.5)(x1)
x2 = Dense(1)(x1)
x2 = Activation('sigmoid', name="real_fake")(x2)
x3 = Dense(10)(x1)
x3 = Activation('softmax', name="class_labels")(x3)
mdl = Model(inputs=inputs, outputs=[x2,x3])
return mdl

def Generator():
inputs = Input(shape=(7,7,2048))
img = Input(shape=(96,96,3))
labels = Input(shape=(1,10))

t = Dense(256, activation = 'relu')(labels)
t1 = Reshape((16,16,1))(t)
t1 = Convolution2D(8,(3,3), strides=(2,2), padding='valid')(t1)

x = Concatenate(axis=3)([inputs,t1])
x = UpSampling2D((2, 2))(x)
x = Convolution2D(512, kernel_size=(3, 3), padding='valid')(x)
x = BatchNormalization()(x)
x = LeakyReLU(0.2)(x)
## More UpSam, Conv, BN and Activation
x = Activation('tanh')(x)
x = Add()([x,img])
model = Model(inputs=[inputs,img,labels] , outputs=x)
return model

def GAN(generator, discriminator):
inputs = Input(shape=(7,7,2048))
img = Input(shape=(96,96,3))
labels = Input(shape=(1,10))
x = generator([inputs, img, labels])
discriminator.trainable = False
y1,y2 = discriminator(x)
mdl = Model([inputs,img,labels], [y1,y2])
return mdl

discriminator = Discriminator()
generator = Generator()
gan = GAN(generator, discriminator)
g_optimizer = Adam(lr=2e-4, beta_1=0.5)
generator.compile(loss='binary_crossentropy', optimizer=g_optimizer)
gan.compile(loss=['binary_crossentropy', 'categorical_crossentropy'], optimizer=g_optimizer)
d_optimizer = Adam(lr=1e-5, beta_1=0.1)
discriminator.trainable = True
discriminator.compile(loss=['binary_crossentropy', CUSTOM_LOSS], optimizer=d_optimizer)



Basically when training the Discriminator for real or fake I want the Discriminator to train for target class labels when the input is a real image and not a fake generated by the Generator. So I want to write a CUSTOM_LOSS that is cce when Discrimnator gets a real image and Zero when it gets a fake.



Thanks





Can you share the code of your network?
– sdcbr
Aug 10 at 19:51









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS