forked from Sentdex/NNfSiX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
P003DotProduct.java
47 lines (35 loc) · 1.09 KB
/
P003DotProduct.java
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
/*
Create a basic neuron layer with dot product in Java
Associated tutorial https://www.youtube.com/watch?v=tMrbN67U9d4
*/
import java.util.Arrays;
public class P003DotProduct {
public static void main(String[] args) {
double[] inputs = { 1.0, 2.0, 3.0, 2.5 };
double[][] weights = {
{ 0.2, 0.8, -0.5, 1 },
{ .5, -0.91, 0.26, -0.5 },
{ -0.26, -0.27, 0.17, 0.87 }
};
double[] biases = { 2, 3, 0.5 };
System.out.println(Arrays.toString(add(dotProduct(weights, inputs), biases)));
}
private static double[] dotProduct(double[][] input1, double[] input2) {
double[] outputs = new double[input1.length];
for (int i = 0; i < input1.length; i++) {
double output = 0;
for (int j = 0; j < input2.length; j++) {
output += input1[i][j] * input2[j];
}
outputs[i] = output;
}
return outputs;
}
private static double[] add(double[] input1, double[] input2) {
double[] output = new double[input1.length];
for (int i = 0; i < input1.length; i++) {
output[i] = input1[i] + input2[i];
}
return output;
}
}