Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

importing tensorflow not generating simple js output. #149

Closed
kallols opened this issue Apr 8, 2018 · 6 comments
Closed

importing tensorflow not generating simple js output. #149

kallols opened this issue Apr 8, 2018 · 6 comments

Comments

@kallols
Copy link

kallols commented Apr 8, 2018

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.

@manrajgrover
Copy link
Contributor

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

@kallols
Copy link
Author

kallols commented Apr 8, 2018

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

<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@0.7.0"> </script>

and input type="button" onclick="go()" value="Display JS Array"
and <script type="text/javascript" src="index.js"></script>

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 ?

@tafsiri
Copy link
Contributor

tafsiri commented Apr 9, 2018

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

@kallols
Copy link
Author

kallols commented Apr 9, 2018

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

@tafsiri
Copy link
Contributor

tafsiri commented Apr 10, 2018

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

easadler pushed a commit to easadler/tfjs that referenced this issue Apr 12, 2018
)

* Add Concat1D/2D and add Array=specific random generators

* Merge remote-tracking branch 'origin/master' into rnn
@kallols
Copy link
Author

kallols commented Apr 12, 2018

@manrajgrover @tafsiri Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants