In [1]:
import numpy as np
from collections import Counter
import tensorflow as tf
from keras.callbacks import ModelCheckpoint
from keras.models import Sequential
from keras.layers.core import Dense, Activation, Dropout, Flatten
from keras import optimizers
from keras.layers.embeddings import Embedding
from keras.layers.recurrent import LSTM


Using TensorFlow backend.

Reading Corpus


In [2]:
corpus_file = open('corpus.txt', mode='r', encoding="utf8")
corpus = corpus_file.read()
corpus = corpus.lower()
print(len(corpus))


21892

In [3]:
vocab_char = set(corpus)
print(vocab_char)


{':', 'w', ')', '_', '”', 'z', 's', 't', 'k', 'n', 'p', '’', 'b', 'y', '“', '!', ';', ']', 'u', '(', 'q', 'e', 'c', 'm', 'd', '[', 'l', 'a', 'i', 'j', '-', 'x', '\n', '?', 'r', 'f', ',', '.', 'g', 'v', ' ', 'h', 'o'}

Wordwise


In [4]:
dict_punctuation = {
        '.':' ||Period|| ',
        ',':' ||Comma|| ',
        '"':' ||Quotation_Mark|| ',
        ';':' ||Semicolon|| ',
        '!':' ||Exclamation_Mark|| ',
        '?':' ||Question_Mark|| ',
        '(':' ||Left_Parenthesis|| ',
        ')':' ||Right_Parenthesis|| ',
        '--':' ||Double_Dash|| ',
        '-':' ||Dash|| ',
        '_':' ||Underscore|| ',
        '*':' ||Star|| ',
        '\n':' ||Return|| ',
        '’' :' ||Left_Quote|| ',
        '“' :' ||Right_Quotation|| ',
        '”' :' ||Left_Quotation|| ',
        '‘' :' ||Right_Quote|| '
    }

for key, token in dict_punctuation.items():
    corpus = corpus.replace(key, token)
    
word_corpus = corpus.split(' ')
print(word_corpus[1:15])
print(len(word_corpus))


['||Return||', 'the', 'black', 'cat', '||Period||', '', '||Return||', '', '||Return||', 'by', 'edgar', 'allan', 'poe', '||Period||']
5752

In [5]:
from collections import Counter
word_counts = Counter(word_corpus)
print(word_counts)


