예제 #1
0
    def test_prob_1d(self):
        mlp = MultilayerPerceptron(
            num_inputs=4, num_hidden_layers=1, num_hidden_nodes=3)
        mlp.fit(X_TRAIN, LABELS_TRAIN, epochnum=5)

        pred_prob = mlp.predict_prob(X_TRAIN)
        assert np.all(np.logical_and(0 <= pred_prob, pred_prob <= 1))
예제 #2
0
 def test_same_state(self):
     self.mlp0 = MultilayerPerceptron(seed=np.random.RandomState(345),
                                      **self.params)
     self.mlp1 = MultilayerPerceptron(seed=np.random.RandomState(345),
                                      **self.params)
     self.mlp0.fit(X_TRAIN, LABELS_TRAIN, epochnum=3)
     self.mlp1.fit(X_TRAIN, LABELS_TRAIN, epochnum=3)
     check_mlp_same_est(self.mlp0, self.mlp1)
예제 #3
0
 def test_same_state(self):
     self.mlp0 = MultilayerPerceptron(seed=np.random.RandomState(345),
                                      **self.params)
     self.mlp1 = MultilayerPerceptron(seed=np.random.RandomState(345),
                                      **self.params)
     self.mlp0.fit(X_TRAIN, LABELS_TRAIN, epochnum=3)
     self.mlp1.fit(X_TRAIN, LABELS_TRAIN, epochnum=3)
     check_mlp_same_est(self.mlp0, self.mlp1)
예제 #4
0
    def test_prob_1d(self):
        mlp = MultilayerPerceptron(num_inputs=4,
                                   num_hidden_layers=1,
                                   num_hidden_nodes=3)
        mlp.fit(X_TRAIN, LABELS_TRAIN, epochnum=5)

        pred_prob = mlp.predict_prob(X_TRAIN)
        assert np.all(np.logical_and(0 <= pred_prob, pred_prob <= 1))
예제 #5
0
    def test_seed_persistence(self):
        self.mlp0 = MultilayerPerceptron(seed=2345, **self.params)
        self.mlp1 = MultilayerPerceptron(seed=2345, **self.params)
        self.mlp0.fit(X_TRAIN, LABELS_TRAIN, epochnum=6)
        self.mlp1.fit(X_TRAIN, LABELS_TRAIN, epochnum=3)
        np.random.normal(size=50)
        self.mlp1.fit(X_TRAIN, LABELS_TRAIN, epochnum=3)

        check_mlp_same_est(self.mlp0, self.mlp1)
예제 #6
0
    def test_seed_persistence(self):
        self.mlp0 = MultilayerPerceptron(seed=2345, **self.params)
        self.mlp1 = MultilayerPerceptron(seed=2345, **self.params)
        self.mlp0.fit(X_TRAIN, LABELS_TRAIN, epochnum=6)
        self.mlp1.fit(X_TRAIN, LABELS_TRAIN, epochnum=3)
        np.random.normal(size=50)
        self.mlp1.fit(X_TRAIN, LABELS_TRAIN, epochnum=3)

        check_mlp_same_est(self.mlp0, self.mlp1)
예제 #7
0
    def test_copy(self):
        mlp0 = MultilayerPerceptron()
        mlp1 = mlp0.copy()

        assert mlp0.params == mlp1.params
        check_mlp_same_est(mlp0, mlp1)
        for l0, l1 in zip(mlp0.layers, mlp1.layers):
            assert not np.may_share_memory(l0.weights, l1.weights)
            assert not np.may_share_memory(l0.activations, l1.activations)
            assert l0.bias == l1.bias
예제 #8
0
    def test_copy(self):
        mlp0 = MultilayerPerceptron()
        mlp1 = mlp0.copy()

        assert mlp0.params == mlp1.params
        check_mlp_same_est(mlp0, mlp1)
        for l0, l1 in zip(mlp0.layers, mlp1.layers):
            assert not np.may_share_memory(l0.weights, l1.weights)
            assert not np.may_share_memory(l0.activations, l1.activations)
            assert l0.bias == l1.bias
예제 #9
0
    def test_prob_2d(self):
        x = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
        y = np.array([[0, 1], [1, 0], [0, 1], [1, 0]])

        mlp = MultilayerPerceptron(
            num_inputs=3, num_outputs=2,
            num_hidden_layers=1)
        mlp.fit(x, y, epochnum=5)

        pred_prob = mlp.predict_prob(x)
        assert np.all(np.logical_and(0 <= pred_prob, pred_prob <= 1))
        assert np.allclose(np.sum(pred_prob, axis=1), 1)
