namenu / tfjs-cljs

A ClojureScript wrapper library for TensorFlow.js
Eclipse Public License 1.0
10 stars 1 forks source link

Uncaught ReferenceError: tf is not defined #2

Closed jamesnyika closed 6 years ago

jamesnyika commented 6 years ago

Not sure if there is a bug in your code but just having the library in my project.clj as

[namenu/tfjs-cljs "0.1.0-SNAPSHOT"]

and an import

(ns app.my.namespace
(:require [tfjs-cljs.core :as t :refer-macros [defvar with-tidy]]
            [tfjs-cljs.train :as train]))

i get the error above. I am actually trying to use this in a react-native app so there is not much of a message

ExceptionsManager.js:73 Error: Uncaught ReferenceError: tf is not defined
    at syncImportScripts (figwheel-bridge.js:133)
    at importJs (figwheel-bridge.js:150)
    at goog.writeScriptTag_ (figwheel-bridge.js:246)
    at Object.goog.importScript_ (base.js:951)
    at Object.goog.writeScripts_ (base.js:1394)
    at goog.require (base.js:706)
    at figwheel-bridge.js:217
    at syncImportScripts (figwheel-bridge.js:136)
    at importJs (figwheel-bridge.js:150)
    at figwheel-bridge.js:212
jamesnyika commented 6 years ago

Is there a missing dependency you did not mention or is there something I need to set up prior ?

namenu commented 6 years ago

Thanks for your interest.

Just yesterday I had published TensorFlow.js into cljsjs. https://github.com/cljsjs/packages/pull/1637

And by this commit, tfjs-cljs is now dependent to cljsjs.tfjs. https://github.com/namenu/tfjs-cljs/commit/0164b2c4778158ea00c40a5e1a3c4dc64c0d39e6#diff-0fff143854a4f5c0469a3819b978a483

I've deployed current master to clojars, so could you try again with your artifact updated?

Please let me know if tf isn't resolved yet.

jamesnyika commented 6 years ago

Hi @namenu

Unless I am doing something wrong - i still have the error. I think it is on line 114 of core.cljs - the reference to tf.Tensor seems to be the problem. Below is what I am doing and in the message that follows I actually showed that your example code too is broken

Here is my example...

Project clj

...

 :dependencies [
                           [org.clojure/clojure "1.9.0"]
                           [org.clojure/clojurescript "1.10.238"]
                           [org.clojure/core.async "0.4.474"]
                           [com.andrewmcveigh/cljs-time "0.5.2"]
                           [com.lucasbradstreet/cljs-uuid-utils "1.0.2"]
                           [io.replikativ/hasch "0.3.5"]
                           [com.taoensso/timbre "4.10.0"]
                           [binaryage/oops "0.6.2"]
                           [com.rpl/specter "1.1.1" :exclusions [org.clojure/clojure org.clojure/clojurescript]]
                           [reagent "0.7.0" :exclusions [cljsjs/react cljsjs/react-dom cljsjs/react-dom-server cljsjs/create-react-class]]
                           [re-frame "0.10.5"]
                           [posh "0.5.5"]  ;; DO NOT UPGRADE> NULL exception in v5.6 beware
                           [datascript "0.16.5"]
                           [org.clojure/core.async "0.4.474"]
                           [org.clojure/core.match "0.3.0-alpha5"]
                           [tongue "0.2.4"] ;; provides translation for i18n purposes
                           [cljs-react-navigation "0.1.1"]
                           [react-native-externs "0.1.0"]
                           [re-frisk-remote "0.5.5"] ;; need this for viewing contents of the app db

                           ;;Prediction
                           [namenu/tfjs-cljs "0.1.0-SNAPSHOT"]
                           ;; this dependency is already part of the library above. not including it twice
                           ;;[cljsjs/tfjs "0.11.6"]

                           ;;checkouts
                           ;; our global resource directory
                           [gigup-resources "1.0.0"]
            ]

Usage


(ns prediction.engine
  (:require [tfjs-cljs.core :as t :refer-macros [defvar with-tidy]]
            [tfjs-cljs.train :as train]))

