Troubleshooting Keras Lambda layer gradient calculation: NoneType dtype error

I’m having trouble with my neural network. It’s supposed to calculate gradients from a dense layer based on some given coordinates, but I’m encountering an error when using the Keras backend in a Lambda layer. During training, I get this message:

AttributeError: 'NoneType' object has no attribute 'dtype'

Below is an alternative version of my code that still reflects the same setup but with different variable names and structure:

import numpy as np
from keras.models import Model
from keras.layers import Input, Dense, Lambda
from keras import backend as backend_lib

# Create example data
data_input = np.random.rand(500, 15)
data_coords = np.random.rand(500, 4, 3)
data_targets = np.random.rand(500, 4, 3)

# Build model components
primary_input = Input((15,))
secondary_input = Input((4, 3))

hidden_layer = Dense(800, activation='relu')(primary_input)
predicted_output = Dense(1, activation='relu')(hidden_layer)

# Use Lambda layer for gradients
gradient_output = Lambda(lambda tensors: backend_lib.gradients(tensors[0], tensors[1]))([predicted_output, secondary_input])

# Compile model
model = Model(inputs=[primary_input, secondary_input], outputs=gradient_output)
model.compile(optimizer='adam', loss='mae')

# Fit model
model.fit([data_input, data_coords], data_targets, epochs=50, batch_size=32)

The model summary indicates a ‘multiple’ output shape for the Lambda layer. I would really appreciate any insights into why the gradient calculation might be failing.

hey there! have you tried using tf.GradientTape instead? it’s more flexible for custom gradient stuff. also, whats the shape of ur predicted_output? maybe it needs reshaping before the gradient calc. curious to hear if you’ve tried any other approaches!

I’ve encountered similar issues with gradient calculations in Keras Lambda layers. The problem likely stems from the shape mismatch between your predicted_output and secondary_input tensors. The gradient function expects compatible shapes, but your setup might not provide that.

To resolve this, try reshaping your predicted_output to match the dimensions of secondary_input before calculating gradients. You can modify your Lambda layer like this:

gradient_output = Lambda(lambda tensors: backend_lib.gradients(
    backend_lib.reshape(tensors[0], backend_lib.shape(tensors[1])),
    tensors[1]
))([predicted_output, secondary_input])

This reshaping should ensure that the gradient calculation has compatible tensor shapes. Additionally, double-check that your loss function is appropriate for the output shape of your gradient_output layer. If issues persist, consider using TensorFlow’s GradientTape for more explicit gradient calculations within a custom layer.

yo man, i had a similar issue. try wrapping ur gradient calc in a try-except block. it might be that the gradient is None for some inputs. also, double check ur tensor shapes match up. sometimes the backend can be finicky with that stuff. good luck!