0
votes

My questions is rather simple but seemed to be unsolved.

Input: (bs, timesteps, input_dim) --> Tensor("stack:0", shape=(?, 4, 400), dtype=float32)

Layer: output = LSTM(100, input_shape = (timesteps, input_feature), return_sequence = True) (input)

Expect: (bs, timesteps, output_dim) --> Tensor("gru_20/transpose_1:0", shape=(?, 4, 100), dtype=float32)

Output: Tensor("gru_20/transpose_1:0", shape=(?, ?, 100), dtype=float32)

Why does Keras not infer the number of timesteps, even it receives an input_shape? When I use the model summary the result it shows has the correct output shape:


lstm_2 (LSTM) (None, 4, 100) 3232

But not during construction. So, when I want to unstacked the Tensor to a list of Tensors for every timesteps * (bs, 10) by using unstack(output, axis=1)] I receive ofc this error: ValueError: Cannot infer num from shape (?, ?, 100)

Where is my mistake?

BTW. Adding TimeDistributed(Dense(100))(questions) results in the correct output dim: Tensor("time_distributed_17/Reshape_1:0", shape=(?, 4, 100), dtype=float32) but not an option because of shared weights. If not, what is the workaround?

1
I do not know why this is, but, as a workaround, if you need the correct shape at the output you can enforce it with a Reshape layer.jdehesa
Thanks for your answer. Probably I am doing it wrong because I got an error. Using: newoutput = Reshape((4, 100))(output) should result in (None, 4, 100) but ERROR: ValueError: total size of new array must be unchanged. Using inference by using -1 like newoutput = Reshape((-1, 100))(output) leads again to (?,?,100). Also not working in combination with TimeDistributedB. - S.

1 Answers

1
votes

Work-around might be a multiplication by 1 (no change).

workaround = TimeDistributed(Lambda(lambda x: x * 1.0))(output)

Inference works here:

Tensor("time_distributed_17/Reshape_1:0", shape=(?, 4, 100), dtype=float32)

Is a TimeDistributed layer always necessary when using return_sequences=True?