如何编写Keras自定义二次层实现逐样本计算X.T*X多项式特征?
Fixing the QuadraticLayer to Compute Polynomial Features Per Training Sample
Got it, let's sort out this issue. The problem with your current QuadraticLayer is that it's calculating the matrix product for the entire batch as a single tensor, instead of handling each training sample independently. Here's how to adjust it properly:
Modified QuadraticLayer Code
import tensorflow as tf from tensorflow.keras.layers import Layer class QuadraticLayer(Layer): def __init__(self, **kwargs): super(QuadraticLayer, self).__init__(**kwargs) def build(self, input_shape): assert isinstance(input_shape, tuple) self.in_shape = input_shape[1] # Output shape is the square of input features (each sample's outer product flattened) self.out_shape = self.in_shape ** 2 super(QuadraticLayer, self).build(input_shape) # Always call this at the end def call(self, x): # x has shape (batch_size, in_features) # Expand dimensions to (batch_size, in_features, 1) for per-sample outer product x_expanded = tf.expand_dims(x, axis=-1) # Compute outer product for each sample: (batch_size, in_features, in_features) # transpose_b=True makes this (in_features,1) * (1,in_features) = (in_features,in_features) per sample outer_product = tf.matmul(x_expanded, x_expanded, transpose_b=True) # Flatten each sample's outer product to (batch_size, in_features^2) return tf.reshape(outer_product, (-1, self.out_shape)) def compute_output_shape(self, input_shape): return (input_shape[0], self.out_shape)
Key Changes Explained
- Expanding Dimensions: We use
tf.expand_dims(x, axis=-1)to turn each sample from a 1D tensor(in_features,)into a 2D tensor(in_features, 1). This lets us compute the outer product for each sample separately. - Per-Sample Outer Product: The
tf.matmulwithtranspose_b=Truecalculates the outer product for every sample in the batch in parallel, resulting in a tensor of shape(batch_size, in_features, in_features). - Correct Reshaping: We flatten each sample's 2D outer product matrix into a 1D vector, giving us the final output shape
(batch_size, in_features²). - Explicit Output Shape: Updated
compute_output_shapeto useinput_shape[0](the batch size) instead of hardcodingNone—this is more clear about preserving the batch dimension.
Why Your Original Code Failed
Your original tf.reshape(x, (self.in_shape, 1, -1)) didn't assign the reshaped tensor back to x, and it was combining all samples into one large 3D tensor. This meant the matmul was computing a Gram matrix for the entire dataset, not generating quadratic features per individual sample.
内容的提问来源于stack exchange,提问作者quester
相关产品推荐
相关产品推荐

