示例#1
0
def run_fold(fold_i, C_list=[1.0]):
    train = tfd.TFD('train', fold=fold_i)
    valid = tfd.TFD('valid', fold=fold_i)
    test  = tfd.TFD('test', fold=fold_i)

    preproc = Standardize(train.X / 255.)
    train.X = preproc.apply(train.X / 255.)
    valid.X = preproc.apply(valid.X / 255.)
    test.X  = preproc.apply(test.X / 255.)
    train_y = process_labels(train.y)
    valid_y = process_labels(valid.y)
    test_y = process_labels(test.y)

    svm = LinearSVC(C=1.0, loss='l2', penalty='l2')
    (svm, valid_error) = cross_validate_svm(svm, (train.X, train_y), (valid.X, valid_y), C_list)
    svm = retrain_svm(svm, (train.X, train_y), (valid.X, valid_y))
    test_error = compute_test_error(svm, (test.X, test_y))
    print 'Fold %i: valid_error = %f\t  test_error = %f' % (fold_i, valid_error, test_error)
    return (valid_error, test_error, svm.C)
示例#2
0
def run_fold(path, fold_i, C_list=[1.0]):
    train = pickle.load(open('%s/fold%i/train.pkl' % (path, fold_i)))
    valid = pickle.load(open('%s/fold%i/valid.pkl' % (path, fold_i)))
    test  = pickle.load(open('%s/fold%i/test.pkl' % (path, fold_i)))

    preproc = Standardize(train.X)
    train.X = preproc.apply(train.X)
    valid.X = preproc.apply(valid.X)
    test.X  = preproc.apply(test.X)
    train_y = process_labels(train.y)
    valid_y = process_labels(valid.y)
    test_y = process_labels(test.y)

    svm = LinearSVC(C=1.0, loss='l2', penalty='l2')
    if len(C_list) == 1:
        svm.set_params(C = C_list[0])
        svm = retrain_svm(svm, (train.X, train_y), (valid.X, valid_y))
        valid_error = -1.
    else:
        (svm, valid_error) = cross_validate_svm(svm, (train.X, train_y), (valid.X, valid_y), C_list)
        svm = retrain_svm(svm, (train.X, train_y), (valid.X, valid_y))
    test_error = compute_test_error(svm, (test.X, test_y))
    print 'Fold %i: valid_error = %f\t  test_error = %f' % (fold_i, valid_error, test_error)
    return (valid_error, test_error, svm.C)