forked from arrayfire/arrayfire-ml
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFFNet.cpp
More file actions
57 lines (44 loc) · 1.44 KB
/
Copy pathFFNet.cpp
File metadata and controls
57 lines (44 loc) · 1.44 KB
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
56
57
/*******************************************************
* Copyright (c) 2015, ArrayFire
* All rights reserved.
*
* This file is distributed under 3-clause BSD license.
* The complete license agreement can be obtained at:
* http://arrayfire.com/licenses/BSD-3-Clause
********************************************************/
#include <afml/nn.h>
using namespace af;
using namespace afml;
using namespace afml::nn;
int main()
{
const int inputSize = 2;
const int hiddenSize = 3;
const int outputSize = 1;
const int numSamples = 4;
const double lr = 0.8;
float hInput[] = {1, 1,
0, 0,
0, 1,
1, 0};
float hOutput[] = {0,
0,
1,
1};
af::array in(inputSize, numSamples, hInput);
af::array out(outputSize, numSamples, hOutput);
FFNet network(inputSize);
network.addLinearNode(hiddenSize, 5).addActivationNode();
network.addLinearNode(outputSize, 5).addActivationNode();
for (int i = 0; i < 1000; i++) {
ArrayVector data = network.forward({in});
double err = af::norm(data[0] - out);
data[0] = out - data[0];
if ((i + 1) % 100 == 0) {
printf("Error at iteration(%d) : %2.10lf\n", i + 1, err);
}
network.backward({in}, data);
network.update(lr);
}
af_print(af::round(network.forward({in})[0]));
}