def run_multiclass():
    from data_loader import toy_data_multiclass_3_classes_non_separable, \
                            toy_data_multiclass_5_classes, \
                            data_loader_mnist

    datasets = [(toy_data_multiclass_3_classes_non_separable(),
                 'Synthetic data', 3),
                (toy_data_multiclass_5_classes(), 'Synthetic data', 5),
                (data_loader_mnist(), 'MNIST', 10)]

    for data, name, num_classes in datasets:
        print('%s: %d class classification' % (name, num_classes))
        X_train, X_test, y_train, y_test = data

        print('One-versus-rest:')
        w, b = OVR_train(X_train, y_train, C=num_classes)
        train_preds = OVR_predict(X_train, w=w, b=b)
        preds = OVR_predict(X_test, w=w, b=b)
        print('train acc: %f, test acc: %f' % (accuracy_score(
            y_train, train_preds), accuracy_score(y_test, preds)))

        print('Multinomial:')
        w, b = multinomial_train(X_train, y_train, C=num_classes)
        train_preds = multinomial_predict(X_train, w=w, b=b)
        preds = multinomial_predict(X_test, w=w, b=b)
        print('train acc: %f, test acc: %f' % (accuracy_score(
            y_train, train_preds), accuracy_score(y_test, preds)))
def run_binary():
    from data_loader import toy_data_binary, \
                            data_loader_mnist

    print('Performing binary classification on synthetic data')
    X_train, X_test, y_train, y_test = toy_data_binary()

    w, b = binary_train(X_train, y_train)

    train_preds = binary_predict(X_train, w, b)
    preds = binary_predict(X_test, w, b)
    print(
        'train acc: %f, test acc: %f' %
        (accuracy_score(y_train, train_preds), accuracy_score(y_test, preds)))

    print('Performing binary classification on binarized MNIST')
    X_train, X_test, y_train, y_test = data_loader_mnist()

    binarized_y_train = [0 if yi < 5 else 1 for yi in y_train]
    binarized_y_test = [0 if yi < 5 else 1 for yi in y_test]

    w, b = binary_train(X_train, binarized_y_train)

    train_preds = binary_predict(X_train, w, b)
    preds = binary_predict(X_test, w, b)
    print('train acc: %f, test acc: %f' %
          (accuracy_score(binarized_y_train, train_preds),
           accuracy_score(binarized_y_test, preds)))
def run_multiclass():
    from data_loader import smile_dataset_clear, \
                            smile_dataset_blur, \
                            data_loader_mnist
    import time
    datasets = [(smile_dataset_clear(), 'Clear smile data', 3),
                (smile_dataset_blur(), 'Blur smile data', 3),
                (data_loader_mnist(), 'MNIST', 10)]

    for data, name, num_classes in datasets:
        print('%s: %d class classification' % (name, num_classes))
        X_train, X_test, y_train, y_test = data
        for gd_type in ["sgd", "gd"]:
            s = time.time()
            w, b = sol.multiclass_train(X_train,
                                        y_train,
                                        C=num_classes,
                                        gd_type=gd_type)
            print(gd_type + ' training time: %0.6f seconds' %
                  (time.time() - s))
            train_preds = sol.multiclass_predict(X_train, w=w, b=b)
            preds = sol.multiclass_predict(X_test, w=w, b=b)
            print('train acc: %f, test acc: %f' % (accuracy_score(
                y_train, train_preds), accuracy_score(y_test, preds)))
        print()
Beispiel #4
0
def run_binary():
    from data_loader import toy_data_binary, \
                            moon_dataset, \
                            data_loader_mnist

    datasets = [(toy_data_binary(), 'Synthetic data'),
                (moon_dataset(), 'Two Moon data'),
                (data_loader_mnist(), 'Binarized MNIST data')]

    for data, name in datasets:
        print(name)
        X_train, X_test, y_train, y_test = toy_data_binary()

        if name == 'Binarized MNIST data':
            y_train = [0 if yi < 5 else 1 for yi in y_train]
            y_test = [0 if yi < 5 else 1 for yi in y_test]
            y_train = np.asarray(y_train)
            y_test = np.asarray(y_test)

        for loss_type in ["perceptron", "logistic"]:
            w, b = sol.binary_train(X_train, y_train, loss="logistic")
            train_preds = sol.binary_predict(X_train, w, b, loss=loss_type)
            preds = sol.binary_predict(X_test, w, b, loss=loss_type)
            print(loss_type + ' train acc: %f, test acc: %f' %
                  (accuracy_score(y_train, train_preds),
                   accuracy_score(y_test, preds)))
        print()
Beispiel #5
0
from data_loader import smile_dataset_clear, \
                            smile_dataset_blur, \
                            data_loader_mnist
import time
datasets = [(smile_dataset_clear(), 'Clear smile data', 3),
            (smile_dataset_blur(), 'Blur smile data', 3),
            (data_loader_mnist(), 'MNIST', 10)]

for data, name, num_classes in datasets:
    print('%s: %d class classification' % (name, num_classes))
    X_train, X_test, y_train, y_test = data