# Dimension problem in keras neural networks

So every time I try to write a CNN or RNN I have problems understanding how the dimensions work.

1. I don't understand why does the model compile, which in my opinion means that the output dimension of the previous layer is the input dimension of the next one and so on(the layers are compatible with each other). So how is it possible that when I fit my model I get an error in the 3rd or 4th layer(or any other layer except the first one) saying that for ex it expects 3 dimensions but got 2.

2. On a more precise example, lets say I have the following one-shot model:

vocab_size =10000
src_txt_length =10
sum_txt_length = 100

inputs = Input(shape=(src_txt_length,))
encoder1 = Embedding(vocab_size, 128)(inputs)
encoder2 = LSTM(128)(encoder1)
encoder3 = RepeatVector(sum_txt_length)(encoder2)

decoder1 = LSTM(128, return_sequences=True)(encoder3)
outputs = TimeDistributed(Dense(vocab_size, activation='softmax'))(decoder1)
model = Model(inputs=inputs, outputs=outputs)
model.fit(X,y)


I get an error in the TimeDistributed layer saying that it expects 3 dimensions but got 2.

X.shape == (20000,10)
len(y) == 20000  # y is a list of labels 0s and 1s


So if I want to calculate the dimension of each layer when I fit my data on paper, how will I do that ?

• What is y.shape? Mar 11 '18 at 14:09

Ok I was able to recreate the error. The TimeDistributed layer applies the dense layer to each of the time slices. In this model you will have 100 different time slices as defined by sum_txt_length and the RepeatVector layer. These time slices will be propagated through the network all the way to the TimeDistributed layer. Thus, your $y$ vector must have dimensions $(None, 100, 10000)$.

You can use model.summary() to get the expected dimensions after each layer.

You will need to fix the output layer to match the label dimensions. I believe you will need to add a Flatten() layer before the densely connected layer, and do not use the TimeDistributed layer at the output. That way your output will be of size $vocab_size$, thus predicting the next vocabulary word given an input.

The shape of the input:

inputs = Input(shape=(src_txt_length,))


should be:

inputs = Input(shape=(src_txt_length,vocab_size))