-
Notifications
You must be signed in to change notification settings - Fork 3
/
generate_vectors.py
203 lines (147 loc) · 5.87 KB
/
generate_vectors.py
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
from __future__ import unicode_literals, print_function, division
from io import open
import unicodedata
import string
import re
import random
from random import shuffle
import torch
import torch.nn as nn
from torch.autograd import Variable
from torch import optim
import torch.nn.functional as F
import argparse
import sys
import os
import time
import math
import pickle
from tasks import *
from training import *
from models import *
from evaluation import *
from role_assignment_functions import *
# Given a trained model, generate encodings for sequences from that model
parser = argparse.ArgumentParser()
parser.add_argument("--prefix", help="prefix for your training/dev data", type=str, default="digits")
parser.add_argument("--encoder", help="encoder type", type=str, default="ltr")
parser.add_argument("--decoder", help="decoder type", type=str, default="ltr")
parser.add_argument("--task", help="training task", type=str, default="auto")
parser.add_argument("--vocab_size", help="vocab size for the training language", type=int, default=10)
parser.add_argument("--emb_size", help="embedding size", type=int, default=10)
parser.add_argument("--hidden_size", help="hidden size", type=int, default=60)
parser.add_argument("--model_prefix", help="prefix for the trained model", type=str, default=None)
args = parser.parse_args()
use_cuda = torch.cuda.is_available()
# Load data
with open('data/' + args.prefix + '.train.pkl', 'rb') as handle:
train_set = pickle.load(handle)
with open('data/' + args.prefix + '.dev.pkl', 'rb') as handle:
dev_set = pickle.load(handle)
with open('data/' + args.prefix + '.test.pkl', 'rb') as handle:
test_set = pickle.load(handle)
input_to_output = lambda sequence: transform(sequence, args.task)
# Load the models
if args.encoder == "ltr":
encoder = EncoderRNN(args.vocab_size, args.emb_size, args.hidden_size)
elif args.encoder == "bi":
encoder = EncoderBiRNN(args.vocab_size, args.emb_size, args.hidden_size)
elif args.encoder == "tree":
encoder = EncoderTreeRNN(args.vocab_size, args.emb_size, args.hidden_size)
else:
print("Invalid encoder type")
if args.decoder == "ltr":
decoder = DecoderRNN(args.vocab_size, args.emb_size, args.hidden_size)
elif args.decoder == "bi":
decoder = DecoderBiRNN(args.vocab_size, args.emb_size, args.hidden_size)
elif args.decoder == "tree":
decoder = DecoderTreeRNN(args.vocab_size, args.emb_size, args.hidden_size)
else:
print("Invalid decoder type")
if use_cuda:
encoder = encoder.cuda()
decoder = decoder.cuda()
encoder.load_state_dict(torch.load("models/encoder_" + args.model_prefix + ".weights"))
decoder.load_state_dict(torch.load("models/decoder_" + args.model_prefix + ".weights"))
# To be populated with 2-tuples of the form (sequence, encoding), where each
# sequence is drawn from the test set
data_from_test = []
accurate = 0
total = 0
# This loop populates the data_from_test list
# It also records performance on the test set
# and prints any incorrect predictions
for example in test_set:
pred, encoding = evaluate(encoder, decoder, example, input_to_output)
data_from_test.append((example, encoding))
if tuple(input_to_output(example)) == tuple(pred):
accurate += 1
else:
# For each incorrect prediction, predict that input and
# the incorrect predicted output
#print(example, tuple(pred))
pass
total += 1
print(args.model_prefix, "test_acc", accurate, total)
# To be populated with 2-tuples of the form (sequence, encoding), where each
# sequence is drawn from the test set
data_from_dev = []
accurate = 0
total = 0
# This loop populates the data_from_test list.
# It also records performance on the test set
# and prints any incorrect predictions
for example in dev_set:
pred, encoding = evaluate(encoder, decoder, example, input_to_output)
data_from_dev.append((example, encoding))
if tuple(input_to_output(example)) == tuple(pred):
accurate += 1
else:
# For each incorrect prediction, predict that input and
# the incorrect predicted output
#print(example, tuple(pred))
pass
total += 1
print(args.model_prefix, "dev_acc", accurate, total)
# To be populated with 2-tuples of the form (sequence, encoding), where each
# sequence is drawn from the test set
data_from_train = []
accurate = 0
total = 0
# This loop populates the data_from_test list.
# It also records performance on the test set
# and prints any incorrect predictions
for example in train_set:
pred, encoding = evaluate(encoder, decoder, example, input_to_output)
data_from_train.append((example, encoding))
if tuple(input_to_output(example)) == tuple(pred):
accurate += 1
else:
# For each incorrect prediction, predict that input and
# the incorrect predicted output
#print(example, tuple(pred))
pass
total += 1
print(args.model_prefix, "train_acc", accurate, total)
# Save all encodings to a file
fo_train = open("data/" + args.model_prefix + ".data_from_train", "w")
fo_dev = open("data/" + args.model_prefix + ".data_from_dev", "w")
fo_test = open("data/" + args.model_prefix + ".data_from_test", "w")
for training_item in data_from_train:
sequence = training_item[0]
encoding = training_item[1].data.cpu().numpy()[0][0]
sequence = [str(x) for x in sequence]
encoding = [str(x) for x in encoding]
fo_train.write(" ".join(sequence) + "\t" + " ".join(encoding) + "\n")
for dev_item in data_from_dev:
sequence = dev_item[0]
encoding = dev_item[1].data.cpu().numpy()[0][0]
sequence = [str(x) for x in sequence]
encoding = [str(x) for x in encoding]
fo_dev.write(" ".join(sequence) + "\t" + " ".join(encoding) + "\n")
for test_item in data_from_test:
sequence = test_item[0]
encoding = test_item[1].data.cpu().numpy()[0][0]
sequence = [str(x) for x in sequence]
encoding = [str(x) for x in encoding]
fo_test.write(" ".join(sequence) + "\t" + " ".join(encoding) + "\n")