示例#1
0
def _test_adaboost():
    metric = metrics.get("categorical_accuracy")
    x_train, y_train = get_test_classification_data()

    Models = []
    weak_model_losses = []
    for _ in range(num_models):
        model = Sequential()
        model.add(Input(input_shape=(x_train.shape[1], )))
        model.add(Dense(10, activation="relu"))
        model.add(Dense(y_train.shape[1], activation="softmax"))
        model.compile(loss="categorical_crossentropy",
                      optimizer="adam",
                      metrics=[metric])
        model.fit(x_train, y_train, epochs=3, batch_size=16, verbose=-1)
        weak_model_losses.append(metric.loss(model.predict(x_train), y_train))
        Models.append(model)

    boosting = AdaBoost(Models)
    boosting.fit(x_train, y_train, verbose=-1)
    y_boosting_pred = boosting.predict(x_train)
    boosting_loss = metric.loss(y_boosting_pred, y_train)

    assert boosting_loss <= min(weak_model_losses)
示例#2
0
def test_l2boosting():
    metric = metrics.get("mse")
    x_train, y_train = get_test_regression_data()

    Models = []
    weak_model_losses = []
    for _ in range(num_models):
        model = Sequential()
        model.add(Input(input_shape=(x_train.shape[1], )))
        model.add(Dense(10, activation="relu"))
        model.add(Dense(y_train.shape[1], activation="linear"))
        model.compile(loss="mean_squared_error",
                      optimizer="adam",
                      metrics=[metric])
        model.fit(x_train, y_train, epochs=3, batch_size=16, verbose=-1)
        weak_model_losses.append(metric.loss(model.predict(x_train), y_train))
        Models.append(model)

    boosting = L2Boosting(Models)
    boosting.fit(x_train, y_train, verbose=-1)
    y_boosting_pred = boosting.predict(x_train)
    boosting_loss = metric.loss(y_boosting_pred, y_train)

    assert boosting_loss <= min(weak_model_losses)
示例#3
0
# coding: utf-8
from kerasy.models import Sequential
from kerasy.layers import Input, Dense
from kerasy import regularizers
from kerasy import metrics

from kerasy.utils import generate_test_data
from kerasy.utils import CategoricalEncoder

num_classes = 2
metric = metrics.get("categorical_accuracy")


def get_test_data():
    (x_train, y_train), _ = generate_test_data(num_train=1000,
                                               num_test=200,
                                               input_shape=(10, ),
                                               classification=True,
                                               num_classes=num_classes,
                                               random_state=123)
    encoder = CategoricalEncoder()
    y_train = encoder.to_onehot(y_train, num_classes)
    return x_train, y_train


def _test_regularizer(regularizer, target=0.75):
    regularizer = regularizers.get(regularizer)
    x_train, y_train = get_test_data()
    model = Sequential()
    model.add(Input(input_shape=(x_train.shape[1], )))
    model.add(Dense(10, activation="relu", kernel_regularizer=regularizer))
示例#4
0
def _test_metrics(identifier):
    metric = metrics.get(identifier)
    metrics_check(metric, y_shape_)
    metrics_check(metric, y_shape_1)
    metrics_check(metric, y_shape_10)