Nick
Nick

Reputation: 2970

AttributeError: 'Tensor' object has no attribute '_keras_history' when using backend random_uniform

I'm implementing a WGAN-GP in Keras where I calculate the random weighted average of two tensors.

def random_weighted_average(self, generated, real):
    alpha = K.random_uniform(shape=K.shape(real))
    diff = keras.layers.Subtract()([generated, real])
    return keras.layers.Add()([real, keras.layers.Multiply()([alpha, diff])])

This is how it's used. It throws the error once I try to create the discriminator_model.

averaged_samples = self.random_weighted_average(
    generated_samples_for_discriminator, 
    real_samples)
averaged_samples_out = self.discriminator(averaged_samples)

discriminator_model = Model(
    inputs=[real_samples, generator_input_for_discriminator],
    outputs=[
        discriminator_output_from_real_samples,
        discriminator_output_from_generator, 
        averaged_samples_out
    ])

My backend is TensorFlow. When I use alpha in the last line I get the following error:

AttributeError: 'Tensor' object has no attribute '_keras_history'

I tried swapping alpha out for both real and generated to see if it had to do with the backend tensor and that was the case (the error disappeared). So what could be causing this issue? I need a random uniformly sampled tensor with the shape of real or generated.

Upvotes: 0

Views: 263

Answers (1)

nuric
nuric

Reputation: 11225

Custom operations that use backend function need to be wrapped around a Layer. If you don't have any trainable weights, as in your case, the simplest approach is to use a Lambda layer:

def random_weighted_average(inputs):
  generated, real = inputs
  alpha = K.random_uniform(shape=K.shape(real))
  diff = generated - real
  return real + alpha * diff
averaged_samples = Lambda(random_weighted_average)([generated_for_discriminator, real_samples])

Upvotes: 1

Related Questions