Ошибка при создании модели внимания: количество данных неоднозначно - PullRequest
0 голосов
/ 07 мая 2020

Моя цель : учитывая одномерный временной ряд, сделать бинарный прогноз, используя модель внимания.

Пример данных: Здесь я оставляю очень простой набор поездов с двумя образцами:

"""
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.

1 Ответ

0 голосов
/ 07 мая 2020

Наконец-то мне удалось это исправить:

  • Прежде всего, я использовал Y_test вместо Y_train
  • Во-вторых, я должен был использовать model.fit(x=[X_train, Y_train], y=Y_train, verbose=1)
...