保存使用tfhub层的keras模型,因此可以在tensorflow.js中使用

问题描述 投票:0回答:1

我正在尝试将模型保存在Google的演示版tfhub的基本文本分类中,以便可以在tensorflow.js中使用它。

模型使用tfhub keras layer进行嵌入。

该模型已经过训练,可以在Google Colab中使用。

我使用tfjs.converters.save_keras_model(model, target_dir)保存模型,然后在javascript中使用const model = await tf.loadLayersModel(MODEL_URL);加载模型。

我已经通过Google IO上显示的简单的临时转换器模型成功完成了此任务,并且可以正常工作。

但是当我在javascript中加载模型时使用tfhub Keras层时,出现错误

Uncaught (in promise) Error: Unknown layer: KerasLayer. This may be due to one of the following reasons:

1. The layer is defined in Python, in which case it needs to be ported to TensorFlow.js or your JavaScript code.

2. The custom layer is defined in JavaScript, but is not registered properly with tf.serialization.registerClass().

    at new e (errors.ts:48)
    at Rp (generic_utils.ts:242)
    at cd (serialization.ts:31)
    at e.fromConfig (models.ts:942)
    at Rp (generic_utils.ts:277)
    at cd (serialization.ts:31)
    at models.ts:300
    at common.ts:14
    at Object.next (common.ts:14)
    at a (common.ts:14)


我的models.json文件是

{“ format”:“ layers-model”,“ generateBy”:“ keras v2.2.4-tf”,“ convertedBy”:“ TensorFlow.js Converter v1.2.6”,“ modelTopology”:{“ keras_version”:“ 2.2.4-tf”,“后端”:“ tensorflow”,“ model_config”:{“ class_name”:“ sequential”,“ config”:{“ name”:“ sequential_1”,“ layers”:[{“ class_name” :“ KerasLayer”,“ config”:{“名称”:“ keras_layer_1”,“可训练”:true,“ batch_input_shape”:[null],“ dtype”:“ string”,“ handle”:“ https://tfhub.dev/google/tf2-preview/gnews-swivel-20dim/1”} },{“ class_name”:“密集”,“ config”:{“ name”:“ dense_2”,“ trainable”:true,“ dtype”:“ float32”,“ units”:16,“ activation”:“ relu “,” use_bias“:true,” kernel_initializer“:{” class_name“:” GlorotUniform“,” config“:{” seed“:null}},” bias_initializer“:{” class_name“:”零“,” config“ :{}},“ kernel_regularizer”:null,“ bias_regularizer”:null,“ activity_regularizer”:null,“ kernel_constraint”:null,“ bias_constraint”:null}},{“ class_name”:“密集”,“配置”: {“名称”:“ dense_3”,“可训练”:true,“ dtype”:“ float32”,“单位”:1,“激活”:“ sigmoid”,“ use_bias”:true,“ kernel_initializer“:{” class_name“:” GlorotUniform“,” config“:{” seed“:null}},” bias_initializer“:{” class_name“:” Zeros“,” config“:{}},” kernel_regularizer“: null,“ bias_regularizer”:null,“ activity_regularizer”:null,“ kernel_constraint”:null,“ bias_constraint”:null}}]}},“ training_config”:{“ loss”:“ binary_crossentropy”,“ metrics”:[“准确性“],” weighted_metrics“:null,” sample_weight_mode“:null,” loss_weights“:null,” optimizer_config“:{” class_name“:” Adam“,” config“:{” name“:” Adam“,” learning_rate “:0.0010000000474974513,”衰变“:0.0,” beta_1“:0.8999999761581421,” beta_2“:0.9990000128746033,” epsilon“:1e-07,” amsgrad“:false}}}},” weightsManifest“:[{” paths“: [“ group1-shard1of1.bin”],“ weights”:[{“ name”:“ dense_2 / kernel”,“ shape”:[20,16],“ dtype”:“ float32”},{“ name”: “ dense_2 / bias”,“ shape”:[16],“ dtype”:“ float32”},{“ name”:“ dense_3 / kernel”,“ shape”:[16,1],“ dtype”:“ float32 “},{”名称“:” dense_3 / bias“,”形状“:[1],” dtype“:” float32“},{”名称“:”变量“,”形状”:[20001,20],“ dtype”:“ float32”}]}]}}]

这是使用tensorflow 2 beta。

任何建议将不胜感激。

tensorflow beta
1个回答
0
投票

我也使用tfhub模块来构建自定义KerasLayer面临同样的问题。因此,我当然可以确定[{“ class_name”:“ KerasLayer”,“ config”:{“ name”:“ keras_layer_1”,“ trainable”:true,“ batch_input_shape”:[null],“ dtype “:” string“,” handle“:” https://tfhub.dev/google/tf2-preview/gnews-swivel-20dim/1“}}

引起了问题。

您现在有什么解决方法吗?

© www.soinside.com 2019 - 2024. All rights reserved.