GithubHelp home page GithubHelp logo

yorkie / tensorflow-nodejs Goto Github PK

View Code? Open in Web Editor NEW
579.0 32.0 88.0 305 KB

TensorFlow Node.js provides idiomatic JavaScript language bindings and a high layer API for Node.js users. TensorFlow Node.js provides idiomatic JavaScript language bindings and a high layer API for Node.js users.

Home Page: https://npmjs.org/package/tensorflow2

License: MIT License

JavaScript 100.00%
tensorflow tensorflow-node nodejs

tensorflow-nodejs's Introduction

TensorFlow for Node.js

NPM Dependency Build Coverage
NPM version Dependency Status Build Status Coverage

This library wraps Tensorflow Python for Node.js developers, it's powered by @pipcook/boa.

Notice: This project is still under active development and not guaranteed to have a stable API. This is especially true because the underlying TensorFlow C API has not yet been stabilized as well.

Installation

$ npm install tensorflow2 --save

Usage

const tf = require('tensorflow2');

// load mnist dataset.
const dataset = tf.keras.dataset.mnist();
// {
//   train: { x: [Getter], y: [Getter] },
//   test: { x: [Getter], y: [Getter] }
// }

// create model.
const model = tf.keras.models.Sequential([
  tf.keras.layers.Flatten({
    input_shape: [28, 28]
  }),
  tf.keras.layers.Dense(128, {
    activation: 'relu'
  }),
  tf.keras.layers.Dropout(0.2),
  tf.keras.layers.Dense(10)
]);
model.summary();

// compile the model.
const loss_fn = tf.keras.losses.SparseCategoricalCrossentropy({ from_logits: true });
model.compile({
  optimizer: 'adam',
  loss: loss_fn,
  metrics: [ 'accuracy' ],
});

// train the model.
model.fit(dataset.train.x, dataset.train.y, { epochs: 5 });

// save the model
model.save('your-model.h5');

See example/mnist.js for complete example.

Tests

$ npm test

License

MIT licensed @ 2020

tensorflow-nodejs's People

Contributors

bhageena avatar ry avatar yorkie avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

tensorflow-nodejs's Issues

feature: support expression function

JavaScript language doesn't have operator overloading, then that we could not bypass to do this like Python or C++:

graph.matmul(x, W) + b

Currently only this way is working at Node.js library:

graph.add(graph.matmul(x, W), b)

The ideal solution for current JavaScript standard is we wrap a function where we use the expression to represent what it is stand in math like this:

session.run(function() {
  graph.matmul(x, W) + b;
});

And we now could use esprima to parse the function body and overload the operator in runtime.

model save in model.ckpt file

Hi,

Please I save my model from python using tf.train.Saver() in a file model.ckpt.
How could I use in node.js ?

Thanks

[Error] Library not loaded: bazel-out/darwin_x86_64-opt/bin/tensorflow/libtensorflow.so

When run in MacOS, throw a error:

Error: dlopen(/Users/xxx/node_modules/tensorflow2/build/Release/tensorflow.node, 1): Library not loaded: bazel-out/darwin_x86_64-opt/bin/tensorflow/libtensorflow.so
  Referenced from: /Users/xxx/node_modules/tensorflow2/build/Release/tensorflow.node
  Reason: image not found
    at Object.Module._extensions..node (module.js:598:18)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Module.require (module.js:498:17)
    at require (internal/module.js:20:19)
    at bindings (/Users/xxx/node_modules/bindings/bindings.js:76:44)
    at Object.<anonymous> (/Users/xxx/node_modules/tensorflow2/lib/index.js:5:56)
    at Module._compile (module.js:571:32)
    at Object.Module._extensions..js (module.js:580:10)

I copy the libtensorflow.so file into project root directory (myproject/bazel-out/darwin_x86_64-opt/bin/tensorflow/libtensorflow.so) sloved this error.

No support for your platform win32

Got a new typescript project going, and I ran into this issue installing tensorflow2
No support for your platform win32

I am fairly confident that I am running 64 windows 10
I tried running the command inside VS Code and Powershell

Full Error

D:\development\rader\node_modules\@pipcook\boa\tools\check-dependence.js:16
  throw new TypeError(`No support for your platform ${PLATFORM}`);
  ^

