使用TimeDistributed Conv2D图层包装器

时间:2018-10-28 12:34:15

标签: python keras

我想创建一个由CNN => RNN => FCN组成的Keras模型。

模型必须能够删除填充为零2D数组的每个序列的元素。

# Create model
model = Sequential()
input_shape = (n_tel, img_w, img_h)

# mask the inputs that correspond to padding
model.add(Masking(mask_value=0., input_shape=input_shape))

# Add CNN feature extractor
model.add(TimeDistributed(
        Conv2D(
                filters=16, 
                kernel_size=(3,3), 
                padding='same',
                activation='relu'
                )
        ))
model.add(TimeDistributed(
        Conv2D(
                filters=32, 
                kernel_size=(3,3), 
                padding='same',
                activation='relu'
                )
        ))
model.add(TimeDistributed(MaxPooling2D(pool_size=(2,2), strides=None)))

# Add LSTM feature extractor
model.add(LSTM(units=100))

model.add(Dense(100))
model.add(Activation('relu'))
model.add(Dense(1))
model.add(Activation('sigmoid'))

上面的代码返回一个错误,该错误在第一个Conv2D层上触发:

IndexError: list index out of range

这是怎么回事?


编辑:完全追溯

  Traceback (most recent call last):

  File "<ipython-input-10-7253565e1e8b>", line 1, in <module>
    runfile('/home/jsevillamol/Documentos/ctlearn_keras/models/build_cnn_rnn.py', wdir='/home/jsevillamol/Documentos/ctlearn_keras/models')

  File "/home/jsevillamol/anaconda3/envs/ctlearn/lib/python3.6/site-packages/spyder_kernels/customize/spydercustomize.py", line 668, in runfile
    execfile(filename, namespace)

  File "/home/jsevillamol/anaconda3/envs/ctlearn/lib/python3.6/site-packages/spyder_kernels/customize/spydercustomize.py", line 108, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "/home/jsevillamol/Documentos/ctlearn_keras/models/build_cnn_rnn.py", line 78, in <module>
    model = build_cnn_rnn(args.n_tel, args.img_w, args.img_h)

  File "/home/jsevillamol/Documentos/ctlearn_keras/models/build_cnn_rnn.py", line 27, in build_cnn_rnn
    activation='relu'

  File "/home/jsevillamol/anaconda3/envs/ctlearn/lib/python3.6/site-packages/tensorflow/python/training/checkpointable/base.py", line 364, in _method_wrapper
    method(self, *args, **kwargs)

  File "/home/jsevillamol/anaconda3/envs/ctlearn/lib/python3.6/site-packages/tensorflow/python/keras/engine/sequential.py", line 184, in add
    output_tensor = layer(self.outputs[0])

  File "/home/jsevillamol/anaconda3/envs/ctlearn/lib/python3.6/site-packages/tensorflow/python/keras/engine/base_layer.py", line 728, in __call__
    self.build(input_shapes)

  File "/home/jsevillamol/anaconda3/envs/ctlearn/lib/python3.6/site-packages/tensorflow/python/keras/layers/wrappers.py", line 213, in build
    self.layer.build(tuple(child_input_shape))

  File "/home/jsevillamol/anaconda3/envs/ctlearn/lib/python3.6/site-packages/tensorflow/python/keras/layers/convolutional.py", line 182, in build
    self.rank + 2))

  File "/home/jsevillamol/anaconda3/envs/ctlearn/lib/python3.6/site-packages/tensorflow/python/ops/nn_ops.py", line 828, in __init__
    input_channels_dim = input_shape[num_spatial_dims + 1]

  File "/home/jsevillamol/anaconda3/envs/ctlearn/lib/python3.6/site-packages/tensorflow/python/framework/tensor_shape.py", line 615, in __getitem__
    return self._dims[key]

IndexError: list index out of range

0 个答案:

没有答案