コード例 #1
0
ファイル: protonn_trainer_test.py プロジェクト: m-zara/ELL
def test():
    testing = Testing()
    # -dd auto -sw 1 -sb 1 -sz 1 -pd 10 -l 2 -mp 5 -v --evaluationFrequency 1 -plf L2

    args = ell.ProtoNNTrainerParameters()
    args.projectedDimension = 10
    args.numPrototypesPerLabel = 5
    args.numLabels = 2
    args.sparsityW = 1
    args.sparsityB = 1
    args.sparsityZ = 1
    args.gamma = -1
    args.lossFunction = ell.ProtoNNLossFunction.L2
    args.numInnerIterations = 1
    args.numFeatures = 0
    args.verbose = True

    trainer = ell.ProtoNNTrainer(args)

    dataset = ell.AutoSupervisedDataset()
    testFile = os.path.join(find_ell.get_ell_root(),
                            "examples/data/protonnTestData.txt")
    print("Loading: " + testFile)
    dataset.Load(testFile)

    total = dataset.NumExamples()
    features = dataset.NumFeatures()
    testing.ProcessTest("Proton dataset loaded",
                        testing.IsEqual(int(total), 200))

    trainer.SetDataset(dataset)

    numIterations = 20

    print("Training...")
    for i in range(numIterations):
        trainer.Update()

    predictor = trainer.GetPredictor()

    accuracy = get_accuracy(predictor, dataset, features)
    print("Accuracy %f" % (accuracy))
    testing.ProcessTest("Proton accuracy test",
                        testing.IsEqual(int(accuracy), 1))

    map = predictor.GetMap()
    map.Save("protonnTestData.ell")
    testing.ProcessTest(
        "Saving  protonnTestData.ell",
        testing.IsEqual(os.path.isfile("protonnTestData.ell"), True))

    if testing.DidTestFail():
        raise Exception("protonn_trainer_test failed")

    return 0
コード例 #2
0
ファイル: dataset_test.py プロジェクト: n-gineer/ELL
def test():
    testing = Testing()
    dataset = ell.data.AutoSupervisedDataset()
    dataset.Load(
        os.path.join(find_ell.get_ell_root(), "examples/data/testData.txt"))
    num = dataset.NumExamples()
    print("Number of Examples:", num)
    testing.ProcessTest("Dataset NumExamples test",
                        testing.IsEqual(int(num), 200))

    features = dataset.NumFeatures()
    print("Number of Features:", features)
    testing.ProcessTest("Dataset NumFeatures test",
                        testing.IsEqual(int(features), 21))

    for i in range(num):
        exampleTest(dataset.GetExample(i))

    testing.ProcessTest("Dataset eumeration test", True)

    return 0
コード例 #3
0
ファイル: vector_test.py プロジェクト: stoneyang/ELL
def test_double():
    testing = Testing()

    # empty vector
    e = ell.math.DoubleVector()
    np.testing.assert_equal(e.size(), 0)

    # vector from list
    l = [
        1.1,
        2.2,
        3.3,
        4.4,
    ]
    e = ell.math.DoubleVector(l)

    np.testing.assert_equal(list(e), l)

    # vector from numpy array
    a = np.array(range(10), dtype=float)
    e = ell.math.DoubleVector(a)

    np.testing.assert_equal(np.asarray(e), a)

    # conver to numpy using array
    b = np.array(e).ravel()
    np.testing.assert_equal(a, b)

    # copy_from numpy array
    e = ell.math.DoubleVector()
    e.copy_from(a)
    np.testing.assert_equal(np.asarray(e), a)

    # convert data types
    a = a.astype(np.float32)
    e = ell.math.DoubleVector(a)
    np.testing.assert_equal(np.asarray(e), a)

    # enumerating array
    for i in range(a.shape[0]):
        x = a[i]
        y = e[i]
        np.testing.assert_equal(x, y)

    # auto-ravel numpy arrays
    a = np.ones((10, 10), dtype=float)
    a *= range(10)
    e = ell.math.DoubleVector(a)
    np.testing.assert_equal(np.asarray(e), a.ravel())

    testing.ProcessTest("DoubleVector test", True)
