-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNeuralNetwork.java
More file actions
132 lines (107 loc) · 3.03 KB
/
NeuralNetwork.java
File metadata and controls
132 lines (107 loc) · 3.03 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import javax.sql.rowset.spi.SyncFactory;
public class NeuralNetwork {
private double synapticWeights[][];
public NeuralNetwork(int numberOfFeatures)
{
for(int i=0; i< numberOfFeatures; i++)
for(int j=0; j<=1; j++)
synapticWeights = new double[i][j];
}
private double[][] addMatrices(double x[][], double y[][])
{
double z[][] = null;
for(int i=0; i<=x.length; i++)
for(int j=0; j<=x[0].length; j++)
z = new double[i][j];
for(int i=0; i<z.length; i++)
for(int j=0; j<z[0].length; j++)
z[i][j] = x[i][j] + y[i][j];
return z;
}
private double[][] subtractMatrices(double x[][], double y[][])
{
double z[][] = null;
for(int i=0; i<=x.length; i++)
for(int j=0; j<=x[0].length; j++)
z = new double[i][j];
for(int i=0; i<z.length; i++)
for(int j=0; j<z[0].length; j++)
z[i][j] = x[i][j] - y[i][j];
return z;
}
private double[][] transpose(double x[][])
{
double z[][] = null;
for(int i=0; i<=x[0].length; i++)
for(int j=0; j<=x.length; j++)
z = new double[i][j];
for(int i=0; i<x.length; i++)
for(int j=0; j<x[0].length; j++)
z[j][i] = x[i][j];
return z;
}
private double[][] dotProduct(double x[][], double y[][])
{
double z[][] = null;
for(int i=0; i<=x.length; i++)
for(int j=0; j<=y[0].length; j++)
z = new double[i][j];
double sum = 0.0;
for (int i = 0 ; i < x.length ; i++ )
{
for ( int j=0; j < y[0].length; j++)
{
for (int k=0; k < x[0].length; k++ )
sum += x[i][k]*y[k][j];
z[i][j] = sum;
sum = 0;
}
}
return z;
}
private double[][] sigmoid(double z[][])
{
for(int i=0; i<z.length; i++)
for(int j=0; j<z[0].length; j++)
z[i][j] = (1/(1 + Math.exp(-z[i][j])));
return z;
}
void print(double x[][])
{
for(int i=0; i<x.length; i++)
{
for(int j=0; j<x[0].length; j++)
System.out.print(x[i][j] + " ");
System.out.println();
}
System.out.println();
}
public void train(double inputFeatures[][], double inputLabels[][], int iterations)
{
for(int i=0; i< inputFeatures[0].length; i++)
for(int j=0; j<1; j++)
synapticWeights[i][j] = 1.0;
// synapticWeights[0][0] = -0.16595599;
// synapticWeights[1][0] = 0.44064899;
// synapticWeights[2][0] = -0.99977125;
double output[][] = null;
double error[][] = null;
double adjustments[][] = null;
for(int i=0; i<iterations; i++)
{
output = predict(inputFeatures);
error = subtractMatrices(inputLabels, output);
adjustments = dotProduct(transpose(inputFeatures), error);
synapticWeights = addMatrices(synapticWeights, adjustments);
}
}
public double[][] predict(double inputs[][])
{
return (sigmoid(dotProduct(inputs, synapticWeights)));
}
public void printWeights()
{
System.out.println("New synaptic weights:");
print(synapticWeights);
}
}