예제 #1
0
def cross_fold(X, Y, n, *model_args, **model_kwargs):
    num_examples = len(Y)
    # shuffle data first
    idx = np.arange(num_examples)
    np.random.shuffle(idx)
    # split into n sets
    splits = np.split(idx, n)
    for i in range(n):
        # get train/test sets
        idx_test = splits[i]
        tr1 = splits[:i]
        tr2 = splits[i + 1:]
        tr1.extend(tr2)
        idx_train = np.concatenate(tr1)
        X_train = X[idx_train, :]
        X_test = X[idx_test, :]
        Y_train = Y[idx_train]
        Y_test = Y[idx_test]
        # create new model
        model = NeuralNet(*model_args, **model_kwargs)
        # train
        num_epochs = model.fit(X_train, Y_train)
        # compare
        out = model.predict(X_test)
        print(i, model.score(X_train, Y_train), num_epochs)
예제 #2
0
파일: blobs.py 프로젝트: lwthatcher/mlp
def run_blobs(n=5):
    X, Y = get_blob_sets(n)
    net = NeuralNet(4,
                    20,
                    6,
                    a_func=Sigmoid,
                    validation_set=(X[2:4], Y[2:4]),
                    multi_vsets=True)
    num_epochs = net.fit(X[:2], Y[:2], True)
    score = net.score(X[-1], Y[-1], multi_sets=False)
    print("accuracy:", score)
    print("epochs:", num_epochs)
예제 #3
0

if __name__ == '__main__':
    args = parse_args()
    train = get_sets(args.train, args.dir, args.ext, "Training")
    test = get_sets(args.test, args.dir, args.ext, "Test")
    validation = get_sets(args.validation, args.dir, args.ext, "Validation")
    model = NeuralNet(args.num_features,
                      args.num_hidden,
                      args.num_classes,
                      validation_set=validation,
                      multi_vsets=True,
                      max_epochs=args.max_epochs,
                      patience=args.patience,
                      learning_rate=args.learning_rate)
    num_epochs = model.fit(train[0], train[1], True)
    score = model.score(test[0], test[1], multi_sets=True)
    print("accuracy:", score)
    print("epochs:", num_epochs)
    if args.out:
        with open(args.out, 'a') as _f:
            print("Training Sets:",
                  args.train,
                  "Validation Sets:",
                  args.validation,
                  "Test Sets:",
                  args.test,
                  file=_f)
            print("hidden nodes:", args.num_hidden, file=_f)
            print("learning rate:",
                  args.learning_rate,