Search in sources :

Example 11 with Model

use of hex.Model in project h2o-3 by h2oai.

the class ModelsHandler method importModel.

public ModelsV3 importModel(int version, ModelImportV3 mimport) {
    ModelsV3 s = Schema.newInstance(ModelsV3.class);
    try {
        URI targetUri = FileUtils.getURI(mimport.dir);
        Persist p = H2O.getPM().getPersistForURI(targetUri);
        InputStream is = p.open(targetUri.toString());
        final AutoBuffer ab = new AutoBuffer(is);
        ab.sourceName = targetUri.toString();
        Model model = (Model) Keyed.readAll(ab);
        s.models = new ModelSchemaV3[] { (ModelSchemaV3) SchemaServer.schema(version, model).fillFromImpl(model) };
    } catch (FSIOException e) {
        throw new H2OIllegalArgumentException("dir", "importModel", mimport.dir);
    }
    return s;
}
Also used : Model(hex.Model) MojoModel(hex.genmodel.MojoModel) Persist(water.persist.Persist) URI(java.net.URI)

Example 12 with Model

use of hex.Model in project h2o-3 by h2oai.

the class ModelsHandler method exportModel.

public ModelExportV3 exportModel(int version, ModelExportV3 mexport) {
    Model model = getFromDKV("model_id", mexport.model_id.key());
    try {
        // Really file, not dir
        URI targetUri = FileUtils.getURI(mexport.dir);
        Persist p = H2O.getPM().getPersistForURI(targetUri);
        OutputStream os = p.create(targetUri.toString(), mexport.force);
        model.writeAll(new AutoBuffer(os, true)).close();
        // Send back
        mexport.dir = "file".equals(targetUri.getScheme()) ? new File(targetUri).getCanonicalPath() : targetUri.toString();
    } catch (IOException e) {
        throw new H2OIllegalArgumentException("dir", "exportModel", e);
    }
    return mexport;
}
Also used : Model(hex.Model) MojoModel(hex.genmodel.MojoModel) Persist(water.persist.Persist) URI(java.net.URI)

Example 13 with Model

use of hex.Model in project h2o-3 by h2oai.

the class ModelBuilderHandler method handle.

// Invoke the handler with parameters.  Can throw any exception the called handler can throw.
@Override
S handle(int version, Route route, Properties parms, String postBody) throws Exception {
    // Peek out the desired algo from the URL
    String[] ss = route._url.split("/");
    // {}/{3}/{ModelBuilders}/{gbm}/{parameters}
    String algoURLName = ss[3];
    // gbm -> GBM; deeplearning -> DeepLearning
    String algoName = ModelBuilder.algoName(algoURLName);
    String schemaDir = ModelBuilder.schemaDirectory(algoURLName);
    // Build a Model Schema and a ModelParameters Schema
    String schemaName = schemaDir + algoName + "V" + version;
    S schema = (S) TypeMap.newFreezable(schemaName);
    schema.init_meta();
    String parmName = schemaDir + algoName + "V" + version + "$" + algoName + "ParametersV" + version;
    P parmSchema = (P) TypeMap.newFreezable(parmName);
    schema.parameters = parmSchema;
    // Only here for train or validate-parms
    String handlerName = route._handler_method.getName();
    boolean doTrain = handlerName.equals("train");
    assert doTrain || handlerName.equals("validate_parameters");
    // User specified key, or make a default?
    String model_id = parms.getProperty("model_id");
    Key<Model> key = doTrain ? (model_id == null ? ModelBuilder.defaultKey(algoName) : Key.<Model>make(model_id)) : null;
    // Default Job for just this training
    Job job = doTrain ? new Job<>(key, ModelBuilder.javaName(algoURLName), algoName) : null;
    // ModelBuilder
    B builder = ModelBuilder.make(algoURLName, job, key);
    // Defaults for this builder into schema
    schema.parameters.fillFromImpl(builder._parms);
    // Overwrite with user parms
    schema.parameters.fillFromParms(parms);
    // Merged parms back over Model.Parameter object
    schema.parameters.fillImpl(builder._parms);
    // validate parameters
    builder.init(false);
    // Fill in the result Schema with the Job at least, plus any extra trainModel errors
    schema.fillFromImpl(builder);
    PojoUtils.copyProperties(schema.parameters, builder._parms, PojoUtils.FieldNaming.ORIGIN_HAS_UNDERSCORES, null, new String[] { "error_count", "messages" });
    schema.setHttpStatus(HttpResponseStatus.OK.getCode());
    if (doTrain)
        schema.job.fillFromImpl(builder.trainModel());
    return schema;
}
Also used : Model(hex.Model) Job(water.Job)

Example 14 with Model

use of hex.Model in project h2o-3 by h2oai.

the class ModelCacheManager method get.

public static <M extends Model, P extends Model.Parameters> M get(P parms) {
    Model[] models = Models.fetchAll();
    long checksum = parms.checksum();
    for (Model model : models) {
        if (model._parms != null && model._parms.checksum() == checksum)
            return (M) model;
    }
    return null;
}
Also used : Model(hex.Model)

Example 15 with Model

use of hex.Model in project h2o-3 by h2oai.

the class ModelsHandler method fetch.

/** Return a single model. */
// called through reflection by RequestServer
@SuppressWarnings("unused")
public ModelsV3 fetch(int version, ModelsV3 s) {
    Model model = getFromDKV("key", s.model_id.key());
    s.models = new ModelSchemaV3[1];
    s.models[0] = (ModelSchemaV3) SchemaServer.schema(version, model).fillFromImpl(model);
    if (s.find_compatible_frames) {
        // TODO: refactor fetchFrameCols so we don't need this Models object
        Models m = new Models();
        m.models = new Model[1];
        m.models[0] = model;
        m.find_compatible_frames = true;
        Frame[] compatible = Models.findCompatibleFrames(model, Frames.fetchAll(), m.fetchFrameCols());
        // TODO: FrameBaseV3
        s.compatible_frames = new FrameV3[compatible.length];
        ((ModelSchemaV3) s.models[0]).compatible_frames = new String[compatible.length];
        int i = 0;
        for (Frame f : compatible) {
            // TODO: FrameBaseV3
            s.compatible_frames[i] = new FrameV3(f).fillFromImpl(f);
            ((ModelSchemaV3) s.models[0]).compatible_frames[i] = f._key.toString();
            i++;
        }
    }
    return s;
}
Also used : Frame(water.fvec.Frame) Model(hex.Model) MojoModel(hex.genmodel.MojoModel)

Aggregations

Model (hex.Model)28 Frame (water.fvec.Frame)15 Grid (hex.grid.Grid)11 HashMap (java.util.HashMap)11 Test (org.junit.Test)11 MojoModel (hex.genmodel.MojoModel)8 Vec (water.fvec.Vec)6 URI (java.net.URI)4 ArrayList (java.util.ArrayList)4 Key (water.Key)4 Persist (water.persist.Persist)4 Random (java.util.Random)3 Set (java.util.Set)3 DataInfo (hex.DataInfo)2 ModelMojoWriter (hex.ModelMojoWriter)1 GLMModel (hex.glm.GLMModel)1 SharedTreeModel (hex.tree.SharedTreeModel)1 DRFModel (hex.tree.drf.DRFModel)1 GBMModel (hex.tree.gbm.GBMModel)1 HashSet (java.util.HashSet)1