Search in sources :

Example 26 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 27 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)

Example 28 with Model

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

the class FramesHandler method doFetch.

private FramesV3 doFetch(int version, FramesV3 s) {
    s.createAndFillImpl();
    // safe
    Frame frame = getFromDKV("key", s.frame_id.key());
    s.frames = new FrameV3[1];
    // TODO: Refactor with FrameBaseV3
    s.frames[0] = new FrameV3(frame, s.row_offset, s.row_count).fillFromImpl(frame, s.row_offset, s.row_count, s.column_offset, s.column_count);
    if (s.find_compatible_models) {
        Model[] compatible = Frames.findCompatibleModels(frame, Models.fetchAll());
        s.compatible_models = new ModelSchemaV3[compatible.length];
        ((FrameV3) s.frames[0]).compatible_models = new String[compatible.length];
        int i = 0;
        for (Model m : compatible) {
            s.compatible_models[i] = (ModelSchemaV3) SchemaServer.schema(version, m).fillFromImpl(m);
            ((FrameV3) s.frames[0]).compatible_models[i] = m._key.toString();
            i++;
        }
    }
    return s;
}
Also used : Frame(water.fvec.Frame) Model(hex.Model)

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