当我使用loadModel函数TensorFlow.JS找不到权重的分片文件时,我正在尝试导入已从keras转换为tensorflow.js的模型。两者都在同一个文件夹中,每当我尝试加载模型时,它都说找不到分片文件(404错误)。我已经尝试了几乎所有内容,并且四处寻找,却找不到任何东西,因此将不胜感激!
async function loadModel(value){
const model = await tf.loadModel('path/to/model.json
console.log(model.predict([value]));
}
更新:我将所有内容都放在同一文件夹中;我的本地服务器和我的服务器上的model.json,shard和html文件。至于代码,上面是我所有的javascript,下面是我的python的外观。
import pandas as pd
from sklearn import linear_model
import matplotlib.pyplot as plt
import keras as k
import tensorflowjs as tfjs
#read data
dataframe = pd.read_fwf('../brain_body.txt')
x_values = dataframe[['Brain']]
y_values = dataframe[['Body']]
model = k.models.Sequential()
model.add(k.layers.Dense(units = 1, activation = 'relu', input_dim=1))
model.compile(loss='mean_squared_logarithmic_error',
optimizer=k.optimizers.SGD(lr=0.01), metrics=['acc'])
model.load_weights('model.h5')
tfjs.converters.save_keras_model(model, 'C:\\Users\\UserName\\Desktop\\ai')
#model.save('model.h5')
brain_weight = float(input("What is the animal's brain weight? "))
print("The weight of the animal is " + str(model.predict([brain_weight])))
这是model.json
{"modelTopology": {"keras_version": "2.1.4", "backend": "tensorflow",
"model_config": {"class_name": "Sequential", "config": [{"class_name":
"Dense", "config": {"name": "dense_1", "trainable": true,
"batch_input_shape": [null, 1], "dtype": "float32", "units": 1, "activation": "relu", "use_bias": true, "kernel_initializer": {"class_name": "VarianceScaling", "config": {"scale": 1.0, "mode": "fan_avg", "distribution": "uniform", "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": {"optimizer_config": {"class_name": "SGD", "config": {"lr": 0.009999999776482582, "momentum": 0.0, "decay": 0.0, "nesterov": false}}, "loss": "mean_squared_logarithmic_error", "metrics": ["acc"], "sample_weight_mode": null, "loss_weights": null}}, "weightsManifest": [{"paths": ["group1-shard1of1"], "weights": [{"name": "dense_1/kernel", "shape": [1, 1], "dtype": "float32"}, {"name": "dense_1/bias", "shape": [1], "dtype": "float32"}]}]}