Search in sources :

Example 6 with Model

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

the class ModelsHandler method exportMojo.

public ModelExportV3 exportMojo(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);
        ModelMojoWriter mojo = model.getMojo();
        mojo.writeTo(os);
        // 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) ModelMojoWriter(hex.ModelMojoWriter) Persist(water.persist.Persist) URI(java.net.URI)

Example 7 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 8 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 9 with Model

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

the class KMeansGridTest method testDuplicatesCarsGrid.

//@Ignore("PUBDEV-1643")
@Test
public void testDuplicatesCarsGrid() {
    Grid grid = null;
    Frame fr = null;
    try {
        fr = parse_test_file("smalldata/iris/iris_wheader.csv");
        fr.remove("class").remove();
        DKV.put(fr);
        // Setup hyperparameter search space
        HashMap<String, Object[]> hyperParms = new HashMap<>();
        hyperParms.put("_k", new Integer[] { 3, 3, 3 });
        hyperParms.put("_init", new KMeans.Initialization[] { KMeans.Initialization.Random, KMeans.Initialization.Random, KMeans.Initialization.Random });
        hyperParms.put("_seed", new Long[] { 123456789L, 123456789L, 123456789L });
        // Fire off a grid search
        KMeansModel.KMeansParameters params = new KMeansModel.KMeansParameters();
        params._train = fr._key;
        // Get the Grid for this modeling class and frame
        Job<Grid> gs = GridSearch.startGridSearch(null, params, hyperParms);
        grid = gs.get();
        // Check that duplicate model have not been constructed
        Model[] models = grid.getModels();
        assertTrue("Number of returned models has to be > 0", models.length > 0);
        // But all off them should be same
        Key<Model> modelKey = models[0]._key;
        for (Model m : models) {
            assertTrue("Number of constructed models has to be equal to 1", modelKey == m._key);
        }
    } finally {
        if (fr != null) {
            fr.remove();
        }
        if (grid != null) {
            grid.remove();
        }
    }
}
Also used : Frame(water.fvec.Frame) HashMap(java.util.HashMap) Grid(hex.grid.Grid) Model(hex.Model) Test(org.junit.Test)

Example 10 with Model

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

the class GLRMGridTest method testMultipleGridInvocation.

@Test
public void testMultipleGridInvocation() {
    Grid<GLRMModel.GLRMParameters> grid = null;
    Frame fr = null;
    try {
        fr = parse_test_file("smalldata/iris/iris_wheader.csv");
        // Hyper-space
        HashMap<String, Object[]> hyperParms = new HashMap<String, Object[]>() {

            {
                put("_k", new Integer[] { 2, 4 });
                // Search over this range of the init enum
                put("_transform", new DataInfo.TransformType[] { DataInfo.TransformType.NONE, DataInfo.TransformType.DEMEAN });
            }
        };
        // Name of used hyper parameters
        String[] hyperParamNames = hyperParms.keySet().toArray(new String[hyperParms.size()]);
        Arrays.sort(hyperParamNames);
        int hyperSpaceSize = ArrayUtils.crossProductSize(hyperParms);
        // Create default parameters
        GLRMModel.GLRMParameters params = new GLRMModel.GLRMParameters();
        params._train = fr._key;
        params._seed = 4224L;
        params._loss = GlrmLoss.Absolute;
        params._init = GlrmInitialization.SVD;
        //
        // Fire off a grid search multiple times with same key and make sure
        // that results are same
        //
        final int ITER_CNT = 2;
        Key<Model>[][] modelKeys = new Key[ITER_CNT][];
        Key<Grid> gridKey = Key.make("GLRM_grid_iris" + Key.rand());
        for (int i = 0; i < ITER_CNT; i++) {
            Job<Grid> gs = GridSearch.startGridSearch(gridKey, params, hyperParms);
            grid = (Grid<GLRMModel.GLRMParameters>) gs.get();
            modelKeys[i] = grid.getModelKeys();
            // Make sure number of produced models match size of specified hyper space
            Assert.assertEquals("Size of grid should match to size of hyper space", hyperSpaceSize, grid.getModelCount() + grid.getFailureCount());
            //
            // Make sure that names of used parameters match
            //
            String[] gridHyperNames = grid.getHyperNames();
            Arrays.sort(gridHyperNames);
            Assert.assertArrayEquals("Hyper parameters names should match!", hyperParamNames, gridHyperNames);
        }
        Assert.assertArrayEquals("The model keys should be same between two iterations!", modelKeys[0], modelKeys[1]);
    } finally {
        if (fr != null) {
            fr.remove();
        }
        if (grid != null) {
            grid.remove();
        }
    }
}
Also used : DataInfo(hex.DataInfo) Frame(water.fvec.Frame) HashMap(java.util.HashMap) Grid(hex.grid.Grid) Model(hex.Model) Key(water.Key) Test(org.junit.Test)

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