-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain_XOR_example.py
55 lines (44 loc) · 2.1 KB
/
train_XOR_example.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
"""
Example to use DiagnoseNET Framework
for training a feedforward network on a desktop machine.
A XOR example is defined by: Entries its Output
[0, 0] or [1, 1] -> [1, 0]
[0, 1] or [1, 0] -> [0, 1]
"""
import numpy as np
from diagnosenet.layers import Relu, Softmax
from diagnosenet.losses import CrossEntropy
from diagnosenet.optimizers import Adam
from diagnosenet.graphs import SequentialGraph
from diagnosenet.executors import DesktopExecution
from diagnosenet.datamanager import MultiTask
#####################################################################
## A simple feedforward network is implemented to solve a XOR example
inputs = np.array([[0, 0], [1, 0], [0, 1], [1, 1],
[0, 0], [1, 0], [0, 1], [1, 1],
[0, 0], [1, 0], [0, 1], [1, 1],
[0, 0], [1, 0], [0, 1], [1, 1],
[0, 0], [1, 0], [0, 1], [1, 1]])
targets = np.array([[1, 0], [0, 1], [0, 1], [1, 0],
[1, 0], [0, 1], [0, 1], [1, 0],
[1, 0], [0, 1], [0, 1], [1, 0],
[1, 0], [0, 1], [0, 1], [1, 0],
[1, 0], [0, 1], [0, 1], [1, 0]])
## 1) Define the stacked layers as the number of layers and their neurons
staked_layers = [Relu(2, 32),
Relu(32, 32),
Softmax(32, 2)]
## 2) Select the neural network architecture and pass the hyper-parameters
model_1 = SequentialGraph(input_size=2, output_size=2,
layers=staked_layers,
loss=CrossEntropy,
optimizer=Adam(lr=0.01))
## 3) Dataset configurations for splitting, batching and target selection
data_config = MultiTask(dataset_name="XOR_example",
valid_size=0.05, test_size=0.15)
## 4) Select the execution machine mode
platform = DesktopExecution(model=model_1,
datamanager=data_config,
max_epochs=50, min_loss=0.02)
## 5) Uses the platform modes for training in an efficient way
platform.training_memory(inputs, targets)