|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751 |
- {
- "cells": [
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "fastNLP10 分钟上手教程\n",
- "-------\n",
- "\n",
- "fastNLP提供方便的数据预处理,训练和测试模型的功能"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "如果您还没有通过pip安装fastNLP,可以执行下面的操作加载当前模块"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 4,
- "metadata": {},
- "outputs": [],
- "source": [
- "import sys\n",
- "sys.path.append(\"../\")"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "DataSet & Instance\n",
- "------\n",
- "\n",
- "fastNLP用DataSet和Instance保存和处理数据。每个DataSet表示一个数据集,每个Instance表示一个数据样本。一个DataSet存有多个Instance,每个Instance可以自定义存哪些内容。\n",
- "\n",
- "有一些read_*方法,可以轻松从文件读取数据,存成DataSet。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 1,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "77\n"
- ]
- }
- ],
- "source": [
- "from fastNLP import DataSet\n",
- "from fastNLP import Instance\n",
- "\n",
- "# 从csv读取数据到DataSet\n",
- "dataset = DataSet.read_csv('sample_data/tutorial_sample_dataset.csv', headers=('raw_sentence', 'label'), sep='\\t')\n",
- "print(len(dataset))"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 2,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "{'raw_sentence': A series of escapades demonstrating the adage that what is good for the goose is also good for the gander , some of which occasionally amuses but none of which amounts to much of a story . type=str,\n",
- "'label': 1 type=str}\n",
- "{'raw_sentence': The plot is romantic comedy boilerplate from start to finish . type=str,\n",
- "'label': 2 type=str}\n"
- ]
- }
- ],
- "source": [
- "# 使用数字索引[k],获取第k个样本\n",
- "print(dataset[0])\n",
- "\n",
- "# 索引也可以是负数\n",
- "print(dataset[-3])"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "## Instance\n",
- "Instance表示一个样本,由一个或多个field(域,属性,特征)组成,每个field有名字和值。\n",
- "\n",
- "在初始化Instance时即可定义它包含的域,使用 \"field_name=field_value\"的写法。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 3,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "{'raw_sentence': fake data type=str,\n",
- "'label': 0 type=str}"
- ]
- },
- "execution_count": 3,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "# DataSet.append(Instance)加入新数据\n",
- "dataset.append(Instance(raw_sentence='fake data', label='0'))\n",
- "dataset[-1]"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "## DataSet.apply方法\n",
- "数据预处理利器"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 4,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "{'raw_sentence': a series of escapades demonstrating the adage that what is good for the goose is also good for the gander , some of which occasionally amuses but none of which amounts to much of a story . type=str,\n",
- "'label': 1 type=str}\n"
- ]
- }
- ],
- "source": [
- "# 将所有数字转为小写\n",
- "dataset.apply(lambda x: x['raw_sentence'].lower(), new_field_name='raw_sentence')\n",
- "print(dataset[0])"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 5,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "{'raw_sentence': a series of escapades demonstrating the adage that what is good for the goose is also good for the gander , some of which occasionally amuses but none of which amounts to much of a story . type=str,\n",
- "'label': 1 type=int}\n"
- ]
- }
- ],
- "source": [
- "# label转int\n",
- "dataset.apply(lambda x: int(x['label']), new_field_name='label')\n",
- "print(dataset[0])"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 6,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "{'raw_sentence': a series of escapades demonstrating the adage that what is good for the goose is also good for the gander , some of which occasionally amuses but none of which amounts to much of a story . type=str,\n",
- "'label': 1 type=int,\n",
- "'words': ['a', 'series', 'of', 'escapades', 'demonstrating', 'the', 'adage', 'that', 'what', 'is', 'good', 'for', 'the', 'goose', 'is', 'also', 'good', 'for', 'the', 'gander', ',', 'some', 'of', 'which', 'occasionally', 'amuses', 'but', 'none', 'of', 'which', 'amounts', 'to', 'much', 'of', 'a', 'story', '.'] type=list}\n"
- ]
- }
- ],
- "source": [
- "# 使用空格分割句子\n",
- "def split_sent(ins):\n",
- " return ins['raw_sentence'].split()\n",
- "dataset.apply(split_sent, new_field_name='words')\n",
- "print(dataset[0])"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 7,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "{'raw_sentence': a series of escapades demonstrating the adage that what is good for the goose is also good for the gander , some of which occasionally amuses but none of which amounts to much of a story . type=str,\n",
- "'label': 1 type=int,\n",
- "'words': ['a', 'series', 'of', 'escapades', 'demonstrating', 'the', 'adage', 'that', 'what', 'is', 'good', 'for', 'the', 'goose', 'is', 'also', 'good', 'for', 'the', 'gander', ',', 'some', 'of', 'which', 'occasionally', 'amuses', 'but', 'none', 'of', 'which', 'amounts', 'to', 'much', 'of', 'a', 'story', '.'] type=list,\n",
- "'seq_len': 37 type=int}\n"
- ]
- }
- ],
- "source": [
- "# 增加长度信息\n",
- "dataset.apply(lambda x: len(x['words']), new_field_name='seq_len')\n",
- "print(dataset[0])"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "## DataSet.drop\n",
- "筛选数据"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 8,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "77\n"
- ]
- }
- ],
- "source": [
- "# 删除低于某个长度的词语\n",
- "dataset.drop(lambda x: x['seq_len'] <= 3)\n",
- "print(len(dataset))"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "## 配置DataSet\n",
- "1. 哪些域是特征,哪些域是标签\n",
- "2. 切分训练集/验证集"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 9,
- "metadata": {},
- "outputs": [],
- "source": [
- "# 设置DataSet中,哪些field要转为tensor\n",
- "\n",
- "# set target,loss或evaluate中的golden,计算loss,模型评估时使用\n",
- "dataset.set_target(\"label\")\n",
- "# set input,模型forward时使用\n",
- "dataset.set_input(\"words\")"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 10,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "54\n",
- "23\n"
- ]
- }
- ],
- "source": [
- "# 分出测试集、训练集\n",
- "\n",
- "test_data, train_data = dataset.split(0.3)\n",
- "print(len(test_data))\n",
- "print(len(train_data))"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Vocabulary\n",
- "------\n",
- "\n",
- "fastNLP中的Vocabulary轻松构建词表,将词转成数字"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 11,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "{'raw_sentence': the performances are an absolute joy . type=str,\n",
- "'label': 4 type=int,\n",
- "'words': [3, 1, 1, 26, 1, 1, 2] type=list,\n",
- "'seq_len': 7 type=int}\n"
- ]
- }
- ],
- "source": [
- "from fastNLP import Vocabulary\n",
- "\n",
- "# 构建词表, Vocabulary.add(word)\n",
- "vocab = Vocabulary(min_freq=2)\n",
- "train_data.apply(lambda x: [vocab.add(word) for word in x['words']])\n",
- "vocab.build_vocab()\n",
- "\n",
- "# index句子, Vocabulary.to_index(word)\n",
- "train_data.apply(lambda x: [vocab.to_index(word) for word in x['words']], new_field_name='words')\n",
- "test_data.apply(lambda x: [vocab.to_index(word) for word in x['words']], new_field_name='words')\n",
- "\n",
- "\n",
- "print(test_data[0])"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 12,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "batch_x has: {'words': tensor([[ 15, 72, 15, 73, 74, 7, 3, 75, 6, 3, 16, 16,\n",
- " 76, 2],\n",
- " [ 15, 72, 15, 73, 74, 7, 3, 75, 6, 3, 16, 16,\n",
- " 76, 2]])}\n",
- "batch_y has: {'label': tensor([ 1, 1])}\n"
- ]
- }
- ],
- "source": [
- "# 如果你们需要做强化学习或者GAN之类的项目,你们也可以使用这些数据预处理的工具\n",
- "from fastNLP.core.batch import Batch\n",
- "from fastNLP.core.sampler import RandomSampler\n",
- "\n",
- "batch_iterator = Batch(dataset=train_data, batch_size=2, sampler=RandomSampler())\n",
- "for batch_x, batch_y in batch_iterator:\n",
- " print(\"batch_x has: \", batch_x)\n",
- " print(\"batch_y has: \", batch_y)\n",
- " break"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "# Model\n",
- "定义一个PyTorch模型"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 15,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "CNNText(\n",
- " (embed): Embedding(\n",
- " 77, 50\n",
- " (dropout): Dropout(p=0.0)\n",
- " )\n",
- " (conv_pool): ConvMaxpool(\n",
- " (convs): ModuleList(\n",
- " (0): Conv1d(50, 3, kernel_size=(3,), stride=(1,), padding=(2,))\n",
- " (1): Conv1d(50, 4, kernel_size=(4,), stride=(1,), padding=(2,))\n",
- " (2): Conv1d(50, 5, kernel_size=(5,), stride=(1,), padding=(2,))\n",
- " )\n",
- " )\n",
- " (dropout): Dropout(p=0.1)\n",
- " (fc): Linear(\n",
- " (linear): Linear(in_features=12, out_features=5, bias=True)\n",
- " )\n",
- ")"
- ]
- },
- "execution_count": 15,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "from fastNLP.models import CNNText\n",
- "model = CNNText((len(vocab), 50), num_classes=5, padding=2, dropout=0.1)\n",
- "model"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "这是上述模型的forward方法。如果你不知道什么是forward方法,请参考我们的PyTorch教程。\n",
- "\n",
- "注意两点:\n",
- "1. forward参数名字叫**word_seq**,请记住。\n",
- "2. forward的返回值是一个**dict**,其中有个key的名字叫**output**。\n",
- "\n",
- "```Python\n",
- " def forward(self, word_seq):\n",
- " \"\"\"\n",
- "\n",
- " :param word_seq: torch.LongTensor, [batch_size, seq_len]\n",
- " :return output: dict of torch.LongTensor, [batch_size, num_classes]\n",
- " \"\"\"\n",
- " x = self.embed(word_seq) # [N,L] -> [N,L,C]\n",
- " x = self.conv_pool(x) # [N,L,C] -> [N,C]\n",
- " x = self.dropout(x)\n",
- " x = self.fc(x) # [N,C] -> [N, N_class]\n",
- " return {'output': x}\n",
- "```"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "这是上述模型的predict方法,是用来直接输出该任务的预测结果,与forward目的不同。\n",
- "\n",
- "注意两点:\n",
- "1. predict参数名也叫**word_seq**。\n",
- "2. predict的返回值是也一个**dict**,其中有个key的名字叫**predict**。\n",
- "\n",
- "```\n",
- " def predict(self, word_seq):\n",
- " \"\"\"\n",
- "\n",
- " :param word_seq: torch.LongTensor, [batch_size, seq_len]\n",
- " :return predict: dict of torch.LongTensor, [batch_size, seq_len]\n",
- " \"\"\"\n",
- " output = self(word_seq)\n",
- " _, predict = output['output'].max(dim=1)\n",
- " return {'predict': predict}\n",
- "```"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Trainer & Tester\n",
- "------\n",
- "\n",
- "使用fastNLP的Trainer训练模型"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 16,
- "metadata": {},
- "outputs": [],
- "source": [
- "from fastNLP import Trainer\n",
- "from copy import deepcopy\n",
- "from fastNLP.core.losses import CrossEntropyLoss\n",
- "from fastNLP.core.metrics import AccuracyMetric\n",
- "\n",
- "\n",
- "# 更改DataSet中对应field的名称,与模型的forward的参数名一致\n",
- "# 因为forward的参数叫word_seq, 所以要把原本叫words的field改名为word_seq\n",
- "# 这里的演示是让你了解这种**命名规则**\n",
- "train_data.rename_field('words', 'word_seq')\n",
- "test_data.rename_field('words', 'word_seq')\n",
- "\n",
- "# 顺便把label换名为label_seq\n",
- "train_data.rename_field('label', 'label_seq')\n",
- "test_data.rename_field('label', 'label_seq')"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "### loss\n",
- "训练模型需要提供一个损失函数\n",
- "\n",
- "下面提供了一个在分类问题中常用的交叉熵损失。注意它的**初始化参数**。\n",
- "\n",
- "pred参数对应的是模型的forward返回的dict的一个key的名字,这里是\"output\"。\n",
- "\n",
- "target参数对应的是dataset作为标签的field的名字,这里是\"label_seq\"。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 17,
- "metadata": {},
- "outputs": [],
- "source": [
- "loss = CrossEntropyLoss(pred=\"output\", target=\"label_seq\")"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "### Metric\n",
- "定义评价指标\n",
- "\n",
- "这里使用准确率。参数的“命名规则”跟上面类似。\n",
- "\n",
- "pred参数对应的是模型的predict方法返回的dict的一个key的名字,这里是\"predict\"。\n",
- "\n",
- "target参数对应的是dataset作为标签的field的名字,这里是\"label_seq\"。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 18,
- "metadata": {},
- "outputs": [],
- "source": [
- "metric = AccuracyMetric(pred=\"predict\", target=\"label_seq\")"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 19,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "input fields after batch(if batch size is 2):\n",
- "\tword_seq: (1)type:torch.Tensor (2)dtype:torch.int64, (3)shape:torch.Size([2, 11]) \n",
- "target fields after batch(if batch size is 2):\n",
- "\tlabel_seq: (1)type:torch.Tensor (2)dtype:torch.int64, (3)shape:torch.Size([2]) \n",
- "\n"
- ]
- },
- {
- "ename": "NameError",
- "evalue": "\nProblems occurred when calling CNNText.forward(self, words, seq_len=None)\n\tmissing param: ['words']\n\tunused field: ['word_seq']\n\tSuggestion: You need to provide ['words'] in DataSet and set it as input. ",
- "output_type": "error",
- "traceback": [
- "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
- "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
- "\u001b[0;32m<ipython-input-19-ff7d68caf88a>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[0msave_path\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 8\u001b[0m \u001b[0mbatch_size\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m32\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 9\u001b[0;31m n_epochs=5)\n\u001b[0m\u001b[1;32m 10\u001b[0m \u001b[0moverfit_trainer\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtrain\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
- "\u001b[0;32m/Users/fdujyn/anaconda3/lib/python3.6/site-packages/fastNLP/core/trainer.py\u001b[0m in \u001b[0;36m__init__\u001b[0;34m(self, train_data, model, optimizer, loss, batch_size, sampler, update_every, n_epochs, print_every, dev_data, metrics, metric_key, validate_every, save_path, prefetch, use_tqdm, device, callbacks, check_code_level)\u001b[0m\n\u001b[1;32m 447\u001b[0m _check_code(dataset=train_data, model=model, losser=losser, metrics=metrics, dev_data=dev_data,\n\u001b[1;32m 448\u001b[0m \u001b[0mmetric_key\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mmetric_key\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcheck_level\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mcheck_code_level\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 449\u001b[0;31m batch_size=min(batch_size, DEFAULT_CHECK_BATCH_SIZE))\n\u001b[0m\u001b[1;32m 450\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 451\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtrain_data\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mtrain_data\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
- "\u001b[0;32m/Users/fdujyn/anaconda3/lib/python3.6/site-packages/fastNLP/core/trainer.py\u001b[0m in \u001b[0;36m_check_code\u001b[0;34m(dataset, model, losser, metrics, batch_size, dev_data, metric_key, check_level)\u001b[0m\n\u001b[1;32m 808\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minfo_str\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 809\u001b[0m _check_forward_error(forward_func=model.forward, dataset=dataset,\n\u001b[0;32m--> 810\u001b[0;31m batch_x=batch_x, check_level=check_level)\n\u001b[0m\u001b[1;32m 811\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 812\u001b[0m \u001b[0mrefined_batch_x\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0m_build_args\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmodel\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mforward\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mbatch_x\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
- "\u001b[0;32m/Users/fdujyn/anaconda3/lib/python3.6/site-packages/fastNLP/core/utils.py\u001b[0m in \u001b[0;36m_check_forward_error\u001b[0;34m(forward_func, batch_x, dataset, check_level)\u001b[0m\n\u001b[1;32m 594\u001b[0m \u001b[0msugg_str\u001b[0m \u001b[0;34m+=\u001b[0m \u001b[0msuggestions\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 595\u001b[0m \u001b[0merr_str\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m'\\n'\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0;34m'\\n'\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mjoin\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0merrs\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0;34m'\\n\\tSuggestion: '\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0msugg_str\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 596\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mNameError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0merr_str\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 597\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0m_unused\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 598\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mcheck_level\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0mWARNING_CHECK_LEVEL\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
- "\u001b[0;31mNameError\u001b[0m: \nProblems occurred when calling CNNText.forward(self, words, seq_len=None)\n\tmissing param: ['words']\n\tunused field: ['word_seq']\n\tSuggestion: You need to provide ['words'] in DataSet and set it as input. "
- ]
- }
- ],
- "source": [
- "# 实例化Trainer,传入模型和数据,进行训练\n",
- "# 先在test_data拟合(确保模型的实现是正确的)\n",
- "copy_model = deepcopy(model)\n",
- "overfit_trainer = Trainer(model=copy_model, train_data=test_data, dev_data=test_data,\n",
- " loss=loss,\n",
- " metrics=metric,\n",
- " save_path=None,\n",
- " batch_size=32,\n",
- " n_epochs=5)\n",
- "overfit_trainer.train()"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 22,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "input fields after batch(if batch size is 2):\n",
- "\tword_seq: (1)type:torch.Tensor (2)dtype:torch.int64, (3)shape:torch.Size([2, 20]) \n",
- "target fields after batch(if batch size is 2):\n",
- "\tlabel_seq: (1)type:torch.Tensor (2)dtype:torch.int64, (3)shape:torch.Size([2]) \n",
- "\n",
- "training epochs started 2019-01-12 17-09-05\n"
- ]
- },
- {
- "data": {
- "text/plain": [
- "HBox(children=(IntProgress(value=0, layout=Layout(flex='2'), max=5), HTML(value='')), layout=Layout(display='i…"
- ]
- },
- "metadata": {},
- "output_type": "display_data"
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "Evaluation at Epoch 1/5. Step:1/5. AccuracyMetric: acc=0.37037\n",
- "Evaluation at Epoch 2/5. Step:2/5. AccuracyMetric: acc=0.37037\n",
- "Evaluation at Epoch 3/5. Step:3/5. AccuracyMetric: acc=0.462963\n",
- "Evaluation at Epoch 4/5. Step:4/5. AccuracyMetric: acc=0.425926\n",
- "Evaluation at Epoch 5/5. Step:5/5. AccuracyMetric: acc=0.481481\n",
- "\n",
- "In Epoch:5/Step:5, got best dev performance:AccuracyMetric: acc=0.481481\n",
- "Reloaded the best model.\n",
- "Train finished!\n"
- ]
- }
- ],
- "source": [
- "# 用train_data训练,在test_data验证\n",
- "trainer = Trainer(model=model, train_data=train_data, dev_data=test_data,\n",
- " loss=CrossEntropyLoss(pred=\"output\", target=\"label_seq\"),\n",
- " metrics=AccuracyMetric(pred=\"predict\", target=\"label_seq\"),\n",
- " save_path=None,\n",
- " batch_size=32,\n",
- " n_epochs=5)\n",
- "trainer.train()\n",
- "print('Train finished!')"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 23,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[tester] \n",
- "AccuracyMetric: acc=0.481481\n",
- "{'AccuracyMetric': {'acc': 0.481481}}\n"
- ]
- }
- ],
- "source": [
- "# 调用Tester在test_data上评价效果\n",
- "from fastNLP import Tester\n",
- "\n",
- "tester = Tester(data=test_data, model=model, metrics=AccuracyMetric(pred=\"predict\", target=\"label_seq\"),\n",
- " batch_size=4)\n",
- "acc = tester.test()\n",
- "print(acc)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "# In summary\n",
- "\n",
- "## fastNLP Trainer的伪代码逻辑\n",
- "### 1. 准备DataSet,假设DataSet中共有如下的fields\n",
- " ['raw_sentence', 'word_seq1', 'word_seq2', 'raw_label','label']\n",
- " 通过\n",
- " DataSet.set_input('word_seq1', word_seq2', flag=True)将'word_seq1', 'word_seq2'设置为input\n",
- " 通过\n",
- " DataSet.set_target('label', flag=True)将'label'设置为target\n",
- "### 2. 初始化模型\n",
- " class Model(nn.Module):\n",
- " def __init__(self):\n",
- " xxx\n",
- " def forward(self, word_seq1, word_seq2):\n",
- " # (1) 这里使用的形参名必须和DataSet中的input field的名称对应。因为我们是通过形参名, 进行赋值的\n",
- " # (2) input field的数量可以多于这里的形参数量。但是不能少于。\n",
- " xxxx\n",
- " # 输出必须是一个dict\n",
- "### 3. Trainer的训练过程\n",
- " (1) 从DataSet中按照batch_size取出一个batch,调用Model.forward\n",
- " (2) 将 Model.forward的结果 与 标记为target的field 传入Losser当中。\n",
- " 由于每个人写的Model.forward的output的dict可能key并不一样,比如有人是{'pred':xxx}, {'output': xxx}; \n",
- " 另外每个人将target可能也会设置为不同的名称, 比如有人是label, 有人设置为target;\n",
- " 为了解决以上的问题,我们的loss提供映射机制\n",
- " 比如CrossEntropyLosser的需要的输入是(prediction, target)。但是forward的output是{'output': xxx}; 'label'是target\n",
- " 那么初始化losser的时候写为CrossEntropyLosser(prediction='output', target='label')即可\n",
- " (3) 对于Metric是同理的\n",
- " Metric计算也是从 forward的结果中取值 与 设置target的field中取值。 也是可以通过映射找到对应的值 \n",
- " \n",
- " \n",
- "\n",
- "## 一些问题.\n",
- "### 1. DataSet中为什么需要设置input和target\n",
- " 只有被设置为input或者target的数据才会在train的过程中被取出来\n",
- " (1.1) 我们只会在设置为input的field中寻找传递给Model.forward的参数。\n",
- " (1.2) 我们在传递值给losser或者metric的时候会使用来自: \n",
- " (a)Model.forward的output\n",
- " (b)被设置为target的field\n",
- " \n",
- "\n",
- "### 2. 我们是通过forwad中的形参名将DataSet中的field赋值给对应的参数\n",
- " (1.1) 构建模型过程中,\n",
- " 例如:\n",
- " DataSet中x,seq_lens是input,那么forward就应该是\n",
- " def forward(self, x, seq_lens):\n",
- " pass\n",
- " 我们是通过形参名称进行匹配的field的\n",
- " \n",
- "\n",
- "\n",
- "### 1. 加载数据到DataSet\n",
- "### 2. 使用apply操作对DataSet进行预处理\n",
- " (2.1) 处理过程中将某些field设置为input,某些field设置为target\n",
- "### 3. 构建模型\n",
- " (3.1) 构建模型过程中,需要注意forward函数的形参名需要和DataSet中设置为input的field名称是一致的。\n",
- " 例如:\n",
- " DataSet中x,seq_lens是input,那么forward就应该是\n",
- " def forward(self, x, seq_lens):\n",
- " pass\n",
- " 我们是通过形参名称进行匹配的field的\n",
- " (3.2) 模型的forward的output需要是dict类型的。\n",
- " 建议将输出设置为{\"pred\": xx}.\n",
- " \n"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": []
- }
- ],
- "metadata": {
- "kernelspec": {
- "display_name": "Python 3",
- "language": "python",
- "name": "python3"
- },
- "language_info": {
- "codemirror_mode": {
- "name": "ipython",
- "version": 3
- },
- "file_extension": ".py",
- "mimetype": "text/x-python",
- "name": "python",
- "nbconvert_exporter": "python",
- "pygments_lexer": "ipython3",
- "version": "3.6.7"
- }
- },
- "nbformat": 4,
- "nbformat_minor": 2
- }
|