I have a Sequential model defined as the following:
model = Sequential([
BatchNormalization(axis=1,input_shape=(2,4)),
Flatten(),
Dense(256, activation='relu'),
BatchNormalization(),
Dropout(0.1),
Dense(2, activation='softmax')
])
I'd like to change this model to take inputs of variable shapes. Specifically, the first dimension needs to be variable. Reading the Keras docs on specifying the input shape, I see that you can use None entries in the input_shape tuple where None indicates that any positive integer may be expected.
With my existing model, if I change the input_shape from (2,4) to (None,4), I receive the error below:
---> Dense(2, activation='softmax')
TypeError: an integer is required
I'm not positive, but I don't believe one can specify variable input shapes when the model contains a Flatten() layer. I've read that Flatten() needs to know the input shape, and so variable input shapes are not compatible with Flatten(). If I remove the Flatten() layer, I receive the same error as above. I wouldn't expect this model to work without the Flatten() layer since I believe it is a requirement that the input is flattened before being passed to a Dense layer.
Given this, can anyone explain how I may be able to utilize variable input shapes? If the problem here is the Flatten() layer, what would be some ways to work around that given that inputs should be flattened before being passed to Dense layers?
Thanks in advance for any advice.
Edit: To give an example of a potential training set-- For the model shown above with input_shape=(2,4), the training set may look like the following, where each 2-d array in the set has shape (2,4):
x_train = np.array([
[[1, 1.02, 1.3, 0.9], [1.1, 1.2, 0.91, 0.99]],
[[1, 1.02, 1.3, 0.9], [1.1, 1.2, 0.91, 0.99]],
[[1.01 ,1, 1.2, 1.2], [1.3, 1.2, 0.89, 0.98]]
])
For the data with input_shape = (None,4), where the shape of the first dimension of each data point can vary, and the second is fixed at 4, the training set may look like:
x_train = np.array([
[[1, 1.02, 1.3, 0.9], [1.1, 1.2, 0.91, 0.99], [1.1, 1.2, 0.91, 0.99]],
[[1, 1.02, 1.3, 0.9], [1.1, 1.2, 0.91, 0.99]],
[[1,1,1,1], [1.3, 1.2, 0.89, 0.98], [1,1,1,1], [1,1,1,1]]
])