我正在使用tf.loadLayersModel()
加载一个简单的Tensorflow.js模型,但是该模型没有构建。我正在使用功能性API来构建模型,但仅由密集层组成。 Lambda层存在类似的错误seems to arise,但在Tf.js中我只使用了2个Dense层和功能层are supported。
完整错误:
Error: Unknown layer: Functional. 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()
触发它的JS代码:
const http = tf.io.http
tf.loadLayersModel(http(url)).then((model) => {
console.log('Loaded model.')
console.log(model)
})
url
的提取内容(又名model.json
文件)
{"format": "layers-model", "generatedBy": "keras v2.4.0", "convertedBy": "TensorFlow.js Converter v2.0.1.post1", "modelTopology": {"keras_version": "2.4.0", "backend": "tensorflow", "model_config": {"class_name": "Functional", "config": {"name": "my_model", "layers": [{"class_name": "InputLayer", "config": {"batch_input_shape": [null, 10], "dtype": "float32", "sparse": false, "ragged": false, "name": "input_1"}, "name": "input_1", "inbound_nodes": []}, {"class_name": "Dense", "config": {"name": "dense", "trainable": true, "dtype": "float32", "units": 20, "activation": "relu", "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}, "name": "dense", "inbound_nodes": [[["input_1", 0, 0, {}]]]}, {"class_name": "Dense", "config": {"name": "dense_1", "trainable": true, "dtype": "float32", "units": 20, "activation": "relu", "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}, "name": "dense_1", "inbound_nodes": [[["dense", 0, 0, {}]]]}, {"class_name": "Dense", "config": {"name": "dense_2", "trainable": true, "dtype": "float32", "units": 10, "activation": "linear", "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}, "name": "dense_2", "inbound_nodes": [[["dense_1", 0, 0, {}]]]}], "input_layers": [["input_1", 0, 0]], "output_layers": [["dense_2", 0, 0]]}}, "training_config": {"loss": "mse", "metrics": "accuracy", "weighted_metrics": null, "loss_weights": null, "optimizer_config": {"class_name": "RMSprop", "config": {"name": "RMSprop", "learning_rate": 0.001, "decay": 0.0, "rho": 0.9, "momentum": 0.0, "epsilon": 1e-07, "centered": false}}}}, "weightsManifest": [{"paths": ["group1-shard1of1.bin"], "weights": [{"name": "dense/kernel", "shape": [10, 20], "dtype": "float32"}, {"name": "dense/bias", "shape": [20], "dtype": "float32"}, {"name": "dense_1/kernel", "shape": [20, 20], "dtype": "float32"}, {"name": "dense_1/bias", "shape": [20], "dtype": "float32"}, {"name": "dense_2/kernel", "shape": [20, 10], "dtype": "float32"}, {"name": "dense_2/bias", "shape": [10], "dtype": "float32"}]}]}
是否要复制模型?这是python代码:
import keras
import keras.layers as layers
import tensorflowjs as tfjs
inputs = keras.Input(shape=(10,))
dense = layers.Dense(20, activation="relu")
x = dense(inputs)
x = layers.Dense(20, activation="relu")(x)
outputs = layers.Dense(10)(x)
# Create the model
model = keras.Model(inputs=inputs, outputs=outputs, name="my_model")
KEY = 'sampleid'
MDL = 'mymodel'
model.compile(loss='mse',metrics='accuracy')
tfjs.converters.save_keras_model(model, MDL)
注意:
该URL有点冗长(这是Firebase Storage downloadURL),我不确定IOHandler(http
)是否可以完美解析weightPathPrefix
。我不确定这是 the 问题,甚至是 an 问题,但是如果它不正确并且我不知道如何检查其计算值,它可能会产生问题。 / p>
版本:
JS: Tensorflow.js : 2.0.1
Py: Tensorflowjs : 2.0.1.post1
Py: Keras : 2.4.3
问题似乎出在模型权重的解析中(请参阅“注释”)。之前,我将此示例添加到有关tf.loadLayersModel()
函数的GitHub ticket中,其中包含有关尝试的解决方案的许多详细信息。
答案 0 :(得分:3)
Python tensorflow使用Functional
作为功能模型的类名,但是tfjs在内部使用不同的名称。
尝试将modelTopology.model_config.class_name
中的model.json
更改为Model
。
答案 1 :(得分:0)
根据您已经写的内容,我尝试使用顺序API而非功能性API来编写模型:
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
import tensorflowjs as tfjs
# create sequential model
model = keras.Sequential(
[
layers.Dense(2, activation="relu", name="layer1"),
layers.Dense(3, activation="relu", name="layer2"),
layers.Dense(4, name="layer3"),
]
)
# Call model on a test input
KEY = 'sampleid'
MDL = 'mymodel'
model.compile(loss='mse',metrics='accuracy')
tfjs.converters.save_keras_model(model, MDL)
将文件加载为:
model = await tf.loadLayersModel('./mymodel/model.json');
model.summary();
似乎正在工作。但是我同意,功能性API也应该起作用。可能会找到更多信息here。