Keras - Theano:ValueError:输入维度不匹配

时间:2017-11-25 19:55:14

标签: python python-3.x keras theano keras-layer

我遇到的问题是,当运行下面的keras代码时,我收到此错误:ValueError: Input dimension mis-match.(详细的错误消息和完整的回溯) 只有在使用Theano后端时才会发生这种情况,对于TensorFlow后端,一切正常。 首先是输入:

>>> X_train.shape
(37606, 16)

目标值y_train仅存储在列表中,如下所示为1和0:

[0,1,1,1,0,0, ... ,1,0,1,1]

单词嵌入模型有300暗淡:

>>> w.shape
(702904, 300)

我已经阅读了类似的问题,其中建议使用了两个输出单元(isn"这只对categorical_crossentropy有用吗?)。我尝试了这一点,同时将目标值重塑为:(37606, 2)

但这并没有解决问题。 我猜测嵌入层存在一些问题,因为输入被指定为max_review_length = 16,嵌入提供了另外300个dims。但如果是这样,我不知道该改变什么。

如上所述,此代码使用TensorFlow后端工作正常。

(Theano是版本1.0.0和Python 3.4.5上的Keras版本2.1.1)

提前感谢任何帮助或想法!

def create_model(X, Y, summary=False):

    batch_size = 512
    epochs = 5
    sequence_input = Input(shape=(max_review_length,), dtype='int32')
    embedding_layer = Embedding(w.shape[0],w.shape[1],
                            weights=[w],
                            input_length=max_review_length,
                            trainable=False)
    embedded_sequences = embedding_layer(sequence_input)

    attention_probs = Dense(300, activation='softmax', name='attention_vec')(embedded_sequences)
    attention_mul = multiply([embedded_sequences, attention_probs], name='attention')

    x = Conv1D(256, kernel_size=3, activation='relu', padding='same')(attention_mul)
    x = Conv1D(128, kernel_size=3, activation='relu', padding='same')(x)
    x = MaxPooling1D(4)(x)

    x = Dense(128, activation='relu')(x)
    x = Dropout(0.5)(x)
    x = Dense(128, activation='relu')(x)
    x = Flatten()(x)
    x = Dense(32, activation='relu')(x)

    attention_probs = Dense(32, activation='softmax', name='attention2_vec')(x)
    attention_mul = multiply([x, attention_probs], name='attention2')
    preds = Dense(1, activation='sigmoid')(attention_mul)

    model = Model(sequence_input, preds)
    model.compile(loss='binary_crossentropy', optimizer='rmsprop', metrics=['accuracy'])

    if summary: print(model.summary())
    model.fit(X, Y, epochs=epochs, batch_size=batch_size, shuffle=True)
    return model
seed = 42
np.random.seed(seed)
rn.seed(seed)

model = create_model(X_train, y_train, summary=True)
predictions = model.predict(X_test)
evaluate(predictions, y_test)

完整错误追溯:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
~/python_env/lib64/python3.4/site-packages/theano/compile/function_module.py in __call__(self, *args, **kwargs)
    902             outputs =\
--> 903                 self.fn() if output_subset is None else\
    904                 self.fn(output_subset=output_subset)

ValueError: Input dimension mis-match. (input[0].shape[1] = 16, input[1].shape[1] = 300)

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
<ipython-input-15-456c0ca552d0> in <module>()
     51 #K.set_session(sess)
     52 
---> 53 model = create_model(X_train, y_train, summary=True)
     54 predictions = model.predict(X_test)
     55 evaluate(predictions, y_test)

<ipython-input-15-456c0ca552d0> in create_model(X, Y, summary)
     39 
     40     if summary: print(model.summary())
---> 41     model.fit(X, Y, epochs=epochs, batch_size=batch_size, shuffle=True)
     42     return model
     43 seed = 42

~/python_env/lib64/python3.4/site-packages/Keras-2.1.1-py3.4.egg/keras/engine/training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, **kwargs)
   1655                               initial_epoch=initial_epoch,
   1656                               steps_per_epoch=steps_per_epoch,
