-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathRNN.java
executable file
·152 lines (132 loc) · 5.4 KB
/
RNN.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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.stream.Collectors;
public class RNN {
Layer[] layers;
HashMap<String, HashMap<String, String>> savedState;
public RNN(Layer[] layers){
this.layers = layers;
savedState = new HashMap<String, HashMap<String, String>>();
}
public void saveCurrentState(HashMap<String, HashMap<String, String>> data){
for(Layer layer : this.layers)
layer.save_params();
savedState = new HashMap<String, HashMap<String, String>>();
for(String prv_name : data.keySet()){
if(prv_name.startsWith("Feat"))
savedState.put(prv_name, new HashMap<String, String>(data.get(prv_name)));
}
}
public void randomInitialize(HashMap<String, HashMap<String, String>> data){
for(Layer layer : this.layers)
layer.assign_random_params();
for(String prv_name : data.keySet()){
if(prv_name.startsWith("Feat"))
for(String assignment : data.get(prv_name).keySet())
data.get(prv_name).replace(assignment, "" + (Math.random() * GlobalParams.maxRandom - GlobalParams.maxRandom / 2));
}
}
public void loadState(HashMap<String, HashMap<String, String>> data){
for(Layer layer : this.layers)
layer.use_best_params();
for(String prv_name : savedState.keySet())
data.put(prv_name, new HashMap<String, String>(savedState.get(prv_name)));
}
public double random_walk_initialize(HashMap<String, HashMap<String, String>> data){
double best_error = 999999999;
for(int i = 0; i < GlobalParams.numRandomWalk; i++){
this.randomInitialize(data);
HashMap<String, HashMap<String, String>> output = new HashMap<String, HashMap<String, String>>();
for(Layer layer : this.layers){
layer.set_data(data);
layer.set_inputs(output);
output = layer.calc_output();
}
String prvName = (String) output.keySet().toArray()[0];
double error = output.get(prvName).values().stream().map(value -> Double.parseDouble(value)).mapToDouble(Double::doubleValue).sum();
if(error < best_error){
System.out.println("Found a better initialization with error: " + error);
best_error = error;
this.saveCurrentState(data);
}
}
this.loadState(data);
return best_error;
}
public void update_unobserved_inputs(HashMap<String, HashMap<String, String>> data, HashMap<String, HashMap<String, Double>> coming_error){
for(String prv_name : data.keySet()){
if(prv_name.startsWith("Feat") && coming_error.containsKey(prv_name)){
for(String assignment : data.get(prv_name).keySet()){
double curValue = Double.parseDouble(data.get(prv_name).get(assignment));
double newValue;
if(GlobalParams.regularizationTypeForHiddens.equals("L2")){
newValue = (curValue - GlobalParams.ethaForHiddens * (coming_error.get(prv_name).getOrDefault(assignment, 0.0) + GlobalParams.lambdaForHiddens * curValue));
}else{
newValue = (curValue - GlobalParams.ethaForHiddens * coming_error.get(prv_name).getOrDefault(assignment, 0.0));
newValue = Helper.softMax(newValue, GlobalParams.lambdaForHiddens);
}
data.get(prv_name).replace(assignment, "" + newValue);
}
}
}
}
public double train(HashMap<String, HashMap<String, String>> data, HashMap<String, String> targets){
this.layers[this.layers.length - 1].set_targets(targets);
double best_error = 999999999;
for(int q = 0; q < GlobalParams.numRandomRestarts; q++){
System.out.println("Random Restart #" + q);
this.randomInitialize(data);
for(int i = 1; i <= GlobalParams.numIteration; i++){
HashMap<String, HashMap<String, String>> output = new HashMap<String, HashMap<String, String>>();
for(Layer layer : this.layers){
layer.set_data(data);
layer.set_inputs(output);
output = layer.calc_output();
if(GlobalParams.debugMode){
System.out.println(layer.my2String());
System.out.println(output.toString());
}
}
String prvName = (String) output.keySet().toArray()[0];
double error = output.get(prvName).values().stream().map(value -> Double.parseDouble(value)).mapToDouble(Double::doubleValue).sum();
System.out.println("Error in iteration #" + i + ": " + error);
if(error < best_error){
this.saveCurrentState(data);
best_error = error;
}
if(GlobalParams.debugMode)
System.out.println("Starting the back prop");
HashMap<String, HashMap<String, Double>> coming_error = new HashMap<String, HashMap<String, Double>>();
for(int j = this.layers.length - 1; j >= 0; j--){
this.layers[j].calc_parameters_d(coming_error);
coming_error = this.layers[j].calc_inputs_d(coming_error);
this.layers[j].update_parameters();
if(GlobalParams.debugMode){
System.out.println(this.layers[j].my2String());
System.out.println(coming_error.toString());
}
if(layers[j].layerType.equals("linear"))
update_unobserved_inputs(data, coming_error);
}
}
}
this.loadState(data);
return best_error;
}
public HashMap<String, HashMap<String, String>> test(HashMap<String, HashMap<String, String>> data){
HashMap<String, HashMap<String, String>> output = new HashMap<String, HashMap<String, String>>();
for(int i = 0; i < this.layers.length - 1; i++){
this.layers[i].set_data(data);
this.layers[i].set_inputs(output);
output = this.layers[i].calc_output();
}
return output;
}
public void print(){
for(Layer layer : this.layers){
System.out.println(layer.my2String());
}
}
}