DEV Community


Posted on

LSTM Learns To Write Shakespeare Fanfiction

Recently, I used node.js and tensorflow.js to train an LSTM (Long Short Term Memory Network) on the life works of Shakespeare. I documented it in a youtube video here Needless to say, it generated some pretty interesting stuff. Below, I have text samples from its training - as it learned to write english words, construct scripts, and even produce something bordering on slightly comprehensible.

My LSTM had 3 layers of 512 cells, and I let it see ten characters into the past. It generated the output text character-by-character, so it had to learn english words all on its own.

Part 1

Epoch 1:

After a single epoch of training, the LSTM has figured out, well, not much. It knos that characters are grouped together, and are occasionally separated by spaces:

RnDj.Rbecdti o-it oenzn t
rhittlt ,ete a oiet ,  a sottt ahitetoa   te    n n uio  r  struuoul l  in

Epoch 5:

By epoch five, it has picked up on the idea of words, and has begun to use newlines effectively. The words are still nonsensical.

ahoetot   o  rt
t e e tn  h
oa md oe  i 
s  e p  a t  
nm e ehsic ds e oe  oer    d e tu de

Epoch 9:

By epoch 9, the neural net produces the most common vowel sounds, most of which are pronounceable.

ste lo uat rie hoe en ha ae tu uare oo eo oe eio te ott oi hhe te hon. Cuo to ae tor  ha whn bw

Epoch 10:

Most of what it generates in epoch 10 is nonsense, but it generates the first remotely english-looking sentence:

I thate hove moitee ou oa totte

Epoch 15:

By epoch 15, the AI is doing much better. It is producing a few english-ish words:

X3A'  ants rios we and yon heme on the bat in the pomite to renroe and his thered af the buth weth a

Epoch 20:

By epoch 20, the AI has learned the "script" structure of Shakespeare, and its improvement is quite significant. Even more english words appear:

Whe sons on thes fon on the bares
Than sot the comshes thes thon on ann he the thas os than to sast he dan theirs,
And anod.

The gowes the snades the ols with the fegins,
The getes burse the saurs bemoses thoy your than to son! Af the sorst me bud af you the paress.

MU thes than the an surn core these the secenss than they bathing hat the sirs that hears, and bures he as wert the peerss,
Ant nor then souts that wall the comtertet that wither.

Epoch 50:

After Epoch 20, improvement is gradual. However, by Epoch 50, the majority of words it is producing are english, and it has picked up on some grammatical structures:

I will no with the scrace his is the hore the stand his noble had enery the is Lore and he shall your gellow, he will 't: and you this are make think he wall bear him in my loved with his lrester the with his some.

Hear hand this a must that that that should have be him the made his mide and, and partion to the house that be lord with the would speak the world that is the generals and say besent me that I think me that are for the people
We have we is chacter this wifans,
A peace the should be mine stat with the meself the say the may your betord the might him that her in my with all the consareit.

Come, I know that his of here deserving, and his thou hath to cheass, and we did helcome be and made this noble dear the mare the wreed the mes the wide
And I think are the faunterang to must, and that no more the stard that he hast the shall me shall shall not be us mone that that this gase that that shall make I have me bear me maked his

Epoch 100:

By epoch 100, it has learned many english words, and the script it generates might look like Shakespeare at a glance.

Where is a matter; the people
Make the senator:
Whose the gods mother's tread the sons to the sourt to give his poppetions and the people, your solders the state the canter him be your wife the ploper to reash of him intent to should not so home and surperity him to might with him be the forter the come.

Second Conspirator:
And he was sach a soldier.

What, if the man if you the son,
The follows
fierte the gods to the people natery frame his reperity and the gods the gods the possed him voice and the markes and the cause to his fierter of more is Rome,
And, I do say the people the can son the state
As the condemned to be so the mother you

Part 2

After training the LSTM with a memory length of ten characters, I expanded its memory to twenty characters and trained it again. The results weren't too different. So I expanded the memory to fifty characters, and continued trainig the model. The scripts it generated were significantly better:

Epoch 1:

At first, there is no noticeable difference - you might noticed it has learned bigger words though - like counterfeit:

Jraarfsaoft me was to sorrow not stay and not such as it
The weeping met, the counterfeit an

Epoch 10:

By Epoch 10, the script is looking a lot better. With its bigger memory, the LSTM can write shorter lines, keep some consistent themes, and even keep track of who is in the scene.
It also generates some hilarious fake words - like "voomest".

I would they shall have now let me it is not the weard,
She would have not the stand on thee of the markys the preverent is your night.

What are with will the friends, stay send our stranger'd the matter.

Then they shall have been you quittle. I am a more with his dear forth
One our royal country on his grace of would proy,
There very in his dear, beoo the sun and the collery is his country.

I will poor the voomest the warrant the house.

I am not, when it is not we present to die all I have been keep him forth so slive the lies in more them bear uson stir.

Second Senator:
I have for the stay madam, and I will be my son sho

Epoch 30:

By Epoch 30, the script really sounds like a Shakespeare one. Virtually all of the words are English, and the LSTM impressively remembers that CORIOLANUS is in the scene, long after he has stopped talking.

Second Citizen:
Then soul, which make you may a peace my death with your son the stebict them.

Which common the office the hols, and then not in his horse are the bold with the people, though you would leaves
be sprong him to be at the regarve the desire their better his hand to the people.

Ay, the then the part them: and first the sontent,
The sun with our worthy he shall should the consul.

