Пример #1
0
def test_020_matmul_build_specification():
    name = "matmul01"
    num_nodes = 8
    num_features = 2
    weights_initialization_scheme = "he"
    expected_spec = {
        _SCHEME: Matmul.class_id(),
        _PARAMETERS: {
            _NAME: name,
            _NUM_NODES: num_nodes,
            _NUM_FEATURES: num_features,  # NOT including bias
            _WEIGHTS: {
                _SCHEME: weights_initialization_scheme
            },
            _OPTIMIZER: SGD.specification(name="sgd")
        }
    }
    actual_spec = Matmul.specification(
        name=name,
        num_nodes=num_nodes,
        num_features=num_features,
        weights_initialization_scheme=weights_initialization_scheme,
    )
    assert expected_spec == actual_spec, \
        "expected\n%s\nactual\n%s\n" % (expected_spec, actual_spec)
def test():
    M = 1
    D = 2
    N = 100

    X, T, V = linear_separable(d=D, n=N)
    x_min, x_max = X[:, 0].min(), X[:, 0].max()
    y_min, y_max = X[:, 1].min(), X[:, 1].max()

    sigmoid_classifier_specification = {
        _NAME: "softmax_classifier",
        _NUM_NODES: M,
        _LOG_LEVEL: logging.ERROR,
        _COMPOSITE_LAYER_SPEC: {
            "matmul01":
            Matmul.specification(
                name="matmul",
                num_nodes=M,
                num_features=D,
                weights_initialization_scheme="he",
                weights_optimizer_specification=SGD.specification(
                    lr=TYPE_FLOAT(0.2), l2=TYPE_FLOAT(1e-3))),
            "loss":
            CrossEntropyLogLoss.specification(
                name="loss",
                num_nodes=M,
                loss_function=sigmoid_cross_entropy_log_loss.__qualname__)
        }
    }
    logistic_classifier = SequentialNetwork.build(
        specification=sigmoid_classifier_specification, )

    for i in range(50):
        logistic_classifier.train(X=X, T=T)

    prediction = logistic_classifier.predict(
        np.array([-1., -1.], dtype=TYPE_FLOAT))
    np.isin(prediction, [0, 1])
    print(prediction)