user7867665
user7867665

Reputation: 882

Concatenate Sum of Features - Shape Error

I have a model in Keras in which I want to explicitly make the neural network look at the sum of a few features. I try to do it like this:

sum_input_p = Lambda(sumFunc)(input_p)
d_input = keras.layers.concatenate(
    [input_p, cont_cond, sum_input_p, one_hot_cond_1, one_hot_cond_2 ], axis=-1)

where

def sumFunc(x):
   return K.reshape(K.sum(x), [1])

But I get an error:

ValueError: Concatenate layer requires inputs with matching shapes except for the concat axis. Got inputs shapes: [(None, 267), (None, 1), (1,), (None, 4), (None, 2)]

Is it because of the reshape step in sumFunc? How can I reshape it correctly so that it can be concatenated with the rest of the features in the neural network?

Upvotes: 0

Views: 205

Answers (1)

benjaminplanche
benjaminplanche

Reputation: 15119

It is because of K.sum() (K.reshape() isn't really needed too).

All you other tensors (input_p, cont_cond, etc.) still contain batched samples I assume (i.e. their shapes are (batch_size, num_features), with batch_size = None as it is only defined when the graph is run). So you probably want sum_input_p to have a shape (batch_size, 1), i.e. computing the sum over all dimensions of your input tensor x, except the first dimension (corresponding to the batch size).

import keras
import keras.backend as K
from keras.layers import Input, Lambda
import numpy as np

def sumFunc(x):
    x_axes = np.arange(0, len(x.get_shape().as_list()))
    # ... or simply x_axes = [0, 1] in your case, since the shape of x is known
    y = K.sum(x, axis=x_axes[1:])   # y of shape (batch_size,)
    y = K.expand_dims(y, -1)        # y of shape (batch_size, 1)
    return y


input_p = Input(shape=(267,))
sum_input_p = Lambda(sumFunc)(input_p)
print(sum_input_p)
# > Tensor("lambda_1/ExpandDims:0", shape=(?, 1), dtype=float32)
d_input = keras.layers.concatenate([input_p, sum_input_p], axis=-1)
print(d_input)
# > Tensor("concatenate_1/concat:0", shape=(?, 268), dtype=float32)

Upvotes: 2

Related Questions