我在 Keras 中定义了一个 LSTM 模型,并用于tfjs.converters.save_keras_model
将其转换为 Tensorflow.js 格式。但是当试图在 JS 中加载 web 友好的模型时,它会导致一个错误,指出预期的形状与权重文件中存在的形状不同:
BenchmarkDialog.vue:47 Error: Based on the provided shape, [2,128], the tensor should have 256 values but has 139
at m (tf-core.esm.js:17)
at new t (tf-core.esm.js:17)
at Function.t.make (tf-core.esm.js:17)
at ke (tf-core.esm.js:17)
at i (tf-core.esm.js:17)
at Object.kh [as decodeWeights] (tf-core.esm.js:17)
at tf-layers.esm.js:17
at tf-layers.esm.js:17
at Object.next (tf-layers.esm.js:17)
at o (tf-layers.esm.js:17)
模型定义:
model = Sequential()
model.add(LSTM(
32,
batch_input_shape=(30, 5, 3),
return_sequences=True,
stateful=True,
activation='tanh',
))
model.add(Dropout(0.25))
model.add(LSTM(
32,
return_sequences=True,
stateful=True,
activation='tanh',
))
model.add(Dropout(0.25))
model.add(LSTM(
32,
return_sequences=False,
stateful=True,
activation='tanh',
))
model.add(Dropout(0.25))
model.add(Dense(3, activation='tanh', kernel_initializer='lecun_uniform'))
model.compile(loss='mse', optimizer=Adam())
有问题的张量属于 model.json 中的 LSTM 层:
{"name": "lstm_1/kernel", "shape": [2, 128], "dtype": "float32"}
这是model.json、权重文件和原始 keras 模型,以防万一。
关于我在这里做错了什么的任何想法?