首页 > 解决方案 > TensorFlow.JS:无法在 loadModel 上找到分片文件

问题描述

我正在尝试导入从 keras 转换为 tensorflow.js 的模型,当我使用 loadModel 函数 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"}]}]}

标签: javascripttensorflowtensorflow.js

解决方案


推荐阅读