diff --git a/tutorials/fastnlp_1min_tutorial.ipynb b/tutorials/fastnlp_1min_tutorial.ipynb deleted file mode 100644 index 64d57bc4..00000000 --- a/tutorials/fastnlp_1min_tutorial.ipynb +++ /dev/null @@ -1,1775 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": { - "collapsed": true - }, - "source": [ - "# fastNLP 1分钟上手教程" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## step 1\n", - "读取数据集" - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [], - "source": [ - "from fastNLP import DataSet\n", - " \n", - "data_path = \"./sample_data/tutorial_sample_dataset.csv\"\n", - "ds = DataSet.read_csv(data_path, headers=('raw_sentence', 'label'), sep='\\t')" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "{'raw_sentence': This quiet , introspective and entertaining independent is worth seeking . type=str,\n", - "'label': 4 type=str}" - ] - }, - "execution_count": 2, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "ds[1]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## step 2\n", - "数据预处理\n", - "1. 类型转换\n", - "2. 切分验证集\n", - "3. 构建词典" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "[['a',\n", - " 'series',\n", - " 'of',\n", - " 'escapades',\n", - " 'demonstrating',\n", - " 'the',\n", - " 'adage',\n", - " 'that',\n", - " 'what',\n", - " 'is',\n", - " 'good',\n", - " 'for',\n", - " 'the',\n", - " 'goose',\n", - " 'is',\n", - " 'also',\n", - " 'good',\n", - " 'for',\n", - " 'the',\n", - " 'gander',\n", - " ',',\n", - " 'some',\n", - " 'of',\n", - " 'which',\n", - " 'occasionally',\n", - " 'amuses',\n", - " 'but',\n", - " 'none',\n", - " 'of',\n", - " 'which',\n", - " 'amounts',\n", - " 'to',\n", - " 'much',\n", - " 'of',\n", - " 'a',\n", - " 'story',\n", - " '.'],\n", - " ['this',\n", - " 'quiet',\n", - " ',',\n", - " 'introspective',\n", - " 'and',\n", - " 'entertaining',\n", - " 'independent',\n", - " 'is',\n", - " 'worth',\n", - " 'seeking',\n", - " '.'],\n", - " ['even',\n", - " 'fans',\n", - " 'of',\n", - " 'ismail',\n", - " 'merchant',\n", - " \"'s\",\n", - " 'work',\n", - " ',',\n", - " 'i',\n", - " 'suspect',\n", - " ',',\n", - " 'would',\n", - " 'have',\n", - " 'a',\n", - " 'hard',\n", - " 'time',\n", - " 'sitting',\n", - " 'through',\n", - " 'this',\n", - " 'one',\n", - " '.'],\n", - " ['a',\n", - " 'positively',\n", - " 'thrilling',\n", - " 'combination',\n", - " 'of',\n", - " 'ethnography',\n", - " 'and',\n", - " 'all',\n", - " 'the',\n", - " 'intrigue',\n", - " ',',\n", - " 'betrayal',\n", - " ',',\n", - " 'deceit',\n", - " 'and',\n", - " 'murder',\n", - " 'of',\n", - " 'a',\n", - " 'shakespearean',\n", - " 'tragedy',\n", - " 'or',\n", - " 'a',\n", - " 'juicy',\n", - " 'soap',\n", - " 'opera',\n", - " '.'],\n", - " ['aggressive',\n", - " 'self-glorification',\n", - " 'and',\n", - " 'a',\n", - " 'manipulative',\n", - " 'whitewash',\n", - " '.'],\n", - " ['a',\n", - " 'comedy-drama',\n", - " 'of',\n", - " 'nearly',\n", - " 'epic',\n", - " 'proportions',\n", - " 'rooted',\n", - " 'in',\n", - " 'a',\n", - " 'sincere',\n", - " 'performance',\n", - " 'by',\n", - " 'the',\n", - " 'title',\n", - " 'character',\n", - " 'undergoing',\n", - " 'midlife',\n", - " 'crisis',\n", - " '.'],\n", - " ['narratively',\n", - " ',',\n", - " 'trouble',\n", - " 'every',\n", - " 'day',\n", - " 'is',\n", - " 'a',\n", - " 'plodding',\n", - " 'mess',\n", - " '.'],\n", - " ['the',\n", - " 'importance',\n", - " 'of',\n", - " 'being',\n", - " 'earnest',\n", - " ',',\n", - " 'so',\n", - " 'thick',\n", - " 'with',\n", - " 'wit',\n", - " 'it',\n", - " 'plays',\n", - " 'like',\n", - " 'a',\n", - " 'reading',\n", - " 'from',\n", - " 'bartlett',\n", - " \"'s\",\n", - " 'familiar',\n", - " 'quotations'],\n", - " ['but', 'it', 'does', \"n't\", 'leave', 'you', 'with', 'much', '.'],\n", - " ['you', 'could', 'hate', 'it', 'for', 'the', 'same', 'reason', '.'],\n", - " ['there',\n", - " \"'s\",\n", - " 'little',\n", - " 'to',\n", - " 'recommend',\n", - " 'snow',\n", - " 'dogs',\n", - " ',',\n", - " 'unless',\n", - " 'one',\n", - " 'considers',\n", - " 'cliched',\n", - " 'dialogue',\n", - " 'and',\n", - " 'perverse',\n", - " 'escapism',\n", - " 'a',\n", - " 'source',\n", - " 'of',\n", - " 'high',\n", - " 'hilarity',\n", - " '.'],\n", - " ['kung',\n", - " 'pow',\n", - " 'is',\n", - " 'oedekerk',\n", - " \"'s\",\n", - " 'realization',\n", - " 'of',\n", - " 'his',\n", - " 'childhood',\n", - " 'dream',\n", - " 'to',\n", - " 'be',\n", - " 'in',\n", - " 'a',\n", - " 'martial-arts',\n", - " 'flick',\n", - " ',',\n", - " 'and',\n", - " 'proves',\n", - " 'that',\n", - " 'sometimes',\n", - " 'the',\n", - " 'dreams',\n", - " 'of',\n", - " 'youth',\n", - " 'should',\n", - " 'remain',\n", - " 'just',\n", - " 'that',\n", - " '.'],\n", - " ['the', 'performances', 'are', 'an', 'absolute', 'joy', '.'],\n", - " ['fresnadillo',\n", - " 'has',\n", - " 'something',\n", - " 'serious',\n", - " 'to',\n", - " 'say',\n", - " 'about',\n", - " 'the',\n", - " 'ways',\n", - " 'in',\n", - " 'which',\n", - " 'extravagant',\n", - " 'chance',\n", - " 'can',\n", - " 'distort',\n", - " 'our',\n", - " 'perspective',\n", - " 'and',\n", - " 'throw',\n", - " 'us',\n", - " 'off',\n", - " 'the',\n", - " 'path',\n", - " 'of',\n", - " 'good',\n", - " 'sense',\n", - " '.'],\n", - " ['i',\n", - " 'still',\n", - " 'like',\n", - " 'moonlight',\n", - " 'mile',\n", - " ',',\n", - " 'better',\n", - " 'judgment',\n", - " 'be',\n", - " 'damned',\n", - " '.'],\n", - " ['a',\n", - " 'welcome',\n", - " 'relief',\n", - " 'from',\n", - " 'baseball',\n", - " 'movies',\n", - " 'that',\n", - " 'try',\n", - " 'too',\n", - " 'hard',\n", - " 'to',\n", - " 'be',\n", - " 'mythic',\n", - " ',',\n", - " 'this',\n", - " 'one',\n", - " 'is',\n", - " 'a',\n", - " 'sweet',\n", - " 'and',\n", - " 'modest',\n", - " 'and',\n", - " 'ultimately',\n", - " 'winning',\n", - " 'story',\n", - " '.'],\n", - " ['a',\n", - " 'bilingual',\n", - " 'charmer',\n", - " ',',\n", - " 'just',\n", - " 'like',\n", - " 'the',\n", - " 'woman',\n", - " 'who',\n", - " 'inspired',\n", - " 'it'],\n", - " ['like',\n", - " 'a',\n", - " 'less',\n", - " 'dizzily',\n", - " 'gorgeous',\n", - " 'companion',\n", - " 'to',\n", - " 'mr.',\n", - " 'wong',\n", - " \"'s\",\n", - " 'in',\n", - " 'the',\n", - " 'mood',\n", - " 'for',\n", - " 'love',\n", - " '--',\n", - " 'very',\n", - " 'much',\n", - " 'a',\n", - " 'hong',\n", - " 'kong',\n", - " 'movie',\n", - " 'despite',\n", - " 'its',\n", - " 'mainland',\n", - " 'setting',\n", - " '.'],\n", - " ['as',\n", - " 'inept',\n", - " 'as',\n", - " 'big-screen',\n", - " 'remakes',\n", - " 'of',\n", - " 'the',\n", - " 'avengers',\n", - " 'and',\n", - " 'the',\n", - " 'wild',\n", - " 'wild',\n", - " 'west',\n", - " '.'],\n", - " ['it',\n", - " \"'s\",\n", - " 'everything',\n", - " 'you',\n", - " \"'d\",\n", - " 'expect',\n", - " '--',\n", - " 'but',\n", - " 'nothing',\n", - " 'more',\n", - " '.'],\n", - " ['best', 'indie', 'of', 'the', 'year', ',', 'so', 'far', '.'],\n", - " ['hatfield',\n", - " 'and',\n", - " 'hicks',\n", - " 'make',\n", - " 'the',\n", - " 'oddest',\n", - " 'of',\n", - " 'couples',\n", - " ',',\n", - " 'and',\n", - " 'in',\n", - " 'this',\n", - " 'sense',\n", - " 'the',\n", - " 'movie',\n", - " 'becomes',\n", - " 'a',\n", - " 'study',\n", - " 'of',\n", - " 'the',\n", - " 'gambles',\n", - " 'of',\n", - " 'the',\n", - " 'publishing',\n", - " 'world',\n", - " ',',\n", - " 'offering',\n", - " 'a',\n", - " 'case',\n", - " 'study',\n", - " 'that',\n", - " 'exists',\n", - " 'apart',\n", - " 'from',\n", - " 'all',\n", - " 'the',\n", - " 'movie',\n", - " \"'s\",\n", - " 'political',\n", - " 'ramifications',\n", - " '.'],\n", - " ['it',\n", - " \"'s\",\n", - " 'like',\n", - " 'going',\n", - " 'to',\n", - " 'a',\n", - " 'house',\n", - " 'party',\n", - " 'and',\n", - " 'watching',\n", - " 'the',\n", - " 'host',\n", - " 'defend',\n", - " 'himself',\n", - " 'against',\n", - " 'a',\n", - " 'frothing',\n", - " 'ex-girlfriend',\n", - " '.'],\n", - " ['that',\n", - " 'the',\n", - " 'chuck',\n", - " 'norris',\n", - " '``',\n", - " 'grenade',\n", - " 'gag',\n", - " \"''\",\n", - " 'occurs',\n", - " 'about',\n", - " '7',\n", - " 'times',\n", - " 'during',\n", - " 'windtalkers',\n", - " 'is',\n", - " 'a',\n", - " 'good',\n", - " 'indication',\n", - " 'of',\n", - " 'how',\n", - " 'serious-minded',\n", - " 'the',\n", - " 'film',\n", - " 'is',\n", - " '.'],\n", - " ['the',\n", - " 'plot',\n", - " 'is',\n", - " 'romantic',\n", - " 'comedy',\n", - " 'boilerplate',\n", - " 'from',\n", - " 'start',\n", - " 'to',\n", - " 'finish',\n", - " '.'],\n", - " ['it',\n", - " 'arrives',\n", - " 'with',\n", - " 'an',\n", - " 'impeccable',\n", - " 'pedigree',\n", - " ',',\n", - " 'mongrel',\n", - " 'pep',\n", - " ',',\n", - " 'and',\n", - " 'almost',\n", - " 'indecipherable',\n", - " 'plot',\n", - " 'complications',\n", - " '.'],\n", - " ['a',\n", - " 'film',\n", - " 'that',\n", - " 'clearly',\n", - " 'means',\n", - " 'to',\n", - " 'preach',\n", - " 'exclusively',\n", - " 'to',\n", - " 'the',\n", - " 'converted',\n", - " '.'],\n", - " ['while',\n", - " 'the',\n", - " 'importance',\n", - " 'of',\n", - " 'being',\n", - " 'earnest',\n", - " 'offers',\n", - " 'opportunities',\n", - " 'for',\n", - " 'occasional',\n", - " 'smiles',\n", - " 'and',\n", - " 'chuckles',\n", - " ',',\n", - " 'it',\n", - " 'does',\n", - " \"n't\",\n", - " 'give',\n", - " 'us',\n", - " 'a',\n", - " 'reason',\n", - " 'to',\n", - " 'be',\n", - " 'in',\n", - " 'the',\n", - " 'theater',\n", - " 'beyond',\n", - " 'wilde',\n", - " \"'s\",\n", - " 'wit',\n", - " 'and',\n", - " 'the',\n", - " 'actors',\n", - " \"'\",\n", - " 'performances',\n", - " '.'],\n", - " ['the',\n", - " 'latest',\n", - " 'vapid',\n", - " 'actor',\n", - " \"'s\",\n", - " 'exercise',\n", - " 'to',\n", - " 'appropriate',\n", - " 'the',\n", - " 'structure',\n", - " 'of',\n", - " 'arthur',\n", - " 'schnitzler',\n", - " \"'s\",\n", - " 'reigen',\n", - " '.'],\n", - " ['more',\n", - " 'vaudeville',\n", - " 'show',\n", - " 'than',\n", - " 'well-constructed',\n", - " 'narrative',\n", - " ',',\n", - " 'but',\n", - " 'on',\n", - " 'those',\n", - " 'terms',\n", - " 'it',\n", - " \"'s\",\n", - " 'inoffensive',\n", - " 'and',\n", - " 'actually',\n", - " 'rather',\n", - " 'sweet',\n", - " '.'],\n", - " ['nothing', 'more', 'than', 'a', 'run-of-the-mill', 'action', 'flick', '.'],\n", - " ['hampered',\n", - " '--',\n", - " 'no',\n", - " ',',\n", - " 'paralyzed',\n", - " '--',\n", - " 'by',\n", - " 'a',\n", - " 'self-indulgent',\n", - " 'script',\n", - " '...',\n", - " 'that',\n", - " 'aims',\n", - " 'for',\n", - " 'poetry',\n", - " 'and',\n", - " 'ends',\n", - " 'up',\n", - " 'sounding',\n", - " 'like',\n", - " 'satire',\n", - " '.'],\n", - " ['ice',\n", - " 'age',\n", - " 'is',\n", - " 'the',\n", - " 'first',\n", - " 'computer-generated',\n", - " 'feature',\n", - " 'cartoon',\n", - " 'to',\n", - " 'feel',\n", - " 'like',\n", - " 'other',\n", - " 'movies',\n", - " ',',\n", - " 'and',\n", - " 'that',\n", - " 'makes',\n", - " 'for',\n", - " 'some',\n", - " 'glacial',\n", - " 'pacing',\n", - " 'early',\n", - " 'on',\n", - " '.'],\n", - " ['there',\n", - " \"'s\",\n", - " 'very',\n", - " 'little',\n", - " 'sense',\n", - " 'to',\n", - " 'what',\n", - " \"'s\",\n", - " 'going',\n", - " 'on',\n", - " 'here',\n", - " ',',\n", - " 'but',\n", - " 'the',\n", - " 'makers',\n", - " 'serve',\n", - " 'up',\n", - " 'the',\n", - " 'cliches',\n", - " 'with',\n", - " 'considerable',\n", - " 'dash',\n", - " '.'],\n", - " ['cattaneo',\n", - " 'should',\n", - " 'have',\n", - " 'followed',\n", - " 'the',\n", - " 'runaway',\n", - " 'success',\n", - " 'of',\n", - " 'his',\n", - " 'first',\n", - " 'film',\n", - " ',',\n", - " 'the',\n", - " 'full',\n", - " 'monty',\n", - " ',',\n", - " 'with',\n", - " 'something',\n", - " 'different',\n", - " '.'],\n", - " ['they',\n", - " \"'re\",\n", - " 'the',\n", - " 'unnamed',\n", - " ',',\n", - " 'easily',\n", - " 'substitutable',\n", - " 'forces',\n", - " 'that',\n", - " 'serve',\n", - " 'as',\n", - " 'whatever',\n", - " 'terror',\n", - " 'the',\n", - " 'heroes',\n", - " 'of',\n", - " 'horror',\n", - " 'movies',\n", - " 'try',\n", - " 'to',\n", - " 'avoid',\n", - " '.'],\n", - " ['it',\n", - " 'almost',\n", - " 'feels',\n", - " 'as',\n", - " 'if',\n", - " 'the',\n", - " 'movie',\n", - " 'is',\n", - " 'more',\n", - " 'interested',\n", - " 'in',\n", - " 'entertaining',\n", - " 'itself',\n", - " 'than',\n", - " 'in',\n", - " 'amusing',\n", - " 'us',\n", - " '.'],\n", - " ['the',\n", - " 'movie',\n", - " \"'s\",\n", - " 'progression',\n", - " 'into',\n", - " 'rambling',\n", - " 'incoherence',\n", - " 'gives',\n", - " 'new',\n", - " 'meaning',\n", - " 'to',\n", - " 'the',\n", - " 'phrase',\n", - " '`',\n", - " 'fatal',\n", - " 'script',\n", - " 'error',\n", - " '.',\n", - " \"'\"],\n", - " ['i',\n", - " 'still',\n", - " 'like',\n", - " 'moonlight',\n", - " 'mile',\n", - " ',',\n", - " 'better',\n", - " 'judgment',\n", - " 'be',\n", - " 'damned',\n", - " '.'],\n", - " ['a',\n", - " 'welcome',\n", - " 'relief',\n", - " 'from',\n", - " 'baseball',\n", - " 'movies',\n", - " 'that',\n", - " 'try',\n", - " 'too',\n", - " 'hard',\n", - " 'to',\n", - " 'be',\n", - " 'mythic',\n", - " ',',\n", - " 'this',\n", - " 'one',\n", - " 'is',\n", - " 'a',\n", - " 'sweet',\n", - " 'and',\n", - " 'modest',\n", - " 'and',\n", - " 'ultimately',\n", - " 'winning',\n", - " 'story',\n", - " '.'],\n", - " ['a',\n", - " 'bilingual',\n", - " 'charmer',\n", - " ',',\n", - " 'just',\n", - " 'like',\n", - " 'the',\n", - " 'woman',\n", - " 'who',\n", - " 'inspired',\n", - " 'it'],\n", - " ['like',\n", - " 'a',\n", - " 'less',\n", - " 'dizzily',\n", - " 'gorgeous',\n", - " 'companion',\n", - " 'to',\n", - " 'mr.',\n", - " 'wong',\n", - " \"'s\",\n", - " 'in',\n", - " 'the',\n", - " 'mood',\n", - " 'for',\n", - " 'love',\n", - " '--',\n", - " 'very',\n", - " 'much',\n", - " 'a',\n", - " 'hong',\n", - " 'kong',\n", - " 'movie',\n", - " 'despite',\n", - " 'its',\n", - " 'mainland',\n", - " 'setting',\n", - " '.'],\n", - " ['as',\n", - " 'inept',\n", - " 'as',\n", - " 'big-screen',\n", - " 'remakes',\n", - " 'of',\n", - " 'the',\n", - " 'avengers',\n", - " 'and',\n", - " 'the',\n", - " 'wild',\n", - " 'wild',\n", - " 'west',\n", - " '.'],\n", - " ['it',\n", - " \"'s\",\n", - " 'everything',\n", - " 'you',\n", - " \"'d\",\n", - " 'expect',\n", - " '--',\n", - " 'but',\n", - " 'nothing',\n", - " 'more',\n", - " '.'],\n", - " ['best', 'indie', 'of', 'the', 'year', ',', 'so', 'far', '.'],\n", - " ['hatfield',\n", - " 'and',\n", - " 'hicks',\n", - " 'make',\n", - " 'the',\n", - " 'oddest',\n", - " 'of',\n", - " 'couples',\n", - " ',',\n", - " 'and',\n", - " 'in',\n", - " 'this',\n", - " 'sense',\n", - " 'the',\n", - " 'movie',\n", - " 'becomes',\n", - " 'a',\n", - " 'study',\n", - " 'of',\n", - " 'the',\n", - " 'gambles',\n", - " 'of',\n", - " 'the',\n", - " 'publishing',\n", - " 'world',\n", - " ',',\n", - " 'offering',\n", - " 'a',\n", - " 'case',\n", - " 'study',\n", - " 'that',\n", - " 'exists',\n", - " 'apart',\n", - " 'from',\n", - " 'all',\n", - " 'the',\n", - " 'movie',\n", - " \"'s\",\n", - " 'political',\n", - " 'ramifications',\n", - " '.'],\n", - " ['it',\n", - " \"'s\",\n", - " 'like',\n", - " 'going',\n", - " 'to',\n", - " 'a',\n", - " 'house',\n", - " 'party',\n", - " 'and',\n", - " 'watching',\n", - " 'the',\n", - " 'host',\n", - " 'defend',\n", - " 'himself',\n", - " 'against',\n", - " 'a',\n", - " 'frothing',\n", - " 'ex-girlfriend',\n", - " '.'],\n", - " ['that',\n", - " 'the',\n", - " 'chuck',\n", - " 'norris',\n", - " '``',\n", - " 'grenade',\n", - " 'gag',\n", - " \"''\",\n", - " 'occurs',\n", - " 'about',\n", - " '7',\n", - " 'times',\n", - " 'during',\n", - " 'windtalkers',\n", - " 'is',\n", - " 'a',\n", - " 'good',\n", - " 'indication',\n", - " 'of',\n", - " 'how',\n", - " 'serious-minded',\n", - " 'the',\n", - " 'film',\n", - " 'is',\n", - " '.'],\n", - " ['the',\n", - " 'plot',\n", - " 'is',\n", - " 'romantic',\n", - " 'comedy',\n", - " 'boilerplate',\n", - " 'from',\n", - " 'start',\n", - " 'to',\n", - " 'finish',\n", - " '.'],\n", - " ['it',\n", - " 'arrives',\n", - " 'with',\n", - " 'an',\n", - " 'impeccable',\n", - " 'pedigree',\n", - " ',',\n", - " 'mongrel',\n", - " 'pep',\n", - " ',',\n", - " 'and',\n", - " 'almost',\n", - " 'indecipherable',\n", - " 'plot',\n", - " 'complications',\n", - " '.'],\n", - " ['a',\n", - " 'film',\n", - " 'that',\n", - " 'clearly',\n", - " 'means',\n", - " 'to',\n", - " 'preach',\n", - " 'exclusively',\n", - " 'to',\n", - " 'the',\n", - " 'converted',\n", - " '.'],\n", - " ['i',\n", - " 'still',\n", - " 'like',\n", - " 'moonlight',\n", - " 'mile',\n", - " ',',\n", - " 'better',\n", - " 'judgment',\n", - " 'be',\n", - " 'damned',\n", - " '.'],\n", - " ['a',\n", - " 'welcome',\n", - " 'relief',\n", - " 'from',\n", - " 'baseball',\n", - " 'movies',\n", - " 'that',\n", - " 'try',\n", - " 'too',\n", - " 'hard',\n", - " 'to',\n", - " 'be',\n", - " 'mythic',\n", - " ',',\n", - " 'this',\n", - " 'one',\n", - " 'is',\n", - " 'a',\n", - " 'sweet',\n", - " 'and',\n", - " 'modest',\n", - " 'and',\n", - " 'ultimately',\n", - " 'winning',\n", - " 'story',\n", - " '.'],\n", - " ['a',\n", - " 'bilingual',\n", - " 'charmer',\n", - " ',',\n", - " 'just',\n", - " 'like',\n", - " 'the',\n", - " 'woman',\n", - " 'who',\n", - " 'inspired',\n", - " 'it'],\n", - " ['like',\n", - " 'a',\n", - " 'less',\n", - " 'dizzily',\n", - " 'gorgeous',\n", - " 'companion',\n", - " 'to',\n", - " 'mr.',\n", - " 'wong',\n", - " \"'s\",\n", - " 'in',\n", - " 'the',\n", - " 'mood',\n", - " 'for',\n", - " 'love',\n", - " '--',\n", - " 'very',\n", - " 'much',\n", - " 'a',\n", - " 'hong',\n", - " 'kong',\n", - " 'movie',\n", - " 'despite',\n", - " 'its',\n", - " 'mainland',\n", - " 'setting',\n", - " '.'],\n", - " ['as',\n", - " 'inept',\n", - " 'as',\n", - " 'big-screen',\n", - " 'remakes',\n", - " 'of',\n", - " 'the',\n", - " 'avengers',\n", - " 'and',\n", - " 'the',\n", - " 'wild',\n", - " 'wild',\n", - " 'west',\n", - " '.'],\n", - " ['it',\n", - " \"'s\",\n", - " 'everything',\n", - " 'you',\n", - " \"'d\",\n", - " 'expect',\n", - " '--',\n", - " 'but',\n", - " 'nothing',\n", - " 'more',\n", - " '.'],\n", - " ['best', 'indie', 'of', 'the', 'year', ',', 'so', 'far', '.'],\n", - " ['hatfield',\n", - " 'and',\n", - " 'hicks',\n", - " 'make',\n", - " 'the',\n", - " 'oddest',\n", - " 'of',\n", - " 'couples',\n", - " ',',\n", - " 'and',\n", - " 'in',\n", - " 'this',\n", - " 'sense',\n", - " 'the',\n", - " 'movie',\n", - " 'becomes',\n", - " 'a',\n", - " 'study',\n", - " 'of',\n", - " 'the',\n", - " 'gambles',\n", - " 'of',\n", - " 'the',\n", - " 'publishing',\n", - " 'world',\n", - " ',',\n", - " 'offering',\n", - " 'a',\n", - " 'case',\n", - " 'study',\n", - " 'that',\n", - " 'exists',\n", - " 'apart',\n", - " 'from',\n", - " 'all',\n", - " 'the',\n", - " 'movie',\n", - " \"'s\",\n", - " 'political',\n", - " 'ramifications',\n", - " '.'],\n", - " ['it',\n", - " \"'s\",\n", - " 'like',\n", - " 'going',\n", - " 'to',\n", - " 'a',\n", - " 'house',\n", - " 'party',\n", - " 'and',\n", - " 'watching',\n", - " 'the',\n", - " 'host',\n", - " 'defend',\n", - " 'himself',\n", - " 'against',\n", - " 'a',\n", - " 'frothing',\n", - " 'ex-girlfriend',\n", - " '.'],\n", - " ['that',\n", - " 'the',\n", - " 'chuck',\n", - " 'norris',\n", - " '``',\n", - " 'grenade',\n", - " 'gag',\n", - " \"''\",\n", - " 'occurs',\n", - " 'about',\n", - " '7',\n", - " 'times',\n", - " 'during',\n", - " 'windtalkers',\n", - " 'is',\n", - " 'a',\n", - " 'good',\n", - " 'indication',\n", - " 'of',\n", - " 'how',\n", - " 'serious-minded',\n", - " 'the',\n", - " 'film',\n", - " 'is',\n", - " '.'],\n", - " ['the',\n", - " 'plot',\n", - " 'is',\n", - " 'romantic',\n", - " 'comedy',\n", - " 'boilerplate',\n", - " 'from',\n", - " 'start',\n", - " 'to',\n", - " 'finish',\n", - " '.'],\n", - " ['it',\n", - " 'arrives',\n", - " 'with',\n", - " 'an',\n", - " 'impeccable',\n", - " 'pedigree',\n", - " ',',\n", - " 'mongrel',\n", - " 'pep',\n", - " ',',\n", - " 'and',\n", - " 'almost',\n", - " 'indecipherable',\n", - " 'plot',\n", - " 'complications',\n", - " '.'],\n", - " ['a',\n", - " 'film',\n", - " 'that',\n", - " 'clearly',\n", - " 'means',\n", - " 'to',\n", - " 'preach',\n", - " 'exclusively',\n", - " 'to',\n", - " 'the',\n", - " 'converted',\n", - " '.'],\n", - " ['i',\n", - " 'still',\n", - " 'like',\n", - " 'moonlight',\n", - " 'mile',\n", - " ',',\n", - " 'better',\n", - " 'judgment',\n", - " 'be',\n", - " 'damned',\n", - " '.'],\n", - " ['a',\n", - " 'welcome',\n", - " 'relief',\n", - " 'from',\n", - " 'baseball',\n", - " 'movies',\n", - " 'that',\n", - " 'try',\n", - " 'too',\n", - " 'hard',\n", - " 'to',\n", - " 'be',\n", - " 'mythic',\n", - " ',',\n", - " 'this',\n", - " 'one',\n", - " 'is',\n", - " 'a',\n", - " 'sweet',\n", - " 'and',\n", - " 'modest',\n", - " 'and',\n", - " 'ultimately',\n", - " 'winning',\n", - " 'story',\n", - " '.'],\n", - " ['a',\n", - " 'bilingual',\n", - " 'charmer',\n", - " ',',\n", - " 'just',\n", - " 'like',\n", - " 'the',\n", - " 'woman',\n", - " 'who',\n", - " 'inspired',\n", - " 'it'],\n", - " ['like',\n", - " 'a',\n", - " 'less',\n", - " 'dizzily',\n", - " 'gorgeous',\n", - " 'companion',\n", - " 'to',\n", - " 'mr.',\n", - " 'wong',\n", - " \"'s\",\n", - " 'in',\n", - " 'the',\n", - " 'mood',\n", - " 'for',\n", - " 'love',\n", - " '--',\n", - " 'very',\n", - " 'much',\n", - " 'a',\n", - " 'hong',\n", - " 'kong',\n", - " 'movie',\n", - " 'despite',\n", - " 'its',\n", - " 'mainland',\n", - " 'setting',\n", - " '.'],\n", - " ['as',\n", - " 'inept',\n", - " 'as',\n", - " 'big-screen',\n", - " 'remakes',\n", - " 'of',\n", - " 'the',\n", - " 'avengers',\n", - " 'and',\n", - " 'the',\n", - " 'wild',\n", - " 'wild',\n", - " 'west',\n", - " '.'],\n", - " ['it',\n", - " \"'s\",\n", - " 'everything',\n", - " 'you',\n", - " \"'d\",\n", - " 'expect',\n", - " '--',\n", - " 'but',\n", - " 'nothing',\n", - " 'more',\n", - " '.'],\n", - " ['best', 'indie', 'of', 'the', 'year', ',', 'so', 'far', '.'],\n", - " ['hatfield',\n", - " 'and',\n", - " 'hicks',\n", - " 'make',\n", - " 'the',\n", - " 'oddest',\n", - " 'of',\n", - " 'couples',\n", - " ',',\n", - " 'and',\n", - " 'in',\n", - " 'this',\n", - " 'sense',\n", - " 'the',\n", - " 'movie',\n", - " 'becomes',\n", - " 'a',\n", - " 'study',\n", - " 'of',\n", - " 'the',\n", - " 'gambles',\n", - " 'of',\n", - " 'the',\n", - " 'publishing',\n", - " 'world',\n", - " ',',\n", - " 'offering',\n", - " 'a',\n", - " 'case',\n", - " 'study',\n", - " 'that',\n", - " 'exists',\n", - " 'apart',\n", - " 'from',\n", - " 'all',\n", - " 'the',\n", - " 'movie',\n", - " \"'s\",\n", - " 'political',\n", - " 'ramifications',\n", - " '.'],\n", - " ['it',\n", - " \"'s\",\n", - " 'like',\n", - " 'going',\n", - " 'to',\n", - " 'a',\n", - " 'house',\n", - " 'party',\n", - " 'and',\n", - " 'watching',\n", - " 'the',\n", - " 'host',\n", - " 'defend',\n", - " 'himself',\n", - " 'against',\n", - " 'a',\n", - " 'frothing',\n", - " 'ex-girlfriend',\n", - " '.'],\n", - " ['that',\n", - " 'the',\n", - " 'chuck',\n", - " 'norris',\n", - " '``',\n", - " 'grenade',\n", - " 'gag',\n", - " \"''\",\n", - " 'occurs',\n", - " 'about',\n", - " '7',\n", - " 'times',\n", - " 'during',\n", - " 'windtalkers',\n", - " 'is',\n", - " 'a',\n", - " 'good',\n", - " 'indication',\n", - " 'of',\n", - " 'how',\n", - " 'serious-minded',\n", - " 'the',\n", - " 'film',\n", - " 'is',\n", - " '.'],\n", - " ['the',\n", - " 'plot',\n", - " 'is',\n", - " 'romantic',\n", - " 'comedy',\n", - " 'boilerplate',\n", - " 'from',\n", - " 'start',\n", - " 'to',\n", - " 'finish',\n", - " '.'],\n", - " ['it',\n", - " 'arrives',\n", - " 'with',\n", - " 'an',\n", - " 'impeccable',\n", - " 'pedigree',\n", - " ',',\n", - " 'mongrel',\n", - " 'pep',\n", - " ',',\n", - " 'and',\n", - " 'almost',\n", - " 'indecipherable',\n", - " 'plot',\n", - " 'complications',\n", - " '.'],\n", - " ['a',\n", - " 'film',\n", - " 'that',\n", - " 'clearly',\n", - " 'means',\n", - " 'to',\n", - " 'preach',\n", - " 'exclusively',\n", - " 'to',\n", - " 'the',\n", - " 'converted',\n", - " '.']]" - ] - }, - "execution_count": 3, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "# 将所有数字转为小写\n", - "ds.apply(lambda x: x['raw_sentence'].lower(), new_field_name='raw_sentence')\n", - "# label转int\n", - "ds.apply(lambda x: int(x['label']), new_field_name='label_seq', is_target=True)\n", - "\n", - "def split_sent(ins):\n", - " return ins['raw_sentence'].split()\n", - "ds.apply(split_sent, new_field_name='words', is_input=True)\n" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Train size: 54\n", - "Test size: 23\n" - ] - } - ], - "source": [ - "# 分割训练集/验证集\n", - "train_data, dev_data = ds.split(0.3)\n", - "print(\"Train size: \", len(train_data))\n", - "print(\"Test size: \", len(dev_data))" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "[[120, 121, 6, 2, 122, 5, 72, 123, 3],\n", - " [14,\n", - " 4,\n", - " 152,\n", - " 153,\n", - " 154,\n", - " 155,\n", - " 8,\n", - " 156,\n", - " 157,\n", - " 9,\n", - " 16,\n", - " 2,\n", - " 158,\n", - " 21,\n", - " 159,\n", - " 30,\n", - " 98,\n", - " 57,\n", - " 4,\n", - " 160,\n", - " 161,\n", - " 13,\n", - " 162,\n", - " 163,\n", - " 164,\n", - " 165,\n", - " 3],\n", - " [4,\n", - " 112,\n", - " 113,\n", - " 15,\n", - " 114,\n", - " 35,\n", - " 10,\n", - " 68,\n", - " 115,\n", - " 69,\n", - " 8,\n", - " 23,\n", - " 116,\n", - " 5,\n", - " 18,\n", - " 36,\n", - " 11,\n", - " 4,\n", - " 70,\n", - " 7,\n", - " 117,\n", - " 7,\n", - " 118,\n", - " 119,\n", - " 71,\n", - " 3],\n", - " [4, 1, 1, 5, 138, 14, 2, 1, 1, 1, 12],\n", - " [2, 27, 11, 139, 140, 141, 15, 142, 8, 143, 3],\n", - " [12, 9, 14, 32, 8, 4, 59, 60, 7, 61, 2, 62, 63, 64, 65, 4, 66, 67, 3],\n", - " [97, 145, 14, 146, 147, 5, 148, 149, 23, 150, 3],\n", - " [4, 1, 1, 5, 138, 14, 2, 1, 1, 1, 12],\n", - " [4, 1, 1, 5, 138, 14, 2, 1, 1, 1, 12],\n", - " [14,\n", - " 4,\n", - " 152,\n", - " 153,\n", - " 154,\n", - " 155,\n", - " 8,\n", - " 156,\n", - " 157,\n", - " 9,\n", - " 16,\n", - " 2,\n", - " 158,\n", - " 21,\n", - " 159,\n", - " 30,\n", - " 98,\n", - " 57,\n", - " 4,\n", - " 160,\n", - " 161,\n", - " 13,\n", - " 162,\n", - " 163,\n", - " 164,\n", - " 165,\n", - " 3],\n", - " [10,\n", - " 2,\n", - " 82,\n", - " 83,\n", - " 84,\n", - " 85,\n", - " 86,\n", - " 87,\n", - " 88,\n", - " 89,\n", - " 90,\n", - " 91,\n", - " 92,\n", - " 93,\n", - " 11,\n", - " 4,\n", - " 28,\n", - " 94,\n", - " 6,\n", - " 95,\n", - " 96,\n", - " 2,\n", - " 17,\n", - " 11,\n", - " 3],\n", - " [12, 73, 20, 33, 74, 75, 5, 76, 77, 5, 7, 78, 79, 27, 80, 3],\n", - " [12, 78, 1, 24, 1, 2, 13, 11, 31, 1, 16, 1, 1, 133, 16, 1, 1, 3],\n", - " [24, 107, 24, 108, 109, 6, 2, 110, 7, 2, 34, 34, 111, 3],\n", - " [2, 27, 11, 139, 140, 141, 15, 142, 8, 143, 3],\n", - " [24, 107, 24, 108, 109, 6, 2, 110, 7, 2, 34, 34, 111, 3],\n", - " [97, 145, 14, 146, 147, 5, 148, 149, 23, 150, 3],\n", - " [4,\n", - " 112,\n", - " 113,\n", - " 15,\n", - " 114,\n", - " 35,\n", - " 10,\n", - " 68,\n", - " 115,\n", - " 69,\n", - " 8,\n", - " 23,\n", - " 116,\n", - " 5,\n", - " 18,\n", - " 36,\n", - " 11,\n", - " 4,\n", - " 70,\n", - " 7,\n", - " 117,\n", - " 7,\n", - " 118,\n", - " 119,\n", - " 71,\n", - " 3],\n", - " [12, 9, 99, 29, 100, 101, 30, 22, 58, 31, 3],\n", - " [12, 9, 99, 29, 100, 101, 30, 22, 58, 31, 3],\n", - " [120, 121, 6, 2, 122, 5, 72, 123, 3],\n", - " [1, 30, 1, 5, 1, 30, 1, 4, 1, 1, 1, 10, 1, 21, 1, 7, 1, 1, 1, 14, 1, 3],\n", - " [1,\n", - " 1,\n", - " 1,\n", - " 1,\n", - " 8,\n", - " 1,\n", - " 89,\n", - " 2,\n", - " 1,\n", - " 16,\n", - " 151,\n", - " 1,\n", - " 1,\n", - " 1,\n", - " 1,\n", - " 1,\n", - " 1,\n", - " 7,\n", - " 1,\n", - " 1,\n", - " 1,\n", - " 2,\n", - " 1,\n", - " 6,\n", - " 28,\n", - " 25,\n", - " 3]]" - ] - }, - "execution_count": 5, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "from fastNLP import Vocabulary\n", - "vocab = Vocabulary(min_freq=2)\n", - "train_data.apply(lambda x: [vocab.add(word) for word in x['words']])\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='word_seq', is_input=True)\n", - "dev_data.apply(lambda x: [vocab.to_index(word) for word in x['words']], new_field_name='word_seq', is_input=True)\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## step 3\n", - " 定义模型" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [], - "source": [ - "from fastNLP.models import CNNText\n", - "model = CNNText((len(vocab),50), num_classes=5, padding=2, dropout=0.1)\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## step 4\n", - "开始训练" - ] - }, - { - "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:numpy.ndarray (2)dtype:object, (3)shape:(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": "AttributeError", - "evalue": "'numpy.ndarray' object has no attribute 'contiguous'", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0mdev_data\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mdev_data\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0mloss\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mCrossEntropyLoss\u001b[0m\u001b[0;34m(\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----> 6\u001b[0;31m \u001b[0mmetrics\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mAccuracyMetric\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[0m\u001b[1;32m 7\u001b[0m )\n\u001b[1;32m 8\u001b[0m \u001b[0mtrainer\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 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--> 813\u001b[0;31m \u001b[0mpred_dict\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mmodel\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m**\u001b[0m\u001b[0mrefined_batch_x\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 814\u001b[0m \u001b[0mfunc_signature\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0m_get_func_signature\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[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 815\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0misinstance\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mpred_dict\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdict\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/torch/nn/modules/module.py\u001b[0m in \u001b[0;36m__call__\u001b[0;34m(self, *input, **kwargs)\u001b[0m\n\u001b[1;32m 489\u001b[0m \u001b[0mresult\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_slow_forward\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 490\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 491\u001b[0;31m \u001b[0mresult\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mforward\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\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 492\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mhook\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_forward_hooks\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mvalues\u001b[0m\u001b[0;34m(\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[1;32m 493\u001b[0m \u001b[0mhook_result\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mhook\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0minput\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mresult\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/models/cnn_text_classification.py\u001b[0m in \u001b[0;36mforward\u001b[0;34m(self, words, seq_len)\u001b[0m\n\u001b[1;32m 58\u001b[0m \u001b[0;34m:\u001b[0m\u001b[0;32mreturn\u001b[0m \u001b[0moutput\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mdict\u001b[0m \u001b[0mof\u001b[0m \u001b[0mtorch\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mLongTensor\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0mbatch_size\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mnum_classes\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 59\u001b[0m \"\"\"\n\u001b[0;32m---> 60\u001b[0;31m \u001b[0mx\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0membed\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mwords\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# [N,L] -> [N,L,C]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 61\u001b[0m \u001b[0mx\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mconv_pool\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# [N,L,C] -> [N,C]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 62\u001b[0m \u001b[0mx\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdropout\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\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/torch/nn/modules/module.py\u001b[0m in \u001b[0;36m__call__\u001b[0;34m(self, *input, **kwargs)\u001b[0m\n\u001b[1;32m 489\u001b[0m \u001b[0mresult\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_slow_forward\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 490\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 491\u001b[0;31m \u001b[0mresult\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mforward\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\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 492\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mhook\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_forward_hooks\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mvalues\u001b[0m\u001b[0;34m(\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[1;32m 493\u001b[0m \u001b[0mhook_result\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mhook\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0minput\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mresult\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/modules/encoder/embedding.py\u001b[0m in \u001b[0;36mforward\u001b[0;34m(self, x)\u001b[0m\n\u001b[1;32m 33\u001b[0m \u001b[0;34m:\u001b[0m\u001b[0;32mreturn\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mtorch\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mTensor\u001b[0m \u001b[0;34m:\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0mbatch\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mseq_len\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0membed_dim\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 34\u001b[0m \"\"\"\n\u001b[0;32m---> 35\u001b[0;31m \u001b[0mx\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0msuper\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mforward\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\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 36\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdropout\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\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/torch/nn/modules/sparse.py\u001b[0m in \u001b[0;36mforward\u001b[0;34m(self, input)\u001b[0m\n\u001b[1;32m 106\u001b[0m return F.embedding(\n\u001b[1;32m 107\u001b[0m \u001b[0minput\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mweight\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpadding_idx\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmax_norm\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 108\u001b[0;31m self.norm_type, self.scale_grad_by_freq, self.sparse)\n\u001b[0m\u001b[1;32m 109\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 110\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mextra_repr\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\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/torch/nn/functional.py\u001b[0m in \u001b[0;36membedding\u001b[0;34m(input, weight, padding_idx, max_norm, norm_type, scale_grad_by_freq, sparse)\u001b[0m\n\u001b[1;32m 1062\u001b[0m [ 0.6262, 0.2438, 0.7471]]])\n\u001b[1;32m 1063\u001b[0m \"\"\"\n\u001b[0;32m-> 1064\u001b[0;31m \u001b[0minput\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0minput\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcontiguous\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[0m\u001b[1;32m 1065\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mpadding_idx\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mnot\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 1066\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mpadding_idx\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[0;31mAttributeError\u001b[0m: 'numpy.ndarray' object has no attribute 'contiguous'" - ] - } - ], - "source": [ - "from fastNLP import Trainer, CrossEntropyLoss, AccuracyMetric\n", - "trainer = Trainer(model=model, \n", - " train_data=train_data, \n", - " dev_data=dev_data,\n", - " loss=CrossEntropyLoss(),\n", - " metrics=AccuracyMetric()\n", - " )\n", - "trainer.train()\n", - "print('Train finished!')\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### 本教程结束。更多操作请参考进阶教程。" - ] - }, - { - "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": 1 -} diff --git a/tutorials/quickstart.ipynb b/tutorials/quickstart.ipynb new file mode 100644 index 00000000..00c30c93 --- /dev/null +++ b/tutorials/quickstart.ipynb @@ -0,0 +1,280 @@ +{ + "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 +} diff --git a/tutorials/tutorial_one.ipynb b/tutorials/tutorial_one.ipynb new file mode 100644 index 00000000..db302238 --- /dev/null +++ b/tutorials/tutorial_one.ipynb @@ -0,0 +1,831 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "collapsed": true + }, + "source": [ + "# 详细指南" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "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": "markdown", + "metadata": {}, + "source": [ + "Instance表示一个样本,由一个或多个field(域,属性,特征)组成,每个field有名字和值。\n", + "\n", + "在初始化Instance时即可定义它包含的域,使用 \"field_name=field_value\"的写法。" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'raw_sentence': fake data type=str,\n", + "'label': 0 type=str}" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "from fastNLP import Instance\n", + "\n", + "dataset.append(Instance(raw_sentence='fake data', label='0'))\n", + "dataset[-1]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 数据处理" + ] + }, + { + "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,\n", + "'target': 1 type=int}" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "from fastNLP import Vocabulary\n", + "\n", + "# 将所有字母转为小写, 并所有句子变成单词序列\n", + "dataset.apply(lambda x: x['raw_sentence'].lower(), new_field_name='sentence')\n", + "dataset.apply_field(lambda x: x.split(), field_name='sentence', new_field_name='words')\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", + "\n", + "# 将label转为整数\n", + "dataset.apply(lambda x: int(x['label']), new_field_name='target')\n", + "dataset[0]" + ] + }, + { + "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", + "'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,\n", + "'seq_len': 37 type=int}\n" + ] + } + ], + "source": [ + "# 增加长度信息\n", + "dataset.apply_field(lambda x: len(x), field_name='words', new_field_name='seq_len')\n", + "print(dataset[0])" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 使用内置模块CNNText\n", + "设置为符合内置模块的名称" + ] + }, + { + "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", + "\n", + "model_cnn = CNNText((len(vocab),50), num_classes=5, padding=2, dropout=0.1)\n", + "model_cnn" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "我们在使用内置模块的时候,还应该使用应该注意把 field 设定成符合内置模型输入输出的名字。" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "words\n", + "seq_len\n", + "target\n" + ] + } + ], + "source": [ + "from fastNLP import Const\n", + "\n", + "dataset.rename_field('words', Const.INPUT)\n", + "dataset.rename_field('seq_len', Const.INPUT_LEN)\n", + "dataset.rename_field('target', Const.TARGET)\n", + "\n", + "dataset.set_input(Const.INPUT, Const.INPUT_LEN)\n", + "dataset.set_target(Const.TARGET)\n", + "\n", + "print(Const.INPUT)\n", + "print(Const.INPUT_LEN)\n", + "print(Const.TARGET)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 分割训练集/验证集/测试集" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "(64, 7, 7)" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "train_dev_data, test_data = dataset.split(0.1)\n", + "train_data, dev_data = train_dev_data.split(0.1)\n", + "len(train_data), len(dev_data), len(test_data)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 训练(model_cnn)" + ] + }, + { + "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": 8, + "metadata": {}, + "outputs": [], + "source": [ + "from fastNLP import CrossEntropyLoss\n", + "\n", + "# loss = CrossEntropyLoss()\n", + "# 等价于\n", + "loss = CrossEntropyLoss(pred=Const.OUTPUT, target=Const.TARGET)" + ] + }, + { + "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": 9, + "metadata": {}, + "outputs": [], + "source": [ + "from fastNLP import AccuracyMetric\n", + "\n", + "# metrics=AccuracyMetric()\n", + "# 等价于\n", + "metrics=AccuracyMetric(pred=Const.OUTPUT, target=Const.TARGET)" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "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, 16]) \n", + "\tseq_len: (1)type:torch.Tensor (2)dtype:torch.int64, (3)shape:torch.Size([2]) \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-12-21-38-34\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.285714\n", + "\n", + "Evaluation at Epoch 2/10. Step:4/20. AccuracyMetric: acc=0.428571\n", + "\n", + "Evaluation at Epoch 3/10. Step:6/20. AccuracyMetric: acc=0.428571\n", + "\n", + "Evaluation at Epoch 4/10. Step:8/20. AccuracyMetric: acc=0.428571\n", + "\n", + "Evaluation at Epoch 5/10. Step:10/20. AccuracyMetric: acc=0.428571\n", + "\n", + "Evaluation at Epoch 6/10. Step:12/20. AccuracyMetric: acc=0.428571\n", + "\n", + "Evaluation at Epoch 7/10. Step:14/20. AccuracyMetric: acc=0.428571\n", + "\n", + "Evaluation at Epoch 8/10. Step:16/20. AccuracyMetric: acc=0.857143\n", + "\n", + "Evaluation at Epoch 9/10. Step:18/20. AccuracyMetric: acc=0.857143\n", + "\n", + "Evaluation at Epoch 10/10. Step:20/20. AccuracyMetric: acc=0.857143\n", + "\n", + "\n", + "In Epoch:8/Step:16, got best dev performance:AccuracyMetric: acc=0.857143\n", + "Reloaded the best model.\n" + ] + }, + { + "data": { + "text/plain": [ + "{'best_eval': {'AccuracyMetric': {'acc': 0.857143}},\n", + " 'best_epoch': 8,\n", + " 'best_step': 16,\n", + " 'seconds': 0.21}" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "from fastNLP import Trainer\n", + "\n", + "trainer = Trainer(model=model_cnn, train_data=train_data, dev_data=dev_data, loss=loss, metrics=metrics)\n", + "trainer.train()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 测试(model_cnn)" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[tester] \n", + "AccuracyMetric: acc=0.857143\n" + ] + }, + { + "data": { + "text/plain": [ + "{'AccuracyMetric': {'acc': 0.857143}}" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "from fastNLP import Tester\n", + "\n", + "tester = Tester(test_data, model_cnn, metrics=AccuracyMetric())\n", + "tester.test()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 编写自己的模型\n", + "\n", + "完全支持 pytorch 的模型,与 pytorch 唯一不同的是返回结果是一个字典,字典中至少需要包含 \"pred\" 这个字段" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [], + "source": [ + "import torch\n", + "import torch.nn as nn\n", + "\n", + "class LSTMText(nn.Module):\n", + " def __init__(self, vocab_size, embedding_dim, output_dim, hidden_dim=64, num_layers=2, dropout=0.5):\n", + " super().__init__()\n", + "\n", + " self.embedding = nn.Embedding(vocab_size, embedding_dim)\n", + " self.lstm = nn.LSTM(embedding_dim, hidden_dim, num_layers=num_layers, bidirectional=True, dropout=dropout)\n", + " self.fc = nn.Linear(hidden_dim * 2, output_dim)\n", + " self.dropout = nn.Dropout(dropout)\n", + "\n", + " def forward(self, words):\n", + " # (input) words : (batch_size, seq_len)\n", + " words = words.permute(1,0)\n", + " # words : (seq_len, batch_size)\n", + "\n", + " embedded = self.dropout(self.embedding(words))\n", + " # embedded : (seq_len, batch_size, embedding_dim)\n", + " output, (hidden, cell) = self.lstm(embedded)\n", + " # output: (seq_len, batch_size, hidden_dim * 2)\n", + " # hidden: (num_layers * 2, batch_size, hidden_dim)\n", + " # cell: (num_layers * 2, batch_size, hidden_dim)\n", + "\n", + " hidden = torch.cat((hidden[-2, :, :], hidden[-1, :, :]), dim=1)\n", + " hidden = self.dropout(hidden)\n", + " # hidden: (batch_size, hidden_dim * 2)\n", + "\n", + " pred = self.fc(hidden.squeeze(0))\n", + " # result: (batch_size, output_dim)\n", + " return {\"pred\":pred}" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "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, 16]) \n", + "\tseq_len: (1)type:torch.Tensor (2)dtype:torch.int64, (3)shape:torch.Size([2]) \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-12-21-38-36\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.571429\n", + "\n", + "Evaluation at Epoch 2/10. Step:4/20. AccuracyMetric: acc=0.571429\n", + "\n", + "Evaluation at Epoch 3/10. Step:6/20. AccuracyMetric: acc=0.571429\n", + "\n", + "Evaluation at Epoch 4/10. Step:8/20. AccuracyMetric: acc=0.571429\n", + "\n", + "Evaluation at Epoch 5/10. Step:10/20. AccuracyMetric: acc=0.714286\n", + "\n", + "Evaluation at Epoch 6/10. Step:12/20. AccuracyMetric: acc=0.857143\n", + "\n", + "Evaluation at Epoch 7/10. Step:14/20. AccuracyMetric: acc=0.857143\n", + "\n", + "Evaluation at Epoch 8/10. Step:16/20. AccuracyMetric: acc=0.857143\n", + "\n", + "Evaluation at Epoch 9/10. Step:18/20. AccuracyMetric: acc=0.857143\n", + "\n", + "Evaluation at Epoch 10/10. Step:20/20. AccuracyMetric: acc=0.857143\n", + "\n", + "\n", + "In Epoch:6/Step:12, got best dev performance:AccuracyMetric: acc=0.857143\n", + "Reloaded the best model.\n" + ] + }, + { + "data": { + "text/plain": [ + "{'best_eval': {'AccuracyMetric': {'acc': 0.857143}},\n", + " 'best_epoch': 6,\n", + " 'best_step': 12,\n", + " 'seconds': 2.15}" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "model_lstm = LSTMText(len(vocab),50,5)\n", + "trainer = Trainer(model=model_lstm, train_data=train_data, dev_data=dev_data, loss=loss, metrics=metrics)\n", + "trainer.train()" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[tester] \n", + "AccuracyMetric: acc=0.857143\n" + ] + }, + { + "data": { + "text/plain": [ + "{'AccuracyMetric': {'acc': 0.857143}}" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "tester = Tester(test_data, model_lstm, metrics=AccuracyMetric())\n", + "tester.test()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 使用 Batch编写自己的训练过程" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Epoch 0 Avg Loss: 3.11 18ms\n", + "Epoch 1 Avg Loss: 2.88 30ms\n", + "Epoch 2 Avg Loss: 2.69 42ms\n", + "Epoch 3 Avg Loss: 2.47 54ms\n", + "Epoch 4 Avg Loss: 2.38 67ms\n", + "Epoch 5 Avg Loss: 2.10 78ms\n", + "Epoch 6 Avg Loss: 2.06 91ms\n", + "Epoch 7 Avg Loss: 1.92 103ms\n", + "Epoch 8 Avg Loss: 1.91 114ms\n", + "Epoch 9 Avg Loss: 1.76 126ms\n", + "[tester] \n", + "AccuracyMetric: acc=0.571429\n" + ] + }, + { + "data": { + "text/plain": [ + "{'AccuracyMetric': {'acc': 0.571429}}" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "from fastNLP import BucketSampler\n", + "from fastNLP import Batch\n", + "import torch\n", + "import time\n", + "\n", + "model = CNNText((len(vocab),50), num_classes=5, padding=2, dropout=0.1)\n", + "\n", + "def train(epoch, data):\n", + " optim = torch.optim.Adam(model.parameters(), lr=0.001)\n", + " lossfunc = torch.nn.CrossEntropyLoss()\n", + " batch_size = 32\n", + "\n", + " # 定义一个Batch,传入DataSet,规定batch_size和去batch的规则。\n", + " # 顺序(Sequential),随机(Random),相似长度组成一个batch(Bucket)\n", + " train_sampler = BucketSampler(batch_size=batch_size, seq_len_field_name='seq_len')\n", + " train_batch = Batch(batch_size=batch_size, dataset=data, sampler=train_sampler)\n", + " \n", + " start_time = time.time()\n", + " for i in range(epoch):\n", + " loss_list = []\n", + " for batch_x, batch_y in train_batch:\n", + " optim.zero_grad()\n", + " output = model(batch_x['words'])\n", + " loss = lossfunc(output['pred'], batch_y['target'])\n", + " loss.backward()\n", + " optim.step()\n", + " loss_list.append(loss.item())\n", + " print('Epoch {:d} Avg Loss: {:.2f}'.format(i, sum(loss_list) / len(loss_list)),end=\" \")\n", + " print('{:d}ms'.format(round((time.time()-start_time)*1000)))\n", + " loss_list.clear()\n", + " \n", + "train(10, train_data)\n", + "tester = Tester(test_data, model, metrics=AccuracyMetric())\n", + "tester.test()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 使用 Callback 实现自己想要的效果" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "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, 16]) \n", + "\tseq_len: (1)type:torch.Tensor (2)dtype:torch.int64, (3)shape:torch.Size([2]) \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-12-21-38-40\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.285714\n", + "\n", + "Sum Time: 51ms\n", + "\n", + "\n", + "Evaluation at Epoch 2/10. Step:4/20. AccuracyMetric: acc=0.285714\n", + "\n", + "Sum Time: 69ms\n", + "\n", + "\n", + "Evaluation at Epoch 3/10. Step:6/20. AccuracyMetric: acc=0.285714\n", + "\n", + "Sum Time: 91ms\n", + "\n", + "\n", + "Evaluation at Epoch 4/10. Step:8/20. AccuracyMetric: acc=0.571429\n", + "\n", + "Sum Time: 107ms\n", + "\n", + "\n", + "Evaluation at Epoch 5/10. Step:10/20. AccuracyMetric: acc=0.571429\n", + "\n", + "Sum Time: 125ms\n", + "\n", + "\n", + "Evaluation at Epoch 6/10. Step:12/20. AccuracyMetric: acc=0.571429\n", + "\n", + "Sum Time: 142ms\n", + "\n", + "\n", + "Evaluation at Epoch 7/10. Step:14/20. AccuracyMetric: acc=0.571429\n", + "\n", + "Sum Time: 158ms\n", + "\n", + "\n", + "Evaluation at Epoch 8/10. Step:16/20. AccuracyMetric: acc=0.571429\n", + "\n", + "Sum Time: 176ms\n", + "\n", + "\n", + "Evaluation at Epoch 9/10. Step:18/20. AccuracyMetric: acc=0.714286\n", + "\n", + "Sum Time: 193ms\n", + "\n", + "\n", + "Evaluation at Epoch 10/10. Step:20/20. AccuracyMetric: acc=0.857143\n", + "\n", + "Sum Time: 212ms\n", + "\n", + "\n", + "\n", + "In Epoch:10/Step:20, got best dev performance:AccuracyMetric: acc=0.857143\n", + "Reloaded the best model.\n" + ] + }, + { + "data": { + "text/plain": [ + "{'best_eval': {'AccuracyMetric': {'acc': 0.857143}},\n", + " 'best_epoch': 10,\n", + " 'best_step': 20,\n", + " 'seconds': 0.2}" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "from fastNLP import Callback\n", + "\n", + "start_time = time.time()\n", + "\n", + "class MyCallback(Callback):\n", + " def on_epoch_end(self):\n", + " print('Sum Time: {:d}ms\\n\\n'.format(round((time.time()-start_time)*1000)))\n", + " \n", + "\n", + "model = CNNText((len(vocab),50), num_classes=5, padding=2, dropout=0.1)\n", + "trainer = Trainer(model=model, train_data=train_data, dev_data=dev_data,\n", + " loss=CrossEntropyLoss(), metrics=AccuracyMetric(), callbacks=[MyCallback()])\n", + "trainer.train()" + ] + }, + { + "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": 1 +}