Counter({'': 640, '||Return||': 376, '||Comma||': 296, 'the': 270, 'of': 178, '||Period||': 176, 'i': 146, '||Dash||': 125, 'and': 122, 'to': 111, 'my': 110, 'a': 92, 'it': 70, '||Underscore||': 57, 'in': 55, 'had': 53, 'was': 51, 'with': 38, 'this': 37, 'me': 37, 'which': 35, 'at': 34, 'as': 33, 'for': 32, 'that': 31, 'not': 30, 'from': 29, 'by': 28, 'but': 25, 'upon': 24, 'one': 23, 'Quotation||': 22, 'been': 22, 'Mark||': 20, 'no': 19, 'its': 18, '||Exclamation': 17, '||Left': 16, '||Semicolon||': 15, 'have': 15, 'into': 15, 'or': 15, 'cat': 14, 'be': 14, 'more': 14, 'very': 13, 'so': 13, '||Right': 13, 'house': 12, 'than': 12, 'an': 11, 'myself': 11, 'even': 11, 'about': 11, 'wife': 10, 'day': 10, 'would': 9, 'night': 9, 'all': 9, 'when': 9, 'heart': 9, 'most': 9, 'is': 9, 'these': 8, 'came': 8, 'now': 8, 'once': 8, 'animal': 8, 'beast': 8, 'some': 8, 'were': 8, 'am': 8, 'wall': 8, 'own': 8, 'knew': 8, 'made': 8, 'could': 8, 'soul': 7, 'pluto': 7, 'he': 7, 'him': 7, 'who': 7, 'cellar': 7, 'up': 7, 'through': 6, 'because': 6, 'them': 6, 'length': 6, 'if': 6, 'such': 6, 'their': 6, 'half': 6, 'what': 6, 'walls': 6, 'fire': 6, 'yet': 6, 'on': 6, 'hand': 6, 'creature': 5, 'make': 5, 's': 5, 'will': 5, 'horror': 5, 'only': 5, 'large': 5, 'black': 5, 'then': 5, 'they': 5, 'Quote||': 5, 'place': 5, 'reason': 5, 'hung': 5, 'terror': 5, 'say': 5, 'little': 5, 'much': 5, 'within': 4, 'less': 4, 'disposition': 4, 'seemed': 4, 'his': 4, 'again': 4, 'violence': 4, 'brute': 4, 'although': 4, 'old': 4, 'mere': 4, 'before': 4, 'she': 4, 'however': 4, 'man': 4, 'spirit': 4, 'deed': 4, 'white': 4, 'eyes': 4, 'great': 4, 'character': 4, 'many': 4, 'dread': 4, 'appearance': 4, 'usual': 4, 'corpse': 4, 'having': 4, 'home': 4, 'nearly': 4, 'during': 4, ']': 4, 'found': 4, 'possible': 4, 'stood': 4, '[illustration:': 4, 'went': 4, 'body': 4, 'our': 4, 'there': 4, 'did': 4, 'well': 4, 'thus': 4, 'whole': 4, 'itself': 3, 'eye': 3, 'should': 3, 'former': 3, 'every': 3, 'slept': 3, 'destroyed': 3, 'loved': 3, 'sat': 3, 'hair': 3, 'remorse': 3, 'dog': 3, 'plaster': 3, 'left': 3, 'just': 3, 'her': 3, 'other': 3, 'time': 3, 'look': 3, 'nothing': 3, 'blow': 3, 'bosom': 3, 'head': 3, 'god': 3, 'thing': 3, 'prepared': 3, 'hatred': 3, 'frequent': 3, 'said': 3, 'morning': 3, 'fact': 3, 'course': 3, 'like': 3, 'murder': 3, 'monster': 3, 'whose': 3, 'are': 3, 'evil': 3, 'present': 3, 'easily': 3, '||Question': 3, 'against': 3, 'thoughts': 3, 'pets': 3, 'party': 3, 'while': 3, 'grew': 3, 'do': 3, 'portion': 3, 'any': 3, 'we': 3, 'has': 3, 'gentlemen': 3, 'appeared': 3, 'feeling': 3, 'long': 3, 'constructed': 3, 'degrees': 3, 'here': 3, 'immediately': 3, 'ill': 3, 'perverseness': 3, 'terrible': 3, 'soon': 3, 'hideous': 3, 'between': 3, 'readily': 3, 'felt': 3, 'attention': 3, 'without': 3, 'doubt': 3, 'far': 3, 'humanity': 3, 'first': 3, 'search': 3, 'crime': 3, 'may': 3, 'object': 3, 'down': 2, 'partiality': 2, 'stairs': 2, 'already': 2, 'friendship': 2, 'finally': 2, 'sooner': 2, 'regard': 2, 'phantasm': 2, 'caresses': 2, 'another': 2, 'intoxicated': 2, 'caused': 2, 'presence': 2, 'followed': 2, 'death': 2, 'unutterable': 2, 'degree': 2, 'figure': 2, 'period': 2, 'descended': 2, 'inflicted': 2, 'spread': 2, 'how': 2, 'vile': 2, 'better': 2, 'covering': 2, 'fury': 2, 'sagacious': 2, 'high': 2, 'tree': 2, 'pet': 2, 'intellect': 2, 'pen': 2, 'tomb': 2, 'remained': 2, 'temper': 2, 'memory': 2, 'sleep': 2, 'proceeded': 2, 'longer': 2, 'done': 2, 'blush': 2, 'scarcely': 2, 'third': 2, 'floor': 2, 'neither': 2, 'wish': 2, 'neck': 2, 'flames': 2, 'bricks': 2, 'Parenthesis||': 2, 'arose': 2, 'looked': 2, 'monkey': 2, 'premises': 2, 'feelings': 2, 'next': 2, 'bed': 2, 'you': 2, 'crowd': 2, 'socket': 2, 'lost': 2, 'take': 2, 'rigorous': 2, 'part': 2, 'sin': 2, 'beneath': 2, 'breath': 2, 'dislike': 2, 'carefully': 2, 'presented': 2, 'haunts': 2, 'saw': 2, 'beyond': 2, 'action': 2, 'approached': 2, 'cut': 2, 'end': 2, 'doing': 2, 'originally': 2, 'wretchedness': 2, 'mad': 2, 'either': 2, 'committing': 2, 'gradually': 2, 'rid': 2, 'accompany': 2, 'atrocity': 2, 'disturbed': 2, 'household': 2, 'triumph': 2, 'arms': 2, 'affection': 2, 'ashamed': 2, 'fell': 2, 'became': 2, 'police': 2, 'similar': 2, 'expect': 2, 'teeth': 2, 'gigantic': 2, 'off': 2, 'resolved': 2, 'breast': 2, 'reject': 2, 'rest': 2, 'nature': 2, 'constituted': 2, 'answered': 2, 'confess': 2, 'maltreating': 2, 'garden': 2, 'satisfied': 2, 'trouble': 2, 'feeble': 2, 'extreme': 2, 'put': 2, 'loss': 2, 'middle': 2, 'remembered': 2, 'events': 2, 'otherwise': 2, 'rabbits': 2, 'agony': 2, 'those': 2, 'given': 2, 'indefinite': 2, 'mark': 2, 'out': 2, 'reader': 2, 'might': 2, 'cause': 2, 'evident': 2, 'minute': 2, 'final': 2, 'sure': 2, 'experienced': 2, 'difficulty': 2, 'best': 2, 'deep': 2, 'walled': 2, 'strange': 2, 'overthrow': 2, 'sole': 2, 'something': 2, 'avoided': 2, 'name': 2, 'voice': 2, 'wrong': 2, 'moment': 2, 'possessed': 2, 'slow': 2, 'indeed': 2, 'fiend': 2, 'alone': 2, 'manner': 2, 'nor': 2, 'filled': 2, 'brickwork': 2, 'still': 2, 'latter': 2, 'fled': 2, 'use': 2, 'instantly': 2, 'disease': 2, 'returning': 2, 'awe': 2, 'gin': 2, 'entire': 2, 'never': 2, 'effects': 2, 'fourth': 2, 'favorite': 2, 'shudder': 2, 'sentiment': 2, 'way': 2, 'above': 2, 'cruelty': 2, 'getting': 2, 'alas': 2, 'relief': 2, 'exception': 2, 'offered': 2, 'purpose': 2, 'happy': 2, 'sense': 2, 'point': 2, 'almost': 2, 'find': 2, 'image': 2, 'impression': 2, 'accomplished': 2, 'cry': 2, 'somewhat': 2, 'least': 2, 'times': 2, '||Left_Parenthesis||': 2, 'wealth': 1, 'irrevocable': 1, 'hourly': 1, 'hundreds': 1, 'projects': 1, 'detect': 1, 'drowned': 1, 'leave': 1, 'contemptuously': 1, 'apparition': 1, 'recovered': 1, 'hogsheads': 1, 'fatal': 1, 'fright': 1, 'blood': 1, 'informing': 1, 'behind': 1, 'second': 1, 'cherished': 1, 'change': 1, 'self': 1, 'feel': 1, 'fidelity': 1, 'oh': 1, 'fear': 1, 'extended': 1, 'buried': 1, 'annoyance': 1, 'cane': 1, 'consequences': 1, 'habitually': 1, 'fiber': 1, 'investigation': 1, 'precaution': 1, 'carcass': 1, 'perceive': 1, 'causes': 1, 'poverty': 1, 'moreover': 1, 'chiefly': 1, 'witches': 1, 'continue': 1, 'regarded': 1, 'infinite': 1, 'attended': 1, 'rested': 1, 'withheld': 1, 'together': 1, 'feet': 1, 'seduced': 1, 'mood': 1, 'pleasures': 1, 'hit': 1, 'insert': 1, 'staggered': 1, 'uncongenial': 1, 'tormentor': 1, 'reverse': 1, 'seen': 1, 'longed': 1, 'remarkably': 1, 'walk': 1, 'penknife': 1, 'swelling': 1, 'looking': 1, 'doubly': 1, 'around': 1, 'detailed': 1, 'fro': 1, 'sobbing': 1, 'roamed': 1, 'true': 1, 'stayed': 1, 'rather': 1, 'rough': 1, 'tears': 1, 'measure': 1, 'yard': 1, 'fate': 1, 'discovery': 1, 'partly': 1, 'remnants': 1, 'silently': 1, 'arrangements': 1, 'prevented': 1, 'physical': 1, 'headlong': 1, 'test': 1, 'frequented': 1, 'good': 1, 'neighbors': 1, 'domesticated': 1, 'struggled': 1, 'terrified': 1, 'glee': 1, 'intensity': 1, 'deliberation': 1, 'vex': 1, 'incumbent': 1, 'circumstances': 1, 'uttered': 1, 'walked': 1, 'fastening': 1, 'darkest': 1, 'withdrew': 1, 'philosophy': 1, 'nurtured': 1, 'held': 1, 'noose': 1, 'dreaded': 1, 'commonplace': 1, 'eternally': 1, 'town': 1, 'disgusted': 1, 'accompanied': 1, 'throat': 1, 'simplest': 1, 'unexplored': 1, 'pertinacity': 1, 'conscience': 1, 'your': 1, 'thoroughly': 1, 'thenceforward': 1, 'imperfect': 1, 'expound': 1, 'dense': 1, 'expected': 1, 'reposing': 1, 'representation': 1, 'mankind': 1, 'effect': 1, 'happens': 1, 'flee': 1, 'malevolence': 1, 'distinguished': 1, 'happiness': 1, 'determined': 1, 'called': 1, 'succession': 1, 'passed': 1, 'lime': 1, 'blows': 1, 'dreams': 1, 'outbursts': 1, 'throw': 1, 'thereupon': 1, 'servant': 1, 'unselfish': 1, 'also': 1, 'claws': 1, 'directly': 1, 'hot': 1, 'why': 1, 'corner': 1, 'guiltlessness': 1, 'must': 1, 'alarmed': 1, 'preventing': 1, 'errand': 1, 'love': 1, 'agreeable': 1, 'human': 1, 'calm': 1, 'allan': 1, 'incarnate': 1, 'gossamer': 1, 'continued': 1, 'dream': 1, 'free': 1, 'allusion': 1, 'succeeding': 1, 'since': 1, 'comprehend': 1, 'open': 1, 'scream': 1, 'others': 1, 'unburden': 1, 'seized': 1, 'matter': 1, 'unfathomable': 1, 'waistcoat': 1, 'describe': 1, 'whatever': 1, 'observed': 1, 'bitterest': 1, 'added': 1, 'apartment': 1, 'lasted': 1, 'complete': 1, 'certain': 1, 'secure': 1, 'marvelous': 1, 'aroused': 1, 'arm': 1, 'compressed': 1, 'aid': 1, 'mortar': 1, 'conflagration': 1, 'rum': 1, 'recently': 1, 'meet': 1, 'absolute': 1, 'imagine': 1, 'direction': 1, 'faculties': 1, 'rose': 1, 'dislodged': 1, 'remember': 1, 'series': 1, 'resigned': 1, 'opposite': 1, 'fireplace': 1, 'destruction': 1, 'packing': 1, 'wailing': 1, 'able': 1, 'erect': 1, 'streaming': 1, 'finished': 1, 'occasion': 1, 'irritable': 1, 'calculation': 1, 'comment': 1, 'growth': 1, 'gave': 1, 'previous': 1, 'last': 1, 'absence': 1, 'stout': 1, 'rage': 1, 'beat': 1, 'inner': 1, 'ruins': 1, 'silly': 1, 'astonishing': 1, 'procuring': 1, 'feeding': 1, 'words': 1, 'attempt': 1, 'loudly': 1, 'particular': 1, 'box': 1, 'throats': 1, 'several': 1, 'seem': 1, 'blissful': 1, 'unexpectedly': 1, 'married': 1, 'splotch': 1, 'intimates': 1, 'deposited': 1, 'months': 1, 'crafty': 1, 'poe': 1, 'disgust': 1, 'demons': 1, 'surprise': 1, 'inhabit': 1, 'fully': 1, 'following': 1, 'outline': 1, 'new': 1, 'retained': 1, 'interference': 1, 'chimney': 1, 'thought': 1, 'officers': 1, 'probably': 1, 'face': 1, 'species': 1, 'remembrance': 1, 'peculiarity': 1, 'forthwith': 1, 'sudden': 1, 'explaining': 1, 'damnation': 1, 'anger': 1, 'gratification': 1, 'shriek': 1, 'guilt': 1, 'neglected': 1, 'understand': 1, 'burn': 1, 'excitable': 1, 'utterly': 1, 'mercy': 1, 'detested': 1, 'accident': 1, 'spent': 1, 'strong': 1, 'chain': 1, 'toiling': 1, 'singular': 1, 'beheld': 1, 'belief': 1, 'false': 1, 'resembling': 1, 'pressure': 1, 'prevent': 1, 'regret': 1, 'dark': 1, 'work': 1, 'curiosity': 1, 'concealment': 1, 'stupefied': 1, 'breathed': 1, 'inscrutability': 1, 'soundly': 1, 'same': 1, 'dig': 1, 'notion': 1, 'altogether': 1, 'sufferers': 1, 'bade': 1, 'propped': 1, 'spring': 1, 'imperceptible': 1, 'merciful': 1, 'occasioned': 1, 'tranquilly': 1, 'adjacent': 1, 'arch': 1, 'among': 1, 'facts': 1, 'curtains': 1, 'swallowed': 1, 'loathsome': 1, 'loathed': 1, 'deliver': 1, 'regardless': 1, 'plainly': 1, 'violently': 1, 'pleasure': 1, 'mournful': 1, 'surely': 1, 'ever': 1, 'evinced': 1, 'freshly': 1, 'hardening': 1, 'compartment': 1, 'world': 1, 'impossible': 1, 'delighted': 1, 'primary': 1, 'instant': 1, 'reflection': 1, 'inclination': 1, 'endeared': 1, 'thrown': 1, 'touched': 1, 'torments': 1, 'axe': 1, 'small': 1, 'original': 1, 'poor': 1, 'judgment': 1, 'slight': 1, 'position': 1, 'sunk': 1, 'consequently': 1, 'excess': 1, 'frightful': 1, 'trait': 1, 'weakness': 1, 'nook': 1, 'heavily': 1, 'conspicuous': 1, 'madness': 1, 'over': 1, 'footsteps': 1, 'gallows': 1, 'strike': 1, 'opportunity': 1, 'demon': 1, 'vain': 1, 'knows': 1, 'approach': 1, 'early': 1, 'desire': 1, 'homely': 1, 'evidence': 1, 'fashioned': 1, 'arrested': 1, 'continuous': 1, 'perceived': 1, 'need': 1, 'sufficient': 1, 'scruple': 1, 'fail': 1, 'suffer': 1, 'portraiture': 1, 'substance': 1, 'used': 1, 'fumes': 1, 'purchase': 1, 'sake': 1, 'variety': 1, 'procured': 1, 'supply': 1, 'years': 1, 'succinctly': 1, 'back': 1, 'logical': 1, 'where': 1, 'fine': 1, 'ancient': 1, 'merely': 1, 'instrumentality': 1, 'suddenly': 1, 'spoken': 1, 'dead': 1, 'too': 1, 'extraordinary': 1, 'things': 1, 'considered': 1, 'fellow': 1, 'loud': 1, 'engine': 1, 'burden': 1, 'solidly': 1, 'edgar': 1, 'irritation': 1, 'takes': 1, 'depart': 1, 'kind': 1, 'account': 1, 'us': 1, 'knees': 1, 'reverberation': 1, 'moodiness': 1, 'rubbish': 1, 'steep': 1, 'rubbed': 1, 'goldfish': 1, 'solicit': 1, 'despair': 1, 'person': 1, 'reach': 1, 'gore': 1, 'uncomplaining': 1, 'wine': 1, 'general': 1, 'shake': 1, 'resemble': 1, 'disaster': 1, 'muffled': 1, 'frenzy': 1, 'respect': 1, 'suspicious': 1, 'worse': 1, 'narrative': 1, 'fallen': 1, 'sharp': 1, 'especially': 1, 'equivocal': 1, 'loathing': 1, 'hogshead': 1, 'yes': 1, 'embarrassment': 1, 'restrain': 1, 'broken': 1, 'demoniacal': 1, 'disguise': 1, 'swooning': 1, 'merchandise': 1, 'fanciful': 1, 'indivisible': 1, 'purest': 1, 'folly': 1, 'ghastly': 1, 'chamber': 1, 'temperament': 1, 'alteration': 1, 'visited': 1, 'dress': 1, 'ages': 1, 'anything': 1, 'jeopardize': 1, 'drawn': 1, 'natural': 1, 'physically': 1, 'sentiments': 1, 'debauch': 1, 'violate': 1, 'delight': 1, 'birds': 1, 'power': 1, 'beautiful': 1, 'firmly': 1, 'den': 1, 'remove': 1, 'hell': 1, 'hitherto': 1, 'fond': 1, 'limb': 1, 'source': 1, 'closely': 1, 'pain': 1, 'tinctured': 1, 'noted': 1, 'derivable': 1, 'playmate': 1, 'supreme': 1, 'slightest': 1, 'entirely': 1, 'began': 1, 'primitive': 1, 'surface': 1, 'vast': 1, 'offer': 1, 'monks': 1, 'victims': 1, 'law': 1, 'whenever': 1, 'annoyed': 1, 'ordinary': 1, 'took': 1, 'folded': 1, 'suffered': 1, 'window': 1, 'labor': 1, 'permitted': 1, 'damned': 1, 'morrow': 1, 'howl': 1, 'after': 1, 'restrained': 1, 'principal': 1, 'calmly': 1, 'touching': 1, 'domestic': 1, 'spot': 1, 'damnable': 1, 'building': 1, 'let': 1, 'clamber': 1, 'chair': 1, 'suspicions': 1, 'observing': 1, 'reduce': 1, 'render': 1, 'patient': 1, 'immense': 1, 'companions': 1, 'notice': 1, 'intelligence': 1, 'fiendish': 1, 'inhuman': 1, 'care': 1, 'uplifting': 1, 'region': 1, 'bas': 1, 'chimeras': 1, 'insufferable': 1, 'docility': 1, 'worldly': 1, 'landlord': 1, 'crowbar': 1, 'felon': 1, 'groan': 1, 'adapted': 1, 'future': 1, 'personal': 1, 'link': 1, 'deprived': 1, 'health': 1, 'peevish': 1, 'lately': 1, 'perpetual': 1, 'examining': 1, 'dozen': 1, 'cutting': 1, 'thick': 1, 'truly': 1, 'deceived': 1, 'attributed': 1, 'forebore': 1, 'speaking': 1, 'grieved': 1, 'ungovernable': 1, 'picked': 1, 'goaded': 1, 'fondness': 1, 'arising': 1, 'destroy': 1, 'extremity': 1, 'brain': 1, 'deliberated': 1, 'parents': 1, 'assassination': 1, 'top': 1, 'furniture': 1, 'ammonia': 1, 'compelled': 1, 'sources': 1, 'baroques': 1, 'accuracy': 1, 'secured': 1, 'weeks': 1, 'aversion': 1, 'wrath': 1, 'wherever': 1, 'bodily': 1, 'minutes': 1, 'expressions': 1, 'cruel': 1, 'aimed': 1, 'startling': 1, 'distinctness': 1, 'inquiries': 1, 'fancy': 1, 'being': 1, 'instituted': 1, 'woe': 1, 'detail': 1, 'loosely': 1, 'rapped': 1, 'paltry': 1, 'language': 1, 'rabid': 1, 'go': 1, 'throughout': 1, 'silence': 1, 'moody': 1, 'anomalous': 1, 'tortured': 1, 'quivered': 1, 'blindly': 1, 'grasp': 1, 'grasped': 1, 'opened': 1, 'view': 1, 'intemperate': 1, 'alcohol': 1, 'assurance': 1, 'fancied': 1, 'abandoned': 1, 'sequence': 1, 'innocence': 1, 'craft': 1, 'decayed': 1, 'recorded': 1, 'fangs': 1, 'mention': 1, 'visible': 1, 'perhaps': 1, 'porter': 1, 'cats': 1, 'purred': 1, 'discovered': 1, 'set': 1, 'conjointly': 1, 'shield': 1, 'aye': 1, 'animals': 1, 'steps': 1, 'returned': 1, 'shame': 1, 'spectators': 1, 'forever': 1, 'cell': 1, 'give': 1, 'infancy': 1, 'consigned': 1, 'arousing': 1, 'dared': 1, 'speak': 1, 'slipped': 1, 'means': 1, 'indulged': 1, 'graven': 1, 'himself': 1, 'tenderness': 1, 'felicity': 1, 'superstition': 1, 'seeking': 1, 'courtesy': 1, 'immortal': 1, 'expedient': 1, 'projection': 1, 'slowly': 1, 'weight': 1, 'blessing': 1, 'fragments': 1, 'establish': 1, 'patting': 1, 'allayed': 1, 'reached': 1, 'plastered': 1, 'wished': 1, 'consummate': 1, 'anticipated': 1, 'rope': 1, 'proved': 1, 'behold': 1, 'distinguishing': 1, 'urged': 1, 'infamy': 1, 'forgetting': 1, 'motionless': 1, 'solitary': 1, 'intemperance': 1, 'childish': 1, 'exactly': 1, 'circumstance': 1, 'relaid': 1, 'excited': 1, 'detailing': 1, 'frame': 1, 'whom': 1, 'wound': 1, 'pocket': 1, 'injury': 1, 'serious': 1, 'increase': 1, 'hereafter': 1, 'structure': 1, 'offense': 1, 'triumphantly': 1, 'case': 1, 'untouched': 1, 'succumbed': 1, 'victim': 1, 'meantime': 1, 'wretched': 1, 'exult': 1, 'define': 1, 'wonder': 1, 'cold': 1, 'excellently': 1, 'lives': 1, 'occasionally': 1, 'risk': 1, 'falling': 1, 'immediate': 1, 'stooping': 1, 'hangman': 1, 'caressing': 1, 'claim': 1, 'word': 1, 'faithful': 1, 'increased': 1, 'goes': 1, 'popular': 1, 'concealing': 1, 'fed': 1, 'clotted': 1, 'plastering': 1, 'child': 1, 'right': 1, 'casting': 1, 'experience': 1, 'bravado': 1, 'resisted': 1, 'introduction': 1, 'escape': 1, 'impulses': 1, 'crouch': 1, 'dampness': 1, 'displace': 1, 'grave': 1, 'plunged': 1, 'pestilence': 1, 'collected': 1, 'going': 1, 'blazing': 1, 'merest': 1, 'burned': 1, 'sacrificing': 1, 'know': 1, 'destroying': 1, 'started': 1, 'minutest': 1, 'die': 1, 'sand': 1, 'eager': 1, 'radical': 1, 'chief': 1, 'nightmare': 1, 'quickly': 1, 'muscle': 1, 'steadily': 1, 'hardly': 1, 'conceive': 1, 'senses': 1, 'difficult': 1, 'derived': 1, 'heightened': 1, 'difference': 1, 'manhood': 1, 'abusing': 1, 'exasperated': 1, 'mouth': 1, 'arisen': 1, 'guilty': 1, 'atmosphere': 1, 'ax': 1, 'red': 1, 'jest': 1, 'accounted': 1, 'odious': 1, 'mind': 1, 'becoming': 1, 'unoffending': 1, 'ascended': 1, 'streets': 1, 'bitterness': 1, 'deliberately': 1, 'inspired': 1, 'brought': 1, 'throwing': 1, 'wild': 1, 'thrilled': 1, 'longing': 1, 'few': 1, 'alarm': 1, 'step': 1, 'flight': 1, 'assumed': 1, 'get': 1, 'task': 1, 'slumbers': 1, 'deadly': 1, 'persons': 1, 'entered': 1})

