Модель Tensorflow.js работает значительно хуже, чем прямо эквивалентная модель Keras - PullRequest
0 голосов
/ 11 сентября 2018

Я пытаюсь преобразовать текстовый генератор LSTM RNN на уровне слов из Keras в Tensorflow.js. Тем не менее, модель tensflowflow.js обучается с меньшей скоростью и дает прогнозы на мусор. Я поливал каждую деталь (по крайней мере, я думаю у меня), но я продолжаю терпеть неудачу.

Позвольте мне объяснить.

Во-первых, это график моей потери в течение первых 100 эпох с использованием Keras (на бэкэнде Tensorflow): enter image description here

... и это график моей потери для того же набора данных за первые 100 эпох с использованием Tensorflow.js: enter image description here

Итак, потеря действительно уменьшается, но гораздо медленнее и застревает с потерей ~ 4.4 при скорости обучения 0,02 (чего не делает модель Keras, она продолжает уменьшаться).

Во-вторых, производительность обеих моделей сильно отличается. Модель Keras выдает типичный вывод для такого небольшого набора данных, примерно так (после 10 итераций):

i i i my am and the the i what of and the and the the and the and i and the the and and the and i and i and i i i i and the and i i i i the the and i the the and i the and mine i and the and and and the burn the my and and i i i i and i burn the it the and it and i i and i the i i and i i the my i the my and and i i i the i

После 100 итераций:

path that winds 'pon the path i find, and claim as mine to ride the waves of unrest made to make me shine as a testament to why the ways of of of will will by shit by dismiss by by dismiss dismiss worship worship worship blood blood blood serpent's serpent's serpent's serpent's like like the the the the of of of of of of to to to we have we decide decide order, order, order, of they seizing rites of of of claim tomorrow tomorrow around around tomorrow at sacrifice of.' sacrifice around around like like the the the of the to to to to to to waves made made tomorrow upon rites of few no few know underworld, the no no no tomorrow around around around around around

Модель тензор потока.js, с другой стороны, только когда-либо предсказывает одно и то же слово (независимо от того, сколько итераций, независимо от того, насколько мал потери):

the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the

Я проверил векторы результата прогнозирования, и все они очень похожи (но не совсем одинаковы), поэтому я почти уверен, что модель только что сломалась.

Теперь я знаю, о чем вы, возможно, думаете - должна быть какая-то проблема в подготовке данных. Но, уверяю вас, данные обучения и целевые данные между двумя программами идентичны . Я проверил все в коде предварительной подготовки с помощью такой расчески с тонкими зубами (то есть, много, много печатных выражений) до такой степени, что я чувствую, что могу быть бредовым. Размер, содержимое, тип данных - это все абсолютно одинаковые между X и y входами в обеих программах. Если вы не верите мне, не стесняйтесь попробовать полный исходный код здесь .

Итак, с учетом всего сказанного, вот то, что я считаю соответствующим кодом, две разные модели. Оригинал Керас:

model = Sequential()
model.add(LSTM(128, return_sequences=True, input_shape=(maxlen, len(words))))
model.add(Dropout(0.2))
model.add(LSTM(128, return_sequences=False))
model.add(Dropout(0.2))
model.add(Dense(len(words)))
model.add(Activation('softmax'))

model.compile(loss='categorical_crossentropy', optimizer=RMSprop(lr=0.002))

model.fit(X, y, batch_size=32, nb_epoch=10)

и вот он в tenorflow.js:

var model = tf.sequential();
  model.add(tf.layers.lstm({
    units: 128,
    returnSequences: true,
    inputShape: [maxlen, words.length]
  }));
  model.add(tf.layers.dropout(0.2))
  model.add(tf.layers.lstm({
    units: 128,
    returnSequences: false
  }));
  model.add(tf.layers.dropout(0.2))
  model.add(tf.layers.dense({units: words.length, activation: 'softmax'}));

  model.compile({loss: 'categoricalCrossentropy', optimizer: tf.train.rmsprop(0.002)});

  await model.fit(x_tensor, y_tensor, {
    epochs: 100,
    batchSize: 32
  })

Насколько я могу судить, оба абсолютно одинаковы. Для справки: maxlen = 30 и words.length = 297 для обоих с моим предоставленным набором данных (это небольшой набор данных, только для демонстрационных целей)

Есть идеи, в чем может быть проблема?

...