You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何编写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.matmul with transpose_b=True calculates 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_shape to use input_shape[0] (the batch size) instead of hardcoding None—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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 08:12:14