我无法将使用@tensorflow/tfjs-node
创建的顺序模型加载回node。
import * as tf from '@tensorflow/tfjs-node';
model = tf.sequential();
// Add a single input layer
model.add(
tf.layers.dense({ inputShape: [1.0], units: 1, useBias: true })
);
model.add(
tf.layers.dense({
inputShape: [1],
units: 10,
activation: 'relu',
kernelRegularizer: tf.regularizers.l2({ l2: 0.01 }),
useBias: true
})
);
// Add additional hidden layers with L2 regularization
model.add(
tf.layers.dense({
units: 8,
activation: 'relu',
kernelRegularizer: tf.regularizers.l2({ l2: 0.01 }),
useBias: true
})
);
model.add(
tf.layers.dense({
units: 6,
activation: 'relu',
kernelRegularizer: tf.regularizers.l2({ l2: 0.01 }),
useBias: true
})
);
// Add an output layer with linear activation
model.add(
tf.layers.dense({ units: 1, activation: 'linear', useBias: true })
);
// Add an output layer
model.add(tf.layers.dense({ units: 1, useBias: true }));
我是这样保存模型的(目前有点粗糙,但看起来可以用其他方法):
const createSavableModel = async () => {
await model.save(tf.io.withSaveHandler(artifacts => {
modelArtifacts = serialize.serialize(artifacts);
return Promise.resolve({
modelArtifactsInfo: {
dateSaved: new Date()
} as any
});
}));
};
我把modelArtifacts
藏在MongoDB里。
然后,在尝试加载模型时,如下所示:
model = await tf.loadLayersModel(tf.io.fromMemory(existingModelArtifacts));
我得到一个很长的错误:
错误:未知层:{"模型拓扑":{"类名称..."
1.该层是用Python定义的,在这种情况下,需要将其移植到TensorFlow.js或JavaScript代码。
1.自定义层是在JavaScript中定义的,但未使用tf. serialization. registerClass()正确注册。
现在,模型完全是在tensorflow.js
中创建的,错误并没有真正告诉我它对哪一层不满意。
可能是什么原因造成的?
编辑
我已减少模型层:
model = tf.sequential();
// Add a single input layer
model.add(
tf.layers.dense({ inputShape: [1.0], units: 1, useBias: true })
);
model.add(tf.layers.dense({ units: 1, useBias: true }));
这是它的拓扑结构
{
"class_name": "Sequential",
"config": {
"name": "sequential_1",
"layers": [
{
"class_name": "Dense",
"config": {
"units": 1,
"activation": "linear",
"use_bias": true,
"kernel_initializer": {
"class_name": "VarianceScaling",
"config": {
"scale": 1,
"mode": "fan_avg",
"distribution": "normal",
"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_Dense1",
"trainable": true,
"batch_input_shape": [
null,
1
],
"dtype": "float32"
}
},
{
"class_name": "Dense",
"config": {
"units": 1,
"activation": "linear",
"use_bias": true,
"kernel_initializer": {
"class_name": "VarianceScaling",
"config": {
"scale": 1,
"mode": "fan_avg",
"distribution": "normal",
"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_Dense2",
"trainable": true
}
}
]
},
"keras_version": "tfjs-layers 4.2.0",
"backend": "tensor_flow.js"
}
那么,为什么我不能用tf.loadLayersModel(tf.io.fromMemory(existingModelArtifacts))
加载它呢?
1条答案
按热度按时间zed5wv101#
好的,我决定通过文件系统: