-
Notifications
You must be signed in to change notification settings - Fork 1
/
xor.py
executable file
·41 lines (35 loc) · 1.16 KB
/
xor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt
import numpy as np
import neural_network.activation_functions as activations
import neural_network.regularizers as regularizers
import neural_network.error_functions as errors
import neural_network.loss_functions as losses
import neural_network.optimizers as optimizers
import neural_network.neural_network as nn
training_set = np.array([[0, 0, 0],
[0, 1, 1],
[1, 0, 1],
[1, 1, 0]])
# hyperparameters
lr = 0.1
epochs = 50
alpha = 0.4
lmbda = 0.001
n_hidden = 15
n_outputs = 1
mb = 4
model = nn.Sequential(
error=errors.MeanEuclideanError(),
loss=losses.MeanSquaredError(),
regularizer=regularizers.L2(lmbda),
optimizer=optimizers.SGD(lr, epochs, mb, alpha))
model.add(nn.Dense(dim=(training_set.shape[1]-n_outputs,n_hidden), activation=activations.Sigmoid()))
model.add(nn.Dense(dim=(n_hidden,1), activation=activations.Sigmoid(), is_output=True))
tr_errors, vl_errors, _, _ = model.fit(training_set, training_set)
plt.plot(tr_errors)
plt.title('Learning curve')
plt.xlabel('Epochs')
plt.ylabel('Error')
plt.show()