0
votes

I'm trying to train a neural network in keras but I'm getting as error that there are no gradients for any variable, which may imply that the graph is disconnected. I'm copying here a stripped down version of the code with only the bit related to the model definition.

The model accepts two inputs that will be fed, one at time, to the same shared model: the encoder. The two outputs of the encoder are then concatenated and sent to a dense layer to compute the final output.

I don't get what's wrong, it looks like that when instantiating the encoder I'm creating additional trainable variables that are not used anywhere.

For the network layout I was getting inspiration from the official keras docs: https://keras.io/guides/functional_api/#all-models-are-callable-just-like-layers

def _get_encoder(self, model_input_shape):
    encoder_input = Input(shape=model_input_shape)

    x = encoder_input

    x = Conv2D(32, (3, 3), strides=1, padding="same")(x)
    x = BatchNormalization(axis=-1)(x)        
    x = LeakyReLU(alpha=0.1)(x)

    latent_z = Flatten()(x)
    latent_z = Dense(self.latent_dim)(latent_z)


    encoder = Model(
            encoder_input,
            latent_z,
            name='encoder'
    )

    return encoder

def build_model(self):
    model_input_shape = (self.height, self.width, self.depth)

    model_input_1 = Input(shape=model_input_shape)
    model_input_2 = Input(shape=model_input_shape)

    self.encoder = self._get_encoder(model_input_shape)

    z_1 = self.encoder(model_input_1)
    z_2 = self.encoder(model_input_2)

    x = concatenate([z_1, z_2])

    prediction = Dense(1, activation='sigmoid')(x)

    self.network = Model(
            inputs=[model_input_1, model_input_2],
            outputs=[prediction],
            name = 'network'
    )

network.network.compile(
            optimizer='rmsprop', 
            loss='mse', 
            metrics=['mae'])

H = network.network.fit(
            x=train_gen,
            validation_data=test_gen,
            epochs=EPOCHS,
            steps_per_epoch=STEPS,
            validation_steps=STEPS)
1

1 Answers

0
votes

I found the problem. My custom data generator was returning a list [x,y] instead of a tuple (x,y). Where x is the input and y the target. A simple mistake that was causing totally unrelated errors.