In [23]:
vocab = set(word_corpus)
num_classes = len(vocab)
print(num_classes)

vocab_to_int = {c:i for i,c in enumerate(vocab)}
int_to_vocab = {i:c for i,c in enumerate(vocab)}
print(int_to_vocab.get(vocab_to_int.get('||Period||')))

encoded = [vocab_to_int.get(i) for i in word_corpus]
print(encoded[1:10])
print(len(encoded))


1260
||Period||
[1161, 573, 266, 447, 400, 0, 1161, 0, 1161]
5752

In [24]:
steps = 50

In [25]:
X = []
y = []

for i in range(0, len(encoded) - steps, 1):
    X.append(encoded[i : i + steps])
    y.append(encoded[i + steps])

X = np.reshape(X, (len(X), steps))
X = X/float(num_classes)

X_train = X
y_train = np.eye(num_classes)[y]

print(X_train.shape)
print(y_train.shape)


(5702, 50)
(5702, 1260)

In [26]:
# Counter(y)
# print(X[1,:,:])

In [35]:
# Hyperparams
# samples = 1300
dropout = 1
epochs = 100
batch_size = 128
embed_dim = 512

In [36]:
model = Sequential()
model.add(Embedding(input_dim=num_classes, output_dim=embed_dim, input_length=steps))
model.add(LSTM(512, return_sequences = True))
model.add(Dropout(dropout))
model.add(Flatten())
# adam = optimizers.Adam(lr=0.1)
model.add(Dense(64, activation='relu'))
model.add(Dense(num_classes, activation='softmax'))
model.compile(loss="categorical_crossentropy", optimizer='adam')

