如何从 typescript 使用 tensorflow js (tfjs)?

How to use tensorflow js (tfjs) from typescript?

我试过安装打字:

npm install --save @type/tfjs
npm install --save @type/tenforflowjs
npm install --save @type/tensorflow

但它不存在。在 tensorflow js github repository 我可以看到它是使用 Typescript 开发的,但是他们似乎没有分发定义。如果没有定义分发(我希望我错了),我如何使用他们的 github 存储库来获取它并仍然从他们未来的版本中受益?

请注意,不幸的是,没有简单的方法可以判断库中的类型是否可用,因为可以通过多种方式导出类型定义。

如果你这样做 npm install --save @tensorflow/tfjs,你会看到有一个 node_modules/@tensorflow/tfjs/dist/index.d.ts。另外在 package.json for the project, there is "types": "dist/index.d.ts".

也就是说这个库有可用的类型。

如果包中未包含类型,您可以检查 DefinitelyTyped 以查看是否有可用的第三方定义 @types/{package}。否则,您将需要靠自己来进行强类型输入。

您可以在打字稿项目中包含一个 javascript 文件,您可以在其中专门包含与 tensorflow 相关的文件。所有的预处理——也就是最重要的部分——以及分类或回归结果,都可以在打字稿中完成。

安装tensorflow.js运行:

npm install --save @tensorflow/tfjs

要导入您的 *.ts 文件,请执行以下操作:

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

still troubles?

make sure tsconfig.json has "compilerOptions" with "moduleResolution": "node"

我试过和你一样的方法。

只需导入模块和运行 npm install。无需安装类型。

对我有用!