javascript - 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"}]}]}
解决方案
推荐阅读
- python - 对象类型没有名为长度的轴,尝试根据数据帧的长度进行排序并将其屏蔽到第二个数据帧
- mysql - makefile CFLAGS 忽略 $(mysql_config --libs)?
- c++ - 为什么 C++ 不读取我的 if-else 语句的下一行,尽管前面的语句不应该是真的?
- python - 有没有办法让 Pandas to_sql 函数变得冗长?
- java - IBM Websphere 中的 Java VisualVM JMX 连接
- java - 有没有一种简单的方法可以将 A 类的对象解析为 B 类的对象,该字段是 A 的子集?
- javascript - 与 HTML 和 Javascript 相关的数组问题
- javascript - 将任何 Nodejs 请求转发到其他服务器
- python - Pandas - groupby 索引作为列
- jquery - 根据其在 div 中的位置删除一项 (jQuery)