如何向TimeDistributed Keras层输入未知大小的时间步长

时间:2016-10-20 15:28:41

标签: machine-learning neural-network tensorflow deep-learning keras

我的输入状态为shape =(84,84,4)

state = Input(shape=(84,84,4), dtype="float")

所以我想把它传递给一些TimeDistributed层,时间步长为1 = 5(范围为1到5),我不确切知道它等于哪一个。

我的下一层是这样的:

conv1 = TimeDistributed(Convolution2D(16, 8, 8, subsample=(4, 4), border_mode='valid',
                                      activation='relu', dim_ordering='tf'))(state)

我在这一层遇到错误:

IndexError: tuple index out of range

我只想传递未知的时间序列大小到TimeDistributed,然后再传递给LSTM。

2 个答案:

答案 0 :(得分:1)

So basically in Keras - you need to provide the sequence length because during computations Keras layers accepts as an input numpy array with a specified shape - what makes compulsory for all inputs (at least in one batch) to have a length fixed. But - you still can deal with varying input size by 0-padding (making all sequence equal size by adding all zero dummy timesteps at the beginning) and then masking what makes your network equivalent to a varying length input network.

答案 1 :(得分:0)

您可以给定可变的序列长度,如下所示:

classifier.add(TimeDistributed(Convolution2D(64,(3,3)),input_shape=(None,None,None,3)))

但是现在,当向量在时间预测中变平或展开时,您将不得不调整向量的长度。