%%capture
from typing import *
from fastai2.basics import *
from fastai2.text.all import *
from fastai2.callback import *

from transformers import AutoConfig, AutoTokenizer, AutoModelWithLMHead, GPT2LMHeadModel, GPT2Tokenizer
from fastai_transformers_utils.all import TransformersTokenizer, TransformersNumericalize, GPT2LMHeadCallback, gpt2_lmhead_split
# all_slow
model_name = 'distilgpt2'
seq_len = 72
bs = 64
tokenizer = AutoTokenizer.from_pretrained(model_name)

RobertaTokenizerFast has an issue when working on mask language modeling where it introduces an extra encoded space before the mask token.See https://github.com/huggingface/transformers/pull/2778 for more information.

Data and Tokenization

path = untar_data(URLs.WIKITEXT_TINY)
df_train = pd.read_csv(path/'train.csv', header=None, names=['text'])
df_valid = pd.read_csv(path/'test.csv', header=None, names=['text'])
df_all = pd.concat([df_train, df_valid])
df_all.head()
text
0 \n = 2013 – 14 York City F.C. season = \n \n The 2013 – 14 season was the <unk> season of competitive association football and 77th season in the Football League played by York City Football Club , a professional football club based in York , North Yorkshire , England . Their 17th @-@ place finish in 2012 – 13 meant it was their second consecutive season in League Two . The season ran from 1 July 2013 to 30 June 2014 . \n Nigel Worthington , starting his first full season as York manager , made eight permanent summer signings . By the turn of the year York were only above the relegation z...
1 \n = Big Boy ( song ) = \n \n " Big Boy " <unk> " I 'm A Big Boy Now " was the first single ever recorded by the Jackson 5 , which was released by Steeltown Records in January 1968 . The group played instruments on many of their Steeltown compositions , including " Big Boy " . The song was neither a critical nor commercial success , but the Jackson family were delighted with the outcome nonetheless . \n The Jackson 5 would release a second single with Steeltown Records before moving to Motown Records . The group 's recordings at Steeltown Records were thought to be lost , but they were re...
2 \n = The Remix ( Lady Gaga album ) = \n \n The Remix is a remix album by American recording artist Lady Gaga . Released in Japan on March 3 , 2010 , it contains remixes of the songs from her first studio album , The Fame ( 2008 ) , and her third extended play , The Fame Monster ( 2009 ) . A revised version of the track list was prepared for release in additional markets , beginning with Mexico on May 3 , 2010 . A number of recording artists have produced the songs , including Pet Shop Boys , Passion Pit and The Sound of Arrows . The remixed versions feature both uptempo and <unk> composit...
3 \n = New Year 's Eve ( Up All Night ) = \n \n " New Year 's Eve " is the twelfth episode of the first season of the American comedy television series Up All Night . The episode originally aired on NBC in the United States on January 12 , 2012 . It was written by Erica <unk> and was directed by Beth McCarthy @-@ Miller . The episode also featured a guest appearance from Jason Lee as Chris and Reagan 's neighbor and Ava 's boyfriend , Kevin . \n During Reagan ( Christina Applegate ) and Chris 's ( Will <unk> ) first New Year 's Eve game night , Reagan 's competitiveness comes out causing Ch...
4 \n = Geopyxis carbonaria = \n \n Geopyxis carbonaria is a species of fungus in the genus Geopyxis , family <unk> . First described to science in 1805 , and given its current name in 1889 , the species is commonly known as the charcoal loving elf @-@ cup , dwarf <unk> cup , <unk> <unk> cup , or pixie cup . The small , <unk> @-@ shaped fruitbodies of the fungus are reddish @-@ brown with a whitish fringe and measure up to 2 cm ( 0 @.@ 8 in ) across . They have a short , tapered stalk . Fruitbodies are commonly found on soil where brush has recently been burned , sometimes in great numbers ....
tok_list = L(parallel_gen(TransformersTokenizer, df_all.text, tokenizer=tokenizer)
            ).sorted().itemgot(1)
