我正在尝试构建CNN-LSTM模型。我的数据集是由一堆大小为(n_spot, n_meas) = (100,102)
的单通道图像组成的。在这个小实例中,我仅得到n_sim = 20
张图片,以便:
print(X_train.shape) -> (12, 100, 102, 1)
print(X_val.shape) -> (4, 100, 102, 1)
print(X_test.shape) -> (4, 100, 102, 1)
print(y_train.shape) -> (12, 200)
print(y_val.shape) -> (4, 200)
print(y_test.shape) -> (4,200)
我的代码如下:
# create model
model = Sequential()
# add model layers
model.add(TimeDistributed(Conv2D(filters = 8, kernel_size=[3,3], padding = 'same',
activation='relu', input_shape=(None,n_spot,n_meas,1))))
model.add(TimeDistributed(MaxPool2D(pool_size=(3,3))))
model.add(TimeDistributed(Flatten()))
model.add(LSTM(20, return_sequences=False))
model.add(Dense(2*n_spot, activation = 'linear'))
#compile model using accuracy to measure model performance
model.compile(optimizer='adam', loss='mean_squared_error')
model.summary()
返回以下错误:
'This model has not yet been built. '
ValueError: This model has not yet been built. Build the model first by calling build() or calling fit() with some data. Or specify input_shape or batch_input_shape in the first layer for automatic build.
每当我尝试使用
训练模型时,我都会得到这个model.fit(X_train, y_train, validation_data=(X_val, y_val), epochs=30)
Using TensorFlow backend.
Traceback (most recent call last):
File "<ipython-input-1-1109305aff30>", line 1, in <module>
runfile('C:/Users/nle5266/Documents/positioning/reduced_data.py', wdir='C:/Users/nle5266/Documents/positioning')
File "C:\Users\nle5266\AppData\Local\conda\conda\envs\tensorflow_env\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 786, in runfile
execfile(filename, namespace)
File "C:\Users\nle5266\AppData\Local\conda\conda\envs\tensorflow_env\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/nle5266/Documents/positioning/reduced_data.py", line 338, in <module>
model.fit(X_train, y_train, validation_data=(X_val, y_val), epochs=30, callbacks=[tensorboard])
File "C:\Users\nle5266\AppData\Local\conda\conda\envs\tensorflow_env\lib\site-packages\keras\engine\training.py", line 952, in fit
batch_size=batch_size)
File "C:\Users\nle5266\AppData\Local\conda\conda\envs\tensorflow_env\lib\site-packages\keras\engine\training.py", line 677, in _standardize_user_data
self._set_inputs(x)
File "C:\Users\nle5266\AppData\Local\conda\conda\envs\tensorflow_env\lib\site-packages\keras\engine\training.py", line 589, in _set_inputs
self.build(input_shape=(None,) + inputs.shape[1:])
File "C:\Users\nle5266\AppData\Local\conda\conda\envs\tensorflow_env\lib\site-packages\keras\engine\sequential.py", line 221, in build
x = layer(x)
File "C:\Users\nle5266\AppData\Local\conda\conda\envs\tensorflow_env\lib\site-packages\keras\engine\base_layer.py", line 457, in __call__
output = self.call(inputs, **kwargs)
File "C:\Users\nle5266\AppData\Local\conda\conda\envs\tensorflow_env\lib\site-packages\keras\layers\wrappers.py", line 248, in call
y = self.layer.call(inputs, **kwargs)
File "C:\Users\nle5266\AppData\Local\conda\conda\envs\tensorflow_env\lib\site-packages\keras\layers\convolutional.py", line 171, in call
dilation_rate=self.dilation_rate)
File "C:\Users\nle5266\AppData\Local\conda\conda\envs\tensorflow_env\lib\site-packages\keras\backend\tensorflow_backend.py", line 3650, in conv2d
data_format=tf_data_format)
File "C:\Users\nle5266\AppData\Local\conda\conda\envs\tensorflow_env\lib\site-packages\tensorflow\python\ops\nn_ops.py", line 779, in convolution
data_format=data_format)
File "C:\Users\nle5266\AppData\Local\conda\conda\envs\tensorflow_env\lib\site-packages\tensorflow\python\ops\nn_ops.py", line 828, in __init__
input_channels_dim = input_shape[num_spatial_dims + 1]
File "C:\Users\nle5266\AppData\Local\conda\conda\envs\tensorflow_env\lib\site-packages\tensorflow\python\framework\tensor_shape.py", line 616, in __getitem__
return self._dims[key]
IndexError: list index out of range
谷歌搜索,我意识到这种问题通常是由于时间分布的conv2D层的输入形状应为4元组。我尝试了(None,n_spot,n_meas,1)
,但没有成功。
我正在使用带有tensorflow 1.12.0的keras 2.2.4。我试图将tensorflow升级到最后一个晚上,但是并没有解决问题。