In [37]:
filepath="weights-improvement-{epoch:02d}-{loss:.4f}.hdf5"
checkpoint = ModelCheckpoint(filepath, monitor='loss', verbose=0, save_best_only=True, mode='min')
callbacks_list = [checkpoint]

In [38]:
# model.fit(X_train, y_train, batch_size = batch_size, epochs=epochs, callbacks=callbacks_list)
model.fit(X_train, y_train, batch_size = batch_size, epochs=epochs)
# 5.58


Epoch 1/100
5702/5702 [==============================] - 86s - loss: 5.8564      
Epoch 2/100
5702/5702 [==============================] - 81s - loss: 5.2923     
Epoch 3/100
5702/5702 [==============================] - 81s - loss: 5.2774     
Epoch 4/100
5702/5702 [==============================] - 79s - loss: 5.2582     
Epoch 5/100
5702/5702 [==============================] - 79s - loss: 5.2545     
Epoch 6/100
5702/5702 [==============================] - 79s - loss: 5.2425     
Epoch 7/100
5702/5702 [==============================] - 81s - loss: 5.2454     
Epoch 8/100
5702/5702 [==============================] - 81s - loss: 5.2452     
Epoch 9/100
5702/5702 [==============================] - 81s - loss: 5.2583     
Epoch 10/100
5702/5702 [==============================] - 81s - loss: 5.2408     
Epoch 11/100
5702/5702 [==============================] - 81s - loss: 5.2384     
Epoch 12/100
5702/5702 [==============================] - 81s - loss: 5.2422     
Epoch 13/100
5702/5702 [==============================] - 81s - loss: 5.2365     
Epoch 14/100
5702/5702 [==============================] - 80s - loss: 5.2406     
Epoch 15/100
5702/5702 [==============================] - 80s - loss: 5.2334     
Epoch 16/100
5702/5702 [==============================] - 82s - loss: 5.2398     
Epoch 17/100
5702/5702 [==============================] - 80s - loss: 5.2413     
Epoch 18/100
5702/5702 [==============================] - 80s - loss: 5.2325     
Epoch 19/100
5702/5702 [==============================] - 80s - loss: 5.2295     
Epoch 20/100
5702/5702 [==============================] - 81s - loss: 5.2352     
Epoch 21/100
5702/5702 [==============================] - 82s - loss: 5.2299     
Epoch 22/100
5702/5702 [==============================] - 82s - loss: 5.2311     
Epoch 23/100
5702/5702 [==============================] - 80s - loss: 5.2340     
Epoch 24/100
5702/5702 [==============================] - 81s - loss: 5.2318     
Epoch 25/100
5702/5702 [==============================] - 81s - loss: 5.2344     
Epoch 26/100
5702/5702 [==============================] - 81s - loss: 5.2285     
Epoch 27/100
5702/5702 [==============================] - 81s - loss: 5.2233     
Epoch 28/100
5702/5702 [==============================] - 81s - loss: 5.2289     
Epoch 29/100
5702/5702 [==============================] - 81s - loss: 5.2274     
Epoch 30/100
5702/5702 [==============================] - 81s - loss: 5.2253     
Epoch 31/100
5702/5702 [==============================] - 80s - loss: 5.2239     
Epoch 32/100
5702/5702 [==============================] - 81s - loss: 5.2258     
Epoch 33/100
5702/5702 [==============================] - 81s - loss: 5.2249     
Epoch 34/100
5702/5702 [==============================] - 83s - loss: 5.2255     
Epoch 35/100
5702/5702 [==============================] - 81s - loss: 5.2294     
Epoch 36/100
5702/5702 [==============================] - 81s - loss: 5.2290     
Epoch 37/100
5702/5702 [==============================] - 80s - loss: 5.2244     
Epoch 38/100
5702/5702 [==============================] - 81s - loss: 5.2285     
Epoch 39/100
5702/5702 [==============================] - 81s - loss: 5.2237     
Epoch 40/100
5702/5702 [==============================] - 81s - loss: 5.2259     
Epoch 41/100
5702/5702 [==============================] - 81s - loss: 5.2253     
Epoch 42/100
5702/5702 [==============================] - 80s - loss: 5.2229     
Epoch 43/100
5702/5702 [==============================] - 81s - loss: 5.2261     
Epoch 44/100
5702/5702 [==============================] - 80s - loss: 5.2224     
Epoch 45/100
5702/5702 [==============================] - 81s - loss: 5.2213     
Epoch 46/100
5702/5702 [==============================] - 81s - loss: 5.2220     
Epoch 47/100
5702/5702 [==============================] - 82s - loss: 5.2216     
Epoch 48/100
5702/5702 [==============================] - 81s - loss: 5.2176     
Epoch 49/100
5702/5702 [==============================] - 81s - loss: 5.2242     
Epoch 50/100
5702/5702 [==============================] - 80s - loss: 5.2191     
Epoch 51/100
5702/5702 [==============================] - 81s - loss: 5.2211     
Epoch 52/100
5702/5702 [==============================] - 82s - loss: 5.2203     
Epoch 53/100
5702/5702 [==============================] - 81s - loss: 5.2200     
Epoch 54/100
5702/5702 [==============================] - 81s - loss: 5.2194     
Epoch 55/100
5702/5702 [==============================] - 81s - loss: 5.2167     
Epoch 56/100
5702/5702 [==============================] - 82s - loss: 5.2191     
Epoch 57/100
5702/5702 [==============================] - 81s - loss: 5.2218     
Epoch 58/100
5702/5702 [==============================] - 81s - loss: 5.2177     
Epoch 59/100
5702/5702 [==============================] - 80s - loss: 5.2199     
Epoch 60/100
5702/5702 [==============================] - 82s - loss: 5.2153     
Epoch 61/100
5702/5702 [==============================] - 82s - loss: 5.2193     
Epoch 62/100
5702/5702 [==============================] - 80s - loss: 5.2210     
Epoch 63/100
5702/5702 [==============================] - 81s - loss: 5.2195     
Epoch 64/100
5702/5702 [==============================] - 81s - loss: 5.2192     
Epoch 65/100
5702/5702 [==============================] - 82s - loss: 5.2178     
Epoch 66/100
5702/5702 [==============================] - 81s - loss: 5.2189     
Epoch 67/100
5702/5702 [==============================] - 81s - loss: 5.2170     
Epoch 68/100
5702/5702 [==============================] - 80s - loss: 5.2151     
Epoch 69/100
5702/5702 [==============================] - 83s - loss: 5.2154     
Epoch 70/100
5702/5702 [==============================] - 81s - loss: 5.2145     
Epoch 71/100
5702/5702 [==============================] - 81s - loss: 5.2164     
Epoch 72/100
5702/5702 [==============================] - 81s - loss: 5.2150     
Epoch 73/100
5702/5702 [==============================] - 81s - loss: 5.2156     
Epoch 74/100
5702/5702 [==============================] - 81s - loss: 5.2175     
Epoch 75/100
5702/5702 [==============================] - 81s - loss: 5.2141     
Epoch 76/100
5702/5702 [==============================] - 80s - loss: 5.2154     
Epoch 77/100
5702/5702 [==============================] - 80s - loss: 5.2177     
Epoch 78/100
5702/5702 [==============================] - 82s - loss: 5.2193     
Epoch 79/100
5702/5702 [==============================] - 80s - loss: 5.2144     
Epoch 80/100
5702/5702 [==============================] - 81s - loss: 5.2140     
Epoch 81/100
5702/5702 [==============================] - 82s - loss: 5.2149     
Epoch 82/100
5702/5702 [==============================] - 83s - loss: 5.2134     
Epoch 83/100
5702/5702 [==============================] - 81s - loss: 5.2169     
Epoch 84/100
5702/5702 [==============================] - 81s - loss: 5.2150     
Epoch 85/100
5702/5702 [==============================] - 81s - loss: 5.2180     
Epoch 86/100
5702/5702 [==============================] - 81s - loss: 5.2132     
Epoch 87/100
4864/5702 [========================>.....] - ETA: 12s - loss: 5.2081
---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
<ipython-input-38-be3088df8a08> in <module>()
      1 # model.fit(X_train, y_train, batch_size = batch_size, epochs=epochs, callbacks=callbacks_list)