tok_df = df_all.copy()
tok_df.text =  tok_list.map(lambda x: ' '.join(x))
tok_df.head()
text
0 Ġ Ċ Ġ= Ġ2013 ĠâĢĵ Ġ14 ĠYork ĠCity ĠF . C . Ġseason Ġ= Ġ Ċ Ġ Ċ ĠThe Ġ2013 ĠâĢĵ Ġ14 Ġseason Ġwas Ġthe Ġ< unk > Ġseason Ġof Ġcompetitive Ġassociation Ġfootball Ġand Ġ77 th Ġseason Ġin Ġthe ĠFootball ĠLeague Ġplayed Ġby ĠYork ĠCity ĠFootball ĠClub Ġ, Ġa Ġprofessional Ġfootball Ġclub Ġbased Ġin ĠYork Ġ, ĠNorth ĠYorkshire Ġ, ĠEngland Ġ. ĠTheir Ġ17 th Ġ@ - @ Ġplace Ġfinish Ġin Ġ2012 ĠâĢĵ Ġ13 Ġmeant Ġit Ġwas Ġtheir Ġsecond Ġconsecutive Ġseason Ġin ĠLeague ĠTwo Ġ. ĠThe Ġseason Ġran Ġfrom Ġ1 ĠJuly Ġ2013 Ġto Ġ30 ĠJune Ġ2014 Ġ. Ġ Ċ ĠNigel ĠWor thing ton Ġ, Ġstarting Ġhis Ġfirst Ġfull Ġseason Ġas ĠYork...
1 Ġ Ċ Ġ= ĠBig ĠBoy Ġ( Ġsong Ġ) Ġ= Ġ Ċ Ġ Ċ Ġ" ĠBig ĠBoy Ġ" Ġ< unk > Ġ" ĠI Ġ' m ĠA ĠBig ĠBoy ĠNow Ġ" Ġwas Ġthe Ġfirst Ġsingle Ġever Ġrecorded Ġby Ġthe ĠJackson Ġ5 Ġ, Ġwhich Ġwas Ġreleased Ġby ĠSte elt own ĠRecords Ġin ĠJanuary Ġ1968 Ġ. ĠThe Ġgroup Ġplayed Ġinstruments Ġon Ġmany Ġof Ġtheir ĠSte elt own Ġcompositions Ġ, Ġincluding Ġ" ĠBig ĠBoy Ġ" Ġ. ĠThe Ġsong Ġwas Ġneither Ġa Ġcritical Ġnor Ġcommercial Ġsuccess Ġ, Ġbut Ġthe ĠJackson Ġfamily Ġwere Ġdelighted Ġwith Ġthe Ġoutcome Ġnonetheless Ġ. Ġ Ċ ĠThe ĠJackson Ġ5 Ġwould Ġrelease Ġa Ġsecond Ġsingle Ġwith ĠSte elt own ĠRecords Ġbefore Ġmoving Ġto...
2 Ġ Ċ Ġ= ĠThe ĠRemix Ġ( ĠLady ĠGaga Ġalbum Ġ) Ġ= Ġ Ċ Ġ Ċ ĠThe ĠRemix Ġis Ġa Ġremix Ġalbum Ġby ĠAmerican Ġrecording Ġartist ĠLady ĠGaga Ġ. ĠReleased Ġin ĠJapan Ġon ĠMarch Ġ3 Ġ, Ġ2010 Ġ, Ġit Ġcontains Ġremix es Ġof Ġthe Ġsongs Ġfrom Ġher Ġfirst Ġstudio Ġalbum Ġ, ĠThe ĠFame Ġ( Ġ2008 Ġ) Ġ, Ġand Ġher Ġthird Ġextended Ġplay Ġ, ĠThe ĠFame ĠMonster Ġ( Ġ2009 Ġ) Ġ. ĠA Ġrevised Ġversion Ġof Ġthe Ġtrack Ġlist Ġwas Ġprepared Ġfor Ġrelease Ġin Ġadditional Ġmarkets Ġ, Ġbeginning Ġwith ĠMexico Ġon ĠMay Ġ3 Ġ, Ġ2010 Ġ. ĠA Ġnumber Ġof Ġrecording Ġartists Ġhave Ġproduced Ġthe Ġsongs Ġ, Ġincluding ĠPet ĠShop ĠBo...
3 Ġ Ċ Ġ= ĠNew ĠYear Ġ' s ĠEve Ġ( ĠUp ĠAll ĠNight Ġ) Ġ= Ġ Ċ Ġ Ċ Ġ" ĠNew ĠYear Ġ' s ĠEve Ġ" Ġis Ġthe Ġtw elfth Ġepisode Ġof Ġthe Ġfirst Ġseason Ġof Ġthe ĠAmerican Ġcomedy Ġtelevision Ġseries ĠUp ĠAll ĠNight Ġ. ĠThe Ġepisode Ġoriginally Ġaired Ġon ĠNBC Ġin Ġthe ĠUnited ĠStates Ġon ĠJanuary Ġ12 Ġ, Ġ2012 Ġ. ĠIt Ġwas Ġwritten Ġby ĠErica Ġ< unk > Ġand Ġwas Ġdirected Ġby ĠBeth ĠMcCarthy Ġ@ - @ ĠMiller Ġ. ĠThe Ġepisode Ġalso Ġfeatured Ġa Ġguest Ġappearance Ġfrom ĠJason ĠLee Ġas ĠChris Ġand ĠReagan Ġ' s Ġneighbor Ġand ĠAv a Ġ' s Ġboyfriend Ġ, ĠKevin Ġ. Ġ Ċ ĠDuring ĠReagan Ġ( ĠChristina ĠApp legate Ġ) ...
4 Ġ Ċ Ġ= ĠGe opy x is Ġcarbon aria Ġ= Ġ Ċ Ġ Ċ ĠGe opy x is Ġcarbon aria Ġis Ġa Ġspecies Ġof Ġfungus Ġin Ġthe Ġgenus ĠGe opy x is Ġ, Ġfamily Ġ< unk > Ġ. ĠFirst Ġdescribed Ġto Ġscience Ġin Ġ18 05 Ġ, Ġand Ġgiven Ġits Ġcurrent Ġname Ġin Ġ1889 Ġ, Ġthe Ġspecies Ġis Ġcommonly Ġknown Ġas Ġthe Ġcharcoal Ġloving Ġelf Ġ@ - @ Ġcup Ġ, Ġdwarf Ġ< unk > Ġcup Ġ, Ġ< unk > Ġ< unk > Ġcup Ġ, Ġor Ġp ixie Ġcup Ġ. ĠThe Ġsmall Ġ, Ġ< unk > Ġ@ - @ Ġshaped Ġfruit b odies Ġof Ġthe Ġfungus Ġare Ġredd ish Ġ@ - @ Ġbrown Ġwith Ġa Ġwhit ish Ġfringe Ġand Ġmeasure Ġup Ġto Ġ2 Ġcm Ġ( Ġ0 Ġ@ . @ Ġ8 Ġin Ġ) Ġacross Ġ. ĠThey Ġhave Ġa...
# tok_df.to_csv(f'{model_name}_tok.csv', index=False)
# tok_df = pd.read_csv(f'{model_name}_tok.csv')

