Sequential Keras model output not matching label input

Viewed 94

I am trying to build a sequential model in Keras that will take an input feature of shape(1, n, 5) and an input label of shape(1, 1) and return an output of shape(1, 1) to match the input label.

I built a model from a tutorial that works well with features shape(n, 5) and labels shape(n, 1) and returns an output of shape(n, 1), which matches the input label.

Here is the model

class Model(keras.Model):

    def __init__(self, units):
        super().__init__()
        self.dense1 = keras.layers.Dense(
            units=units,
            activation=tf.nn.relu,
            kernel_initializer=tf.random.normal,
            bias_initializer=tf.random.normal,
        )
        self.dense2 = keras.layers.Dense(1)

    def call(self, x2, training=True, mask=None):
        x1 = self.dense1(x2)
        x1 = self.dense2(x1)
        tf.squeeze(x1, axis=-1)
        return x1

Here is how I call it

            model.fit(
                features, labels,
                epochs=100,
                verbose=1,
            )

When I give the model features of shape(1, n, 5) and labels of shape(1, 1), it returns shape(1, n, 5). Does this mean all n feature sets were checked against the one label? I want the model to treat the shape(n, 5) as a single input.

To do that, I tried this

self.dense1 = keras.layers.Dense(
            input_shape=[None, 5],
            units=units,
            activation=tf.nn.relu,
            kernel_initializer=tf.random.normal,
            bias_initializer=tf.random.normal,
        )

but that did not do anything to the output shape.

How do I get the model to treat the shape(n, 5) as a single input?

1 Answers

I ended up feeding the model features of shape(1, 5*n) by transforming the features into another shape. This ends up concatenating the feature set of 5 values n times in order, so I assume that is the best way to approach this particular problem.

        features = tf.reshape(features, [-1])
        features = tf.expand_dims(features, 0)
Related