-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPT_Language_Model-1Cycle-Tests.py
More file actions
188 lines (87 loc) · 2.92 KB
/
PT_Language_Model-1Cycle-Tests.py
File metadata and controls
188 lines (87 loc) · 2.92 KB
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
# coding: utf-8
# # General-domain LM pretraining
# In[1]:
import json
import pathlib
from fastai.text import *
import numpy as np
import pandas as pd
import html
# In[2]:
BOS = 'xbos' # beginning-of-sentence tag
FLD = 'xfld' # data field tag
PATH = pathlib.Path("lm/pt/data/")
# In[3]:
LM_PATH=Path('lm/pt/pt_lm/')
LM_PATH.mkdir(exist_ok=True)
# ## Loading dataset
# In[4]:
tok_trn = np.load(LM_PATH/'tmp'/'tok_trn.npy')
tok_val = np.load(LM_PATH/'tmp'/'tok_val.npy')
# In[5]:
# Identify the most common tokens and numericalizing the text
freq = Counter(p for o in tok_trn for p in o)
freq.most_common(25)
# In[6]:
em_sz,nh,nl = 400,1150,3
wd=1e-7
bptt=70
opt_fn = partial(optim.Adam, betas=(0.8, 0.99))
# ### 1-cycle
# In[7]:
# Truncating our vocab to ignore the rare words
max_vocab = 30000
min_freq = 5
itos = [o for o,c in freq.most_common(max_vocab) if c>min_freq] # getting rid of the rare words
itos.insert(0, '_pad_') #
itos.insert(0, '_unk_') # itos is the list of all the strings in the vocab
# In[8]:
# creating a index-key dictionary for our vocabulary
stoi = collections.defaultdict(lambda:0, {v:k for k,v in enumerate(itos)})
len(itos)
# In[9]:
# creating a index representation for our train and validation dataset
trn_lm = np.array([[stoi[o] for o in p] for p in tok_trn])
val_lm = np.array([[stoi[o] for o in p] for p in tok_val])
# In[10]:
# saving our indexed representation of our dataset to disk
# we also save the index-word mapping to retrieve the complete text representation from these numpy arrays
np.save(LM_PATH/'tmp'/'trn_ids.npy', trn_lm)
np.save(LM_PATH/'tmp'/'val_ids.npy', val_lm)
pickle.dump(itos, open(LM_PATH/'tmp'/'itos.pkl', 'wb'))
# In[11]:
# Loading the indexed representation of our dataset from disk
# we also load the index-word mapping to to help us convert the indexes to word datasets, if need be.
trn_lm = np.load(LM_PATH/'tmp'/'trn_ids.npy')
val_lm = np.load(LM_PATH/'tmp'/'val_ids.npy')
itos = pickle.load(open(LM_PATH/'tmp'/'itos.pkl', 'rb'))
# In[12]:
# checking vocabulary size
vs=len(itos)
vs,len(trn_lm)
# In[13]:
bs = 52
# In[14]:
trn_dl = LanguageModelLoader(np.concatenate(trn_lm), bs, bptt)
val_dl = LanguageModelLoader(np.concatenate(val_lm), bs, bptt)
md = LanguageModelData(PATH, 1, vs, trn_dl, val_dl, bs=bs, bptt=bptt)
# In[15]:
opt_fn = partial(optim.SGD, momentum=0.9)
# In[16]:
drops = np.array([0.25, 0.1, 0.2, 0.02, 0.15])*0.05 #the higher the lr, the lower dp
# In[17]:
learner= md.get_model(opt_fn, em_sz, nh, nl,
dropouti=drops[0], dropout=drops[1], wdrop=drops[2], dropoute=drops[3], dropouth=drops[4])
# In[18]:
learner.load('lm_PT_1_cycle')
# In[19]:
learner.metrics = [accuracy]
learner.unfreeze()
# In[20]:
lr = 5
# In[21]:
learner.fit(lr, 1, cycle_len=10, use_clr_beta=(10,10,0.95,0.85))
# In[ ]:
learner.save('lm_PT_1_cycle_10_epochs')
# In[ ]:
learner.save_encoder('lm_PT_1_cycle_10_epochs_enc')