DataLoaders

splits = [list(range(0, len(df_train))), list(range(len(df_train), len(df_all)))]
ds_tfms = [
    [attrgetter('text'), lambda x: x.split(' '), TransformersNumericalize(tokenizer)]
]
dss = Datasets(tok_df, tfms=ds_tfms, splits=splits)
dss.train[0], dss.decode(dss.train[0])
((TensorText([220, 198, 796,  ..., 198, 220, 198]),),
 (" \n = 2013 – 14 York City F.C. season = \n \n The 2013 – 14 season was the <unk> season of competitive association football and 77th season in the Football League played by York City Football Club, a professional football club based in York, North Yorkshire, England. Their 17th @-@ place finish in 2012 – 13 meant it was their second consecutive season in League Two. The season ran from 1 July 2013 to 30 June 2014. \n Nigel Worthington, starting his first full season as York manager, made eight permanent summer signings. By the turn of the year York were only above the relegation zone on goal difference, before a 17 @-@ match unbeaten run saw the team finish in seventh @-@ place in the 24 @-@ team 2013 – 14 Football League Two. This meant York qualified for the play @-@ offs, and they were eliminated in the semi @-@ final by Fleetwood Town. York were knocked out of the 2013 – 14 FA Cup, Football League Cup and Football League Trophy in their opening round matches. \n 35 players made at least one appearance in nationally organised first @-@ team competition, and there were 12 different <unk>. Defender Ben Davies missed only five of the fifty @-@ two competitive matches played over the season. Wes Fletcher finished as leading scorer with 13 goals, of which 10 came in league competition and three came in the FA Cup. The winner of the <unk> of the Year award, voted for by the club's supporters, was <unk> Oyebanjo. \n \n = = Background and pre @-@ season = = \n \n The 2012 – 13 season was York City's first season back in the Football League, having won the Conference Premier play @-@ offs in 2011 – 12 after <unk> years in the Football Conference. Manager Gary Mills was sacked in March 2013 following an 11 @-@ match run without a victory, and was replaced by former Northern Ireland manager Nigel Worthington. Despite being in the relegation zone with three matches remaining, Worthington led the team to safety from relegation after a 1 – 0 win away to Dagenham & Redbridge on the final day of the season. York finished the season in 17th @-@ place in the 2012 – 13 League Two table. \n Following the previous season's conclusion Lee <unk>, Jon <unk>, Chris <unk>, Ben Everson, Scott Kerr, David <unk>, Patrick <unk>, Michael Potts, Jamie Reed and Jason Walker were released by York, while <unk> Blair departed for Fleetwood Town. David McGurk, <unk> Oyebanjo, Danny Parslow, Tom Platt and Chris Smith signed new contracts with the club. New players signed ahead of the start of the season were goalkeeper Chris <unk> on a season @-@ long loan from Blackpool, defender Ben Davies on loan from Preston North End, midfielders Craig Clay from Chesterfield and Lewis Montrose from Gillingham, winger <unk> Puri from St <unk> and strikers Ryan Bowman from Hereford United, Richard Cresswell from Sheffield United, Wes Fletcher from Burnley and Ryan Jarvis from Torquay United. Defender Mike Atkinson and striker Chris Dickinson entered the first @-@ team squad from the youth team after agreeing professional contracts. \n York retained the previous season's home and away kits. The home kit comprised red shirts with white sleeves, light blue shorts and white socks. The away kit included light blue shirts with white sleeves, white shorts and light blue socks. <unk> Health continued as shirt sponsors for the second successive season. \n \n = = Review = = \n \n \n = = = August = = = \n \n York began the season with a 1 – 0 home win over the previous season's play @-@ off finalists, Northampton Town, with <unk> Jarvis scoring the winning goal in the 90th @-@ minute. However, defeat came in York's match against Championship side Burnley in the first round of the League Cup, going down 4 – 0 at home. The team endured their first league defeat of the season in the following game after being beaten 2 – 0 away by Dagenham & Redbridge, the home team scoring in each half. York then held Hartlepool United to a 0 – 0 home draw, before being beaten 3 – 2 away by Bristol Rovers, in which Jarvis scored twice before John @-@ Joe O 'Toole scored the winning goal for the home team in the 67th @-@ minute. Two signings were made shortly before the transfer deadline ; defender George Taft was signed on a one @-@ month loan from Leicester City, while Middlesbrough midfielder Ryan Brobbel joined on a one @-@ month loan. <unk> John <unk>, who had been told he had no future with the club, departed after signing for FC Halifax Town. Jarvis gave York the lead away at Exeter City before Alan <unk> scored in each half to see the home team win 2 – 1. \n \n = = = September = = = \n \n York suffered their first home league defeat of the season after AFC Wimbledon won 2 – 0, with Michael Smith scoring in each half. Former Ipswich Town midfielder Josh Carson, who had a spell on loan with York the previous season, signed a contract until the end of 2013 – 14 and Sheffield United midfielder Elliott <unk> signed on a one @-@ month loan. Brobbel opened the scoring in the second minute of his home debut against Mansfield Town, although the away team went on to score twice to win 2 – 1. York's run of four defeats ended following a 1 – 1 draw away to Wycombe Wanderers, in which McGurk gave York the lead before the home team levelled through Dean Morgan. Taft was sent back to Leicester after he fell behind McGurk, Parslow and Smith in the pecking order for a central defensive berth. York achieved their first win since the opening day of the season after beating Portsmouth 4 – 2 at home, with Fletcher ( 2 ), Montrose and Jarvis scoring. \n \n = = = October = = = \n \n Defender Luke O 'Neill was signed from Burnley on a 28 @-@ day emergency loan. He made his debut in York's 3 – 0 win away at Torquay, which was the team's first successive win of the season. York were knocked out of the Football League Trophy in the second round after being beaten 3 – 0 at home by League One team Rotherham United, before their winning streak in the league was ended with a 3 – 0 defeat away to Newport County. York drew 2 – 2 away to Chesterfield, having taken a two @-@ goal lead through O 'Neill and Jarvis, before the home team fought back through Armand <unk> and Jay O <unk>. The team then hosted Fleetwood Town, and the visitors won 2 – 0 with goals scored in each half by Gareth Evans and <unk> Matt. Scunthorpe United were beaten 4 – 1 at home to end York's three @-@ match run without a win, with all the team's goals coming in the first half from Carson, Fletcher and Brobbel ( 2 ). \n \n = = = November = = = \n \n Bowman scored his first goals for York away to Cheltenham Town, as York twice fought back from behind to draw 2 – 2. York drew 3 – 3 away to Bristol Rovers to earn a first round replay in the FA Cup, taking the lead through Jarvis before Eliot Richards equalised for the home team. Carson scored a 30 yard volley to put York back in the lead, and after Bristol Rovers goals from Matt <unk> and Chris <unk>, Fletcher scored an 86th @-@ minute equaliser for York. Bowman scored with a header from an O 'Neill cross to open the scoring at home to Plymouth Argyle, which was the first goal the visitors had conceded in 500 minutes of action. However, Plymouth equalised 11 minutes later through <unk> <unk> and the match finished a 1 – 1 draw. York were knocked out of the FA Cup after losing 3 – 2 at home to Bristol Rovers in a first round replay ; the visitors were 3 – 0 up by 50 @-@ minutes before Fletcher pulled two back for York with a penalty and a long @-@ range strike. \n Defender Keith Lowe, of Cheltenham, and goalkeeper Nick Pope, of Charlton Athletic, were signed on loan until January 2014. They both played in York's first league defeat in four weeks, 2 – 1 away, to Southend United. <unk> <unk> gave Southend the lead early into the match and Bowman equalised for York with a low strike during the second half, before Luke Prosser scored the winning goal for the home side in stoppage time. With Pope preferred in goal, <unk> returned to Blackpool on his own accord, although his loan agreement would stay in place until January 2014. York then drew 0 – 0 away to Morecambe. After Pope was recalled from his loan by Charlton, York signed Wolverhampton Wanderers goalkeeper Aaron McCarey on loan until January 2014. McCarey kept a clean sheet in York's 0 – 0 home draw with Rochdale. \n \n = = = December = = = \n \n Cresswell retired from playing as a result of an eye complaint and a knee injury. York drew 1 – 1 away to Burton Albion, with an own goal scored by Shane <unk> @-@ <unk> giving York the lead in the 64th @-@ minute before the home team equalised eight minutes later through Billy <unk>. Atkinson was released after failing to force himself into the first team and signed for Scarborough Athletic, with whom he had been on loan. York drew 0 – 0 at home with second @-@ placed Oxford United, in which Carson came closest to scoring with a volley that <unk> across the face of the goal. This was followed by another draw after the match away to Accrington Stanley finished 1 – 1, with the home team <unk> 10 minutes after a Fletcher penalty had given York the lead in the 35th @-@ minute. Striker <unk> McDonald, who had been released by Peterborough United, was signed on a contract until the end of the season. York's last match of 2013 was a 2 – 1 defeat away at Bury, a result that ended York's run of consecutive draws at five. The home team were 2 – 0 up by the 19th @-@ minute, before Michael Coulson scored York's goal in the 73rd @-@ minute. This result meant York would begin 2014 in 22nd @-@ position in the table, only out of the relegation zone on goal difference. \n \n = = = January = = = \n \n Jarvis scored the only goal in York's first win since October 2013, a 1 – 0 home victory over Morecambe on New Year's Day. McCarey was recalled by Wolverhampton Wanderers due to an injury to one of their <unk>, while O 'Neill was recalled by Burnley to take part in their FA Cup match. York achieved back @-@ to @-@ back wins for the first time since October 2013 after Dagenham & Redbridge were beaten 3 – 1 at home, with Bowman opening the scoring in the second half before Fletcher scored twice. Adam Reed, who had a spell on loan with York in the previous season, was signed on a contract until the end of the season after parting company with Burton. Davies'loan was extended, while Brobbel and <unk> returned to their parent clubs. Cheltenham club captain Russell Penn, a midfielder, was signed on a two @-@ and @-@ a @-@ half @-@ year contract for an undisclosed fee. Lowe was subsequently signed permanently from Cheltenham on a two @-@ and @-@ a @-@ half @-@ year contract for an undisclosed fee. Having been allowed to leave the club on a free transfer, Ashley Chambers signed for Conference Premier club Cambridge United. \n York achieved three successive wins for the first time in 2013 – 14 after beating Northampton 2 – 0 away, with Bowman and Fletcher scoring in three @-@ second half minutes. Defender John McCombe was signed on a two @-@ and @-@ a @-@ half @-@ year contract following his release from Mansfield, before Clay and Jamal <unk> left York by mutual consent. Pope returned to York on loan from Charlton for the remainder of the season. York's run of wins ended with a 0 – 0 draw at home to Bristol Rovers, before their first defeat of the year came after losing 2 – 0 away to Hartlepool. Preston winger Will Hayhurst, a Republic of Ireland under @-@ 21 international, was signed on a one @-@ month loan. York fell to a successive defeat for the first time since September 2013 after being beaten 2 – 0 at home by Chesterfield. Shortly after the match, Smith left the club by mutual consent to pursue first @-@ team football. \n \n = = = February = = = \n \n Fletcher scored a 90th @-@ minute winner for York away to Fleetwood in a 2 – 1 win, a result that ended Fleetwood's five @-@ match unbeaten run. York then drew 0 – 0 at home to fellow mid @-@ table team Cheltenham, before beating Plymouth 4 – 0 away with goals from Fletcher, McCombe ( 2 ) and Carson as the team achieved successive away wins for the first time in 2013 – 14. York went without scoring for a fourth consecutive home match after drawing 0 – 0 with Southend. Having worn the <unk> since an injury to McGurk, Penn was appointed captain for the rest of the season, a position that had earlier been held by Smith and Parslow. \n \n = = = March = = = \n \n York achieved their first home win in five matches after beating Exeter 2 – 1, with first half goals scored by McCombe and Coulson. Hayhurst's loan was extended to the end of the season, having impressed in his six appearances for the club. Coulson scored again with the only goal, a 41st @-@ minute header, in York's 1 – 0 away win over AFC Wimbledon. Bowman scored the only goal with a 32nd @-@ minute penalty as York won 1 – 0 away against Mansfield, in which Fletcher missed the opportunity to extend the lead when his stoppage time penalty was saved by Alan Marriott. York moved one place outside the play @-@ offs with a 2 – 0 home win over Wycombe, courtesy of a second Bowman penalty in as many matches and a Carson goal from the edge of the penalty area. Coulson scored York's only goal in a 1 – 0 away win over struggling Portsmouth with a low volley in the fifth @-@ minute ; this result meant York moved into the play @-@ offs in seventh @-@ place with eight fixtures remaining. \n Striker Calvin Andrew, who had been released by Mansfield in January 2014, was signed on a contract for the remainder of the season. He made his debut as a substitute in York's 1 – 0 home win over bottom of the table Torquay, in which Hayhurst scored the only goal in the 11th @-@ minute with an 18 yard shot that <unk> off Aaron <unk>. Middlesbrough winger Brobbel rejoined on loan until the end of the season, following an injury to Carson. York's run of successive wins ended on six matches after a 0 – 0 home draw with Burton, and this result saw York drop out of the play @-@ offs in eighth @-@ place. With the team recording six wins and one draw in March 2014, including six clean sheets, Worthington was named League Two Manager of the Month. \n \n = = = April = = = \n \n Pope made a number of saves as York held league leaders Rochdale to a 0 – 0 away draw, with a point being enough to lift the team back into seventh @-@ place. York were prevented from equalling a club record of eight consecutive clean sheets when Accrington scored a stoppage time equaliser in a 1 – 1 home draw, in which York had taken earlier taken the lead with a Coulson penalty. A 1 – 0 win away win over Oxford, which was decided by a second half Coulson penalty, resulted in York moving one place above their opponents and back into seventh @-@ place. York consolidated their place in a play @-@ off position after beating Bury 1 – 0 at home with a fifth @-@ minute goal scored by Lowe from a Hayhurst corner. The result meant York opened up a five @-@ point lead over eighth @-@ placed Oxford with two fixtures remaining. A place in the League Two play @-@ offs was secured following a 1 – 0 win over Newport at home, in which Coulson scored the only goal in the 77th @-@ minute with a 25 yard free kick. Pope earned a nomination for League Two Player of the Month for April 2014, having conceded only one goal in five matches in that period. \n \n = = = May = = = \n \n The league season concluded with an away match against divisional runners @-@ up Scunthorpe ; having gone two goals down York fought back to draw 2 – 2 with goals scored by Brobbel and Andrew. This result meant York finished the season in seventh @-@ place in League Two, and would thus play fourth @-@ placed Fleetwood in the play @-@ off semi @-@ final on the back of a 17 @-@ match unbeaten run. York lost 1 – 0 to Fleetwood in the first leg at <unk> Crescent ; the goal came from former York player <unk> Blair in the 50th @-@ minute, who scored from close range after Antoni <unk>'s shot was blocked on the line. A 0 – 0 draw away to Fleetwood in the second leg meant York were eliminated 1 – 0 on aggregate, ending the prospect of a second promotion in three seasons. At an awards night held at York Racecourse, Oyebanjo was voted <unk> of the Year for 2013 – 14. \n \n = = Summary and aftermath = = \n \n York mostly occupied the bottom half of the table before the turn of the year, and dropped as low as 23rd in September 2013. During February 2014 the team broke into the top half of the table and with one match left were in sixth @-@ place. York's defensive record was the third best in League Two with 41 goals conceded, bettered only by Southend ( 39 ) and Chesterfield ( 40 ). Davies made the highest number of appearances over the season, appearing in 47 of York's 52 matches. Fletcher was York's top scorer in the league and in all competitions, with 10 league goals and 13 in total. He was the only player to reach double figures, and was followed by Jarvis with nine goals. \n After the season ended York released Tom Allan, Andrew, Dickinson, McDonald, Puri and Reed, while McGurk retired from professional football. Bowman and Oyebanjo left to sign for Torquay and Crawley Town respectively while Coulson signed a new contract with the club. York's summer signings included goalkeeper Jason <unk> from Tranmere Rovers, defenders <unk> <unk> from Dagenham, Marvin McCoy from Wycombe and Dave Winfield from Shrewsbury Town, midfielders <unk> <unk> from Mansfield, Anthony <unk> from Southend and Luke <unk> from Shrewsbury and striker Jake Hyde from <unk>. \n \n = = Match details = = \n \n League positions are sourced by <unk>, while the remaining information is referenced individually. \n \n = = = Football League Two = = = \n \n \n = = = League table ( part ) = = = \n \n \n = = = FA Cup = = = \n \n \n = = = League Cup = = = \n \n \n = = = Football League Trophy = = = \n \n \n = = = Football League Two play @-@ offs = = = \n \n \n = = <unk> = = \n \n \n = = = In = = = \n \n <unk> around club names denote the player's contract with that club had expired before he joined York. \n \n = = = Out = = = \n \n <unk> around club names denote the player joined that club after his York contract expired. \n \n = = = Loan in = = = \n \n \n = = = Loan out = = = \n \n \n = = Appearances and goals = = \n \n Source : \n Numbers in parentheses denote appearances as substitute. \n Players with names struck through and marked left the club during the playing season. \n Players with names in italics and marked * were on loan from another club for the whole of their season with York. \n Players listed with no appearances have been in the <unk> squad but only as unused <unk>. \n Key to positions : <unk> – <unk> ; <unk> – Defender ; <unk> – <unk> ; <unk> – Forward \n \n",))
def get_dataloaders(dss, bs: int, seq_len: int):
    return dss.dataloaders(bs=bs, seq_len=seq_len, dl_type=LMDataLoader)

dls = get_dataloaders(dss, bs=2, seq_len=seq_len)
for x, y in dls.train:
    print(x.shape, x.dtype, x.device)
    print(x.shape, y.dtype, y.device)
    break
torch.Size([2, 72]) torch.int64 cuda:0
torch.Size([2, 72]) torch.int64 cuda:0
dls.show_batch(max_n=2)
text text_
0 \n = Liu Kang = \n \n Liu Kang ( Chinese : <unk> ; pinyin : <unk> ) is a fictional character from the Mortal Kombat fighting game series from Midway Games, introduced as one of the original seven player characters in the 1992 first game as a Shaolin monk who enters the Mortal Kombat tournament to save \n = Liu Kang = \n \n Liu Kang ( Chinese : <unk> ; pinyin : <unk> ) is a fictional character from the Mortal Kombat fighting game series from Midway Games, introduced as one of the original seven player characters in the 1992 first game as a Shaolin monk who enters the Mortal Kombat tournament to save Earth
1 , and Guatemala. Its frequency of appearance in the Appalachian Mountains of the United States has been described as " occasional to locally common ". Mycologist David Arora notes that in the United States, the species is found with ponderosa pine in Arizona, but is absent in California's ponderosa pine forests. It has also been collected from China, India and Guatemala. Its frequency of appearance in the Appalachian Mountains of the United States has been described as " occasional to locally common ". Mycologist David Arora notes that in the United States, the species is found with ponderosa pine in Arizona, but is absent in California's ponderosa pine forests. It has also been collected from China, India,

