Variable length input for a simple neural network (Keras)

人走茶凉 提交于 2020-04-30 07:04:19

问题


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!!


回答1:


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)


来源:https://stackoverflow.com/questions/51292010/variable-length-input-for-a-simple-neural-network-keras

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!