karpathy / convnetjs

Deep Learning in Javascript. Train Convolutional Neural Networks (or ordinary ones) in your browser.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ReferenceError: deepqlearn is not defined

kengz opened this issue · comments

Followed your example and suddenly it cannot find deepqlearn. I'm doing this in Node.js.

var convnetjs = require('convnetjs');
// the normal examples with layer_defs etc. worked, but not this:
var brain = new deepqlearn.Brain(3, 2); // 3 inputs, 2 possible outputs (0,1)
var state = [Math.random(), Math.random(), Math.random()];
for(var k=0;k<10000;k++) {
    var action = brain.forward(state); // returns index of chosen action
    var reward = action === 0 ? 1.0 : 0.0;
    brain.backward(reward); // <-- learning magic happens here
    state[Math.floor(Math.random()*3)] += Math.random()*2-0.5;
}
brain.epsilon_test_time = 0.0; // don't make any more random choices
brain.learning = false;
// get an optimal action from the learned policy
var action = brain.forward(array_with_num_inputs_numbers);

Seems like it can't find deepqlearn. Saw under node_modules/convnet/deepqlearn.js there's an export statement; but your package.json specifies convnet.js as the main.

You have to require deepqlearn by itself and separately, I think. Does that work? It's not part of the library as default, it's only provided on a side because I didn't want to bloat the library.

Yes I can do this

var deepqlearn = require(__dirname+'/../node_modules/convnetjs/build/deepqlearn.js')

but then the deepqlearn wouldn't recognize its neighbor in node_modules/convnetjs

/Users/kengz/Google Drive/Quiver/node_modules/convnetjs/build/deepqlearn.js:98
    this.value_net = new convnetjs.Net();
                         ^
ReferenceError: convnetjs is not defined
    at Object.Brain (/Users/kengz/Google Drive/Quiver/node_modules/convnetjs/build/deepqlearn.js:98:26)

Crap. I don't have experience with nodejs, so I didn't really foresee these issues and dependency problems. I'm not sure I fully understand why convnetjs is not defined, when you've already required it first, shouldn't the variable exist? Not sure how to fix this in cleanest way

Yeah node.js has a slightly different way of importing dependencies.

A quick and dirty solution is to simply go into node_modules/convnetjs/build/deepqlearn.js manually and add these two lines at the top:

// deepqlearn.js
var convnetjs = require(__dirname+'/convnet.js');
var cnnutil = require(__dirname+'/util.js');

@kengz can convnetjs work good in node js?

@SimplyY If by that you mean if it can run like an efficient program on Java/C++, then yes. Nodejs is a complete and performant language like Python. I just wanted to get rid of the browser dependency here and use convnet like a programming library.