(defonce vars
         (atom {:a (t/variable (t/scalar (Math.random)))
                :b (t/variable (t/scalar (Math.random)))
                :c (t/variable (t/scalar (Math.random)))
                :d (t/variable (t/scalar (Math.random)))}))

;; number of training runs for a model
(def num-iterations 75)

;; stochastic gradient descent learning rate - how quickly will it find a minima
(def learning-rate 0.2)

;; the optimizer
(defonce optimizer (train/sgd learning-rate))

;; Prediction function used in training. We have started with an initial suggestion model.
;; This will repeatedly be used to predict values with weights a,b,c and d being changed
;; as the model is trained.
(defn predict [x]
  (with-tidy
    (let [ax3 (t/mul (:a @vars) (t/pow x 3))
          bx2 (t/mul (:b @vars) (t/pow x 2))
          cx (t/mul (:c @vars) x)
          d (:d @vars)]
      (-> ax3
          (t/add bx2)
          (t/add cx)
          (t/add d)))))

.....

And then i use it in my core.cljs file

;;namespace require
(ns giguptal.core
  (:require [reagent.core :as r :refer [atom]]
            [re-frame.core :refer [subscribe dispatch dispatch-sync]]
            [cljs.core.async :refer [go timeout put! chan <! >!] :as async]
            [prediction.engine :as pe]
     ))

...