----> 2 model.fit(X_train, y_train, batch_size = batch_size, epochs=epochs)
      3 # 5.58

C:\Users\abjilani\AppData\Local\Continuum\Anaconda3\envs\dlnd-tf-lab\lib\site-packages\keras\models.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, **kwargs)
    854                               class_weight=class_weight,
    855                               sample_weight=sample_weight,
--> 856                               initial_epoch=initial_epoch)
    857 
    858     def evaluate(self, x, y, batch_size=32, verbose=1,

C:\Users\abjilani\AppData\Local\Continuum\Anaconda3\envs\dlnd-tf-lab\lib\site-packages\keras\engine\training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, **kwargs)
   1496                               val_f=val_f, val_ins=val_ins, shuffle=shuffle,
   1497                               callback_metrics=callback_metrics,
-> 1498                               initial_epoch=initial_epoch)
   1499 
   1500     def evaluate(self, x, y, batch_size=32, verbose=1, sample_weight=None):

C:\Users\abjilani\AppData\Local\Continuum\Anaconda3\envs\dlnd-tf-lab\lib\site-packages\keras\engine\training.py in _fit_loop(self, f, ins, out_labels, batch_size, epochs, verbose, callbacks, val_f, val_ins, shuffle, callback_metrics, initial_epoch)
   1150                 batch_logs['size'] = len(batch_ids)
   1151                 callbacks.on_batch_begin(batch_index, batch_logs)
