1

我正在尝试将有状态的 LSTM 模型从 tf.keras 保存的模型(.h5)转换为 tfjs。由于 tfjs 还不支持有状态的 lstms,我需要处理模型之外的状态。现在,为了实现这一点,我只需要将现有 (.h5) 模型的一部分转换为 tfjs 模型。

我对 Keras 模型的权重进行了以下切片,并成功地将生成的子模型转换为 tfjs 层模型:

来自python代码的片段:

weights = model.get_weights()
model_1.set_weights(weights[:8])

# convert sub model
tfjs.converters.save_keras_model(model_1, os.path.join(target_name,'model_js1'))

当我尝试将此模型加载到 JS 中tf.loadLayersModel时,以下是我得到的错误 -

UnhandledPromiseRejectionWarning:错误:未知层:TensorFlowOpLayer。这可能是由于以下原因之一:

该层是在 Python 中定义的,在这种情况下,它需要移植到 TensorFlow.js 或您的 JavaScript 代码。自定义层在 JavaScript 中定义,但未使用 tf.serialization.registerClass() 正确注册。

子模型总结:

Layer (type)                    Output Shape         Param #     Connected to
==================================================================================================
input_3 (InputLayer)            [(1, 2, 128, 2)]     0
__________________________________________________________________________________________________
input_2 (InputLayer)            [(1, 1, 257)]        0
__________________________________________________________________________________________________
tf_op_layer_strided_slice (Tens [(1, 128)]           0           input_3[0][0]
__________________________________________________________________________________________________
tf_op_layer_strided_slice_1 (Te [(1, 128)]           0           input_3[0][0]
__________________________________________________________________________________________________
lstm_4 (LSTM)                   [(1, 1, 128), (1, 12 197632      input_2[0][0]
__________________________________________________________________________________________________
dropout_2 (Dropout)             (1, 1, 128)          0           lstm_4[0][0]
__________________________________________________________________________________________________
tf_op_layer_strided_slice_2 (Te [(1, 128)]           0           input_3[0][0]
__________________________________________________________________________________________________
tf_op_layer_strided_slice_3 (Te [(1, 128)]           0           input_3[0][0]
__________________________________________________________________________________________________
lstm_5 (LSTM)                   [(1, 1, 128), (1, 12 131584      dropout_2[0][0]
__________________________________________________________________________________________________
tf_op_layer_stack (TensorFlowOp [(2, 1, 128)]        0           lstm_4[0][1]
                                                                 lstm_5[0][1]
__________________________________________________________________________________________________
tf_op_layer_stack_1 (TensorFlow [(2, 1, 128)]        0           lstm_4[0][2]
                                                                 lstm_5[0][2]
__________________________________________________________________________________________________
dense_2 (Dense)                 (1, 1, 257)          33153       lstm_5[0][0]
__________________________________________________________________________________________________
tf_op_layer_Reshape (TensorFlow [(1, 2, 128)]        0           tf_op_layer_stack[0][0]
__________________________________________________________________________________________________
tf_op_layer_Reshape_1 (TensorFl [(1, 2, 128)]        0           tf_op_layer_stack_1[0][0]
__________________________________________________________________________________________________
activation_2 (Activation)       (1, 1, 257)          0           dense_2[0][0]
__________________________________________________________________________________________________
tf_op_layer_stack_2 (TensorFlow [(1, 2, 128, 2)]     0           tf_op_layer_Reshape[0][0]
                                                                 tf_op_layer_Reshape_1[0][0]
==================================================================================================
Total params: 362,369
Trainable params: 362,369
Non-trainable params: 0

我了解针对我的状态处理要求对张量执行的切片、整形和堆叠正在创建 tf_op_layers(我不确定它们是什么或如何管理它们)。现在,我不确定如何在 tfjs 中创建自定义层来加载这个模型。你能帮我创建自定义层tf_op_layer_strided_slice吗?以及如何处理 -tf_op_layer_Reshape作为tf_op_layer_stacktfjs 自定义层。

4

0 回答 0