Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use tensorflow js (tfjs) from typescript?

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?

like image 296
ozgur Avatar asked Nov 16 '18 16:11

ozgur


2 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.

like image 186
Explosion Pills Avatar answered Sep 16 '22 20:09

Explosion Pills


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"

like image 20
Trident D'Gao Avatar answered Sep 16 '22 20:09

Trident D'Gao