How to use tensorflow.js for writing hello world without any server

544 views
Skip to first unread message

tensorflow.r...@gmail.com

unread,
Apr 4, 2018, 1:42:55 PM4/4/18
to TensorFlow.js Discussion
I am trying to get familiar with tensorflow.js.
Hence I want to try it with minimal setup possible. So opted for importing the tensorflow.js thorugh script src pointed to the url as follows
var imported = document.createElement('script');
imported.type = 'module';
document.head.appendChild(imported);
import * as tf from '@tensorflow/tfjs';


in test.js file and using it in test.html file. However I get the following error 
Uncaught SyntaxError: Unexpected token *


Is it possible to use tensorflow.js without server.

Thanks.

zaid Alyafeai

unread,
Apr 4, 2018, 1:58:41 PM4/4/18
to TensorFlow.js Discussion
Just install apache server and make sure you have nodejs version 8. I have tried without a server and it didnt work. You can also compile the examples offered in the GitHub repository. For that you dont need to setup apache server.

tensorflow.r...@gmail.com

unread,
Apr 4, 2018, 2:02:26 PM4/4/18
to TensorFlow.js Discussion
Thanks for the reply. So that means I must need server if I want to practice my examples except the ones that are already hosted in google cloud given in the github examples.

Thanks for the clarification.

tensorflow.r...@gmail.com

unread,
Apr 4, 2018, 2:08:57 PM4/4/18
to TensorFlow.js Discussion
Btw, I saw your example application of tensorflow.js in sentiment analysis of imdb reviews. That was cool.. 


On Wednesday, April 4, 2018 at 11:28:41 PM UTC+5:30, zaid Alyafeai wrote:

yass...@google.com

unread,
Apr 5, 2018, 9:49:33 AM4/5/18
to TensorFlow.js Discussion
Hi, I think with your can change your snippet a bit to work, try:

var imported = document.createElement('script');
document.head.appendChild(imported);

imported.onload = function(){
  // Once the script has loaded tf should be available
  // the rest of your program would need to be in this function
  var aTensor = tf.tensor1d([1,2,3]);
};

// Start the loading process


However it might be easier to make an html page with a script tag that loads the library and then another script tag for your code. This will all work without a server, though it can be useful in the long run to have a local server to get data into your application.

tensorflow.r...@gmail.com

unread,
Apr 5, 2018, 1:33:42 PM4/5/18
to TensorFlow.js Discussion
Thanks a lot!!! This works now. You are awesome!!

zaid Alyafeai

unread,
Apr 5, 2018, 1:50:07 PM4/5/18
to TensorFlow.js Discussion
Didn't know it would work, sorry for that. BTW, the latest script is https://cdn.jsdelivr.net/npm/@tensorflow/tf...@0.7.0.  You can also use https://beta.observablehq.com/ to play a bit with it. I am preparing a Notebook there. 

tensorflow.r...@gmail.com

unread,
Apr 5, 2018, 2:05:57 PM4/5/18
to TensorFlow.js Discussion
Thanks. I am basically from python background with no fluency in Javascript. So please forgive my basic questions. I got interested in this tensorflow.js because of its unique ability to run it in the browser. Hence trying it out.

yass...@google.com

unread,
Apr 5, 2018, 2:56:00 PM4/5/18
to TensorFlow.js Discussion
You are most welcome! Glad to see some python folk getting excited about the browser!

zaid Alyafeai

unread,
Apr 6, 2018, 8:32:15 AM4/6/18
to TensorFlow.js Discussion
Javascript is not that difficult to learn. I prepared a notebook https://beta.observablehq.com/@zaidalyafeai/an-intractive-introduction-to-tensorflow-js. Play around with it and let me know if you have any difficulty. 

Jeremy Ellis

unread,
Apr 24, 2018, 2:26:07 AM4/24/18
to TensorFlow.js Discussion

As I have posted before, the following page is all for beginner to intermediate Javascript programmers wanting to learn about Machine and Deep Learning. I am making it as I learn.


I am just working on changing my Deeplearnjs examples and videos to Tensorflowjs. I hope to have a Kickstarter out soon.
Reply all
Reply to author
Forward
0 new messages