예제 #10
0
    def test_easy_multidim_y(self):
        x = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
        y = np.array([[0, 1], [1, 0], [0, 1], [1, 0]])

        mlp = MultilayerPerceptron(
            num_inputs=3, num_outputs=2,
            num_hidden_layers=1, num_hidden_nodes=6, seed=323440,
            learn_rate = .8, learn_rate_evol='constant', momentum=.1
        )
        mlp.fit(x, y, epochnum=50)
        results = mlp.classify(x, max_ind=True)
        assert np.allclose(to_dummies(results), y)
예제 #11
0
    def test_prob_2d(self):
        x = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
        y = np.array([[0, 1], [1, 0], [0, 1], [1, 0]])

        mlp = MultilayerPerceptron(num_inputs=3,
                                   num_outputs=2,
                                   num_hidden_layers=1)
        mlp.fit(x, y, epochnum=5)

        pred_prob = mlp.predict_prob(x)
        assert np.all(np.logical_and(0 <= pred_prob, pred_prob <= 1))
        assert np.allclose(np.sum(pred_prob, axis=1), 1)
예제 #12
0
    def test_easy(self):
        x = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
        y = np.array([0, 1, 0, 1])

        mlp = MultilayerPerceptron(
            num_inputs=3, num_hidden_layers=1, num_hidden_nodes=6, seed=323440,
            learn_rate = .8, learn_rate_evol='constant', momentum=.1
        )
        mlp.fit(x, y, add_constant=True, epochnum=100, verbose=True)

        print(mlp.classify(x, add_constant=True))
        assert np.allclose(mlp.classify(x, add_constant=True).reshape(len(y)),
                           y)
예제 #13
0
    def test_easy_multidim_y(self):
        x = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
        y = np.array([[0, 1], [1, 0], [0, 1], [1, 0]])

        mlp = MultilayerPerceptron(num_inputs=3,
                                   num_outputs=2,
                                   num_hidden_layers=1,
                                   num_hidden_nodes=6,
                                   seed=323440,
                                   learn_rate=.8,
                                   learn_rate_evol='constant',
                                   momentum=.1)
        mlp.fit(x, y, epochnum=50)
        results = mlp.classify(x, max_ind=True)
        assert np.allclose(to_dummies(results), y)
예제 #14
0
 def test_fit(self):
     x = np.column_stack([np.ones(len(X_TRAIN)), X_TRAIN])
     mlp = MultilayerPerceptron(num_inputs=4,
                                num_hidden_layers=1,
                                num_hidden_nodes=3)
     mlp.fit(x, LABELS_TRAIN, epochnum=5, add_constant=False)
     mlp.classify(x, add_constant=False)
예제 #15
0
    def test_params(self):
        param_dict = {
            'num_inputs': 4,
            'num_outputs': 2,
            'num_hidden_layers': 2,
            'num_hidden_nodes': [2, 3],
            'learn_rate': .4,
            'momentum': .2,
            'seed': 24545,
            'sigmoid': metrics.tanh,
            'weight_init_range': .2
        }

        mlp = MultilayerPerceptron(**param_dict)
        for key, val in param_dict.items():
            assert mlp.params[key] == val
예제 #16
0
    def test_easy(self):
        x = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
        y = np.array([0, 1, 0, 1])

        mlp = MultilayerPerceptron(num_inputs=3,
                                   num_hidden_layers=1,
                                   num_hidden_nodes=6,
                                   seed=323440,
                                   learn_rate=.8,
                                   learn_rate_evol='constant',
                                   momentum=.1)
        mlp.fit(x, y, add_constant=True, epochnum=100, verbose=True)

        print(mlp.classify(x, add_constant=True))
        assert np.allclose(
            mlp.classify(x, add_constant=True).reshape(len(y)), y)
from simpleml.neural import MultilayerPerceptron
from simpleml.transform import to_dummies


# Load data
with gzip.open('../data/mnist.gz', 'rb') as f:
    data = pickle.load(f)

data = {key: (val[0], val[1], to_dummies(val[1])) for key, val in data.items()}


# Setup estimator
num_hidden_nodes = [101]
mlp = MultilayerPerceptron(
    num_inputs=data['train'][0].shape[1]+1,
    num_outputs=data['train'][2].shape[1],
    num_hidden_layers=len(num_hidden_nodes), num_hidden_nodes=num_hidden_nodes,
    learn_rate=.5, momentum=.1, seed=23456
)

# Estimate multilayer perceptron
start = time.perf_counter()
mlp.fit(data['train'][0], data['train'][2],
        epochnum=10, verbose=1)
pred = mlp.classify(data['test'][0], max_ind=True)
print("Time: {:5.2f}, Error: {:5.4f}".format(
    time.perf_counter() - start,
    1 - np.mean(pred == data['test'][1])
))


# Visualize first hidden layer
예제 #18
0
# x = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
# y = np.array([0, 1, 1, 0])

