Search in sources :

Example 1 with MetaItem

use of ml.shifu.shifu.container.meta.MetaItem in project shifu by ShifuML.

the class GridSearch method convertItemValue.

private Object convertItemValue(Map<String, MetaItem> metaWarehouse, String itemKey, String itemValueStr) throws ShifuException {
    MetaItem itemMeta = metaWarehouse.get(getItemKeyInMeta(itemKey));
    if (itemMeta == null) {
        throw new ShifuException(ShifuErrorCode.ERROR_GRID_SEARCH_FILE_CONFIG, "Train param name not recognized: " + itemKey);
    }
    itemValueStr = itemValueStr.trim();
    if (itemMeta.getType().equals("text")) {
        return itemValueStr;
    } else if (itemMeta.getType().equals("integer") || itemMeta.getType().equals("int")) {
        try {
            return Integer.parseInt(itemValueStr);
        } catch (NumberFormatException e) {
            String message = String.format("Train param %s should be integer type, actual value got is %s", itemKey, itemValueStr);
            LOG.error(message);
            throw new ShifuException(ShifuErrorCode.ERROR_GRID_SEARCH_FILE_CONFIG, e, message);
        }
    } else if (itemMeta.getType().equals("number")) {
        try {
            return Double.parseDouble(itemValueStr);
        } catch (NumberFormatException e) {
            String message = String.format("Train param %s should be number type, actual value got is %s", itemKey, itemValueStr);
            LOG.error(message);
            throw new ShifuException(ShifuErrorCode.ERROR_GRID_SEARCH_FILE_CONFIG, e, message);
        }
    } else if (itemMeta.getType().equals("float")) {
        try {
            System.out.println("create float value for " + itemValueStr);
            return Float.parseFloat(itemValueStr);
        } catch (NumberFormatException e) {
            String message = String.format("Train param %s should be number type, actual value got is %s", itemKey, itemValueStr);
            LOG.error(message);
            throw new ShifuException(ShifuErrorCode.ERROR_GRID_SEARCH_FILE_CONFIG, e, message);
        }
    } else if (itemMeta.getType().equals("boolean")) {
        return itemValueStr.equalsIgnoreCase("true");
    } else if (itemMeta.getType().equals("list")) {
        if (itemKey.equals("NumHiddenNodes") && itemMeta.getElementType().equals("number") && itemValueStr.matches("\\[[0-9\\+ ,]+\\]") && itemValueStr.length() > 2) {
            List<Integer> itemValue = new ArrayList<Integer>();
            itemValueStr = itemValueStr.substring(1, itemValueStr.length() - 1).trim();
            String[] splits = itemValueStr.split(",");
            try {
                for (String valueSplit : splits) {
                    itemValue.add(Integer.parseInt(valueSplit.trim()));
                }
            } catch (NumberFormatException e) {
                String message = String.format("Train param %s should be integer type, actual value got is %s", itemKey, itemValueStr);
                LOG.error(message);
                throw new ShifuException(ShifuErrorCode.ERROR_GRID_SEARCH_FILE_CONFIG, e, message);
            }
            return itemValue;
        } else if (itemKey.equals("ActivationFunc") && itemMeta.getElementType().equals("text") && itemValueStr.matches("\\[[a-zA-Z0-9 ,]+\\]") && itemValueStr.length() > 2) {
            List<String> itemValue = new ArrayList<String>();
            itemValueStr = itemValueStr.substring(1, itemValueStr.length() - 1).trim();
            String[] splits = itemValueStr.split(",");
            for (String valueSplit : splits) {
                itemValue.add(valueSplit.trim());
            }
            return itemValue;
        }
    }
    throw new ShifuException(ShifuErrorCode.ERROR_GRID_SEARCH_FILE_CONFIG, "Train param and value not recognized: " + itemKey + ":" + itemValueStr);
}
Also used : MetaItem(ml.shifu.shifu.container.meta.MetaItem) ShifuException(ml.shifu.shifu.exception.ShifuException)

Example 2 with MetaItem

use of ml.shifu.shifu.container.meta.MetaItem in project shifu by ShifuML.

the class ItemMetaGroupTest method testOutput.

@Test
public void testOutput() throws JsonGenerationException, JsonMappingException, IOException {
    List<MetaGroup> groupList = new ArrayList<MetaGroup>();
    MetaGroup itemGrpA = new MetaGroup();
    itemGrpA.setGroup("basic");
    List<MetaItem> metaList = new ArrayList<MetaItem>();
    MetaItem meta = new MetaItem();
    meta.setName("author");
    meta.setType("text");
    meta.setDirective("input");
    meta.setMinLength(1);
    metaList.add(meta);
    itemGrpA.setMetaList(metaList);
    groupList.add(itemGrpA);
    MetaGroup itemGrpB = new MetaGroup();
    itemGrpB.setGroup("sourceData");
    List<MetaItem> metaListB = new ArrayList<MetaItem>();
    MetaItem metaB = new MetaItem();
    metaB.setName("dataPath");
    metaB.setType("text");
    metaB.setDirective("input");
    metaB.setMinLength(1);
    metaListB.add(metaB);
    itemGrpB.setMetaList(metaListB);
    groupList.add(itemGrpB);
    File file = new File("test-meta.json");
    jsonMapper.writerWithDefaultPrettyPrinter().writeValue(file, groupList);
    MetaGroup[] ga = jsonMapper.readValue(file, MetaGroup[].class);
    Assert.assertEquals(groupList.size(), ga.length);
    file.deleteOnExit();
}
Also used : MetaItem(ml.shifu.shifu.container.meta.MetaItem) ArrayList(java.util.ArrayList) MetaGroup(ml.shifu.shifu.container.meta.MetaGroup) File(java.io.File) Test(org.testng.annotations.Test)

Example 3 with MetaItem

use of ml.shifu.shifu.container.meta.MetaItem in project shifu by ShifuML.

the class ItemMetaGroupTest method testCloneMeta.

@Test
public void testCloneMeta() {
    MetaGroup group = new MetaGroup();
    MetaGroup cloneObj = group.clone();
    Assert.assertNull(cloneObj.getGroup());
    group.setGroup("testGroup");
    List<MetaItem> itemList = new ArrayList<MetaItem>();
    MetaItem meta = new MetaItem();
    itemList.add(meta);
    itemList.add(meta.clone());
    itemList.add(meta.clone());
    group.setMetaList(itemList);
    cloneObj = group.clone();
    Assert.assertEquals("testGroup", cloneObj.getGroup());
    Assert.assertEquals(3, cloneObj.getMetaList().size());
}
Also used : MetaItem(ml.shifu.shifu.container.meta.MetaItem) ArrayList(java.util.ArrayList) MetaGroup(ml.shifu.shifu.container.meta.MetaGroup) Test(org.testng.annotations.Test)

Aggregations

MetaItem (ml.shifu.shifu.container.meta.MetaItem)3 ArrayList (java.util.ArrayList)2 MetaGroup (ml.shifu.shifu.container.meta.MetaGroup)2 Test (org.testng.annotations.Test)2 File (java.io.File)1 ShifuException (ml.shifu.shifu.exception.ShifuException)1