AssertionError使用Keras将Timeencoder与TimeDistributed Layer堆叠在一起时

时间:2016-06-15 19:07:57

标签: python deep-learning keras

我正在使用Keras实现自动编码器,后跟TimeDistributed图层。但是,我遇到了AssertionError

追溯记录:

Traceback (most recent call last):
  File "test3.py", line 115, in <module>
    model.fit(x_train, y_train, batch_size=batch_size, validation_split=0.05, nb_epoch=1, shuffle=True)
  File "/home/xiaoyong/xiaoyong/local/lib/python2.7/site-packages/keras/engine/training.py", line 1022, in fit
    self._make_test_function()
  File "/home/xiaoyong/xiaoyong/local/lib/python2.7/site-packages/keras/engine/training.py", line 686, in _make_test_function
    **self._function_kwargs)
  File "/home/xiaoyong/xiaoyong/local/lib/python2.7/site-packages/keras/backend/theano_backend.py", line 528, in function
    return Function(inputs, outputs, updates=updates, **kwargs)
  File "/home/xiaoyong/xiaoyong/local/lib/python2.7/site-packages/keras/backend/theano_backend.py", line 514, in __init__
    **kwargs)
  File "/home/xiaoyong/xiaoyong/local/lib/python2.7/site-packages/theano/compile/function.py", line 322, in function
    output_keys=output_keys)
  File "/home/xiaoyong/xiaoyong/local/lib/python2.7/site-packages/theano/compile/pfunc.py", line 480, in pfunc
    output_keys=output_keys)
  File "/home/xiaoyong/xiaoyong/local/lib/python2.7/site-packages/theano/compile/function_module.py", line 1827, in orig_function
    output_keys=output_keys).create(
  File "/home/xiaoyong/xiaoyong/local/lib/python2.7/site-packages/theano/compile/function_module.py", line 1507, in __init__
    optimizer_profile = optimizer(fgraph)
  File "/home/xiaoyong/xiaoyong/local/lib/python2.7/site-packages/theano/gof/opt.py", line 102, in __call__
    return self.optimize(fgraph)
  File "/home/xiaoyong/xiaoyong/local/lib/python2.7/site-packages/theano/gof/opt.py", line 90, in optimize
    ret = self.apply(fgraph, *args, **kwargs)
  File "/home/xiaoyong/xiaoyong/local/lib/python2.7/site-packages/theano/gof/opt.py", line 233, in apply
    sub_prof = optimizer.optimize(fgraph)
  File "/home/xiaoyong/xiaoyong/local/lib/python2.7/site-packages/theano/gof/opt.py", line 86, in optimize
    self.add_requirements(fgraph)
  File "/home/xiaoyong/xiaoyong/local/lib/python2.7/site-packages/theano/tensor/opt.py", line 1434, in add_requirements
    fgraph.attach_feature(ShapeFeature())
  File "/home/xiaoyong/xiaoyong/local/lib/python2.7/site-packages/theano/gof/fg.py", line 566, in attach_feature
    attach(self)
  File "/home/xiaoyong/xiaoyong/local/lib/python2.7/site-packages/theano/tensor/opt.py", line 1261, in on_attach
    self.on_import(fgraph, node, reason='on_attach')
  File "/home/xiaoyong/xiaoyong/local/lib/python2.7/site-packages/theano/tensor/opt.py", line 1314, in on_import
    self.set_shape(r, s)
  File "/home/xiaoyong/xiaoyong/local/lib/python2.7/site-packages/theano/tensor/opt.py", line 1113, in set_shape
    shape_vars.append(self.unpack(s[i]))
  File "/home/xiaoyong/xiaoyong/local/lib/python2.7/site-packages/theano/tensor/opt.py", line 1035, in unpack
    assert s_i >= 0
AssertionError

我的模特是:

activation = LeakyReLU(alpha=0.3)

input_model = Input(shape=(timestamp, gene_classes,))
model0 = BatchNormalization()(input_model)
model1 = Convolution1D(512, 3, activation='relu', border_mode='same', W_regularizer=l2(0.01))(model0)
encoded = MaxPooling1D(pool_length=2, border_mode='same')(model1)
model2 = Convolution1D(512, 3, activation='relu', border_mode='same', W_regularizer=l2(0.01))(encoded)
decoded = UpSampling1D(length=2)(model2)
model3 = TimeDistributed(Dense(64, activation='relu'))(decoded)
model3 = BatchNormalization()(model3)
model3 = Dropout(0.5)(model3)
model3 = TimeDistributed(Dense(30, activation='softmax'))(model3)
model = Model(input_model, model3)
sgd = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True, clipvalue=2)
model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=["accuracy"])

1 个答案:

答案 0 :(得分:0)

我有同样的错误消息。部分网络的代码看起来大致如此。如您所见,我也在使用Convolution1D。

qenc = Sequential()
qenc.add(Embedding(output_dim=WORD2VEC_EMBED_SIZE, input_dim=vocab_size,
                   input_length=seq_maxlen,
                   weights=[embedding_weights]))
qenc.add(LSTM(QA_EMBED_SIZE, return_sequences=True))
qenc.add(Dropout(0.3))
qenc.add(Convolution1D(QA_EMBED_SIZE // 2, 5, border_mode="valid"))
qenc.add(MaxPooling1D(pool_length=2, border_mode="valid"))
qenc.add(Dropout(0.3))
qenc.add(Flatten())

我对参数进行了一些实验,并在MaxPooling1D图层中将border_mode从“same”更改为“valid”,修复了错误。然后我回去改变了Convolution1D中的border_mode。从建模POV中,这可能对您有用,也可能不起作用,但这是我使用的解决方法 - 希望这会有所帮助。我的Keras版本是1.0.5。