Skip to content

How to correctly load model matrices into a learner (e.g., a Random Forest)? #125

Description

@DanielTakeshi

BIDMach version: 1037ae7 (August 12)
BIDMat version: 1383cb4ccf3933a8175073b8eab9819be7e252bf (August 12)
OS: Linux (it's on "stout")

Here's the problem setup. I have run Random Forests on some training data. At the end of my script, I call the model's save method to save the model matrices:

// 'a' and 'c' are the training data and labels, respectively
val (nn, opts) = RandomForest.learner(a, c);

// Set a bunch of options, such as opts.seed = 0 to set a random seed
opts.batchSize = 1000
opts.depth = 20
opts.gain = 0.001f
opts.ntrees = 10
opts.nsamps = 24
opts.nnodes = 2500000
opts.nbits = 16
opts.ncats = 100;
opts.regression = true;
opts.seed = 0
opts.useGPU = true;

nn.train
nn.model.save("seed_0/")

This creates four files since RFs have four model matrices (ctrees.fmat.lz4, ftrees.imat.lz4, itrees.imat.lz4, vtrees.imat.lz4). Now, in a separate script, I want to create a new Random Forests that will load in these model matrices so that it doesn't have to train. Here's what an example script might look like, with file names removed for privacy:

// Test data
val ta = loadFMat("...")
val tc = loadFMat("...")

// Train data
val a = loadFMat("...")
val c = loadFMat("...")

// Establish the Random Forest with same parameters as before.
val (nn, opts) = RandomForest.learner(a, c); 
opts.batchSize = 1000
opts.depth = 20
opts.gain = 0.001f
opts.ntrees = 10
opts.nsamps = 24
opts.nnodes = 2500000
opts.nbits = 16
opts.ncats = 100;
opts.regression = true;
opts.useGPU = true;

// IMPORTANT, I am trying to load the model matrices from the correct directory.
// My main concern, is this command located in the correct spot? Am I missing
// anything I need to call in addition to this?
nn.model.load("seed_0/")

// This prediction does not work.
val model = nn.model.asInstanceOf[RandomForest]
val (mm, mopts) = RandomForest.predictor(model, ta);
mopts.batchSize = 1000
mm.predict

I have the training data there even though I don't think it's needed. I have it there because I am trying to keep everything consistent with the original script that ran training. I'm assuming that if the Random Forest got trained with tree depth 20, then here, we should also have a tree depth of 20 if we're going to be loading the model matrices, and soon.

Unfortunately, running the above (with the appropriate data, but I think any data will do) I get:

model: BIDMach.models.RandomForest = BIDMach.models.RandomForest@234d5408
mm: BIDMach.Learner = Learner(BIDMach.datasources.MatSource@1be2bc0,BIDMach.models.RandomForest@234d5408,null,null,BIDMach.datasinks.MatSink@6c2a4b24,BIDMach.models.RandomForest$PredOpts@4cab5ff6)
mopts: BIDMach.models.RandomForest.PredOpts = BIDMach.models.RandomForest$PredOpts@4cab5ff6
mopts.batchSize: Int = 1000
java.lang.NullPointerException
  at BIDMach.models.RandomForest.init(RandomForest.scala:247)
  at BIDMach.Learner.predict(Learner.scala:199)
  ... 54 elided

This error happens in the init method, implying that the Random Forests have to be initialized somehow. This happens automatically when you call the train method, but I don't know how to get it initialized without calling train. Are there some examples of scripts that do that here? I couldn't find any by searching. The Random Forest's 'load' method looks like it "returns" a Random Forest model, but I cannot simply do:

val model = nn.model.load("seed_0/")

Do you have some advice? I'm currently working through this issue so hopefully I can find how to do it, but even if I do, it would be great to have confirmation that I'm doing the steps the way it's supposed to work.

Final (somewhat unrelated) comment: the Random Forest model matrices all have dimension (opts.nnodes, opts.ntrees). Therefore, if we want to combine multiple Random Forest trained trees together for a testing set, we have to horizontally concatenate the matrices (not vertically) to make more columns. The Random Forest code doesn't seem to have a method for that but I can do that offline myself.

-Daniel

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions