-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathp1.html
302 lines (246 loc) · 45.5 KB
/
p1.html
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
<!DOCTYPE html>
<html>
<link rel="shortcut icon" href="favicon.ico">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" href="highlight.css">
<meta name="author" content="sunprinceS" >
<meta property="og:image" content="joy.png"/>
<title>Machine Learning (2017, Spring)</title>
<xmp theme="cerulean" style="display:none;">
# Problem 1: Build Convolution Neural Network
Problem Description:
* tune performance
* record model structure
* record training procedure
## 範例
**[Note] 請不要直接使用助教的圖來當成作業交上來**
#### Model Structure
<center>Keras vis_utils</center>| <center> Tensorboard Graph</center>
:-------------------------:|:-------------------------:
<img src="http://i.imgur.com/XYkDQvY.png" alt="Drawing" style="width: 350px;"/> | <img src="http://i.imgur.com/Ucdf0ys.png" alt="Drawing" style="width: 1400px;"/>
#### Training Procedure
<img src="http://i.imgur.com/7w3WnTV.png" alt="Drawing" style="width: 700px;"/>
## TA hour
以下會以 **Keras** 為使用的套件來做介紹
<i class="fa fa-diamond"></i> Keywords: `keras.models`, `keras.layers`, `keras.callbacks`, `keras.utils.visualize_util.plot`, `Tensorboard`
<img src="str.png" alt="Structure" style="width: 200px;"/>
#### I/O (io.py)
<div class="highlight"><pre><span></span><span class="ch">#!/usr/bin/env python</span>
<span class="c1"># -*- coding: utf-8 -*-</span>
<span class="kn">import</span> <span class="nn">os</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="kn">as</span> <span class="nn">np</span>
<span class="kn">import</span> <span class="nn">random</span>
<span class="kn">from</span> <span class="nn">keras.utils.np\_utils</span> <span class="kn">import</span> <span class="n">to\_categorical</span>
<span class="n">base\_dir</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">dirname</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">dirname</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">dirname</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">realpath</span><span class="p">(</span><span class="vm">\_\_file\_\_</span><span class="p">))))</span>
<span class="n">data\_dir</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">base\_dir</span><span class="p">,</span><span class="s1">'data'</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">read\_dataset</span><span class="p">(</span><span class="n">mode</span><span class="o">=</span><span class="s1">'train'</span><span class="p">,</span><span class="n">isFeat</span><span class="o">=</span><span class="bp">True</span><span class="p">):</span>
<span class="sd">"""</span>
<span class="sd"> Return:</span>
<span class="sd"> # features: (int. list) list</span>
<span class="sd"> # labels: int32 2D array</span>
<span class="sd"> data\_ids: int. list</span>
<span class="sd"> """</span>
<span class="c1"># num\_data = 0</span>
<span class="n">datas</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">data\_dir</span><span class="p">,</span><span class="s1">'{}.csv'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">mode</span><span class="p">)))</span> <span class="k">as</span> <span class="nb">file</span><span class="p">:</span>
<span class="k">for</span> <span class="n">line\_id</span><span class="p">,</span><span class="n">line</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="nb">file</span><span class="p">):</span>
<span class="k">if</span> <span class="n">isFeat</span><span class="p">:</span>
<span class="n">label</span><span class="p">,</span> <span class="n">feat</span><span class="o">=</span><span class="n">line</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">','</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">\_</span><span class="p">,</span><span class="n">feat</span> <span class="o">=</span> <span class="n">line</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">','</span><span class="p">)</span>
<span class="n">feat</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">fromstring</span><span class="p">(</span><span class="n">feat</span><span class="p">,</span><span class="n">dtype</span><span class="o">=</span><span class="nb">int</span><span class="p">,</span><span class="n">sep</span><span class="o">=</span><span class="s1">' '</span><span class="p">)</span>
<span class="c1"># print(feat)</span>
<span class="n">feat</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">reshape</span><span class="p">(</span><span class="n">feat</span><span class="p">,(</span><span class="mi">48</span><span class="p">,</span><span class="mi">48</span><span class="p">,</span><span class="mi">1</span><span class="p">))</span>
<span class="k">if</span> <span class="n">isFeat</span><span class="p">:</span>
<span class="n">datas</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="n">feat</span><span class="p">,</span><span class="nb">int</span><span class="p">(</span><span class="n">label</span><span class="p">),</span><span class="n">line\_id</span><span class="p">))</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">datas</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">feat</span><span class="p">)</span>
<span class="c1"># random.shuffle(datas) # shuffle outside</span>
<span class="k">if</span> <span class="n">isFeat</span><span class="p">:</span>
<span class="n">feats</span><span class="p">,</span><span class="n">labels</span><span class="p">,</span><span class="n">line\_ids</span> <span class="o">=</span> <span class="nb">zip</span><span class="p">(</span><span class="o">*</span><span class="n">datas</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">feats</span> <span class="o">=</span> <span class="n">datas</span>
<span class="n">feats</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">asarray</span><span class="p">(</span><span class="n">feats</span><span class="p">)</span>
<span class="k">if</span> <span class="n">isFeat</span><span class="p">:</span>
<span class="n">labels</span> <span class="o">=</span> <span class="n">to\_categorical</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">asarray</span><span class="p">(</span><span class="n">labels</span><span class="p">,</span><span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">int32</span><span class="p">))</span>
<span class="k">return</span> <span class="n">feats</span><span class="p">,</span><span class="n">labels</span><span class="p">,</span><span class="n">line\_ids</span>
<span class="k">else</span><span class="p">:</span>
<span class="k">return</span> <span class="n">feats</span>
<span class="k">def</span> <span class="nf">dump\_history</span><span class="p">(</span><span class="n">store\_path</span><span class="p">,</span><span class="n">logs</span><span class="p">):</span>
<span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">store\_path</span><span class="p">,</span><span class="s1">'train\_loss'</span><span class="p">),</span><span class="s1">'a'</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
<span class="k">for</span> <span class="n">loss</span> <span class="ow">in</span> <span class="n">logs</span><span class="o">.</span><span class="n">tr\_losses</span><span class="p">:</span>
<span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s1">'{}</span><span class="se">\n</span><span class="s1">'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">loss</span><span class="p">))</span>
<span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">store\_path</span><span class="p">,</span><span class="s1">'train\_accuracy'</span><span class="p">),</span><span class="s1">'a'</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
<span class="k">for</span> <span class="n">acc</span> <span class="ow">in</span> <span class="n">logs</span><span class="o">.</span><span class="n">tr\_accs</span><span class="p">:</span>
<span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s1">'{}</span><span class="se">\n</span><span class="s1">'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">acc</span><span class="p">))</span>
<span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">store\_path</span><span class="p">,</span><span class="s1">'valid\_loss'</span><span class="p">),</span><span class="s1">'a'</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
<span class="k">for</span> <span class="n">loss</span> <span class="ow">in</span> <span class="n">logs</span><span class="o">.</span><span class="n">val\_losses</span><span class="p">:</span>
<span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s1">'{}</span><span class="se">\n</span><span class="s1">'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">loss</span><span class="p">))</span>
<span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">store\_path</span><span class="p">,</span><span class="s1">'valid\_accuracy'</span><span class="p">),</span><span class="s1">'a'</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
<span class="k">for</span> <span class="n">acc</span> <span class="ow">in</span> <span class="n">logs</span><span class="o">.</span><span class="n">val\_accs</span><span class="p">:</span>
<span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s1">'{}</span><span class="se">\n</span><span class="s1">'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">acc</span><span class="p">))</span>
</pre></div>
#### 定義模型
<div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">os</span>
<span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="p">[</span><span class="s1">'TF_CPP_MIN_LOG_LEVEL'</span><span class="p">]</span> <span class="o">=</span> <span class="s1">'2'</span>
<span class="kn">from</span> <span class="nn">keras.models</span> <span class="kn">import</span> <span class="n">Sequential</span>
<span class="kn">from</span> <span class="nn">keras.layers</span> <span class="kn">import</span> <span class="n">Input</span><span class="p">,</span><span class="n">Dense</span><span class="p">,</span><span class="n">Dropout</span><span class="p">,</span><span class="n">Flatten</span><span class="p">,</span><span class="n">Activation</span>
<span class="kn">from</span> <span class="nn">keras.layers</span> <span class="kn">import</span> <span class="n">Convolution2D</span><span class="p">,</span><span class="n">MaxPooling2D</span>
<span class="kn">from</span> <span class="nn">keras.optimizers</span> <span class="kn">import</span> <span class="n">SGD</span>
<span class="n">nb_class</span> <span class="o">=</span> <span class="mi">7</span>
<span class="k">def</span> <span class="nf">build_model</span><span class="p">(</span><span class="n">mode</span><span class="p">):</span>
<span class="sd">"""Return the Keras model for training</span>
<span class="sd"> Keyword arguments:</span>
<span class="sd"> mode: model name specified in training and predicting script</span>
<span class="sd"> """</span>
<span class="n">model</span> <span class="o">=</span> <span class="n">Sequential</span><span class="p">()</span>
<span class="k">if</span> <span class="n">mode</span> <span class="o">==</span> <span class="s1">'easy'</span><span class="p">:</span>
<span class="c1"># CNN part (you can repeat this part several times)</span>
<span class="n">model</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">Convolution2D</span><span class="p">(</span><span class="mi">8</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="n">border_mode</span><span class="o">=</span><span class="s1">'valid'</span><span class="p">,</span><span class="n">input_shape</span><span class="o">=</span><span class="p">(</span><span class="mi">48</span><span class="p">,</span><span class="mi">48</span><span class="p">,</span><span class="mi">1</span><span class="p">)))</span>
<span class="n">model</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">Activation</span><span class="p">(</span><span class="s1">'relu'</span><span class="p">))</span>
<span class="n">model</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">MaxPooling2D</span><span class="p">(</span><span class="n">pool_size</span><span class="o">=</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">)))</span>
<span class="n">model</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">Dropout</span><span class="p">(</span><span class="mf">0.8</span><span class="p">))</span>
<span class="c1"># Fully connected part</span>
<span class="n">model</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">Flatten</span><span class="p">())</span>
<span class="n">model</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">Dense</span><span class="p">(</span><span class="mi">16</span><span class="p">))</span>
<span class="n">model</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">Activation</span><span class="p">(</span><span class="s1">'relu'</span><span class="p">))</span>
<span class="n">model</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">Dense</span><span class="p">(</span><span class="n">nb_class</span><span class="p">))</span>
<span class="n">model</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">Activation</span><span class="p">(</span><span class="s1">'softmax'</span><span class="p">))</span>
<span class="n">opt</span> <span class="o">=</span> <span class="n">SGD</span><span class="p">(</span><span class="n">lr</span><span class="o">=</span><span class="mf">0.01</span><span class="p">,</span><span class="n">decay</span><span class="o">=</span><span class="mf">0.0</span><span class="p">)</span>
<span class="n">model</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="n">loss</span><span class="o">=</span><span class="s1">'categorical_crossentropy'</span><span class="p">,</span>
<span class="n">optimizer</span><span class="o">=</span><span class="n">opt</span><span class="p">,</span>
<span class="n">metrics</span><span class="o">=</span><span class="p">[</span><span class="s1">'accuracy'</span><span class="p">])</span>
<span class="n">model</span><span class="o">.</span><span class="n">summary</span><span class="p">()</span> <span class="c1"># show the whole model in terminal</span>
<span class="k">return</span> <span class="n">model</span>
</pre></div>
#### 紀錄訓練過程
<div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">keras.callbacks</span> <span class="kn">import</span> <span class="n">Callback</span>
<span class="k">class</span> <span class="nc">History</span><span class="p">(</span><span class="n">Callback</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">on_train_begin</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">logs</span><span class="o">=</span><span class="p">{}):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">tr_losses</span><span class="o">=</span><span class="p">[]</span>
<span class="bp">self</span><span class="o">.</span><span class="n">val_losses</span><span class="o">=</span><span class="p">[]</span>
<span class="bp">self</span><span class="o">.</span><span class="n">tr_accs</span><span class="o">=</span><span class="p">[]</span>
<span class="bp">self</span><span class="o">.</span><span class="n">val_accs</span><span class="o">=</span><span class="p">[]</span>
<span class="k">def</span> <span class="nf">on_epoch_end</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">epoch</span><span class="p">,</span><span class="n">logs</span><span class="o">=</span><span class="p">{}):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">tr_losses</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">logs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">'loss'</span><span class="p">))</span>
<span class="bp">self</span><span class="o">.</span><span class="n">val_losses</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">logs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">'val_loss'</span><span class="p">))</span>
<span class="bp">self</span><span class="o">.</span><span class="n">tr_accs</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">logs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">'acc'</span><span class="p">))</span>
<span class="bp">self</span><span class="o">.</span><span class="n">val_accs</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">logs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">'val_acc'</span><span class="p">))</span>
</pre></div>
#### 畫出模型 (plot_model.py)
<div class="highlight"><pre><span></span><span class="ch">#!/usr/bin/env python</span>
<span class="c1"># -*- coding: utf-8 -*-</span>
<span class="kn">import</span> <span class="nn">os</span>
<span class="kn">from</span> <span class="nn">termcolor</span> <span class="kn">import</span> <span class="n">colored</span><span class="p">,</span> <span class="n">cprint</span>
<span class="kn">import</span> <span class="nn">argparse</span>
<span class="kn">from</span> <span class="nn">keras.utils.visualize\_util</span> <span class="kn">import</span> <span class="n">plot</span>
<span class="kn">from</span> <span class="nn">keras.models</span> <span class="kn">import</span> <span class="n">load\_model</span>
<span class="n">base\_dir</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">dirname</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">dirname</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">realpath</span><span class="p">(</span><span class="vm">\_\_file\_\_</span><span class="p">)))</span>
<span class="n">exp\_dir</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">base\_dir</span><span class="p">,</span><span class="s1">'exp'</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">main</span><span class="p">():</span>
<span class="n">parser</span> <span class="o">=</span> <span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentParser</span><span class="p">(</span><span class="n">prog</span><span class="o">=</span><span class="s1">'plot\_model.py'</span><span class="p">,</span>
<span class="n">description</span><span class="o">=</span><span class="s1">'Plot the model.'</span><span class="p">)</span>
<span class="n">parser</span><span class="o">.</span><span class="n">add\_argument</span><span class="p">(</span><span class="s1">'--model'</span><span class="p">,</span><span class="nb">type</span><span class="o">=</span><span class="nb">str</span><span class="p">,</span><span class="n">default</span><span class="o">=</span><span class="s1">'simple'</span><span class="p">,</span><span class="n">choices</span><span class="o">=</span><span class="p">[</span><span class="s1">'simple'</span><span class="p">,</span><span class="s1">'easy'</span><span class="p">,</span><span class="s1">'strong'</span><span class="p">],</span>
<span class="n">metavar</span><span class="o">=</span><span class="s1">'<model>'</span><span class="p">)</span>
<span class="n">parser</span><span class="o">.</span><span class="n">add\_argument</span><span class="p">(</span><span class="s1">'--epoch'</span><span class="p">,</span><span class="nb">type</span><span class="o">=</span><span class="nb">int</span><span class="p">,</span><span class="n">metavar</span><span class="o">=</span><span class="s1">'<#epoch>'</span><span class="p">,</span><span class="n">default</span><span class="o">=</span><span class="mi">80</span><span class="p">)</span>
<span class="n">parser</span><span class="o">.</span><span class="n">add\_argument</span><span class="p">(</span><span class="s1">'--batch'</span><span class="p">,</span><span class="nb">type</span><span class="o">=</span><span class="nb">int</span><span class="p">,</span><span class="n">metavar</span><span class="o">=</span><span class="s1">'<batch\_size>'</span><span class="p">,</span><span class="n">default</span><span class="o">=</span><span class="mi">64</span><span class="p">)</span>
<span class="n">parser</span><span class="o">.</span><span class="n">add\_argument</span><span class="p">(</span><span class="s1">'--idx'</span><span class="p">,</span><span class="nb">type</span><span class="o">=</span><span class="nb">int</span><span class="p">,</span><span class="n">metavar</span><span class="o">=</span><span class="s1">'<suffix>'</span><span class="p">,</span><span class="n">required</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
<span class="n">args</span> <span class="o">=</span> <span class="n">parser</span><span class="o">.</span><span class="n">parse\_args</span><span class="p">()</span>
<span class="n">store\_path</span> <span class="o">=</span> <span class="s2">"{}\_epoch{}\_{}"</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">args</span><span class="o">.</span><span class="n">model</span><span class="p">,</span><span class="n">args</span><span class="o">.</span><span class="n">epoch</span><span class="p">,</span><span class="n">args</span><span class="o">.</span><span class="n">idx</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="n">colored</span><span class="p">(</span><span class="s2">"Loading model from {}"</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">store\_path</span><span class="p">),</span><span class="s1">'yellow'</span><span class="p">,</span><span class="n">attrs</span><span class="o">=</span><span class="p">[</span><span class="s1">'bold'</span><span class="p">]))</span>
<span class="n">model\_path</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">exp\_dir</span><span class="p">,</span><span class="n">store\_path</span><span class="p">,</span><span class="s1">'model.h5'</span><span class="p">)</span>
<span class="n">emotion\_classifier</span> <span class="o">=</span> <span class="n">load\_model</span><span class="p">(</span><span class="n">model\_path</span><span class="p">)</span>
<span class="n">emotion\_classifier</span><span class="o">.</span><span class="n">summary</span><span class="p">()</span>
<span class="n">plot</span><span class="p">(</span><span class="n">emotion\_classifier</span><span class="p">,</span><span class="n">to\_file</span><span class="o">=</span><span class="s1">'model.png'</span><span class="p">)</span>
</pre></div>
#### Put them together
<div class="highlight"><pre><span></span><span class="ch">#!/usr/bin/env python</span>
<span class="c1"># -*- coding: utf-8 -*-</span>
<span class="kn">import</span> <span class="nn">argparse</span>
<span class="kn">import</span> <span class="nn">os</span>
<span class="kn">import</span> <span class="nn">model</span>
<span class="kn">from</span> <span class="nn">utils</span> <span class="kn">import</span> <span class="o">\*</span>
<span class="n">base\_dir</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">dirname</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">dirname</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">realpath</span><span class="p">(</span><span class="vm">\_\_file\_\_</span><span class="p">)))</span>
<span class="n">exp\_dir</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">base\_dir</span><span class="p">,</span><span class="s1">'exp'</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">main</span><span class="p">():</span>
<span class="n">parser</span> <span class="o">=</span> <span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentParser</span><span class="p">(</span><span class="n">prog</span><span class="o">=</span><span class="s1">'train.py'</span><span class="p">,</span>
<span class="n">description</span><span class="o">=</span><span class="s1">'ML-Assignment training script.'</span><span class="p">)</span>
<span class="n">parser</span><span class="o">.</span><span class="n">add\_argument</span><span class="p">(</span><span class="s1">'--model'</span><span class="p">,</span><span class="nb">type</span><span class="o">=</span><span class="nb">str</span><span class="p">,</span><span class="n">default</span><span class="o">=</span><span class="s1">'simple'</span><span class="p">,</span><span class="n">choices</span><span class="o">=</span><span class="p">[</span><span class="s1">'easy'</span><span class="p">,</span><span class="s1">'simple'</span><span class="p">,</span><span class="s1">'strong'</span><span class="p">],</span>
<span class="n">metavar</span><span class="o">=</span><span class="s1">'<model>'</span><span class="p">)</span>
<span class="n">parser</span><span class="o">.</span><span class="n">add\_argument</span><span class="p">(</span><span class="s1">'--epoch'</span><span class="p">,</span><span class="nb">type</span><span class="o">=</span><span class="nb">int</span><span class="p">,</span><span class="n">metavar</span><span class="o">=</span><span class="s1">'<#epoch>'</span><span class="p">,</span><span class="n">default</span><span class="o">=</span><span class="mi">80</span><span class="p">)</span>
<span class="n">parser</span><span class="o">.</span><span class="n">add\_argument</span><span class="p">(</span><span class="s1">'--batch'</span><span class="p">,</span><span class="nb">type</span><span class="o">=</span><span class="nb">int</span><span class="p">,</span><span class="n">metavar</span><span class="o">=</span><span class="s1">'<batch\_size>'</span><span class="p">,</span><span class="n">default</span><span class="o">=</span><span class="mi">64</span><span class="p">)</span>
<span class="n">args</span> <span class="o">=</span> <span class="n">parser</span><span class="o">.</span><span class="n">parse\_args</span><span class="p">()</span>
<span class="c1"># set the path</span>
<span class="n">dir\_cnt</span> <span class="o">=</span> <span class="mi">0</span>
<span class="n">log\_path</span> <span class="o">=</span> <span class="s2">"{}\_epoch{}"</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">args</span><span class="o">.</span><span class="n">model</span><span class="p">,</span><span class="nb">str</span><span class="p">(</span><span class="n">args</span><span class="o">.</span><span class="n">epoch</span><span class="p">))</span>
<span class="n">log\_path</span> <span class="o">+=</span> <span class="s1">'\_'</span>
<span class="n">store\_path</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">exp\_dir</span><span class="p">,</span><span class="n">log\_path</span><span class="o">+</span><span class="nb">str</span><span class="p">(</span><span class="n">dir\_cnt</span><span class="p">))</span>
<span class="k">while</span> <span class="n">dir\_cnt</span> <span class="o"><</span> <span class="mi">30</span><span class="p">:</span>
<span class="k">if</span> <span class="ow">not</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">isdir</span><span class="p">(</span><span class="n">store\_path</span><span class="p">):</span>
<span class="n">os</span><span class="o">.</span><span class="n">mkdir</span><span class="p">(</span><span class="n">store\_path</span><span class="p">)</span>
<span class="k">break</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">dir\_cnt</span> <span class="o">+=</span> <span class="mi">1</span>
<span class="n">store\_path</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">exp\_dir</span><span class="p">,</span><span class="n">log\_path</span><span class="o">+</span><span class="nb">str</span><span class="p">(</span><span class="n">dir\_cnt</span><span class="p">))</span>
<span class="n">emotion\_classifier</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">build\_model</span><span class="p">(</span><span class="n">args</span><span class="o">.</span><span class="n">model</span><span class="p">)</span>
<span class="n">tr\_feats</span><span class="p">,</span><span class="n">tr\_labels</span><span class="p">,</span><span class="n">\_</span> <span class="o">=</span> <span class="n">read\_dataset</span><span class="p">(</span><span class="s1">'train'</span><span class="p">)</span>
<span class="n">dev\_feats</span><span class="p">,</span><span class="n">dev\_labels</span><span class="p">,</span><span class="n">\_</span> <span class="o">=</span> <span class="n">read\_dataset</span><span class="p">(</span><span class="s1">'valid'</span><span class="p">)</span> <span class="c1"># 10% of original training data</span>
<span class="n">history</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">History</span><span class="p">()</span>
<span class="n">emotion\_classifier</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span><span class="n">x</span> <span class="o">=</span> <span class="n">tr\_feats</span><span class="p">,</span><span class="n">y</span> <span class="o">=</span> <span class="n">tr\_labels</span><span class="p">,</span>
<span class="n">batch\_size</span><span class="o">=</span><span class="n">args</span><span class="o">.</span><span class="n">batch</span><span class="p">,</span><span class="n">nb\_epoch</span><span class="o">=</span><span class="n">args</span><span class="o">.</span><span class="n">epoch</span><span class="p">,</span><span class="n">validation\_data</span><span class="o">=</span><span class="p">(</span><span class="n">dev\_feats</span><span class="p">,</span><span class="n">dev\_labels</span><span class="p">),</span>
<span class="n">callbacks</span><span class="o">=</span><span class="p">[</span><span class="n">history</span><span class="p">])</span>
<span class="n">dump\_history</span><span class="p">(</span><span class="n">store\_path</span><span class="p">,</span><span class="n">history</span><span class="p">)</span>
<span class="n">emotion\_classifier</span><span class="o">.</span><span class="n">save</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">store\_path</span><span class="p">,</span><span class="s1">'model.h5'</span><span class="p">))</span>
<span class="k">if</span> <span class="vm">\_\_name\_\_</span> <span class="o">==</span> <span class="s2">"\_\_main\_\_"</span><span class="p">:</span>
<span class="n">main</span><span class="p">()</span>
</pre></div>
#### 預測在 testing data 上 (predict.py)
<div class="highlight"><pre><span></span><span class="ch">#!/usr/bin/env python</span>
<span class="c1"># -*- coding: utf-8 -*-</span>
<span class="kn">import</span> <span class="nn">os</span>
<span class="kn">import</span> <span class="nn">argparse</span>
<span class="kn">from</span> <span class="nn">keras.models</span> <span class="kn">import</span> <span class="n">load\_model</span>
<span class="kn">from</span> <span class="nn">termcolor</span> <span class="kn">import</span> <span class="n">colored</span><span class="p">,</span><span class="n">cprint</span>
<span class="kn">from</span> <span class="nn">utils</span> <span class="kn">import</span> <span class="o">*</span>
<span class="n">base\_dir</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">dirname</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">dirname</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">realpath</span><span class="p">(</span><span class="vm">\_\_file\_\_</span><span class="p">)))</span>
<span class="n">exp\_dir</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">base\_dir</span><span class="p">,</span><span class="s1">'exp'</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">main</span><span class="p">():</span>
<span class="n">parser</span> <span class="o">=</span> <span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentParser</span><span class="p">(</span><span class="n">prog</span><span class="o">=</span><span class="s1">'predict.py'</span><span class="p">,</span>
<span class="n">description</span><span class="o">=</span><span class="s1">'ML-Assignment3 testing script.'</span><span class="p">)</span>
<span class="n">parser</span><span class="o">.</span><span class="n">add\_argument</span><span class="p">(</span><span class="s1">'--model'</span><span class="p">,</span><span class="nb">type</span><span class="o">=</span><span class="nb">str</span><span class="p">,</span><span class="n">default</span><span class="o">=</span><span class="s1">'simple'</span><span class="p">,</span><span class="n">choices</span><span class="o">=</span><span class="p">[</span><span class="s1">'easy'</span><span class="p">,</span><span class="s1">'simple'</span><span class="p">,</span><span class="s1">'strong'</span><span class="p">],</span>
<span class="n">metavar</span><span class="o">=</span><span class="s1">'<model>'</span><span class="p">)</span>
<span class="n">parser</span><span class="o">.</span><span class="n">add\_argument</span><span class="p">(</span><span class="s1">'--epoch'</span><span class="p">,</span><span class="nb">type</span><span class="o">=</span><span class="nb">int</span><span class="p">,</span><span class="n">metavar</span><span class="o">=</span><span class="s1">'<#epoch>'</span><span class="p">,</span><span class="n">default</span><span class="o">=</span><span class="mi">80</span><span class="p">)</span>
<span class="n">parser</span><span class="o">.</span><span class="n">add\_argument</span><span class="p">(</span><span class="s1">'--batch'</span><span class="p">,</span><span class="nb">type</span><span class="o">=</span><span class="nb">int</span><span class="p">,</span><span class="n">metavar</span><span class="o">=</span><span class="s1">'<batch\_size>'</span><span class="p">,</span><span class="n">default</span><span class="o">=</span><span class="mi">64</span><span class="p">)</span>
<span class="n">parser</span><span class="o">.</span><span class="n">add\_argument</span><span class="p">(</span><span class="s1">'--idx'</span><span class="p">,</span><span class="nb">type</span><span class="o">=</span><span class="nb">int</span><span class="p">,</span><span class="n">metavar</span><span class="o">=</span><span class="s1">'<suffix>'</span><span class="p">,</span><span class="n">required</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
<span class="n">args</span> <span class="o">=</span> <span class="n">parser</span><span class="o">.</span><span class="n">parse\_args</span><span class="p">()</span>
<span class="n">store\_path</span> <span class="o">=</span> <span class="s2">"{}\_epoch{}\_{}"</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">args</span><span class="o">.</span><span class="n">model</span><span class="p">,</span><span class="n">args</span><span class="o">.</span><span class="n">epoch</span><span class="p">,</span><span class="n">args</span><span class="o">.</span><span class="n">idx</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="n">colored</span><span class="p">(</span><span class="s2">"Loading model from {}"</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">store\_path</span><span class="p">),</span><span class="s1">'yellow'</span><span class="p">,</span><span class="n">attrs</span><span class="o">=</span><span class="p">[</span><span class="s1">'bold'</span><span class="p">]))</span>
<span class="n">model\_path</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">exp\_dir</span><span class="p">,</span><span class="n">store\_path</span><span class="p">,</span><span class="s1">'model.h5'</span><span class="p">)</span>
<span class="n">emotion\_classifier</span> <span class="o">=</span> <span class="n">load\_model</span><span class="p">(</span><span class="n">model\_path</span><span class="p">)</span>
<span class="n">emotion\_classifier</span><span class="o">.</span><span class="n">summary</span><span class="p">()</span>
<span class="n">te\_feats</span><span class="p">,</span><span class="n">\_</span><span class="p">,</span><span class="n">\_</span> <span class="o">=</span> <span class="n">read\_dataset</span><span class="p">(</span><span class="s1">'test'</span><span class="p">,</span><span class="bp">False</span><span class="p">)</span>
<span class="n">ans</span> <span class="o">=</span> <span class="n">emotion\_classifier</span><span class="o">.</span><span class="n">predict\_classes</span><span class="p">(</span><span class="n">te\_feats</span><span class="p">,</span><span class="n">batch\_size</span><span class="o">=</span><span class="n">args</span><span class="o">.</span><span class="n">batch</span><span class="p">)</span>
<span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="s1">'Answer'</span><span class="p">,</span><span class="s1">'w'</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
<span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s1">'id,label</span><span class="se">\n</span><span class="s1">'</span><span class="p">)</span>
<span class="k">for</span> <span class="n">idx</span><span class="p">,</span><span class="n">a</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">ans</span><span class="p">):</span>
<span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s1">'{},{}</span><span class="se">\n</span><span class="s1">'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">idx</span><span class="p">,</span><span class="n">a</span><span class="p">))</span>
<span class="k">if</span> <span class="vm">\_\_name\_\_</span> <span class="o">==</span> <span class="s2">"\_\_main\_\_"</span><span class="p">:</span>
<span class="n">main</span><span class="p">()</span>
</pre></div>
</xmp>
</xmp> <script src="strapdown.js"></script> </html>
<footer>
<center><a href="./index.html"><i class="fa fa-home"></i></a></center>
<center><i class="fa fa-github"></i></a> Posted by: <a href="https://github.com/sunprinceS/" target="_blank">sunprinceS</a> </center>
<center><i class="fa fa-envelope"></i> Contact information: <a href="mailto:"> [email protected] </a>.</center>
<center><i class="fa fa-mortar-board"></i> Course information: <a href="http://speech.ee.ntu.edu.tw/~tlkagk/courses_ML17.html", target="_blank">Machine Learning (2017, Spring) @ National Taiwan University</a>.</center>
</footer>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-59748795-2', 'auto');
ga('send', 'pageview');
</script>
</html>