Exemple #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)
Exemple #2
0
 def test_predict(self):
     # init with preset values
     nn = NeuralNet(2, 3, 2)
     nn.W[0] = np.array([[.6, .2, 1], [1, .5, 0]])
     nn.W[1] = np.array([[.1, 1], [1, .25], [.1, 1]])
     nn.b[0] = np.array([[0., 0., 0.]])
     nn.b[1] = np.array([[1., -1.]])
     nn._classification = False
     # input values
     X = np.array([[1., .4], [0., 0.], [1., 1.], [2., .8]])
     # expected output
     Y = np.array([[1.6, 1.1], [1., 0.], [1.96, 1.775], [2.2, 3.2]])
     # actual output
     Z = nn.predict(X)
     # compare
     np.testing.assert_array_almost_equal(Y, Z)