Search code examples
tensorflowkerastheanosequential

Variable length input for a simple neural network (Keras)


Can a variable length, i.e. input_dim=None, be applied to a simple neural network? Specifically, the Keras Sequential model. I've been running into errors when trying to employ the same concept. I've already seen the documentation that seems to support this functionality:

https://keras.io/getting-started/functional-api-guide/

But when I do the following...

model = Sequential()
model.add(Dense(num_feat, input_dim = None, kernel_initializer = 'normal', activation='relu'))
model.add(Dense(num_feat, kernel_initializer = 'normal', activation = 'relu'))
model.add(Dropout(.2))
model.add(Dense(num_feat, kernel_initializer = 'normal', activation = 'relu'))
model.add(Dropout(.2))
model.add(Dense(num_feat, kernel_initializer = 'normal', activation = 'relu'))
model.add(Dropout(.2))
model.add(Dense(ouput.shape[1], kernel_initializer = 'normal', activation = 'linear'))

...I get this error:

ValueError: ('Only Theano variables and integers are allowed in a size-tuple.', (None, 63), None)

Any help, ideas, or clarification would be greatly appreciated!!


Solution

  • No, you can't. (And you can't with the functional API either)

    The weight matrix has a fixed size and this size depends on the input dim.

    The possible variable dimensions are:

    • In convolution: the spatial dimensions, but not the channels/filters
      • 1D: input_shape=(None,channels)
      • 2D: input_shape=(None,None,channels)
      • 3D: input_shape=(None,None,None,channels)
    • In recurrent layers: the timesteps dimension, but not the features dimension
      • input_shape = (None, features)
    • In any layer, the "batch" dimension, but this is not usually set unless you use batch_shape or batch_input_shape instead of input_dim
      • For Dense: batch_shape=(None,input_dim) or batch_input_shape=(None,input_dim)