safetyduck
safetyduck

Reputation: 6874

Is there a tensorflow keras that is a wrapper for a stack of Dense layers?

For example, this is trivial but is there a layer for this? Is not really a convolution ... there is one "Dense layer" (weights) per data point.

In [266]: X = np.random.randn(10, 3); W = np.random.randn(10, 3, 4); (X[:, :, None] * W).sum(axis=1).shape
Out[266]: (10, 4)

Upvotes: 0

Views: 20

Answers (1)

Daniel Möller
Daniel Möller

Reputation: 86650

Create your own layer:

Warning: works only with fixed batch size, you need to define batch_shape or batch_input_shape in your models!!!!

class SampleDense(Layer):
    def __init__(self, units, **kwargs):
        self.units = units
        super(SampleDense, self).__init__(**kwargs)

    def build(self, input_shape):
        weight_shape = input_shape + (self.units,)

        self.kernel = self.add_weight(name='kernel', 
                                      shape=weight_shape,
                                      initializer='uniform',
                                      trainable=True)
        self.built = True

    def call(self, inputs):
        inputs = K.expand_dims(inputs, axis=-1)
        outputs = inputs * self.kernel
        outputs = K.sum(outputs, axis=-2)

        return outputs

    def compute_output_shape(self, input_shape):
        return input_shape[:-1] + (self.units,)

Upvotes: 1

Related Questions