# Setup mesh for graphing boundary
x0_min, x1_min = np.min(x, axis=0) - .5
x0_max, x1_max = np.max(x, axis=0) + .5

x0, x1 = np.meshgrid(np.linspace(x0_min, x0_max, 500),
                     np.linspace(x1_min, x1_max, 500))
x_flatmesh = np.column_stack([x0.ravel(), x1.ravel()])

# Setup estimator
mlp = MultilayerPerceptron(num_inputs=3,
                           num_outputs=1,
                           num_hidden_layers=1,
                           num_hidden_nodes=6,
                           learn_rate=1,
                           learn_rate_evol='sqrt',
                           momentum=.1,
                           seed=23456)

# Estimate and plot
start = time.perf_counter()
with PdfPages('ex_mlp_2d.pdf') as pdf:
    for i in range(0, 25):
        mlp.fit(x, y, epochnum=20, add_constant=True)
        Z = mlp.classify(x_flatmesh, add_constant=True)
        Z = Z.reshape(x0.shape)

        fig = plt.figure()
        ax = fig.add_subplot(1, 1, 1)
from simpleml.neural import MultilayerPerceptron
from simpleml.transform import to_dummies

# Load data
with gzip.open('../data/mnist.gz', 'rb') as f:
    data = pickle.load(f)

data = {key: (val[0], val[1], to_dummies(val[1])) for key, val in data.items()}

# Setup estimator
num_hidden_nodes = [101]
mlp = MultilayerPerceptron(num_inputs=data['train'][0].shape[1] + 1,
                           num_outputs=data['train'][2].shape[1],
                           num_hidden_layers=len(num_hidden_nodes),
                           num_hidden_nodes=num_hidden_nodes,
                           learn_rate=.5,
                           momentum=.1,
                           seed=23456)

# Estimate multilayer perceptron
start = time.perf_counter()
mlp.fit(data['train'][0], data['train'][2], epochnum=10, verbose=1)
pred = mlp.classify(data['test'][0], max_ind=True)
print("Time: {:5.2f}, Error: {:5.4f}".format(
    time.perf_counter() - start, 1 - np.mean(pred == data['test'][1])))

# Visualize first hidden layer
fig1 = plt.figure(figsize=(10, 10))
for i in range(num_hidden_nodes[0] - 1):
    side = np.sqrt(num_hidden_nodes[0] - 1)
예제 #20
0
# # XOR
# x = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
# y = np.array([0, 1, 1, 0])


# Setup mesh for graphing boundary
x0_min, x1_min = np.min(x, axis=0)-.5
x0_max, x1_max = np.max(x, axis=0)+.5

x0, x1 = np.meshgrid(np.linspace(x0_min, x0_max, 500),
                     np.linspace(x1_min, x1_max, 500))
x_flatmesh = np.column_stack([x0.ravel(), x1.ravel()])

# Setup estimator
mlp = MultilayerPerceptron(
    num_inputs=3, num_outputs=1, num_hidden_layers=1, num_hidden_nodes=6,
    learn_rate=1, learn_rate_evol='sqrt', momentum=.1, seed=23456
)

# Estimate and plot
start = time.perf_counter()
with PdfPages('ex_mlp_2d.pdf') as pdf:
    for i in range(0, 25):
        mlp.fit(x, y, epochnum=20, add_constant=True)
        Z = mlp.classify(x_flatmesh, add_constant=True)
        Z = Z.reshape(x0.shape)

        fig = plt.figure()
        ax = fig.add_subplot(1, 1, 1)

        ax.contour(x0, x1, Z, cmap=plt.cm.Paired)
        ax.scatter(x[:,0], x[:,1], c=y, cmap=plt.cm.Paired)
예제 #21
0
 def test_same_seed(self):
     self.mlp0 = MultilayerPerceptron(seed=2345, **self.params)
     self.mlp1 = MultilayerPerceptron(seed=2345, **self.params)
     self.mlp0.fit(X_TRAIN, LABELS_TRAIN, epochnum=3)
     self.mlp1.fit(X_TRAIN, LABELS_TRAIN, epochnum=3)
     check_mlp_same_est(self.mlp0, self.mlp1)
