-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
62 lines (53 loc) · 2.14 KB
/
main.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
from Model import Model
import numpy as np
import pickle
# Note: Total sentences are 2113 in number.
if __name__ == '__main__':
#####################################
# don't change this
np.random.seed(43)
# hyper parameters to be tweaked here
load_rnn_from_pickle = False
training_size = 2113 # maximum of 2113
l_rate = 0.01
mini_batch_size = 2
reg_cost = 0.001
epochs = 100
activation_func = "tanh"
dim = 100
######################################
# perform 60-15-25 percent split into train-val-test sets
train = np.ceil(0.6 * training_size)
val = np.ceil(0.15 * training_size)
test = training_size - train - val
# load parsed trees from file in PTB format
if load_rnn_from_pickle is False:
with open('WikiTreebankQuartiles_second.txt', 'rb') as fh:
RNN = Model(dim=dim, l_rate=l_rate, mini_batch=mini_batch_size, reg_cost=reg_cost,
epochs=epochs, activation_func=activation_func)
for i, line in enumerate(fh):
p = line.find(' ')
ptb_string = line[p + 1:]
rid = line[:p]
# Add to the list of trees
RNN.add_tree(ptb_string, rid)
with open('rnn.pickle', 'wb') as pickle_file:
pickle.dump(RNN, pickle_file, pickle.HIGHEST_PROTOCOL)
else:
with open('rnn.pickle', 'rb') as pickle_file:
RNN = pickle.load(pickle_file)
indices = np.arange(0, training_size)
# create separate indices for the 3 data sets
np.random.shuffle(RNN.trees)
np.random.shuffle(indices)
RNN.tree_train = indices[:train]
RNN.tree_val = indices[train:train + val]
RNN.tree_test = indices[train + val:]
# print RNN.cross_validate()
RNN.train(True)
# RNN.check_model_veracity()
print "Test Cost Function, Accuracy, Incorrectly classified sentence Ids"
print RNN.test()
hyper_params = "training_size={0}\nl_rate={1}\nmini_batch_size={2}\nreg_cost={3}\nepochs={4}\ndim={5}\nactivation_func={6}".format(
training_size, l_rate, mini_batch_size, reg_cost, epochs, dim, activation_func)
print hyper_params