TypeError: No support for your platform win32
    at Object.<anonymous> (D:\development\rader\node_modules\@pipcook\boa\tools\check-dependence.js:16:9)
    at Module._compile (internal/modules/cjs/loader.js:1063:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
    at internal/main/run_main_module.js:17:47
npm WARN @uniswap/[email protected] requires a peer of @ethersproject/address@^5.0.0-beta but none is installed. You must install peer dependencies yourself.
npm WARN @uniswap/[email protected] requires a peer of @ethersproject/contracts@^5.0.0-beta but none is installed. You must install peer dependencies yourself.
npm WARN @uniswap/[email protected] requires a peer of @ethersproject/networks@^5.0.0-beta but none is installed. You must install peer dependencies yourself.
npm WARN @uniswap/[email protected] requires a peer of @ethersproject/providers@^5.0.0-beta but none is installed. You must install peer dependencies yourself.
npm WARN @uniswap/[email protected] requires a peer of @ethersproject/solidity@^5.0.0-beta but none is installed. You must install peer dependencies yourself.
npm WARN [email protected] No description
npm WARN [email protected] No repository field.
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: [email protected] (node_modules\fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for [email protected]: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"})

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! @pipcook/[email protected] preinstall: `node tools/check-dependence.js && make -C ./pybind11/ && node tools/install-python.js && node tools/install-requirements.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the @pipcook/[email protected] preinstall script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\digim\AppData\Roaming\npm-cache\_logs\2021-02-07T07_56_52_487Z-debug.log
PS D:\development\rader>

proto path to load fails

The error log:

/Users/yorkie/workspace/tf-example/node_modules/protobufjs/src/root.js:94
            throw err;
            ^

Error: ENOENT: no such file or directory, open 'protobuf/config.proto'
    at Object.fs.openSync (fs.js:651:18)
    at Object.fs.readFileSync (fs.js:553:33)
    at fetch (/Users/yorkie/workspace/tf-example/node_modules/protobufjs/src/root.js:160:34)
    at Root.load (/Users/yorkie/workspace/tf-example/node_modules/protobufjs/src/root.js:194:13)
    at Root.loadSync (/Users/yorkie/workspace/tf-example/node_modules/protobufjs/src/root.js:235:17)
    at Object.loadSync (/Users/yorkie/workspace/tf-example/node_modules/protobufjs/src/index-light.js:69:17)
    at Object.<anonymous> (/Users/yorkie/workspace/tf-example/node_modules/tensorflow2/lib/index.js:10:30)
    at Module._compile (module.js:569:30)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:503:32)

To reproduce this bug, install this package via npm and require('tensorflow') on another directory except for the project root.

some ops not work.

For example, graph.shape:

const tf = require('../../');
const graph = tf.graph();
const session = tf.session();

const input = tf.tensor([[1, 2, 3], [4, 5, 6]], tf.dtype.int32, [2, 3]);
const shape = graph.shape(input);
2017-08-02 21:53:07.141389: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.2 instructions, but these are available on your machine and could speed up CPU computations.
2017-08-02 21:53:07.141973: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations.
2017-08-02 21:53:07.141979: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX2 instructions, but these are available on your machine and could speed up CPU computations.
2017-08-02 21:53:07.141984: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use FMA instructions, but these are available on your machine and could speed up CPU computations.
[1]    27975 segmentation fault  node test/ops/nn.test.js
2017-08-02 21:53:09.626448: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.2 instructions, but these are available on your machine and could speed up CPU computations.
2017-08-02 21:53:09.627109: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations.
2017-08-02 21:53:09.627115: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX2 instructions, but these are available on your machine and could speed up CPU computations.
2017-08-02 21:53:09.627119: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use FMA instructions, but these are available on your machine and could speed up CPU computations.
node(27999,0x7fff720ef000) malloc: *** mach_vm_map(size=17042544937633144832) failed (error code=3)
*** error: can't allocate region
*** set a breakpoint in malloc_error_break to debug
libc++abi.dylib: terminating with uncaught exception of type std::bad_alloc: std::bad_alloc
[1]    27999 abort      node test/ops/nn.test.js
2017-08-02 21:53:10.690841: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.2 instructions, but these are available on your machine and could speed up CPU computations.
2017-08-02 21:53:10.691403: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations.
2017-08-02 21:53:10.691409: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX2 instructions, but these are available on your machine and could speed up CPU computations.
2017-08-02 21:53:10.691414: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use FMA instructions, but these are available on your machine and could speed up CPU computations.
/Users/eric/work/github/tensorflow-nodejs.bk/lib/graph.js:140
      op.finish();
         ^

Error
    at Error (native)
    at Graph.createOperationFromBuilder [as shape] (/Users/eric/work/github/tensorflow-nodejs.bk/lib/graph.js:140:10)
    at Object.<anonymous> (/Users/eric/work/github/tensorflow-nodejs.bk/test/ops/nn.test.js:8:21)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.runMain (module.js:604:10)
    at run (bootstrap_node.js:389:7)

some errors above.

API: renaming const and import

As @ry mentioned:

"import" and "const" are keywords in JS, so I think "graph.import" and "graph.const" are undesirable choices.

Now a propose is that:

  • use constant instead of const by keeping consistence with Python
  • use load or other name not in keywords list should address this issue basically

Hi @ry this looks good to you?

node > python > C wrapper ?

my understanding is that tensorflowJS provides bindings and direct access to the raw C libraries, but is currently only available up to TF 1.0

So this project wraps the python with node bindings, eg a "wrapper of a wrapper"
but perhaps since google is seeming to support the python bindings more than JS, this is why you have made it?

just trying to understand how much of an adoption risk and benefit there is here?

possibly relevant:
tensorflow/tfjs#4317

Image.decodeJpeg

var tf = require('tensorflow2');
var fs = require('fs');

const graph = tf.graph();
const session = tf.session();

let image = fs.readFileSync(process.cwd() + "/example.jpg").toString('base64');
let imageTensor = tf.tensor(image, tf.dtype.string);
let imageConstant = graph.constant(imageTensor);
let result = graph.image.decodeJpeg(imageConstant); //line 10, input is an Operation

Error:

2017-10-14 12:30:09.064477: I tensorflow/core/platform/cpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX
S:\Electron\tensorflow-nodejs\lib\graph.js:146
      op.finish();
         ^

Error
    at Error (native)
    at Graph.createOperationFromBuilder (S:\Electron\tensorflow-nodejs\lib\graph.js:146:10)
    at Object.<anonymous> (S:\Electron\tensf2-test\index.js:10:26)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.runMain (module.js:604:10)
    at run (bootstrap_node.js:389:7)

Another question: is it possible to use Tensorflow Object Detection API with this library?
object_detection_tutorial.ipynb

System:
Windows 10 64bit
Visual C++ Build Tools 2015

Used package for windows:
libtensorflow-cpu-windows-x86_64-1.4.0-rc0.zip

PMR: attempting to use uninitialized value Variable

When running a model ported by PMR(Predefined Model Running), got the error stack:

Error: Attempting to use uninitialized value Variable
	 [[Node: Variable/read = Identity[T=DT_FLOAT, _class=["loc:@Variable"], _device="/job:localhost/replica:0/task:0/cpu:0"](Variable)]]
    at Session.sessionRun [as run] (/Users/yorkie/workspace/tensorflow/tensorflow/nodejs/client/lib/session.js:39:26)
    at dataset.load.then (/Users/yorkie/workspace/tensorflow/tensorflow/nodejs/mnistup/index.js:21:23)
    at <anonymous>

Adding sess.run(tf.global_variables_initializer()) still not working for this case, I guess this is because the session is different, so we must implement global_variables_initializer in JavaScript again.

PMR: initial values for parameters/variables

We usually are using variables with initial values in Python's graph script like this:

W = tf.Variable(tf.zeros([784, 10]), name="var_W")
b = tf.Variable(tf.zeros([10]), name="var_b")

However we can't get these values from GraphDef binary files, and Python saves these values in its runtime for session, so when we use this predefined model, we would meet problems with these variable.

Currently I don't have no idea to how to get these initial values from Python to our Node.js runtime :(

npm prebuild issue

We are putting the prebuilt so library from OSX before publishing to NPM, this causes a corruption on linux.

Let's remove this prebuilt and downloading on fly :)

Could not locate the bindings file

I'm running in WIndows 10, but show me this error below

Error: Could not locate the bindings file. Tried:
→ \node_modules\tensorflow2\build\tensorflow.node
→ \node_modules\tensorflow2\build\Debug\tensorflow.node
→ \node_modules\tensorflow2\build\Release\tensorflow.node
→ \node_modules\tensorflow2\out\Debug\tensorflow.node
→ \node_modules\tensorflow2\Debug\tensorflow.node
→ \node_modules\tensorflow2\out\Release\tensorflow.node
→ \node_modules\tensorflow2\Release\tensorflow.node
→ \node_modules\tensorflow2\build\default\tensorflow.node
→ \node_modules\tensorflow2\compiled\8.6.0\win32\x64\tensorflow.node

Any idea to solve my problem?

Import all ops by TF_GetAllOpList

TensorFlow has many ops, and the list is not static, so we recommend generating the functions for adding ops to a graph instead of writing them by individually by hand (though writing a few by hand is a good way to figure out what the generator should generate). The information needed to generate a function is contained in an OpDef protocol message.

TensorFlow in other languages

implementation status

Providing TensorFlow functionality in a programming language can be broken down into broad categories:

  • Run a predefined graph
  • Graph construction
  • Gradients (AKA automatic differentiation)
  • Functions
  • Control Flow
  • Neural Network library

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.