예제 #22
0
class TestMLPSeed:
    params = {'num_inputs': 4, 'num_hidden_layers': 1, 'num_hidden_nodes': 3}

    def test_same_seed(self):
        self.mlp0 = MultilayerPerceptron(seed=2345, **self.params)
        self.mlp1 = MultilayerPerceptron(seed=2345, **self.params)
        self.mlp0.fit(X_TRAIN, LABELS_TRAIN, epochnum=3)
        self.mlp1.fit(X_TRAIN, LABELS_TRAIN, epochnum=3)
        check_mlp_same_est(self.mlp0, self.mlp1)

    def test_same_state(self):
        self.mlp0 = MultilayerPerceptron(seed=np.random.RandomState(345),
                                         **self.params)
        self.mlp1 = MultilayerPerceptron(seed=np.random.RandomState(345),
                                         **self.params)
        self.mlp0.fit(X_TRAIN, LABELS_TRAIN, epochnum=3)
        self.mlp1.fit(X_TRAIN, LABELS_TRAIN, epochnum=3)
        check_mlp_same_est(self.mlp0, self.mlp1)

    def test_seed_persistence(self):
        self.mlp0 = MultilayerPerceptron(seed=2345, **self.params)
        self.mlp1 = MultilayerPerceptron(seed=2345, **self.params)
        self.mlp0.fit(X_TRAIN, LABELS_TRAIN, epochnum=6)
        self.mlp1.fit(X_TRAIN, LABELS_TRAIN, epochnum=3)
        np.random.normal(size=50)
        self.mlp1.fit(X_TRAIN, LABELS_TRAIN, epochnum=3)

        check_mlp_same_est(self.mlp0, self.mlp1)
예제 #23
0
 def test_values(case):
     mlp = MultilayerPerceptron(learn_rate_evol=case[0])
     for arg, out in case[1]:
         assert np.isclose(mlp.learn_rate_evol(arg), out)
예제 #24
0
 def test_values(case):
     mlp = MultilayerPerceptron(learn_rate_evol=case[0])
     for arg, out in case[1]:
         assert np.isclose(mlp.learn_rate_evol(arg), out)
예제 #25
0
 def test_nodes(self):
     mlp = MultilayerPerceptron(num_hidden_layers=3, num_hidden_nodes=3)
     assert mlp.params['num_hidden_nodes'] == [3, 3, 3]
예제 #26
0
 def test_not_recognized(self):
     MultilayerPerceptron(learn_rate_evol='unrecognized')
예제 #27
0
 def test_same_seed(self):
     self.mlp0 = MultilayerPerceptron(seed=2345, **self.params)
     self.mlp1 = MultilayerPerceptron(seed=2345, **self.params)
     self.mlp0.fit(X_TRAIN, LABELS_TRAIN, epochnum=3)
     self.mlp1.fit(X_TRAIN, LABELS_TRAIN, epochnum=3)
     check_mlp_same_est(self.mlp0, self.mlp1)
예제 #28
0
class TestMLPSeed:
    params = {'num_inputs':4, 'num_hidden_layers':1, 'num_hidden_nodes':3}

    def test_same_seed(self):
        self.mlp0 = MultilayerPerceptron(seed=2345, **self.params)
        self.mlp1 = MultilayerPerceptron(seed=2345, **self.params)
        self.mlp0.fit(X_TRAIN, LABELS_TRAIN, epochnum=3)
        self.mlp1.fit(X_TRAIN, LABELS_TRAIN, epochnum=3)
        check_mlp_same_est(self.mlp0, self.mlp1)

    def test_same_state(self):
        self.mlp0 = MultilayerPerceptron(seed=np.random.RandomState(345),
                                         **self.params)
        self.mlp1 = MultilayerPerceptron(seed=np.random.RandomState(345),
                                         **self.params)
        self.mlp0.fit(X_TRAIN, LABELS_TRAIN, epochnum=3)
        self.mlp1.fit(X_TRAIN, LABELS_TRAIN, epochnum=3)
        check_mlp_same_est(self.mlp0, self.mlp1)

    def test_seed_persistence(self):
        self.mlp0 = MultilayerPerceptron(seed=2345, **self.params)
        self.mlp1 = MultilayerPerceptron(seed=2345, **self.params)
        self.mlp0.fit(X_TRAIN, LABELS_TRAIN, epochnum=6)
        self.mlp1.fit(X_TRAIN, LABELS_TRAIN, epochnum=3)
        np.random.normal(size=50)
        self.mlp1.fit(X_TRAIN, LABELS_TRAIN, epochnum=3)

        check_mlp_same_est(self.mlp0, self.mlp1)
예제 #29
0
 def test_nodes_wrong_number(self):
     MultilayerPerceptron(num_hidden_layers=1, num_hidden_nodes=[3, 3])
예제 #30
0
 def test_print(self):
     mlp = MultilayerPerceptron()
     print(mlp)
예제 #31
0
 def test_fit(self):
     x = np.column_stack([np.ones(len(X_TRAIN)), X_TRAIN])
     mlp = MultilayerPerceptron(
         num_inputs=4, num_hidden_layers=1, num_hidden_nodes=3)
     mlp.fit(x, LABELS_TRAIN, epochnum=5, add_constant=False)
     mlp.classify(x, add_constant=False)