karpathy / convnetjs

Deep Learning in Javascript. Train Convolutional Neural Networks (or ordinary ones) in your browser.
MIT License
10.8k stars 2.04k forks source link

Allow to work in non-browser env #85

Open cpansprout opened 7 years ago

cpansprout commented 7 years ago

‘window’ is specific to browser environments. ‘this’ (the global object is the default ‘this’ value) allows it to work in non-browser environments other than node.

cpansprout commented 7 years ago

Oh, wait. That doesn’t work, nor does it make any sense.

The "use strict" prevents ‘this’ from returning the global object. But in any case, you don’t even need this, since you are already defining a convnetjs variable. (The combined file begins with ‘var convnetjs’.)

So I suspect that line can simply be deleted, but I am not certain.

cpansprout commented 7 years ago

I am not using a browser. Nor am I using Node. I am embedding a JavaScript interpreter directly in my application. The JS environment contains only objects defined by the ECMAScript specification. This means I get an error when trying to run convnet.js, since it is trying to access nonexistent variables.

(I happen to be using Duktape, but I could have used SpiderMokney or JE and the same problem would have occurred.)

In a browser environment, BTW, the window is the global object, which happens to have a window property referring to itself (so, yes, you can say window.window.window.window.convnetjs).

sacdallago commented 7 years ago

Thanks @cpansprout , I understand now. I don't know how to help you out there / how to debug as I've never run JS in such an environment. If you get around solving it, it'd be cool to make to look into the solution

cpansprout commented 7 years ago

I have solved it locally by simply commenting out the

window.convnetjs = lib;

line, which is actually unnecessary, as convnetjs has already been declared as a global variable. If you change that line to alert(window.convnetjs == lib) then you will get an alert box saying ‘true’.

I don’t know how you update an existing pull request.

sacdallago commented 7 years ago

@cpansprout you can do that by pulling your fork of the repo and checking out the patch-1 branch (this). Then you modify that, commit, push, and you should see the new commit appear here.

cpansprout commented 7 years ago

Thank you. That worked.