How to use tensorflow js (tfjs) from typescript?

Viewed 7587

I've tried to install typings:

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

But it doesn't exist. On tensorflow js github repository I can see that it is developed using Typescript, but it seems they did not distribute definitions. If there is no definition distribution (I hope I am wrong), how can I use their github repository to obtain it and still benefit from their future releases?

4 Answers

Note that there is unfortunately no simple way to tell whether types are available in a library since type definitions can be exported in a variety of ways.

If you do npm install --save @tensorflow/tfjs, you will see that there is a node_modules/@tensorflow/tfjs/dist/index.d.ts. Additionally in the package.json for the project, there is "types": "dist/index.d.ts".

That is to say types are available for this library.

If types are not included with the package, you can check DefinitelyTyped to see if there is a third party definition available at @types/{package}. Otherwise you'll be on your own for strong typing.

to install tensorflow.js run:

npm install --save @tensorflow/tfjs

to import to your *.ts file do:

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

still troubles?

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

You can include a javascript file in you typescript project where you include exclusively which is related to tensorflow. All the proprocessing -that is the heavy part- and the classification or regression result, can be done in typescript.

I tried the same thing as you did.

Simply import the module and run npm install. No need to install types.

Worked for me!

Related