Learner

dls = get_dataloaders(dss, bs=bs, seq_len=seq_len)
model = AutoModelWithLMHead.from_pretrained(model_name)
learn = Learner(dls, 
                model, 
                loss_func=CrossEntropyLossFlat(), 
                opt_func=ranger,
                splitter=gpt2_lmhead_split, 
                cbs=[GPT2LMHeadCallback()],
                metrics=[accuracy, Perplexity()],
               )

learn.freeze_to(-1)
# Seems like my gpt2_lmhead_split is useless for lm_head but I don't know why, so I manually unfreeze the last layer.
learn.model.lm_head.requires_grad_(True)
learn.summary()
GPT2LMHeadModel (Input shape: ['64 x 72'])
================================================================
Layer (type)         Output Shape         Param #    Trainable 
================================================================
Embedding            64 x 72 x 768        38,597,376 True      
________________________________________________________________
Embedding            64 x 72 x 768        786,432    False     
________________________________________________________________
Dropout              64 x 72 x 768        0          False     
________________________________________________________________
LayerNorm            64 x 72 x 768        1,536      False     
________________________________________________________________
Conv1D               64 x 72 x 2304       1,771,776  False     
________________________________________________________________
Conv1D               64 x 72 x 768        590,592    False     
________________________________________________________________
Dropout              64 x 12 x 72 x 72    0          False     
________________________________________________________________
Dropout              64 x 72 x 768        0          False     
________________________________________________________________
LayerNorm            64 x 72 x 768        1,536      False     
________________________________________________________________
Conv1D               64 x 72 x 3072       2,362,368  False     
________________________________________________________________
Conv1D               64 x 72 x 768        2,360,064  False     
________________________________________________________________
Dropout              64 x 72 x 768        0          False     
________________________________________________________________
LayerNorm            64 x 72 x 768        1,536      False     
________________________________________________________________
Conv1D               64 x 72 x 2304       1,771,776  False     
________________________________________________________________
Conv1D               64 x 72 x 768        590,592    False     
________________________________________________________________
Dropout              64 x 12 x 72 x 72    0          False     
________________________________________________________________
Dropout              64 x 72 x 768        0          False     
________________________________________________________________
LayerNorm            64 x 72 x 768        1,536      False     
________________________________________________________________
Conv1D               64 x 72 x 3072       2,362,368  False     
________________________________________________________________
Conv1D               64 x 72 x 768        2,360,064  False     
________________________________________________________________
Dropout              64 x 72 x 768        0          False     
________________________________________________________________
LayerNorm            64 x 72 x 768        1,536      False     
________________________________________________________________
Conv1D               64 x 72 x 2304       1,771,776  False     
________________________________________________________________
Conv1D               64 x 72 x 768        590,592    False     
________________________________________________________________
Dropout              64 x 12 x 72 x 72    0          False     
________________________________________________________________
Dropout              64 x 72 x 768        0          False     
________________________________________________________________
LayerNorm            64 x 72 x 768        1,536      False     
________________________________________________________________
Conv1D               64 x 72 x 3072       2,362,368  False     
________________________________________________________________
Conv1D               64 x 72 x 768        2,360,064  False     
________________________________________________________________
Dropout              64 x 72 x 768        0          False     
________________________________________________________________
LayerNorm            64 x 72 x 768        1,536      False     
________________________________________________________________
Conv1D               64 x 72 x 2304       1,771,776  False     
________________________________________________________________
Conv1D               64 x 72 x 768        590,592    False     
________________________________________________________________
Dropout              64 x 12 x 72 x 72    0          False     
________________________________________________________________
Dropout              64 x 72 x 768        0          False     
________________________________________________________________
LayerNorm            64 x 72 x 768        1,536      False     
________________________________________________________________
Conv1D               64 x 72 x 3072       2,362,368  False     
________________________________________________________________
Conv1D               64 x 72 x 768        2,360,064  False     
________________________________________________________________
Dropout              64 x 72 x 768        0          False     
________________________________________________________________
LayerNorm            64 x 72 x 768        1,536      False     
________________________________________________________________
Conv1D               64 x 72 x 2304       1,771,776  False     
________________________________________________________________
Conv1D               64 x 72 x 768        590,592    False     
________________________________________________________________
Dropout              64 x 12 x 72 x 72    0          False     
________________________________________________________________
Dropout              64 x 72 x 768        0          False     
________________________________________________________________
LayerNorm            64 x 72 x 768        1,536      False     
________________________________________________________________
Conv1D               64 x 72 x 3072       2,362,368  False     
________________________________________________________________
Conv1D               64 x 72 x 768        2,360,064  False     
________________________________________________________________
Dropout              64 x 72 x 768        0          False     
________________________________________________________________
LayerNorm            64 x 72 x 768        1,536      False     
________________________________________________________________
Conv1D               64 x 72 x 2304       1,771,776  False     
________________________________________________________________
Conv1D               64 x 72 x 768        590,592    False     
________________________________________________________________
Dropout              64 x 12 x 72 x 72    0          False     
________________________________________________________________
Dropout              64 x 72 x 768        0          False     
________________________________________________________________
LayerNorm            64 x 72 x 768        1,536      False     
________________________________________________________________
Conv1D               64 x 72 x 3072       2,362,368  False     
________________________________________________________________
Conv1D               64 x 72 x 768        2,360,064  False     
________________________________________________________________
Dropout              64 x 72 x 768        0          False     
________________________________________________________________
LayerNorm            64 x 72 x 768        1,536      True      
________________________________________________________________
Linear               64 x 72 x 50257      38,597,376 True      
________________________________________________________________

