我正在尝试实现CNN + RNN应用程序来玩游戏。网络应该以图像作为输入,并产生一系列控制器命令作为输出。
我发现,为了开始RNN工作,我需要在CNN部分(Source)中使用TimeDistribute层。在起草模型时,我发现即使尺寸正确,它也会引发IndexError。
以下代码可重现该错误:
from keras.models import Sequential
from keras.layers.convolutional import Conv2D, MaxPooling2D
from keras.layers.core import Dense, Flatten
from keras.layers import TimeDistributed
from keras.layers.recurrent import LSTM
import numpy as np
training_x = np.random.rand(1,320,240,1) # (n,x,y,c)
training_y = np.random.rand(5)
m = Sequential()
m.add(TimeDistributed(Conv2D(filters=64, kernel_size=(10, 10), input_shape=(320,240,1))))
m.add(TimeDistributed(MaxPooling2D(4, 4)))
m.add(TimeDistributed(Flatten()))
m.add(LSTM(10))
m.add(Dense(5))
m.compile(optimizer='Adam')
m.fit(training_x, training_y)
我在Windows 10上使用tensorflow-gpu 1.10.0,Keras 2.2.2
完整的堆栈跟踪:
Traceback (most recent call last):
File "...\app\utils\test.py", line 17, in <module>
m.fit(training_x, training_y)
File "...\Anaconda3\lib\site-packages\keras\engine\training.py", line 950, in fit
batch_size=batch_size)
File "...\Anaconda3\lib\site-packages\keras\engine\training.py", line 671, in _standardize_user_data
self._set_inputs(x)
File "...\Anaconda3\lib\site-packages\keras\engine\training.py", line 577, in _set_inputs
self.build(input_shape=(None,) + inputs.shape[1:])
File "...\Anaconda3\lib\site-packages\keras\engine\sequential.py", line 225, in build
x = layer(x)
File "...\Anaconda3\lib\site-packages\keras\engine\base_layer.py", line 457, in __call__
output = self.call(inputs, **kwargs)
File "...\Anaconda3\lib\site-packages\keras\layers\wrappers.py", line 248, in call
y = self.layer.call(inputs, **kwargs)
File "...\Anaconda3\lib\site-packages\keras\layers\convolutional.py", line 168, in call
dilation_rate=self.dilation_rate)
File "...\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py", line 3565, in conv2d
data_format=tf_data_format)
File "...\Anaconda3\lib\site-packages\tensorflow\python\ops\nn_ops.py", line 779, in convolution
data_format=data_format)
File "...\Anaconda3\lib\site-packages\tensorflow\python\ops\nn_ops.py", line 828, in __init__
input_channels_dim = input_shape[num_spatial_dims + 1]
File "...\Anaconda3\lib\site-packages\tensorflow\python\framework\tensor_shape.py", line 615, in __getitem__
return self._dims[key]
IndexError: list index out of range