【问题标题】:keras polynomial feature layerkeras 多项式特征层
【发布时间】:2019-06-08 21:17:12
【问题描述】:

我想要创建乘以 X.T * X 的多项式特征的层:

class QuadraticLayer(Layer):

def __init__(self, **kwargs):
    super(QuadraticLayer, self).__init__(**kwargs)

def build(self, input_shape):
    assert isinstance(input_shape, tuple)
    print(input_shape)
    self.in_shape = input_shape[1]
    self.out_shape = input_shape[1] ** 2
    super(QuadraticLayer, self).build(input_shape)  # Be sure to call this at the end

def call(self, x):
    print(x.shape)
    tf.reshape(x, (self.in_shape, 1, -1), name=None)
    x = tf.matmul(x, x, transpose_a=True)
    return tf.reshape(x, (-1, self.out_shape))

def compute_output_shape(self, input_shape):
    return (None, self.out_shape)

我的问题是 call 张量 x 是一个批量张量 - 如何编写适用于每个训练示例的层而不是处理整个批量张量?

【问题讨论】:

    标签: tensorflow machine-learning keras keras-layer


    【解决方案1】:

    这是一个想法:

    def call(self, x):
        x = K.backend.batch_dot(tf.reshape(x, (-1, 1, self.in_shape)), tf.reshape(x, (-1, self.in_shape, 1)), axes=[1,2])
        return tf.reshape(x, (-1, self.out_shape))
    

    但问题是有更好的解决方案吗?

    【讨论】:

      猜你喜欢
      • 2019-10-26
      • 1970-01-01
      • 2017-07-28
      • 2019-11-16
      • 1970-01-01
      • 2018-02-12
      • 1970-01-01
      • 1970-01-01
      • 2019-09-14
      相关资源
      最近更新 更多