Keras:input_shape = train_data.shape产生“列表索引超出范围". [英] Keras: input_shape=train_data.shape produces "list index out of range"

查看:421
本文介绍了Keras:input_shape = train_data.shape产生“列表索引超出范围".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Keras构建CNN-LSTM网络.但是,我很难为第一层的input_shape参数找到正确的形状.

I want to use Keras to build a CNN-LSTM network. However, I have trouble finding the right shape for the first layer's input_shape parameter.

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

My train_data is a ndarray of the shape (1433, 32, 32); 1433 pictures of size 32x32.

此示例中可以找到,我尝试使用input_shape=train_data.shape[1:],导致与input_shape=train_data.shape相同的错误:

As found in this example, I tried using input_shape=train_data.shape[1:], which results in the same error as input_shape=train_data.shape:

IndexError:列表索引超出范围

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.那么,我该如何调整输入的形状?我是否必须在完全不同的地方寻找错误?

All the results I found for this error were produced under different dircumstances; not through input_shape. So how do I have to shape my Input? Do I have to look for the error somewhere completely different?

更新: 完成错误:

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)

When using a TimeDistributed layer combined with a Conv2D layer, it seems that input_shape requires a tuple of length 4 at least: input_shape = (number_of_timesteps, height, width, number_of_channels).

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

You could try to modify your code like this for example:

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

更多信息此处.

这篇关于Keras:input_shape = train_data.shape产生“列表索引超出范围".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