-
Notifications
You must be signed in to change notification settings - Fork 0
/
TFRelationTagger.java
344 lines (308 loc) · 13.5 KB
/
TFRelationTagger.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
package edu.nyu.jetlite;
/**
* https://github.com/tensorflow/tensorflow/blob/r1.3/tensorflow/java/src/main/java/org/tensorflow/examples/LabelImage.java
*/
import java.io.IOException;
import java.io.PrintStream;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.util.Random;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import org.tensorflow.DataType;
import org.tensorflow.Graph;
import org.tensorflow.Output;
import org.tensorflow.Session;
import org.tensorflow.Tensor;
import org.tensorflow.TensorFlow;
public class TFRelationTagger implements AutoCloseable {
public static String MODEL_DIR = "RTFmodel/";
public static String DATA_DIR = "data/";
public static Path PRETRAINED_WORD_EMBEDDINGS_PATH = Paths.get(DATA_DIR, "glove.6B.300d.txt");
public static Path PRETRAINED_BROWN_CLUSTERS_PATH = Paths.get(DATA_DIR, "brownClusters10-2014.txt");
public static int INPUT_LEN = 15; // Max distance between two entity heads for an example to be considered.
public static int POS_EMBED_LEN = INPUT_LEN * 2 - 1;
public static boolean DIVIDE_TOKENS = true; // If true, divides tokens with spaces into separate vars.
public static int WORD_EMBED_LEN = 300; // # of dims in word embedding
public static int BROWN_CLUSTER_LEN = 17; // # of dims in brown clusters
public static int WORD_FEAT_LEN = WORD_EMBED_LEN + BROWN_CLUSTER_LEN + 2; // per word features: word embedding + brown clusters + 2 positional inputs
public static String[] BASIC_LABELS = new String[] { "OTHER", "GEN-AFF", "ORG-AFF",
"PART-WHOLE", "PER-SOC", "PHYS", "ART" };
public static String[] SUBTYPE_LABELS = new String[] { "OTHER", "GEN-AFF:Citizen-Resident-Religion-Ethnicity",
"ORG-AFF:Employment", "PART-WHOLE:Subsidiary", "ORG-AFF:Membership", "ORG-AFF:Ownership", "PER-SOC:Business",
"GEN-AFF:Org-Location", "PHYS:Located", "PART-WHOLE:Geographical", "ORG-AFF:Founder",
"ART:User-Owner-Inventor-Manufacturer", "PHYS:Near", "PER-SOC:Family", "PART-WHOLE:Artifact",
"PER-SOC:Lasting-Personal", "ORG-AFF:Student-Alum", "ORG-AFF:Investor-Shareholder", "ORG-AFF:Sports-Affiliation" };
public static String[] SUBTYPE_WITH_ORDER_LABELS = new String[] { "OTHER", "GEN-AFF:Citizen-Resident-Religion-Ethnicity",
"ORG-AFF:Employment-1", "ORG-AFF:Employment", "GEN-AFF:Citizen-Resident-Religion-Ethnicity-1",
"PART-WHOLE:Subsidiary-1", "ORG-AFF:Membership", "ORG-AFF:Ownership", "PER-SOC:Business",
"GEN-AFF:Org-Location", "PHYS:Located-1", "PHYS:Located", "PART-WHOLE:Geographical", "ORG-AFF:Founder-1",
"ORG-AFF:Membership-1", "PART-WHOLE:Geographical-1", "ART:User-Owner-Inventor-Manufacturer", "PHYS:Near",
"ART:User-Owner-Inventor-Manufacturer-1", "PART-WHOLE:Subsidiary", "PHYS:Near-1", "PER-SOC:Family",
"GEN-AFF:Org-Location-1", "PER-SOC:Family-1", "PART-WHOLE:Artifact-1", "PER-SOC:Business-1",
"PART-WHOLE:Artifact", "PER-SOC:Lasting-Personal", "ORG-AFF:Student-Alum-1", "ORG-AFF:Founder",
"ORG-AFF:Student-Alum", "ORG-AFF:Ownership-1", "ORG-AFF:Investor-Shareholder", "ORG-AFF:Investor-Shareholder-1",
"ORG-AFF:Sports-Affiliation", "ORG-AFF:Sports-Affiliation-1", "PER-SOC:Lasting-Personal-1" };
private static byte[] readAllBytesOrExit(Path path) {
try {
return Files.readAllBytes(path);
} catch (IOException e) {
System.err.println("Failed to read [" + path + "]: " + e.getMessage());
System.exit(1);
}
return null;
}
public static float[] convertToFloatArray(double[] doubleArray) {
float[] floatArray = new float[doubleArray.length];
for(int i = 0; i < doubleArray.length; i++) floatArray[i] = (float) doubleArray[i];
return floatArray;
}
public static float[] resizeArray(float[] arr, int newSize) {
float[] newArr = new float[newSize];
for(int i = 0; i < newSize && i < arr.length; i++) {
newArr[i] = arr[i];
}
return newArr;
}
public static Map<String, float[]> getAllEmbeddings() {
Map<String, float[]> tokens = new HashMap<String, float[]>();
try(Stream<String> stream = Files.lines(PRETRAINED_WORD_EMBEDDINGS_PATH)) {
stream.forEach(l -> {
String[] data = l.split(" ");
double[] vals = Arrays.stream(data).skip(1)
.mapToDouble(Double::parseDouble)
.toArray();
tokens.put(data[0], convertToFloatArray(vals));
});
return tokens;
} catch(IOException e) {
System.out.println(e);
} catch(Exception e) {
System.out.println(e);
}
return null;
}
public static Map<String, float[]> getAllClusters() {
Map<String, float[]> tokens = new HashMap<String, float[]>();
int line = 0;
try(Stream<String> stream = Files.lines(PRETRAINED_BROWN_CLUSTERS_PATH)) {
stream.forEach(l -> {
String[] data = l.split("\\s");
double[] vals = Arrays.stream(data[0].split(""))
.mapToDouble(Double::parseDouble)
.toArray();
tokens.put(data[1], resizeArray(convertToFloatArray(vals), BROWN_CLUSTER_LEN));
});
return tokens;
} catch(IOException e) {
System.out.println(e);
} catch(Exception e) {
System.out.println(e);
}
return null;
}
public static float[] UNKNOWN_WORD_EMBEDDING = new float[WORD_EMBED_LEN];
public static float[] UNKNOWN_BROWN_CLUSTER = new float[BROWN_CLUSTER_LEN];
// The detail with which to classify a relation
RelationTagger.TypeDetail typeDetail;
// Number of outcome classes
int numClasses;
// Word embeddings
Map<String, float[]> wordEmbeddings;
// Brown clusters
Map<String, float[]> brownClusters;
// Class ID to class name mapping
Map<Integer, String> classMapping;
// Model path
Path modelPath;
// Graph Definiton
byte[] graphDef;
public TFRelationTagger(RelationTagger.TypeDetail typeDetail) {
this.typeDetail = typeDetail;
String[] labels;
switch(typeDetail) {
case Basic:
default:
numClasses = 7;
labels = BASIC_LABELS;
modelPath = Paths.get(MODEL_DIR, "optimized_basic.pb");
break;
case Subtype:
numClasses = 19;
labels = SUBTYPE_LABELS;
modelPath = Paths.get(MODEL_DIR, "optimized_subtype.pb");
break;
case SubtypeWithOrder:
numClasses = 37;
labels = SUBTYPE_WITH_ORDER_LABELS;
modelPath = Paths.get(MODEL_DIR, "optimized_subtype_with_order.pb");
break;
}
graphDef = readAllBytesOrExit(modelPath);
classMapping = IntStream.range(0, labels.length).boxed().collect(Collectors.toMap(i -> i, i -> labels[i]));
wordEmbeddings = getAllEmbeddings();
brownClusters = getAllClusters();
}
private float[] getWordEmbedding(String word) {
float[] emb = wordEmbeddings.get(word);
if(emb != null) {
return emb;
} else {
return UNKNOWN_WORD_EMBEDDING;
}
}
private float[] getBrownCluster(String word) {
float[] clus = brownClusters.get(word);
return clus != null ? clus : UNKNOWN_BROWN_CLUSTER;
}
// Convert a single example into a float tensor for graph input
public float[][][] convertSingle(List<WordInfo> example) {
float[][][] input = new float[1][INPUT_LEN][WORD_FEAT_LEN];
for(int j = 0; j < example.size(); j++) {
WordInfo info = example.get(j);
float[] emb = getWordEmbedding(info.word);
int k;
for(k = 0; k < emb.length; k++) {
input[0][j][k] = emb[k];
}
float[] clus = getBrownCluster(info.word);
for(int t = 0; t < clus.length; t++) {
input[0][j][k] = clus[t];
k++;
}
input[0][j][k] = info.distance1;
input[0][j][k+1] = info.distance2;
}
return input;
}
// Convert a list of examples into a float tensor for graph input
public float[][][] convertMultiple(List<List<WordInfo>> examples) {
float[][][] input = new float[examples.size()][INPUT_LEN][WORD_FEAT_LEN];
for(int i = 0; i < examples.size(); i++) {
List<WordInfo> example = examples.get(i);
for(int j = 0; j < example.size(); j++) {
WordInfo info = example.get(j);
float[] emb = getWordEmbedding(info.word);
int k;
for(k = 0; k < emb.length; k++) {
input[i][j][k] = emb[k];
}
float[] clus = getBrownCluster(info.word);
for(int t = 0; t < clus.length; t++) {
input[i][j][k] = clus[t];
k++;
}
input[i][j][k] = info.distance1;
input[i][j][k+1] = info.distance2;
}
}
return input;
}
// Predict a single example
public String predictSingle(List<WordInfo> example) {
float[][][] input = convertSingle(example);
return predict(input).get(0);
}
// Predict a list of examples
public List<String> predictMultiple(List<List<WordInfo>> examples) {
float[][][] input = convertMultiple(examples);
return predict(input);
}
private List<String> predict(float[][][] input) {
float[][] res = runGraph(input);
List<String> predictions = new ArrayList<String>();
for(int i = 0; i < res.length; i++) {
predictions.add(classMapping.get(maxIndex(res[i])));
}
return predictions;
}
private float[][] runGraph(float[][][] input) {
try(Graph g = new Graph()) {
g.importGraphDef(graphDef);
try(Session s = new Session(g);
Tensor inputTensor = Tensor.create(input);
Tensor keepProb = Tensor.create(1f);
Tensor result = s.runner().feed("input", inputTensor).feed("keep_prob", keepProb).fetch("output").run().get(0)) {
return result.copyTo(new float[input.length][numClasses]);
}
}
}
public void close() { }
public static void main(String[] args) {
Random generator = new Random();
int nExamples = 1;
int seqLen = 15;
int featLen = 302;
float[][][] input = new float[nExamples][seqLen][featLen];
for(int i = 0; i < nExamples; i++) {
for(int j = 0; j < seqLen; j++) {
for(int k = 0; k < featLen; k++) {
input[i][j][k] = generator.nextFloat();
}
}
}
// preprocess the image to feed into inception model
Tensor keepProb = Tensor.create(1f);
byte[] graphDef = readAllBytesOrExit(Paths.get(MODEL_DIR, "frozen_basic.pb"));
float[][] output = executeGraph(graphDef, createInput(input), keepProb);
for(int i = 0; i < nExamples; i++) {
System.out.println(maxIndex(output[i]));
}
}
private static Tensor createInput(float[][][] input) {
try (Graph g = new Graph()) {
GraphBuilder b = new GraphBuilder(g);
// Since the graph is being constructed once per execution here, we can use a constant for the
// input image. If the graph were to be re-used for multiple input images, a placeholder would
// have been more appropriate.
final Output output = b.constant("input", input);
try (Session s = new Session(g)) {
return s.runner().fetch(output.op().name()).run().get(0);
}
}
}
private static float[][] executeGraph(byte[] graphDef, Tensor image, Tensor keepProb) {
try (Graph g = new Graph()) {
g.importGraphDef(graphDef);
try (Session s = new Session(g);
Tensor result = s.runner().feed("input", image).feed("keep_prob", keepProb).fetch("output").run().get(0)) {
return result.copyTo(new float[1][7]);
}
}
}
private static int maxIndex(float[] probabilities) {
int best = 0;
for (int i = 1; i < probabilities.length; ++i) {
if (probabilities[i] > probabilities[best]) {
best = i;
}
}
return best;
}
// In the fullness of time, equivalents of the methods of this class should be auto-generated from
// the OpDefs linked into libtensorflow_jni.so. That would match what is done in other languages
// like Python, C++ and Go.
static class GraphBuilder {
GraphBuilder(Graph g) {
this.g = g;
}
Output constant(String name, Object value) {
try (Tensor t = Tensor.create(value)) {
return g.opBuilder("Const", name)
.setAttr("dtype", t.dataType())
.setAttr("value", t)
.build()
.output(0);
}
}
private Graph g;
}
}