gpt4 book ai didi

python - Keras:input_shape=train_data.shape 产生 "list index out of range"

转载 作者:太空宇宙 更新时间:2023-11-03 14:45:52 26 4
gpt4 key购买 nike

我想用Keras搭建一个CNN-LSTM网络。但是,我无法为第一层的 input_shape 参数找到正确的形状。

我的 train_data 是形状为 (1433, 32, 32) 的 ndarray; 1433 张尺寸为 32x32 的图片。

this example 中找到,我尝试使用 input_shape=train_data.shape[1:],这会导致与 input_shape=train_data.shape 相同的错误:

IndexError: list index out of range

相关代码为:

train_data, train_labels = get_training_data()
# train_data = train_data.reshape(train_data.shape + (1,))
model = Sequential()
model.add(TimeDistributed(Conv2D(
CONV_FILTER_SIZE[0],
CONV_KERNEL_SIZE,
activation="relu",
padding="same"),
input_shape=train_data.shape[1:]))

我发现的这个错误的所有结果都是在不同的环境下产生的;不是通过 input_shape。那么我该如何塑造我的输入呢?我是否必须在完全不同的地方寻找错误?

更新:完整错误:

Traceback (most recent call last):
File "trajecgen_keras.py", line 131, in <module>
tf.app.run()
File "/home/.../lib/python3.5/site-packages/tensorflow/python/platform/app.py", line 124, in run
_sys.exit(main(argv))
File "trajecgen_keras.py", line 85, in main
input_shape=train_data.shape))
File "/home/.../lib/python3.5/site-packages/keras/models.py", line 467, in add
layer(x)
File "/home/.../lib/python3.5/site-packages/keras/engine/topology.py", line 619, in __call__
output = self.call(inputs, **kwargs)
File "/home/.../lib/python3.5/site-packages/keras/layers/wrappers.py", line 211, in call
y = self.layer.call(inputs, **kwargs)
File "/home/.../lib/python3.5/site-packages/keras/layers/convolutional.py", line 168, in call
dilation_rate=self.dilation_rate)
File "/home/.../lib/python3.5/site-packages/keras/backend/tensorflow_backend.py", line 3335, in conv2d
data_format=tf_data_format)
File "/home/.../lib/python3.5/site-packages/tensorflow/python/ops/nn_ops.py", line 753, in convolution
name=name, data_format=data_format)
File "/home/.../lib/python3.5/site-packages/tensorflow/python/ops/nn_ops.py", line 799, in __init__
input_channels_dim = input_shape[num_spatial_dims + 1]
File "/home/../lib/python3.5/site-packages/tensorflow/python/framework/tensor_shape.py", line 521, in __getitem__
return self._dims[key]
IndexError: list index out of range

最佳答案

TimeDistributed 层与 Conv2D 层结合使用时,input_shape 似乎至少需要一个长度为 4 的元组:input_shape = (number_of_timesteps, height, width, number_of_channels)

您可以尝试像这样修改您的代码,例如:

model = Sequential()
model.add(TimeDistributed(Conv2D(
CONV_FILTER_SIZE[0],
CONV_KERNEL_SIZE,
activation="relu",
padding="same"),
input_shape=(None, 32, 32, 1))

更多信息 here .

关于python - Keras:input_shape=train_data.shape 产生 "list index out of range",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49538940/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com