Tfjs: importing tensorflow not generating simple js output.

Created on 8 Apr 2018  路  6Comments  路  Source: tensorflow/tfjs

Hi,
When I am calling this tiny javascript code below in my html code:
function go(){
var array = new Array();
document.write("hello");
}
its giving correct ans, but when I am including tensorflow its not giving any output like below:

import * as tf from '@tensorflow/tfjs';
function go(){
var array = new Array();
document.write("hello");
}

please tell me why am I not getting any output and how to solve the issue.

Most helpful comment

Hi @kallols,

I see you are running this example in a browser.

You cannot call imports in a browser since browsers don't support such imports. Instead of loading tfjs as import * as tf from '@tensorflow/tfjs';, you need to import the library by either transpiling the node module and load it as a bundle using tools like webpack or load it using ready-to-use cdn mentioned here.

TL;DR
Remove import * as tf from '@tensorflow/tfjs'; from your JS code and load library using <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@latest"></script> in your HTML

All 6 comments

Hi @kallols,

I see you are running this example in a browser.

You cannot call imports in a browser since browsers don't support such imports. Instead of loading tfjs as import * as tf from '@tensorflow/tfjs';, you need to import the library by either transpiling the node module and load it as a bundle using tools like webpack or load it using ready-to-use cdn mentioned here.

TL;DR
Remove import * as tf from '@tensorflow/tfjs'; from your JS code and load library using <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@latest"></script> in your HTML

Thanks for your answer @ManrajGrover now i have changed my js code to
function go(){
a=tf.constant(7)
b=tf.constant(10)
c = tf.add(a,b)

simple_session = tf.Session()
value_of_c = simple_session.run(c)
simple_session.close()
    document.write(value_of_c);

}

and in html i have written

and input type="button" onclick="go()" value="Display JS Array"
and

still i am not getting any output. Could you please help me what to write in my js or html file because i couldn't instantiate tf if I need to do it then how ?

@kallols you might be mixing in js and python code. We don't have tf.constant or tf.Session. Here is my suggestion for getting a working setup going:

  1. Check out this repository https://github.com/tensorflow/tfjs-examples, you can use git or download a zip
  2. Open the 'getting_started' folder in that repository
  3. Open index.html in your browser and confirm that it works
  4. Now you can edit index.html to add your button and index.js to add your js code/function.

@ManrajGrover @tafsiri Thanks, This is working fine. But I have one more query.

I have created a pre-trained keras model and called the model inside the javascript. This model takes only array as input. So I need to convert the text coming as input to array format. I have a python function that does that using gensim package.
I want to import that function inside the javascript so I could get the array output. Could you please tell me how to import that python function in my javascript file.

@kallols You cannot do that as they are two different languages with two different runtimes. As @caisq mentioned in #156 you would need to either reimplement the function in javascript, or wrap your python function in a webserver that can respond to requests from the browser.

@ManrajGrover @tafsiri Thanks

Was this page helpful?
0 / 5 - 0 ratings