Reputation: 300
consider the following code
input = Input(batch_shape=(None,1))
x1 = np.random.random((6,1))
ReduceSum = Lambda(lambda z: K.sum(z, axis=0))
output_ = ReduceSum(input)
model = Model(input, output)
model.predict(x1)
I don't understand why the dimension is not reduced. I got the same behavior with tf.reduce_sum
How to reduce the dimension along the first axis as I would normally do with numpy ?
Upvotes: 2
Views: 5381
Reputation: 86610
Keras models don't support outputting a different number of samples from the input samples.
It's not a problem with the reduction, but with the model.
You have 6 input samples, the model will do everything possible to output 6 samples, no matter what. (If it can't it will throw an error).
To test this properly, you need to have 1 extra dimension for the input:
input = Input(batch_shape=(None,None,1))
x1 = np.random.random((1,6,1))
ReduceSum = Lambda(lambda z: K.sum(z, axis=1))
output = ReduceSum(input)
model = Model(input, output)
model.predict(x1)
Now you will see the reduction.
If you're using it in the middle of a model, all reductions will work correctly, as long as in the final output you manage to restore the same number of samples as the input.
Upvotes: 2