Total params: 120,509,952
Total trainable params: 77,196,288
Total non-trainable params: 43,313,664

Optimizer used: <function ranger at 0x7f7bc6100730>
Loss function: FlattenedLoss of CrossEntropyLoss()

Model frozen up to parameter group number 8

Callbacks:
  - TrainEvalCallback
  - Recorder
  - ProgressCallback
  - GPT2LMHeadCallback
learn.fit_one_cycle(1, 1e-2, moms=(0.8,0.7,0.8), cbs=[ShortEpochCallback(short_valid=False)])
epoch train_loss valid_loss accuracy perplexity time
0 4.349194 0.304022 77.416046 00:26
# learn.lr_find()
# learn.fit_one_cycle(2, 1e-2, moms=(0.8,0.7,0.8))
# learn.save(f'{model_name}_1')
# # learn.load(f'{model_name}_1')
# learn.freeze_to(-2)
# # Seems like my gpt2_lmhead_split is useless for lm_head but I don't know why, so I manually unfreeze the last layer.
# learn.model.lm_head.requires_grad_(True)
# learn.fit_one_cycle(2, 1e-3, moms=(0.8,0.7,0.8))
# learn.save(f'{model_name}_2')

Export

# # fastai model
# learn.save(f'{model_name}_final')
# # transformeres model
# Path('./models/transformers_model').mkdir(exist_ok=True)
# learn.model.save_pretrained('./models/transformers_model')
# Path('./models/tokenizer').mkdir(exist_ok=True)
# tokenizer.save_pretrained('./models/tokenizer')

Client inference

# inference_model = GPT2LMHeadModel.from_pretrained('./models/transformers_model')
# inference_model.eval()
# tokenizer = GPT2Tokenizer.from_pretrained('./models/tokenizer')
# test_sentences = ['The dog', 'I think we']
# num_return_sequences = 2
# with torch.no_grad():
#     for sentence in test_sentences:
#         numeric_sentence = tokenizer.encode(sentence)
#         x = torch.tensor([numeric_sentence])
#         outputs = inference_model.generate(x, max_length=20, do_sample=True, num_beams=5, num_return_sequences=num_return_sequences, temperature=1.5)
#         for j in range(num_return_sequences): #  3 output sequences were generated
#             print('Generated {}: {}'.format(j, tokenizer.decode(outputs[0][j])))
#         print()