def test_accuracysgd(idx):
    test_X, test_y = readmnist.testadata()
    test_X = construct_matrix(test_X)
    test_y = construct_labels(test_y)
    # test_X = normalizedata(test_X)
    
    def test_model(network):
        test_predictions = network.run(test_X)
        correct = 0
        total = 0
        for i in range(len(test_predictions)):
            total += 1
            if np.argmax(np.array(test_predictions[i])) == test_y[i]:
                correct += 1
        return correct/total
    print(test_model(network_sgd))
Пример #2
0
def accuracy_train(w):
    data_test, label_test = readmnist.testadata()
    test = construct_matrix(data_test)
    x = np.insert(test, 0, 1, axis=1)
    label = construct_labels(label_test)
    limit = test.shape[0]

    counter = 0
    for i in range(limit):
        score = np.zeros((10))
        for j in range(10):
            score[j] = np.matmul(x[i], w[:, j])
        lb_pred = np.argmax(score)
        if lb_pred == label[i]:
            counter += 1

    accuracy = counter / limit
    return accuracy
    t_corr_labels = []
    for i in range(te_size):
        if t_lbls[i] == test_y[i]:
            t_corr_labels.append(1)
    t_acc = 1.0 * np.array(t_corr_labels).sum() / te_size

    print(t_acc)


if __name__ == "__main__":

    train_X, train_y = readdata()
    idx = np.random.randint(train_X.shape[0], size=60000)
    train_X = train_X[idx, :]
    train_y = train_y[idx, :]

    data_test, label_test = readmnist.testadata()
    test = construct_matrix(data_test)
    label_test = construct_labels(label_test)

    nonlinear_svm(train_X, train_y, data_test, label_test,
                  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
    train_X, train_y = readdata()
    idx = np.random.randint(train_X.shape[0], size=60000)
    train_X = train_X[idx, :]
    train_y = train_y[idx, :]
    data_test, label_test = readmnist.testadata()
    test = construct_matrix(data_test)
    label_test = construct_labels(label_test)
    print(accuracy_train(multiclass(10, test, label_test), train_X, train_y))