You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

fastnlp_tutorial_3.ipynb 6.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "markdown",
  5. "id": "213d538c",
  6. "metadata": {},
  7. "source": [
  8. "# T3. dataloader 的内部结构和基本使用\n",
  9. "\n",
  10. "  1   \n",
  11. " \n",
  12. "    1.1   \n",
  13. "\n",
  14. "    1.2   \n",
  15. "\n",
  16. "  2   \n",
  17. "\n",
  18. "    2.1   \n",
  19. "\n",
  20. "    2.2   \n",
  21. "\n",
  22. "  3   \n",
  23. " \n",
  24. "    3.1   \n",
  25. "\n",
  26. "    3.2   "
  27. ]
  28. },
  29. {
  30. "cell_type": "markdown",
  31. "id": "d74d0523",
  32. "metadata": {},
  33. "source": [
  34. "## 3. fastNLP 中的 dataloader\n",
  35. "\n",
  36. "### 3.1 collator 的概念与使用\n",
  37. "\n",
  38. "在`fastNLP 0.8`中,在数据加载模块`DataLoader`之前,还存在其他的一些模块,负责例如对文本数据\n",
  39. "\n",
  40. "  进行补零对齐,即 **核对器`collator`模块**,进行分词标注,即 **分词器`tokenizer`模块**\n",
  41. "\n",
  42. "  本节将对`fastNLP`中的核对器`collator`等展开介绍,分词器`tokenizer`将在下一节中详细介绍\n",
  43. "\n",
  44. "在`fastNLP 0.8`中,**核对器`collator`模块负责文本序列的补零对齐**,通过"
  45. ]
  46. },
  47. {
  48. "cell_type": "code",
  49. "execution_count": null,
  50. "id": "aca72b49",
  51. "metadata": {
  52. "pycharm": {
  53. "name": "#%%\n"
  54. }
  55. },
  56. "outputs": [],
  57. "source": [
  58. "from fastNLP import Collator\n",
  59. "\n",
  60. "collator = Collator()\n",
  61. "# collator.set_pad(field_name='text', pad_val='<pad>')"
  62. ]
  63. },
  64. {
  65. "cell_type": "markdown",
  66. "id": "51bf0878",
  67. "metadata": {},
  68. "source": [
  69. "&emsp; "
  70. ]
  71. },
  72. {
  73. "cell_type": "code",
  74. "execution_count": null,
  75. "id": "3fd2486f",
  76. "metadata": {
  77. "pycharm": {
  78. "name": "#%%\n"
  79. }
  80. },
  81. "outputs": [],
  82. "source": []
  83. },
  84. {
  85. "cell_type": "markdown",
  86. "id": "f9bbd9a7",
  87. "metadata": {},
  88. "source": [
  89. "### 3.2 dataloader 的结构与使用"
  90. ]
  91. },
  92. {
  93. "cell_type": "code",
  94. "execution_count": null,
  95. "id": "651baef6",
  96. "metadata": {
  97. "pycharm": {
  98. "name": "#%%\n"
  99. }
  100. },
  101. "outputs": [],
  102. "source": [
  103. "from fastNLP import prepare_torch_dataloader\n",
  104. "\n",
  105. "dl_bundle = prepare_torch_dataloader(data_bundle, train_batch_size=2)\n",
  106. "\n",
  107. "print(type(dl_bundle), type(dl_bundle['train']))"
  108. ]
  109. },
  110. {
  111. "cell_type": "code",
  112. "execution_count": null,
  113. "id": "726ba357",
  114. "metadata": {
  115. "pycharm": {
  116. "name": "#%%\n"
  117. }
  118. },
  119. "outputs": [],
  120. "source": [
  121. "dataloader = prepare_torch_dataloader(datasets['train'], train_batch_size=2)\n",
  122. "print(type(dataloader))\n",
  123. "print(dir(dataloader))"
  124. ]
  125. },
  126. {
  127. "cell_type": "code",
  128. "execution_count": null,
  129. "id": "d0795b3e",
  130. "metadata": {
  131. "pycharm": {
  132. "name": "#%%\n"
  133. }
  134. },
  135. "outputs": [],
  136. "source": [
  137. "dataloader.collate_fn"
  138. ]
  139. },
  140. {
  141. "cell_type": "code",
  142. "execution_count": null,
  143. "id": "b0c3c58d",
  144. "metadata": {
  145. "pycharm": {
  146. "name": "#%%\n"
  147. }
  148. },
  149. "outputs": [],
  150. "source": [
  151. "dataloader.batch_sampler"
  152. ]
  153. },
  154. {
  155. "cell_type": "markdown",
  156. "id": "7ed431cc",
  157. "metadata": {},
  158. "source": [
  159. "### 3.3 实例:NG20 的加载预处理\n",
  160. "\n",
  161. "在`fastNLP 0.8`中,**`Trainer`模块和`Evaluator`模块分别表示“训练器”和“评测器”**\n",
  162. "\n",
  163. "&emsp; 对应于之前的`fastNLP`版本中的`Trainer`模块和`Tester`模块,其定义方法如下所示\n",
  164. "\n",
  165. "在`fastNLP 0.8`中,需要注意,在同个`python`脚本中先使用`Trainer`训练,然后使用`Evaluator`评测\n",
  166. "\n",
  167. "&emsp; 非常关键的问题在于**如何正确设置二者的`driver`**。这就引入了另一个问题:什么是 `driver`?"
  168. ]
  169. },
  170. {
  171. "cell_type": "code",
  172. "execution_count": null,
  173. "id": "a89ef613",
  174. "metadata": {
  175. "pycharm": {
  176. "name": "#%%\n"
  177. }
  178. },
  179. "outputs": [],
  180. "source": [
  181. "import pandas as pd\n",
  182. "\n",
  183. "from fastNLP import DataSet\n",
  184. "from fastNLP import Vocabulary\n",
  185. "\n",
  186. "dataset = DataSet.from_pandas(pd.read_csv('./data/ng20_test.csv'))"
  187. ]
  188. },
  189. {
  190. "cell_type": "code",
  191. "execution_count": null,
  192. "id": "1624b0fa",
  193. "metadata": {
  194. "pycharm": {
  195. "name": "#%%\n"
  196. }
  197. },
  198. "outputs": [],
  199. "source": [
  200. "from functools import partial\n",
  201. "\n",
  202. "encode = partial(tokenizer.encode_plus, max_length=100, truncation=True,\n",
  203. " return_attention_mask=True)\n",
  204. "# 会新增 input_ids 、 attention_mask 和 token_type_ids 这三个 field\n",
  205. "dataset.apply_field_more(encode, field_name='text')"
  206. ]
  207. },
  208. {
  209. "cell_type": "code",
  210. "execution_count": null,
  211. "id": "0991a8ee",
  212. "metadata": {
  213. "pycharm": {
  214. "name": "#%%\n"
  215. }
  216. },
  217. "outputs": [],
  218. "source": [
  219. "target_vocab = Vocabulary(padding=None, unknown=None)\n",
  220. "\n",
  221. "target_vocab.from_dataset(*[ds for _, ds in data_bundle.iter_datasets()], field_name='label')\n",
  222. "target_vocab.index_dataset(*[ds for _, ds in data_bundle.iter_datasets()], field_name='label',\n",
  223. " new_field_name='labels')\n",
  224. "# 需要将 input_ids 的 pad 值设置为 tokenizer 的 pad 值\n",
  225. "dataset.set_pad('input_ids', pad_val=tokenizer.pad_token_id)\n",
  226. "dataset.set_ignore('label', 'text') # 因为 label 是原始的不需要的 str ,所以我们可以忽略它,让它不要在 batch 的输出中出现"
  227. ]
  228. },
  229. {
  230. "cell_type": "code",
  231. "execution_count": null,
  232. "id": "b369137f",
  233. "metadata": {},
  234. "outputs": [],
  235. "source": []
  236. }
  237. ],
  238. "metadata": {
  239. "kernelspec": {
  240. "display_name": "Python 3 (ipykernel)",
  241. "language": "python",
  242. "name": "python3"
  243. },
  244. "language_info": {
  245. "codemirror_mode": {
  246. "name": "ipython",
  247. "version": 3
  248. },
  249. "file_extension": ".py",
  250. "mimetype": "text/x-python",
  251. "name": "python",
  252. "nbconvert_exporter": "python",
  253. "pygments_lexer": "ipython3",
  254. "version": "3.7.4"
  255. },
  256. "pycharm": {
  257. "stem_cell": {
  258. "cell_type": "raw",
  259. "metadata": {
  260. "collapsed": false
  261. },
  262. "source": []
  263. }
  264. }
  265. },
  266. "nbformat": 4,
  267. "nbformat_minor": 5
  268. }