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