1
+ {
2
+ "nbformat" : 4 ,
3
+ "nbformat_minor" : 0 ,
4
+ "metadata" : {
5
+ "colab" : {
6
+ "name" : " fastai_py.ipynb" ,
7
+ "version" : " 0.3.2" ,
8
+ "provenance" : [],
9
+ "collapsed_sections" : []
10
+ },
11
+ "kernelspec" : {
12
+ "name" : " python3" ,
13
+ "display_name" : " Python 3"
14
+ },
15
+ "accelerator" : " GPU"
16
+ },
17
+ "cells" : [
18
+ {
19
+ "cell_type" : " code" ,
20
+ "metadata" : {
21
+ "id" : " hGILgIBehFOz" ,
22
+ "colab_type" : " code" ,
23
+ "colab" : {}
24
+ },
25
+ "source" : [
26
+ " import fastai"
27
+ ],
28
+ "execution_count" : 0 ,
29
+ "outputs" : []
30
+ },
31
+ {
32
+ "cell_type" : " code" ,
33
+ "metadata" : {
34
+ "id" : " LuK4B1zThJze" ,
35
+ "colab_type" : " code" ,
36
+ "colab" : {}
37
+ },
38
+ "source" : [
39
+ " from fastai.text import * "
40
+ ],
41
+ "execution_count" : 0 ,
42
+ "outputs" : []
43
+ },
44
+ {
45
+ "cell_type" : " code" ,
46
+ "metadata" : {
47
+ "id" : " 0sdYbjfvhXNW" ,
48
+ "colab_type" : " code" ,
49
+ "outputId" : " 6046aa28-e752-4342-dd7e-9dc455b10bdf" ,
50
+ "colab" : {
51
+ "base_uri" : " https://localhost:8080/" ,
52
+ "height" : 34
53
+ }
54
+ },
55
+ "source" : [
56
+ " fastai.text.annealing_no"
57
+ ],
58
+ "execution_count" : 7 ,
59
+ "outputs" : [
60
+ {
61
+ "output_type" : " execute_result" ,
62
+ "data" : {
63
+ "text/plain" : [
64
+ " <function fastai.callback.annealing_no>"
65
+ ]
66
+ },
67
+ "metadata" : {
68
+ "tags" : []
69
+ },
70
+ "execution_count" : 7
71
+ }
72
+ ]
73
+ },
74
+ {
75
+ "cell_type" : " code" ,
76
+ "metadata" : {
77
+ "id" : " bWdN1Y-uiBZd" ,
78
+ "colab_type" : " code" ,
79
+ "outputId" : " 51b91942-46ee-41d3-a9e8-85f74719cb16" ,
80
+ "colab" : {
81
+ "base_uri" : " https://localhost:8080/" ,
82
+ "height" : 34
83
+ }
84
+ },
85
+ "source" : [
86
+ " path = untar_data(URLs.IMDB_SAMPLE)\n " ,
87
+ " path"
88
+ ],
89
+ "execution_count" : 8 ,
90
+ "outputs" : [
91
+ {
92
+ "output_type" : " execute_result" ,
93
+ "data" : {
94
+ "text/plain" : [
95
+ " PosixPath('/root/.fastai/data/imdb_sample')"
96
+ ]
97
+ },
98
+ "metadata" : {
99
+ "tags" : []
100
+ },
101
+ "execution_count" : 8
102
+ }
103
+ ]
104
+ },
105
+ {
106
+ "cell_type" : " code" ,
107
+ "metadata" : {
108
+ "id" : " 5NYLx5gziR54" ,
109
+ "colab_type" : " code" ,
110
+ "outputId" : " 14096442-f8c1-40b5-df31-16ddf0396da6" ,
111
+ "colab" : {
112
+ "base_uri" : " https://localhost:8080/" ,
113
+ "height" : 204
114
+ }
115
+ },
116
+ "source" : [
117
+ " df = pd.read_csv(path/'texts.csv')\n " ,
118
+ " df.head()"
119
+ ],
120
+ "execution_count" : 9 ,
121
+ "outputs" : [
122
+ {
123
+ "output_type" : " execute_result" ,
124
+ "data" : {
125
+ "text/html" : [
126
+ " <div>\n " ,
127
+ " <style scoped>\n " ,
128
+ " .dataframe tbody tr th:only-of-type {\n " ,
129
+ " vertical-align: middle;\n " ,
130
+ " }\n " ,
131
+ " \n " ,
132
+ " .dataframe tbody tr th {\n " ,
133
+ " vertical-align: top;\n " ,
134
+ " }\n " ,
135
+ " \n " ,
136
+ " .dataframe thead th {\n " ,
137
+ " text-align: right;\n " ,
138
+ " }\n " ,
139
+ " </style>\n " ,
140
+ " <table border=\" 1\" class=\" dataframe\" >\n " ,
141
+ " <thead>\n " ,
142
+ " <tr style=\" text-align: right;\" >\n " ,
143
+ " <th></th>\n " ,
144
+ " <th>label</th>\n " ,
145
+ " <th>text</th>\n " ,
146
+ " <th>is_valid</th>\n " ,
147
+ " </tr>\n " ,
148
+ " </thead>\n " ,
149
+ " <tbody>\n " ,
150
+ " <tr>\n " ,
151
+ " <th>0</th>\n " ,
152
+ " <td>negative</td>\n " ,
153
+ " <td>Un-bleeping-believable! Meg Ryan doesn't even ...</td>\n " ,
154
+ " <td>False</td>\n " ,
155
+ " </tr>\n " ,
156
+ " <tr>\n " ,
157
+ " <th>1</th>\n " ,
158
+ " <td>positive</td>\n " ,
159
+ " <td>This is a extremely well-made film. The acting...</td>\n " ,
160
+ " <td>False</td>\n " ,
161
+ " </tr>\n " ,
162
+ " <tr>\n " ,
163
+ " <th>2</th>\n " ,
164
+ " <td>negative</td>\n " ,
165
+ " <td>Every once in a long while a movie will come a...</td>\n " ,
166
+ " <td>False</td>\n " ,
167
+ " </tr>\n " ,
168
+ " <tr>\n " ,
169
+ " <th>3</th>\n " ,
170
+ " <td>positive</td>\n " ,
171
+ " <td>Name just says it all. I watched this movie wi...</td>\n " ,
172
+ " <td>False</td>\n " ,
173
+ " </tr>\n " ,
174
+ " <tr>\n " ,
175
+ " <th>4</th>\n " ,
176
+ " <td>negative</td>\n " ,
177
+ " <td>This movie succeeds at being one of the most u...</td>\n " ,
178
+ " <td>False</td>\n " ,
179
+ " </tr>\n " ,
180
+ " </tbody>\n " ,
181
+ " </table>\n " ,
182
+ " </div>"
183
+ ],
184
+ "text/plain" : [
185
+ " label text is_valid\n " ,
186
+ " 0 negative Un-bleeping-believable! Meg Ryan doesn't even ... False\n " ,
187
+ " 1 positive This is a extremely well-made film. The acting... False\n " ,
188
+ " 2 negative Every once in a long while a movie will come a... False\n " ,
189
+ " 3 positive Name just says it all. I watched this movie wi... False\n " ,
190
+ " 4 negative This movie succeeds at being one of the most u... False"
191
+ ]
192
+ },
193
+ "metadata" : {
194
+ "tags" : []
195
+ },
196
+ "execution_count" : 9
197
+ }
198
+ ]
199
+ },
200
+ {
201
+ "cell_type" : " code" ,
202
+ "metadata" : {
203
+ "id" : " 4KFlG9RLilnm" ,
204
+ "colab_type" : " code" ,
205
+ "colab" : {}
206
+ },
207
+ "source" : [
208
+ " # Language model data\n " ,
209
+ " data_lm = TextLMDataBunch.from_csv(path, 'texts.csv')\n " ,
210
+ " # Classifier model data\n " ,
211
+ " data_clas = TextClasDataBunch.from_csv(path, 'texts.csv', vocab=data_lm.train_ds.vocab, bs=32)"
212
+ ],
213
+ "execution_count" : 0 ,
214
+ "outputs" : []
215
+ },
216
+ {
217
+ "cell_type" : " code" ,
218
+ "metadata" : {
219
+ "id" : " 1TPi_DfTio26" ,
220
+ "colab_type" : " code" ,
221
+ "outputId" : " 11b99a8e-ac0c-4b16-d7ac-4ce7bb8fb865" ,
222
+ "colab" : {
223
+ "base_uri" : " https://localhost:8080/" ,
224
+ "height" : 80
225
+ }
226
+ },
227
+ "source" : [
228
+ " learn = language_model_learner(data_lm, AWD_LSTM, drop_mult=0.5)\n " ,
229
+ " learn.fit_one_cycle(1, 1e-2)"
230
+ ],
231
+ "execution_count" : 11 ,
232
+ "outputs" : [
233
+ {
234
+ "output_type" : " display_data" ,
235
+ "data" : {
236
+ "text/html" : [
237
+ " <table border=\" 1\" class=\" dataframe\" >\n " ,
238
+ " <thead>\n " ,
239
+ " <tr style=\" text-align: left;\" >\n " ,
240
+ " <th>epoch</th>\n " ,
241
+ " <th>train_loss</th>\n " ,
242
+ " <th>valid_loss</th>\n " ,
243
+ " <th>accuracy</th>\n " ,
244
+ " <th>time</th>\n " ,
245
+ " </tr>\n " ,
246
+ " </thead>\n " ,
247
+ " <tbody>\n " ,
248
+ " <tr>\n " ,
249
+ " <td>0</td>\n " ,
250
+ " <td>4.302421</td>\n " ,
251
+ " <td>3.922743</td>\n " ,
252
+ " <td>0.285108</td>\n " ,
253
+ " <td>00:10</td>\n " ,
254
+ " </tr>\n " ,
255
+ " </tbody>\n " ,
256
+ " </table>"
257
+ ],
258
+ "text/plain" : [
259
+ " <IPython.core.display.HTML object>"
260
+ ]
261
+ },
262
+ "metadata" : {
263
+ "tags" : []
264
+ }
265
+ }
266
+ ]
267
+ },
268
+ {
269
+ "cell_type" : " code" ,
270
+ "metadata" : {
271
+ "id" : " 45AIvHEDjBdT" ,
272
+ "colab_type" : " code" ,
273
+ "colab" : {
274
+ "base_uri" : " https://localhost:8080/" ,
275
+ "height" : 54
276
+ },
277
+ "outputId" : " 9526f459-7cfe-4441-d97d-c3c4d301d515"
278
+ },
279
+ "source" : [
280
+ " learn.predict(\" Life\" , 50)"
281
+ ],
282
+ "execution_count" : 15 ,
283
+ "outputs" : [
284
+ {
285
+ "output_type" : " execute_result" ,
286
+ "data" : {
287
+ "text/plain" : [
288
+ " 'Life : The comic book of Michael Jackson u II ? ( 1949 ) , Dream Days in the Woods Hotel , a core movie sequence , bears their names , their names in their respective names : A. G.'"
289
+ ]
290
+ },
291
+ "metadata" : {
292
+ "tags" : []
293
+ },
294
+ "execution_count" : 15
295
+ }
296
+ ]
297
+ },
298
+ {
299
+ "cell_type" : " code" ,
300
+ "metadata" : {
301
+ "id" : " 7_GTIAbJNmt4" ,
302
+ "colab_type" : " code" ,
303
+ "colab" : {}
304
+ },
305
+ "source" : [
306
+ " "
307
+ ],
308
+ "execution_count" : 0 ,
309
+ "outputs" : []
310
+ }
311
+ ]
312
+ }
0 commit comments