First Senator:
I shall be man, the matters to the country may be the thing man be do their our with thee.

I have should you come, good mark us the man, whom
That is the common to the consul men't to be with not a sport.

O, it is not they shall as he hath a man's blood
With the proud. Where he content him stoble to gentles to be the sone to the

Epoch 50:

While the generated text here is similar to that of Epoch 30, the LSTM has learned how to generate monologues, in addition to quick lines of dialogue.

Come, the bear come, the tribunes
I'ld be not be vanience to the common home the people.

Must of the show'd them.

My some house, what some and feel you me the common and to your
some own the meating, therefore a bear of thee be so head to breast
Than you see the war be so man a bear them
The dead to hear me see the some consider,
And the proved deven by the streech a common,
Come the mother whose mother?


I have speak be so shill for whose head,
The prithee see him they be not some with
the sent to take a speak them them my shall
I have precend so that you shall be continues
The voices with the people, they come,
And but he that the power hath we me here the stroke my man the people
That whoo's stand of the words, and now
The noble house thee with me this hither.

I know the selt him the strength.

I would be consul
The one but a come, and

Epoch 100:

After Epoch 50, the performance of the LSTM leveled off. The "Murderer" script it generated is here for closure (and it is also a pretty decent script - ha ha):

First Murderer:
I have done to him, then the man have much in the counsel,
When thou shalt not sent to the queen,
And the curse to have down the angel and the world
see the sun of the reasons so sent to see the remompy.

First Murderer:
Mary man!

Alas the mother to the air,
I have done to the death of shines, and his lawful.

First Murderer:
I will not shall not the business with a warrant the man
The such a dayn from him his absence for thy disconeen and offence
To the presence and still the reason to give
The world of did of heart me of men to the world
To see you in the hatchens to the stabbed for my world of clarge.

First Murderer:
To friends to meaning that thou art thou hadst ender peace.

Second Murderer:
So will not have saw to medness so he honour
To entertain with me on the sword, and the heaven.

One Last Script...

One particularly amusing script that was generated by the LSTM talks about "the people"... a lot:

Sir, comes when the people!

Do you a persons
That have not sword in the people end
That shall for the people.

What cause the people vail to the people
What you will be consul, the people.

Hear me death him to make the people!

No, no, be consul.

Nay, you shall therefore, sir, the people,
Which you should the one that the gracing for the wise the sun
Of the march of the people in the people.

You are to the people,
When he will the varture of the people his senate
That is the consul of your best
And sure of the honours they worthy little that is the death.

Therefore so are in the noble worthy request
The ground was take the people though the lattle and bench
That you shall to make the consent of the voices
What shall many in their proud to cold prosperous
True his found of the people,
Be content of the people.

And I'll leave you with that amazing work of literature. I hope you enjoyed looking at the hilarious results of my LSTM. Thanks for reading!

Top comments (11)

shadowtime2000 profile image

I have always tried to implement something like this in javascript because I don't like python syntax much but I have never been able to do so. Are you thinking of posting the source code on Github?

n8python profile image

Yes, the source code is here:

marcellothearcane profile image
marcellothearcane • Edited

Want some fun? Train it off Finnegan's Wake.

n8python profile image

This is the initial result with 10 characters of memory... more training is needed:

be she be dear to the 
could that won't 
he have a cories dound the fall for the tome to your deepon and he proyn 
will the prands and sire to the heal for the bear 
to the prast what's parturus of her and free of carce 
deal purked be the but a tather me ower be the dear and for the 
starled my merread of his stope when I sen all to tatter her never conned and most the 
bellasting porksing him to the mound to the lave a 
frean the grand. What he cranta and a beerny under the 
man the ever off the mubball to be the pras the cracker of his 
prinish with his 
be or the mean and say your ranish pare of the 
fing for you conder he's death 
to the 
love, be the bimbrean, my yulling be and redand and me handrear under your 
went the man he swall your nistent and the light of his litter be on the 
ceam with the burky 
of the gracked the 
ton the contapion, your 
the for the ball of the clan I will here's a time, 
he's dade of sullare and 
and dean of his love a
Enter fullscreen mode Exit fullscreen mode
marcellothearcane profile image

It's perfect.

Thread Thread
n8python profile image
n8programs • Edited

It does a bit better with 50 characters of memory:

Vara world Mestiel up 
the time a 
streem of the endab 
with the heard as he shin like the played his strike into the 
porter like Borner Dolly denties, the Green and his persicing the some of the 
slippy of a salt of the 
and beact and could his plister with a triphifing from the was and under his tone the whom the same a wells he had 
turned by a luck of his hick, the evergest best she person, in your sumplished by a farm 
and returning 
the potter which he was to the heads to the persowal prince of his 
such a 
merting his 
the persing the recembers to his bull you as the park the strike me he was the with the 

not a plushed least when the suntines 
of all the recondating the the his 
was a hused and the reding the but where, had the preating about the denice of his conninned who so a 
mill the back at the brown the like the same ded the mindly for the sheeling, in the pinklers and been the courtel 
and the return the sensed of the fight of the warves the cork of his were which he 

Here's a github repo of all the Finnigan's Wake stuff it generated:

n8python profile image

I just looked at Finnegan's Wake... it is even less comprehensible than Shakespeare. I probably will.

andrewmplummer profile image
Andrew Plummer

I'm beginning to get very interested in NLP haha

n8python profile image

Text generating models are very fun! I'd totally recommend you get into them!

kretaceous profile image
Abhijit Hota

This is awesome!

n8python profile image