|
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775 |
- {
- "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<ipython-input-7-4b34d005949c>\u001b[0m in \u001b[0;36m<module>\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
- }
|