创建注意模型时出错:数据基数不明确



我的目标:给定一个单变量时间序列,使用注意力模型进行二元预测。

数据示例:这里我留下一个非常简单的列集,包含两个样本:

"""
After reshaping, it looks like this:
X_train = [
[[1], [2], [3]],
[[4], [2], [3]]
]
Y_train = [
[0],
[1]
]
"""
X_train = np.array([1, 2, 3, 4, 2, 3]).reshape(2, 3, 1)
Y_train = np.array([0, 1]).reshape(2, 1)

代码:我用它作为参考:https://wanasit.github.io/attention-based-sequence-to-sequence-in-keras.html(基本上我从一开始就删除了嵌入(

sequence_length = X_train.shape[1]
encoder_input = Input(shape=(sequence_length, 1))
decoder_input = Input(shape=(1, 1))
encoder = LSTM(64, return_sequences=True, unroll=True)(encoder_input)
encoder_last = encoder[:,-1,:]
decoder = LSTM(64, return_sequences=True, unroll=True)(decoder_input,
initial_state=[encoder_last, encoder_last])
attention = dot([decoder, encoder], axes=[2, 2])
attention = Activation('softmax', name='attention')(attention)
context = dot([attention, encoder], axes=[2,1])
decoder_combined_context = concatenate([context, decoder])
output = TimeDistributed(Dense(64, activation="tanh"))(decoder_combined_context)
output = TimeDistributed(Dense(1, activation="softmax"))(output)
model = Model(inputs=[encoder_input, decoder_input], outputs=[output])
model.compile(optimizer=tf.keras.optimizers.Adam(),
loss=tf.keras.losses.BinaryCrossentropy(),
metrics=['accuracy'])

错误:当我执行model.fit(x=X_train, y=Y_test, verbose=1)时,我得到一个错误,但我不知道它意味着什么:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-31-6c44292c5a8d> in <module>
59 history = model.fit(x=X_train,
60                     y=Y_test,
---> 61                     verbose=1)
~/opt/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py in _method_wrapper(self, *args, **kwargs)
69   def _method_wrapper(self, *args, **kwargs):
70     if not self._in_multi_worker_mode():  # pylint: disable=protected-access
---> 71       return method(self, *args, **kwargs)
72 
73     # Running inside `run_distribute_coordinator` already.
~/opt/anaconda3/lib/python3.7/site-packages/tensorflow/python/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, validation_batch_size, validation_freq, max_queue_size, workers, use_multiprocessing, **kwargs)
880           use_multiprocessing=use_multiprocessing,
881           model=self,
--> 882           steps_per_execution=self._steps_per_execution)
883 
884       # Container that configures and calls `tf.keras.Callback`s.
~/opt/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/engine/data_adapter.py in __init__(self, x, y, sample_weight, batch_size, steps_per_epoch, initial_epoch, epochs, shuffle, class_weight, max_queue_size, workers, use_multiprocessing, model, steps_per_execution)
1136         use_multiprocessing=use_multiprocessing,
1137         distribution_strategy=ds_context.get_strategy(),
-> 1138         model=model)
1139 
1140     strategy = ds_context.get_strategy()
~/opt/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/engine/data_adapter.py in __init__(self, x, y, sample_weights, sample_weight_modes, batch_size, epochs, steps, shuffle, **kwargs)
281             label, ", ".join(str(i.shape[0]) for i in nest.flatten(data)))
282       msg += "Please provide data which shares the same first dimension."
--> 283       raise ValueError(msg)
284     num_samples = num_samples.pop()
285 
ValueError: Data cardinality is ambiguous:
x sizes: 2
y sizes: 1081
Please provide data which shares the same first dimension.

最后我设法修复了它:

  • 首先,我使用的是Y_test而不是Y_train
  • 其次,我应该使用model.fit(x=[X_train, Y_train], y=Y_train, verbose=1)

最新更新