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.