-> 1657                               validation_steps=validation_steps)
   1658 
   1659     def evaluate(self, x=None, y=None,

~/python_env/lib64/python3.4/site-packages/Keras-2.1.1-py3.4.egg/keras/engine/training.py in _fit_loop(self, f, ins, out_labels, batch_size, epochs, verbose, callbacks, val_f, val_ins, shuffle, callback_metrics, initial_epoch, steps_per_epoch, validation_steps)
   1211                     batch_logs['size'] = len(batch_ids)
   1212                     callbacks.on_batch_begin(batch_index, batch_logs)
-> 1213                     outs = f(ins_batch)
   1214                     if not isinstance(outs, list):
   1215                         outs = [outs]

~/python_env/lib64/python3.4/site-packages/Keras-2.1.1-py3.4.egg/keras/backend/theano_backend.py in __call__(self, inputs)
   1222     def __call__(self, inputs):
   1223         assert isinstance(inputs, (list, tuple))
-> 1224         return self.function(*inputs)
   1225 
   1226 

~/python_env/lib64/python3.4/site-packages/theano/compile/function_module.py in __call__(self, *args, **kwargs)
    915                     node=self.fn.nodes[self.fn.position_of_error],
    916                     thunk=thunk,
--> 917                     storage_map=getattr(self.fn, 'storage_map', None))
    918             else:
    919                 # old-style linkers raise their own exceptions

~/python_env/lib64/python3.4/site-packages/theano/gof/link.py in raise_with_op(node, thunk, exc_info, storage_map)
    323         # extra long error message in that case.
    324         pass
--> 325     reraise(exc_type, exc_value, exc_trace)
    326 
    327 

~/python_env/lib64/python3.4/site-packages/six.py in reraise(tp, value, tb)
    690                 value = tp()
    691             if value.__traceback__ is not tb:
--> 692                 raise value.with_traceback(tb)
    693             raise value
    694         finally:

~/python_env/lib64/python3.4/site-packages/theano/compile/function_module.py in __call__(self, *args, **kwargs)
    901         try:
    902             outputs =\
--> 903                 self.fn() if output_subset is None else\
    904                 self.fn(output_subset=output_subset)
    905         except Exception:

ValueError: Input dimension mis-match. (input[0].shape[1] = 16, input[1].shape[1] = 300)
Apply node that caused the error: Elemwise{Add}[(0, 0)](Reshape{3}.0, InplaceDimShuffle{x,0,x}.0)
Toposort index: 105
Inputs types: [TensorType(float32, 3D), TensorType(float32, (True, False, True))]
Inputs shapes: [(512, 16, 300), (1, 300, 1)]
Inputs strides: [(19200, 1200, 4), (1200, 4, 4)]
Inputs values: ['not shown', 'not shown']
Outputs clients: [[Reduce{maximum}{2}(Elemwise{Add}[(0, 0)].0), Elemwise{Composite{exp((i0 - i1))}}(Elemwise{Add}[(0, 0)].0, InplaceDimShuffle{0,1,x}.0), Elemwise{Composite{(i0 + (i1 * EQ(i2, i3) * i4))}}[(0, 0)](Elemwise{Composite{(((i0 / i1) + i2) * i3)}}[(0, 0)].0, TensorConstant{(1, 1, 1) of -1.0}, InplaceDimShuffle{0,1,x}.0, Elemwise{Add}[(0, 0)].0, InplaceDimShuffle{0,1,x}.0)]]

HINT: Re-running with most Theano optimization disabled could give you a back-trace of when this node was created. This can be done with by setting the Theano flag 'optimizer=fast_compile'. If that does not work, Theano optimizations can be disabled with 'optimizer=None'.
HINT: Use the Theano flag 'exception_verbosity=high' for a debugprint and storage map footprint of this apply node.

0 个答案:

没有答案