convnetjs icon indicating copy to clipboard operation
convnetjs copied to clipboard

ReferenceError: deepqlearn is not defined

Open kengz opened this issue 8 years ago • 6 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.

kengz avatar Aug 28 '15 13:08 kengz

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.

karpathy avatar Aug 28 '15 14:08 karpathy

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)

kengz avatar Aug 28 '15 14:08 kengz

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

karpathy avatar Aug 28 '15 14:08 karpathy

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 avatar Aug 28 '15 14:08 kengz

@kengz can convnetjs work good in node js?

SimplyY avatar Nov 02 '15 13:11 SimplyY

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

kengz avatar Nov 02 '15 16:11 kengz