-> 1152                 outs = f(ins_batch)
   1153                 if not isinstance(outs, list):
   1154                     outs = [outs]

C:\Users\abjilani\AppData\Local\Continuum\Anaconda3\envs\dlnd-tf-lab\lib\site-packages\keras\backend\tensorflow_backend.py in __call__(self, inputs)
   2227         session = get_session()
   2228         updated = session.run(self.outputs + [self.updates_op],
-> 2229                               feed_dict=feed_dict)
   2230         return updated[:len(self.outputs)]
   2231 

C:\Users\abjilani\AppData\Local\Continuum\Anaconda3\envs\dlnd-tf-lab\lib\site-packages\tensorflow\python\client\session.py in run(self, fetches, feed_dict, options, run_metadata)
    765     try:
    766       result = self._run(None, fetches, feed_dict, options_ptr,
--> 767                          run_metadata_ptr)
    768       if run_metadata:
    769         proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)

C:\Users\abjilani\AppData\Local\Continuum\Anaconda3\envs\dlnd-tf-lab\lib\site-packages\tensorflow\python\client\session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
    963     if final_fetches or final_targets:
    964       results = self._do_run(handle, final_targets, final_fetches,
--> 965                              feed_dict_string, options, run_metadata)
    966     else:
    967       results = []