コード例 #4
0
def test_float():
    testing = Testing()

    # empty vector
    e = ELL.FloatVector()
    np.testing.assert_equal(e.size(), 0)

    # vector from list of floats
    l = [1.1, 2.2, 3.3, 4.4]
    e = ELL.FloatVector(l)

    assert_compare_floats(e, l)

    # vector from numpy array
    a = np.array(range(10), dtype=np.float32)
    e = ELL.FloatVector(a)

    np.testing.assert_equal(np.asarray(e), a)

    # convert to numpy using array
    b = np.array(e).ravel()
    np.testing.assert_equal(a, b)

    # copy_from numpy array
    e = ELL.FloatVector()
    e.copy_from(a)
    np.testing.assert_equal(np.asarray(e), a)

    # convert data types
    a = a.astype(np.float)
    e = ELL.FloatVector(a)
    np.testing.assert_equal(np.asarray(e), a)

    # enumerating array
    for i in range(a.shape[0]):
        x = a[i]
        y = e[i]
        np.testing.assert_equal(x, y)

    # auto-ravel numpy arrays
    a = np.ones((10, 10), dtype=np.float32)
    a *= range(10)
    e = ELL.FloatVector(a)
    np.testing.assert_equal(np.asarray(e), a.ravel())

    testing.ProcessTest("FloatVector test", True)
コード例 #5
0
ファイル: protonn_trainer_test.py プロジェクト: zeta1999/ELL
def test():
    testing = Testing()
    # -dd auto -sw 1 -sb 1 -sz 1 -pd 10 -l 2 -mp 5 -v --evaluationFrequency 1 -plf L2

    args = ell.trainers.ProtoNNTrainerParameters()
    args.projectedDimension = 10
    args.numPrototypesPerLabel = 5
    args.numLabels = 2
    args.sparsityW = 1
    args.sparsityB = 1
    args.sparsityZ = 1
    args.gamma = -1
    args.lossFunction = ell.trainers.ProtoNNLossFunction.L2
    args.numInnerIterations = 1
    args.numFeatures = 0
    args.verbose = True

    trainer = ell.trainers.ProtoNNTrainer(args)

    dataset = ell.data.AutoSupervisedDataset()
    testFile = os.path.join(find_ell.get_ell_root(),
                            "examples/data/protonnTestData.txt")
    print("Loading: " + testFile)
    dataset.Load(testFile)

    total = dataset.NumExamples()
    features = dataset.NumFeatures()
    testing.ProcessTest("ProtoNN dataset loaded",
                        testing.IsEqual(int(total), 200))

    trainer.SetDataset(dataset)

    numIterations = 20

    print("Training...")
    for i in range(numIterations):
        trainer.Update()

    predictor = trainer.GetPredictor()

    accuracy = get_accuracy(predictor, dataset, features, predictor.Predict)
    print("Accuracy %f" % (accuracy))
    testing.ProcessTest("ProtoNN accuracy test",
                        testing.IsEqual(int(accuracy), 1))

    map = predictor.GetMap()
    map.Save("protonnTestData.ell")
    testing.ProcessTest(
        "Saving  protonnTestData.ell",
        testing.IsEqual(os.path.isfile("protonnTestData.ell"), True))

    # make sure we can compile this map.
    try:
        compilerSettings = ell.model.MapCompilerOptions()
        compilerSettings.useBlas = False
        optimizerSettings = ell.model.ModelOptimizerOptions()
        compiledMap = map.CompileDouble("host", "protonn", "predict",
                                        compilerSettings, optimizerSettings)
        compiled = True
    except Exception as e:
        print("Compile ProtoNN model failed: {}", e)
        compiled = False

    testing.ProcessTest("Compiling protonnTestData.ell",
                        testing.IsEqual(compiled, True))
    if compiled:
        accuracy = get_accuracy(predictor, dataset, features,
                                compiledMap.ComputeDouble)
        print("Compiled Accuracy %f" % (accuracy))
        testing.ProcessTest("ProtoNN compiled accuracy test",
                            testing.IsEqual(int(accuracy), 1))

    if testing.DidTestFail():
        raise Exception("protonn_trainer_test failed")

    return 0