我正在尝试将有状态的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'))
当我尝试使用tf.loadLayersModel
将此模型加载到JS中时,出现以下错误-
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_stack
作为tfjs自定义图层。