C:\Users\abjilani\AppData\Local\Continuum\Anaconda3\envs\dlnd-tf-lab\lib\site-packages\tensorflow\python\client\session.py in _do_run(self, handle, target_list, fetch_list, feed_dict, options, run_metadata)
   1013     if handle is None:
   1014       return self._do_call(_run_fn, self._session, feed_dict, fetch_list,
-> 1015                            target_list, options, run_metadata)
   1016     else:
   1017       return self._do_call(_prun_fn, self._session, handle, feed_dict,

C:\Users\abjilani\AppData\Local\Continuum\Anaconda3\envs\dlnd-tf-lab\lib\site-packages\tensorflow\python\client\session.py in _do_call(self, fn, *args)
   1020   def _do_call(self, fn, *args):
   1021     try:
-> 1022       return fn(*args)
   1023     except errors.OpError as e:
   1024       message = compat.as_text(e.message)

C:\Users\abjilani\AppData\Local\Continuum\Anaconda3\envs\dlnd-tf-lab\lib\site-packages\tensorflow\python\client\session.py in _run_fn(session, feed_dict, fetch_list, target_list, options, run_metadata)
   1002         return tf_session.TF_Run(session, options,
   1003                                  feed_dict, fetch_list, target_list,
-> 1004                                  status, run_metadata)
   1005 
   1006     def _prun_fn(session, handle, feed_dict, fetch_list):

KeyboardInterrupt: 

In [58]:
y_pred = model.predict(np.reshape(X_train[102,:],(1,steps)))
print(y_pred)
y_transformed = np.argmax(y_pred)
print(y_transformed)
print(int_to_vocab[y_transformed])


[[  1.34227455e-01   1.44253527e-05   1.20604573e-05 ...,   1.02025828e-04
    1.26223258e-05   1.27296735e-05]]
0


In [59]:
seed = np.random.randint(len(encoded)-steps)
x_seed = corpus[seed:seed + steps]
x_seed = [vocab_to_int[char] for char in x_seed]
x_seed = np.reshape(x_seed, (1, steps, 1))
x_seed = x_seed/float(num_classes)
print(x_seed)


---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-59-a2e935af8c26> in <module>()
      1 seed = np.random.randint(len(encoded)-steps)
      2 x_seed = corpus[seed:seed + steps]
----> 3 x_seed = [vocab_to_int[char] for char in x_seed]
      4 x_seed = np.reshape(x_seed, (1, steps, 1))
      5 x_seed = x_seed/float(num_classes)

<ipython-input-59-a2e935af8c26> in <listcomp>(.0)
      1 seed = np.random.randint(len(encoded)-steps)
      2 x_seed = corpus[seed:seed + steps]
----> 3 x_seed = [vocab_to_int[char] for char in x_seed]
      4 x_seed = np.reshape(x_seed, (1, steps, 1))
      5 x_seed = x_seed/float(num_classes)

KeyError: ' '

In [60]:
x = x_seed
out = []
charsize = 500

for i in range(charsize):
    y_pred = model.predict(x)
    y_transformed = np.argmax(y_pred)
    output = int_to_vocab[y_transformed]
    out.append(output)
    x_new = y_transformed/float(num_classes)
    x = np.append(x[:,1:100,:], np.reshape(x_new, (1,1,1)), axis = 1)

print('completed')


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-60-1a59bd882619> in <module>()
      4 
      5 for i in range(charsize):
----> 6     y_pred = model.predict(x)
      7     y_transformed = np.argmax(y_pred)
      8     output = int_to_vocab[y_transformed]

C:\Anaconda\envs\py35\lib\site-packages\keras\models.py in predict(self, x, batch_size, verbose)
    900         if self.model is None:
    901             self.build()
--> 902         return self.model.predict(x, batch_size=batch_size, verbose=verbose)
    903 
    904     def predict_on_batch(self, x):

C:\Anaconda\envs\py35\lib\site-packages\keras\engine\training.py in predict(self, x, batch_size, verbose)
   1565         x = _standardize_input_data(x, self._feed_input_names,
   1566                                     self._feed_input_shapes,
-> 1567                                     check_batch_axis=False)
   1568         if self.stateful:
   1569             if x[0].shape[0] > batch_size and x[0].shape[0] % batch_size != 0:

C:\Anaconda\envs\py35\lib\site-packages\keras\engine\training.py in _standardize_input_data(data, names, shapes, check_batch_axis, exception_prefix)
     97                             ': data should be a Numpy array, '
     98                             'or list/dict of Numpy arrays. '
---> 99                             'Found: ' + str(data)[:200] + '...')
    100         if len(names) > 1:
    101             # Case: model expects multiple inputs but only received

TypeError: Error when checking model : data should be a Numpy array, or list/dict of Numpy arrays. Found: e lost no opportunity of procuring those of the mo...

In [14]:
print(len(out))
print(''.join(out))


500
ot tead the sout and the soutd and the soutd and the soutd and the soutd and the soutd and the soutd and the soutd and the soutd and the soutd and the soutd and the soutd and the soutd and the soutd and the soutd and the soutd and the soutd and the soutd and the soutd and the soutd and the soutd and the soutd and the soutd and the soutd and the soutd and the soutd and the soutd and the soutd and the soutd and the soutd and the soutd and the soutd and the soutd and the soutd and the soutd and the