首页 > 解决方案 > Tensorflow.js 使用节点保存模型

问题描述

我想使用这个函数从 node.js 中保存一个训练有素的模型

async function tfModelExperiment(model) {
  try {
    let tsModelTraining = await model.save('file:///tmp/my-model-1');
  } 
  catch (error) {
    console.log(error);
  }
}

但是在保存模型时它会返回

(节点:23756)UnhandledPromiseRejectionWarning:错误:找不到 URL 'file:///tmp/my-model-1' 的任何保存处理程序

我发现另一个人在这个问题上苦苦挣扎,但通过包括

const tf = require('@tensorflow/tfjs');

我已经拥有了,我尝试将目录更改为我的主目录,但这并没有解决问题,也没有将它作为 sudo 运行,我做错了什么?

软件 我使用的是 Ubuntu Ubuntu 18.04.1 LTS,最新的 TensorFlow.js 包 (0.13.0) 安装了 npm

编辑:

应该注意的是,我尝试过

import * as tf from '@tensorflow/tfjs';
import '@tensorflow/tfjs-node';

如此处所提供(https://github.com/caisq/tfjs-node),它返回

TypeError: tf.sequential is not a function at file:///home/sjors/node.mjs:7:18 at ModuleJob.run (internal/loader/ModuleJob.js:94:14) at

我试过了:

const tf = require('@tensorflow/tfjs');
require('@tensorflow/tfjs-node');

返回与UnhandledPromiseRejectionWarning以前相同的错误

标签: javascriptnode.jstensorflowasync-awaittensorflow.js

解决方案


在 github 上 tfjs 的人的帮助下,我现在开始工作了。

基本上你只需要安装 tfjs-node 依赖项:

npm i @tensorflow/tfjs-node

然后你可以只需要 tfjs 它应该可以工作。

const tf = require('@tensorflow/tfjs');
require('@tensorflow/tfjs-node');

const model = tf.sequential();
model.add(tf.layers.dense({units: 1, inputShape: [1]}));

model.save('file://./model-1a');

推荐阅读