Search in sources :

Example 16 with Model

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

the class ModelsHandler method fetchJavaCode.

public StreamingSchema fetchJavaCode(int version, ModelsV3 s) {
    final Model model = getFromDKV("key", s.model_id.key());
    final String filename = JCodeGen.toJavaId(s.model_id.key().toString()) + ".java";
    // Return stream writer for given model
    return new StreamingSchema(model.new JavaModelStreamWriter(s.preview), filename);
}
Also used : Model(hex.Model) MojoModel(hex.genmodel.MojoModel)

Example 17 with Model

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

the class ModelsHandler method delete.

/** Remove an unlocked model.  Fails if model is in-use. */
// called through reflection by RequestServer
@SuppressWarnings("unused")
public ModelsV3 delete(int version, ModelsV3 s) {
    Model model = getFromDKV("key", s.model_id.key());
    // lock & remove
    model.delete();
    return s;
}
Also used : Model(hex.Model) MojoModel(hex.genmodel.MojoModel)

Example 18 with Model

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

the class ModelsHandler method fetchMojo.

// called from the RequestServer through reflection
@SuppressWarnings("unused")
public StreamingSchema fetchMojo(int version, ModelsV3 s) {
    Model model = getFromDKV("key", s.model_id.key());
    String filename = JCodeGen.toJavaId(s.model_id.key().toString()) + ".zip";
    return new StreamingSchema(model.getMojo(), filename);
}
Also used : Model(hex.Model) MojoModel(hex.genmodel.MojoModel)

Example 19 with Model

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

the class ModelsHandler method exportModelDetails.

public ModelExportV3 exportModelDetails(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);
        //Make model schema before exporting
        ModelSchemaV3 modelSchema = (ModelSchemaV3) SchemaServer.schema(version, model).fillFromImpl(model);
        //Output model details to JSON
        OutputStream os = p.create(targetUri.toString(), mexport.force);
        os.write(modelSchema.writeJSON(new AutoBuffer()).buf());
        // Send back
        mexport.dir = "file".equals(targetUri.getScheme()) ? new File(targetUri).getCanonicalPath() : targetUri.toString();
    } catch (IOException e) {
        throw new H2OIllegalArgumentException("dir", "exportModelDetails", e);
    }
    return mexport;
}
Also used : Model(hex.Model) MojoModel(hex.genmodel.MojoModel) Persist(water.persist.Persist) URI(java.net.URI)

Example 20 with Model

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

the class GridSchemaV99 method fillFromImpl.

@Override
public GridSchemaV99 fillFromImpl(Grid grid) {
    Key<Model>[] gridModelKeys = grid.getModelKeys();
    // Return only keys which are referencing to existing objects in DKV
    // However, here is still implicit race, since we are sending
    // keys to client, but referenced models can be deleted in meantime
    // Hence, client has to be responsible for handling this situation
    // - call getModel and check for null model
    // pre-allocate
    List<Key<Model>> modelKeys = new ArrayList<>(gridModelKeys.length);
    for (Key k : gridModelKeys) {
        if (k != null && DKV.get(k) != null) {
            modelKeys.add(k);
        }
    }
    // Default sort order -- TODO: Outsource
    if (sort_by == null && modelKeys.size() > 0 && modelKeys.get(0) != null) {
        Model m = DKV.getGet(modelKeys.get(0));
        if (m != null && m.isSupervised()) {
            if (m._output.nclasses() > 1) {
                sort_by = "logloss";
                decreasing = false;
            } else {
                sort_by = "residual_deviance";
                decreasing = false;
            }
        }
    }
    // If not, show all possible metrics
    if (modelKeys.size() > 0 && sort_by != null) {
        Set<String> possibleMetrics = ModelMetrics.getAllowedMetrics(modelKeys.get(0));
        if (!possibleMetrics.contains(sort_by.toLowerCase())) {
            throw new H2OIllegalArgumentException("Invalid argument for sort_by specified. Must be one of: " + Arrays.toString(possibleMetrics.toArray(new String[0])));
        }
    }
    // Are we sorting by model metrics?
    if (null != sort_by && !sort_by.isEmpty()) {
        // sort the model keys
        modelKeys = ModelMetrics.sortModelsByMetric(sort_by, decreasing, modelKeys);
        // fill the metrics arrays
        training_metrics = new ModelMetricsBaseV3[modelKeys.size()];
        validation_metrics = new ModelMetricsBaseV3[modelKeys.size()];
        cross_validation_metrics = new ModelMetricsBaseV3[modelKeys.size()];
        cross_validation_metrics_summary = new TwoDimTableV3[modelKeys.size()];
        for (int i = 0; i < modelKeys.size(); i++) {
            Model m = DKV.getGet(modelKeys.get(i));
            if (m != null) {
                Model.Output o = m._output;
                if (null != o._training_metrics)
                    training_metrics[i] = (ModelMetricsBaseV3) SchemaServer.schema(3, o._training_metrics).fillFromImpl(o._training_metrics);
                if (null != o._validation_metrics)
                    validation_metrics[i] = (ModelMetricsBaseV3) SchemaServer.schema(3, o._validation_metrics).fillFromImpl(o._validation_metrics);
                if (null != o._cross_validation_metrics)
                    cross_validation_metrics[i] = (ModelMetricsBaseV3) SchemaServer.schema(3, o._cross_validation_metrics).fillFromImpl(o._cross_validation_metrics);
                if (o._cross_validation_metrics_summary != null)
                    cross_validation_metrics_summary[i] = new TwoDimTableV3(o._cross_validation_metrics_summary);
            }
        }
    }
    KeyV3.ModelKeyV3[] modelIds = new KeyV3.ModelKeyV3[modelKeys.size()];
    Key<Model>[] keys = new Key[modelKeys.size()];
    for (int i = 0; i < modelIds.length; i++) {
        modelIds[i] = new KeyV3.ModelKeyV3(modelKeys.get(i));
        keys[i] = modelIds[i].key();
    }
    grid_id = new KeyV3.GridKeyV3(grid._key);
    model_ids = modelIds;
    hyper_names = grid.getHyperNames();
    failed_params = toModelParametersSchema(grid.getFailedParameters());
    failure_details = grid.getFailureDetails();
    failure_stack_traces = grid.getFailureStackTraces();
    failed_raw_params = grid.getFailedRawParameters();
    TwoDimTable t = grid.createSummaryTable(keys, sort_by, decreasing);
    if (t != null)
        summary_table = new TwoDimTableV3().fillFromImpl(t);
    TwoDimTable h = grid.createScoringHistoryTable();
    if (h != null)
        scoring_history = new TwoDimTableV3().fillFromImpl(h);
    return this;
}
Also used : ArrayList(java.util.ArrayList) H2OIllegalArgumentException(water.exceptions.H2OIllegalArgumentException) TwoDimTable(water.util.TwoDimTable) Model(hex.Model) Key(water.Key)

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