我正在尝试提高EEG分类器的准确性。目前,我仅使用转换层和完全连接的层进行分类。在文献中,我发现人们在其CNN模型中使用LSTM层。我想尝试一下,但是出现以下错误。
我使用的数据是时间序列EEG数据64通道x 325个样本(以500 Hz采样,即650 ms)。 X =(1923,63,325,1)和y =(1923,)
model = Sequential()
model.add(TimeDistributed(Conv2D(64, (1, 3), input_shape=X.shape[1:])))
model.add(Activation('relu'))
model.add(TimeDistributed(Conv2D(64, (1, 3))))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(1, 2)))
model.add(TimeDistributed(Conv2D(64, (1, 3))))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(1, 2)))
model.add(TimeDistributed(Flatten()))
model.add(LSTM(128,return_sequences=True))
model.add(LSTM(128,return_sequences=True))
model.add(Dense(16))
model.add(Activation('relu'))
model.add(Dropout(0.3))
model.add(Dense(2))
model.add(Activation('softmax'))
model.compile(loss='sparse_categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
return model
我得到的错误:
IndexError: list index out of range
我在网上看到了有关此问题的其他一些问题,但大多数问题并没有真正适用于我的应用程序。
编辑:错误被抛出
File "<ipython-input-8-0b3d7307ea53>", line 1, in <module>
runfile('D:/ AA TestPrograms/LALALAL/ModelV20.py', wdir='D:/ AA TestPrograms/LALALAL')
File "d:\anaconda\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 668, in runfile
execfile(filename, namespace)
File "d:\anaconda\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 108, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "D:/ AA TestPrograms/LALALAL/ModelV20.py", line 115, in <module>
history = train_model(model, xtrain, ytrain, xval, yval)
File "D:/ AA TestPrograms/LALALAL/ModelV20.py", line 94, in train_model
shuffle=True
File "d:\anaconda\lib\site-packages\keras\engine\training.py", line 950, in fit
batch_size=batch_size)
File "d:\anaconda\lib\site-packages\keras\engine\training.py", line 671, in _standardize_user_data
self._set_inputs(x)
File "d:\anaconda\lib\site-packages\keras\engine\training.py", line 577, in _set_inputs
self.build(input_shape=(None,) + inputs.shape[1:])
File "d:\anaconda\lib\site-packages\keras\engine\sequential.py", line 225, in build
x = layer(x)
File "d:\anaconda\lib\site-packages\keras\engine\base_layer.py", line 457, in __call__
output = self.call(inputs, **kwargs)
File "d:\anaconda\lib\site-packages\keras\layers\wrappers.py", line 248, in call
y = self.layer.call(inputs, **kwargs)
File "d:\anaconda\lib\site-packages\keras\layers\convolutional.py", line 168, in call
dilation_rate=self.dilation_rate)
File "d:\anaconda\lib\site-packages\keras\backend\tensorflow_backend.py", line 3565, in conv2d
data_format=tf_data_format)
File "d:\anaconda\lib\site-packages\tensorflow\python\ops\nn_ops.py", line 779, in convolution
data_format=data_format)
File "d:\anaconda\lib\site-packages\tensorflow\python\ops\nn_ops.py", line 828, in __init__
input_channels_dim = input_shape[num_spatial_dims + 1]
File "d:\anaconda\lib\site-packages\tensorflow\python\framework\tensor_shape.py", line 615, in __getitem__
return self._dims[key]
IndexError: list index out of range
我是Python和Tensorflow的初学者,但我认为这是检查尺寸的最后一行?如果我在tensor_shape.py中查找,则找不到IndexError。
感谢您的帮助!
答案 0 :(得分:1)
我尝试重现它,并在更换后出现此错误
model.add(TimeDistributed(Conv2D(64, (1, 3), input_shape=X.shape[1:])))
有了这个
model.add(TimeDistributed(Conv2D(64, (1, 3)), input_shape=X.shape[1:]))
,其中X是形状的随机数组(1923,63,325,1)。
因为数组的维数必须等于4,才能对每个样本顺序应用Conv2D(或没有批处理的维数),所以会引发错误。
换句话说,X必须具有形状(批处理维,时间维,行维,列维,通道维)。