|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280 |
- {
- "cells": [
- {
- "cell_type": "markdown",
- "metadata": {
- "collapsed": true
- },
- "source": [
- "# 快速入门"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 1,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "{'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}"
- ]
- },
- "execution_count": 1,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "from fastNLP.io import CSVLoader\n",
- "\n",
- "loader = CSVLoader(headers=('raw_sentence', 'label'), sep='\\t')\n",
- "dataset = loader.load(\"./sample_data/tutorial_sample_dataset.csv\")\n",
- "dataset[0]"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 2,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "{'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",
- "'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",
- "'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}"
- ]
- },
- "execution_count": 2,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "# 将所有字母转为小写, 并所有句子变成单词序列\n",
- "dataset.apply(lambda x: x['raw_sentence'].lower(), new_field_name='sentence')\n",
- "dataset.apply(lambda x: x['sentence'].split(), new_field_name='words', is_input=True)\n",
- "dataset[0]"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 3,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "{'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",
- "'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",
- "'words': [4, 1, 6, 1, 1, 2, 1, 11, 153, 10, 28, 17, 2, 1, 10, 1, 28, 17, 2, 1, 5, 154, 6, 149, 1, 1, 23, 1, 6, 149, 1, 8, 30, 6, 4, 35, 3] type=list}"
- ]
- },
- "execution_count": 3,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "from fastNLP import Vocabulary\n",
- "\n",
- "# 使用Vocabulary类统计单词,并将单词序列转化为数字序列\n",
- "vocab = Vocabulary(min_freq=2).from_dataset(dataset, field_name='words')\n",
- "vocab.index_dataset(dataset, field_name='words',new_field_name='words')\n",
- "dataset[0]"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 4,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "{'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",
- "'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",
- "'words': [4, 1, 6, 1, 1, 2, 1, 11, 153, 10, 28, 17, 2, 1, 10, 1, 28, 17, 2, 1, 5, 154, 6, 149, 1, 1, 23, 1, 6, 149, 1, 8, 30, 6, 4, 35, 3] type=list,\n",
- "'target': 1 type=int}"
- ]
- },
- "execution_count": 4,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "# 将label转为整数,并设置为 target\n",
- "dataset.apply(lambda x: int(x['label']), new_field_name='target', is_target=True)\n",
- "dataset[0]"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 5,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "CNNText(\n",
- " (embed): Embedding(\n",
- " 177, 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(in_features=12, out_features=5, bias=True)\n",
- ")"
- ]
- },
- "execution_count": 5,
- "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": "code",
- "execution_count": 8,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "(62, 15)"
- ]
- },
- "execution_count": 8,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "# 分割训练集/验证集\n",
- "train_data, dev_data = dataset.split(0.2)\n",
- "len(train_data), len(dev_data)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 7,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "input fields after batch(if batch size is 2):\n",
- "\twords: (1)type:torch.Tensor (2)dtype:torch.int64, (3)shape:torch.Size([2, 26]) \n",
- "target fields after batch(if batch size is 2):\n",
- "\ttarget: (1)type:torch.Tensor (2)dtype:torch.int64, (3)shape:torch.Size([2]) \n",
- "\n",
- "training epochs started 2019-05-09-10-59-39\n"
- ]
- },
- {
- "data": {
- "application/vnd.jupyter.widget-view+json": {
- "model_id": "",
- "version_major": 2,
- "version_minor": 0
- },
- "text/plain": [
- "HBox(children=(IntProgress(value=0, layout=Layout(flex='2'), max=20), HTML(value='')), layout=Layout(display='…"
- ]
- },
- "metadata": {},
- "output_type": "display_data"
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "Evaluation at Epoch 1/10. Step:2/20. AccuracyMetric: acc=0.333333\n",
- "\n",
- "Evaluation at Epoch 2/10. Step:4/20. AccuracyMetric: acc=0.533333\n",
- "\n",
- "Evaluation at Epoch 3/10. Step:6/20. AccuracyMetric: acc=0.533333\n",
- "\n",
- "Evaluation at Epoch 4/10. Step:8/20. AccuracyMetric: acc=0.533333\n",
- "\n",
- "Evaluation at Epoch 5/10. Step:10/20. AccuracyMetric: acc=0.6\n",
- "\n",
- "Evaluation at Epoch 6/10. Step:12/20. AccuracyMetric: acc=0.8\n",
- "\n",
- "Evaluation at Epoch 7/10. Step:14/20. AccuracyMetric: acc=0.8\n",
- "\n",
- "Evaluation at Epoch 8/10. Step:16/20. AccuracyMetric: acc=0.733333\n",
- "\n",
- "Evaluation at Epoch 9/10. Step:18/20. AccuracyMetric: acc=0.733333\n",
- "\n",
- "Evaluation at Epoch 10/10. Step:20/20. AccuracyMetric: acc=0.733333\n",
- "\n",
- "\n",
- "In Epoch:6/Step:12, got best dev performance:AccuracyMetric: acc=0.8\n",
- "Reloaded the best model.\n"
- ]
- },
- {
- "data": {
- "text/plain": [
- "{'best_eval': {'AccuracyMetric': {'acc': 0.8}},\n",
- " 'best_epoch': 6,\n",
- " 'best_step': 12,\n",
- " 'seconds': 0.22}"
- ]
- },
- "execution_count": 7,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "from fastNLP import Trainer, CrossEntropyLoss, AccuracyMetric\n",
- "\n",
- "# 定义trainer并进行训练\n",
- "trainer = Trainer(model=model, train_data=train_data, dev_data=dev_data,\n",
- " loss=CrossEntropyLoss(), metrics=AccuracyMetric())\n",
- "trainer.train()"
- ]
- }
- ],
- "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": 1
- }
|