;;use the prediction engine here
(defn init []

  (let [true-coefficients [-0.8 -0.2 0.9 0.5]
        gen-data (pe/generate-data 100 true-coefficients)
        fitted  (pe/fit gen-data)
        ]

    (util/log ":::PE::: - Generated data : " gen-data)
    (util/log ":::PE::: - fitted  " fitted)

    ;; predict something
    (util/log ":::PE::: Predicting y for x = 0.5 " (pe/predict 0.5))
    )

....

But I still see the exception in my console

Error: Uncaught ReferenceError: tf is not defined
    at syncImportScripts (figwheel-bridge.js:133)
    at importJs (figwheel-bridge.js:150)
    at goog.writeScriptTag_ (figwheel-bridge.js:246)
    at Object.goog.importScript_ (base.js:951)
    at Object.goog.writeScripts_ (base.js:1394)
    at goog.require (base.js:706)
    at figwheel-bridge.js:217
    at syncImportScripts (figwheel-bridge.js:136)
    at importJs (figwheel-bridge.js:150)
    at figwheel-bridge.js:212
...
jamesnyika commented 6 years ago

Second Example - trying to run example code you provided: I deleted your examples.js file and recompiled it. Compile was successful as shown below is my output

➜  tfjs-cljs-master cd examples
➜  examples ls
project.clj resources   src
➜  examples lein cljsbuild
Retrieving lein-figwheel/lein-figwheel/0.5.16/lein-figwheel-0.5.16.pom from clojars
Retrieving lein-figwheel/lein-figwheel/0.5.16/lein-figwheel-0.5.16.jar from clojars
Run the cljsbuild plugin.

Subtasks available:
once          Compile the ClojureScript project once.
auto          Automatically recompile when files are modified.
test          Run ClojureScript tests.
repl-listen   Run a REPL that will listen for incoming connections.
repl-launch   Run a REPL and launch a custom command to connect to it.
repl-rhino    Run a Rhino-based REPL.
sample        Display a sample project.clj.

Run `lein help cljsbuild $SUBTASK` for subtask details.

Arguments: ([once auto test repl-listen repl-launch repl-rhino sample])
➜  examples lein once
'once' is not a task. See 'lein help'.

Did you mean this?
         new
         vcs
➜  examples lein cljsbuild once
Compiling ClojureScript...
Retrieving cljsbuild/cljsbuild/1.1.7/cljsbuild-1.1.7.pom from clojars
Retrieving cljsjs/p5/0.5.8-4/p5-0.5.8-4.pom from clojars
Retrieving figwheel-sidecar/figwheel-sidecar/0.5.16/figwheel-sidecar-0.5.16.pom from clojars
Retrieving http-kit/http-kit/2.3.0/http-kit-2.3.0.pom from clojars
Retrieving ring-cors/ring-cors/0.1.12/ring-cors-0.1.12.pom from clojars
Retrieving co/deps/ring-etag-middleware/0.2.0/ring-etag-middleware-0.2.0.pom from clojars
Retrieving figwheel/figwheel/0.5.16/figwheel-0.5.16.pom from clojars
Retrieving cider/piggieback/0.3.1/piggieback-0.3.1.pom from clojars
Retrieving cljsjs/p5/0.5.8-4/p5-0.5.8-4.jar from clojars
Retrieving ring-cors/ring-cors/0.1.12/ring-cors-0.1.12.jar from clojars
Retrieving http-kit/http-kit/2.3.0/http-kit-2.3.0.jar from clojars
Retrieving cljsbuild/cljsbuild/1.1.7/cljsbuild-1.1.7.jar from clojars
Retrieving figwheel-sidecar/figwheel-sidecar/0.5.16/figwheel-sidecar-0.5.16.jar from clojars
Retrieving co/deps/ring-etag-middleware/0.2.0/ring-etag-middleware-0.2.0.jar from clojars
Retrieving cider/piggieback/0.3.1/piggieback-0.3.1.jar from clojars
Retrieving figwheel/figwheel/0.5.16/figwheel-0.5.16.jar from clojars
Compiling ["resources/public/js/compiled/examples.js"] from ["src"]...
Successfully compiled ["resources/public/js/compiled/examples.js"] in 7.852 seconds.
➜  examples

When I open the index.html file though I no longer see the polynomial curve being fitted like I would see using the examples.js that comes in the repo. So there is something missing when I run the compile of your example... Here is the error followed by a screenshot

Error message

core.cljs?rel=1528752786850:113 Uncaught ReferenceError: tf is not defined
    at core.cljs?rel=1528752786850:113
core.cljs?rel=1528752786850:19 Uncaught ReferenceError: tf is not defined
    at Function.tfjs_cljs.core.scalar.cljs$core$IFn$_invoke$arity$1 (core.cljs?rel=1528752786850:19)
    at tfjs_cljs$core$scalar (core.cljs?rel=1528752786850:16)
    at polynomial_regression_core.cljs?rel=1528752787003:7
util.cljs?rel=1528752791260:136 CLJS DevTools: some custom formatters were not rendered.

Screen shot

screen shot 2018-06-11 at 5 34 21 pm

Problematic line appears to be on line 114 of core.cljs :

;; it does not like this tf.Tensor call.. not sure why 
;; should it be tf.tensor in lower case T or not ? I am not sure. ....
...

(defmethod pow js/tf.Tensor [base exp]
  (js/tf.pow base exp))   
namenu commented 6 years ago

Line 114 you mentioned is to support both tf/scalar and Number for exp argument. Uppercase means Tensor class.

e.g.

(t/pow x (t/scalar 3))
(t/pow x 2) 

I couldn't reproduce the error even after doing lein cleanup then removing examples.js.

What ClojureScript version do you use? (It's only tested on 1.10.238) Are your lein build use tfjs-cljs-0.1.0-20180611.052017-7 as it's dependency?

jamesnyika commented 6 years ago

I am on [org.clojure/clojurescript "1.10.238"]

Maybe the dependency I am using is the problem as you mentioned. I will try that...

jamesnyika commented 6 years ago

Ok. Using this dependency worked 👍

[namenu/tfjs-cljs "0.1.0-20180611.052017-7"]

It still does not work for me because your library assumes use on the browser and I think I need to switch over to using it for node.js. However, this issue is fixed.

namenu commented 6 years ago

Good! Thanks for spending your time on this highly experimental project. 😊

jamesnyika commented 6 years ago

Question for you: have you run this code on Node.js ?

namenu commented 6 years ago

No. According to the document, example code should work just with 'cpu' backend but it didn't. I suppose we need extra binding @tensorflow/tfjs-node to run under Node.js to workaround this.

Besides, tfjs-node is still unstable so I'm sticking with browser support only for now.