upload rhyme_generator.py and its notebook

This commit is contained in:
tfavory
2018-10-04 22:27:25 +08:00
committed by GitHub
parent 83e44529d5
commit 36656caae3
2 changed files with 500 additions and 0 deletions
+395
View File
@@ -0,0 +1,395 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Using TensorFlow backend.\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"corpus length: 203492\n"
]
}
],
"source": [
"from __future__ import print_function\n",
"from keras.callbacks import LambdaCallback\n",
"from keras.models import Sequential\n",
"from keras.layers import Dense\n",
"from keras.layers import LSTM\n",
"from keras.optimizers import RMSprop\n",
"from keras.utils.data_utils import get_file\n",
"import numpy as np\n",
"import random\n",
"import sys\n",
"import io"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"corpus length: 34517\n",
"total chars: 6960\n"
]
}
],
"source": [
"path = 'rhymes.txt'\n",
"with open(path, encoding='utf-8') as f:\n",
" text = f.read().lower()\n",
"\n",
"text = text.split()\n",
"print('corpus length:', len(text))\n",
"\n",
"chars = sorted(list(set(text)))\n",
"print('total chars:', len(chars))"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"nb sequences: 34477\n",
"Vectorization...\n",
"Build model...\n",
"Epoch 1/20\n",
"34477/34477 [==============================] - 221s 6ms/step - loss: 7.9745\n",
"\n",
"----- Generating text after Epoch: 0\n",
"----- diversity: 0.2\n",
"----- Generating with seed: \"['oystermouth', 'way', 'bay', 'alone', 'stone', 'tolled', 'when', 'cry', 'men', 'he', 'sea', 'said', 'head', 'roar', 'oar', 'sons', 'guns', 'love', 'above', 'bed', 'head', 'blew', 'crew', 'afloat', 'boat', 'high', 'caves', 'waves', 'storm', 'form', 'breath', 'death', 'lips', 'ships', 'and', 'lighthouse', 'shore', 'hand', 'land', 'save']\"\n",
"['be', ['oystermouth', 'way', 'bay', 'alone', 'stone', 'tolled', 'when', 'cry', 'men', 'he', 'sea', 'said', 'head', 'roar', 'oar', 'sons', 'guns', 'love', 'above', 'bed', 'head', 'blew', 'crew', 'afloat', 'boat', 'high', 'caves', 'waves', 'storm', 'form', 'breath', 'death', 'lips', 'ships', 'and', 'lighthouse', 'shore', 'hand', 'land', 'save']]['day']['day']['away']['away']['day']['away']['day']['day']['there']['day']['day']['away']['day']['away']['day']['away']['day']['day']['away']['day']['day']['day']['way']['day']['day']['away']['night']['away']['day']['away']['day']['away']['day']['away']['there']['day']['away']['light']['sky']['away']\n",
"----- diversity: 0.5\n",
"----- Generating with seed: \"['oystermouth', 'way', 'bay', 'alone', 'stone', 'tolled', 'when', 'cry', 'men', 'he', 'sea', 'said', 'head', 'roar', 'oar', 'sons', 'guns', 'love', 'above', 'bed', 'head', 'blew', 'crew', 'afloat', 'boat', 'high', 'caves', 'waves', 'storm', 'form', 'breath', 'death', 'lips', 'ships', 'and', 'lighthouse', 'shore', 'hand', 'land', 'save']\"\n",
"['be', ['oystermouth', 'way', 'bay', 'alone', 'stone', 'tolled', 'when', 'cry', 'men', 'he', 'sea', 'said', 'head', 'roar', 'oar', 'sons', 'guns', 'love', 'above', 'bed', 'head', 'blew', 'crew', 'afloat', 'boat', 'high', 'caves', 'waves', 'storm', 'form', 'breath', 'death', 'lips', 'ships', 'and', 'lighthouse', 'shore', 'hand', 'land', 'save']]['away']['made']['out']['light']['her']['fall']['there']['blow']['still']['hand']['morn']['way']['again']['be']['again']['heaven']['place']['strong']['ring']['world']['be']['it']['it']['it']['it']['it']['it']['it']['it']['it']['it']['it']['it']['it']['it']['it']['it']['it']['it']['it']\n",
"----- diversity: 1.0\n",
"----- Generating with seed: \"['oystermouth', 'way', 'bay', 'alone', 'stone', 'tolled', 'when', 'cry', 'men', 'he', 'sea', 'said', 'head', 'roar', 'oar', 'sons', 'guns', 'love', 'above', 'bed', 'head', 'blew', 'crew', 'afloat', 'boat', 'high', 'caves', 'waves', 'storm', 'form', 'breath', 'death', 'lips', 'ships', 'and', 'lighthouse', 'shore', 'hand', 'land', 'save']\"\n",
"['be', ['oystermouth', 'way', 'bay', 'alone', 'stone', 'tolled', 'when', 'cry', 'men', 'he', 'sea', 'said', 'head', 'roar', 'oar', 'sons', 'guns', 'love', 'above', 'bed', 'head', 'blew', 'crew', 'afloat', 'boat', 'high', 'caves', 'waves', 'storm', 'form', 'breath', 'death', 'lips', 'ships', 'and', 'lighthouse', 'shore', 'hand', 'land', 'save']]['meets']['tree']['made']['sorrow']['mine']['dwell']['amount']['morrow']['way']['shame']['children']['scorn']['play']['sinfulness']['eye']['wood']['intent']['wing']['prayer']['will']['light']['god']['struck']['close']['true']['road']['things']['flare']['sight']['war']['pain']['rough']['home']['out']['close']['green']['head']['curses']['spirit']['nerves']\n",
"----- diversity: 1.2\n",
"----- Generating with seed: \"['oystermouth', 'way', 'bay', 'alone', 'stone', 'tolled', 'when', 'cry', 'men', 'he', 'sea', 'said', 'head', 'roar', 'oar', 'sons', 'guns', 'love', 'above', 'bed', 'head', 'blew', 'crew', 'afloat', 'boat', 'high', 'caves', 'waves', 'storm', 'form', 'breath', 'death', 'lips', 'ships', 'and', 'lighthouse', 'shore', 'hand', 'land', 'save']\"\n",
"['be', ['oystermouth', 'way', 'bay', 'alone', 'stone', 'tolled', 'when', 'cry', 'men', 'he', 'sea', 'said', 'head', 'roar', 'oar', 'sons', 'guns', 'love', 'above', 'bed', 'head', 'blew', 'crew', 'afloat', 'boat', 'high', 'caves', 'waves', 'storm', 'form', 'breath', 'death', 'lips', 'ships', 'and', 'lighthouse', 'shore', 'hand', 'land', 'save']]['shore']['take']['strife']['boom']['hum']['starlight']['talk']['win']['washington']['you']['bound']['war']['bride']['will']['shook']['snarls']['threshold']['sweetness']['thee']['cured']['fail']['trundle-bed']['sheep']['afternoon']['sly']['pine-tree']['we']['reveal']['smirk']['miles']['france']['care']['so']['sweet']['rendezvous']['understood']['demurred—']['morn']['flower-like']['with']\n",
"Epoch 2/20\n",
"34477/34477 [==============================] - 218s 6ms/step - loss: 7.4730\n",
"Epoch 3/20\n",
"34477/34477 [==============================] - 219s 6ms/step - loss: 6.9996\n",
"Epoch 4/20\n",
"34477/34477 [==============================] - 218s 6ms/step - loss: 6.7760\n",
"Epoch 5/20\n",
"34477/34477 [==============================] - 219s 6ms/step - loss: 6.6035\n",
"Epoch 6/20\n",
"34477/34477 [==============================] - 221s 6ms/step - loss: 6.3921\n",
"\n",
"----- Generating text after Epoch: 5\n",
"----- diversity: 0.2\n",
"----- Generating with seed: \"['strife', 'life', 'urge', 'surge', 'stride', 'beside', 'passed', 'on', 'fawn', 'strained', 'trained', 'cost', 'lost', 'strength', 'length', 'mine--no', 'blow', 'gun', 'one', 'lump', 'rump', 'bolder', 'shoulder', 'steed', 'heed', 'calls', 'falls', 'storm', 'form', 'hand', 'stand', 'done', 'won', 'say', 'men', 'again', 'surf', 'turf', 'years', 'peers']\"\n",
"['be', ['strife', 'life', 'urge', 'surge', 'stride', 'beside', 'passed', 'on', 'fawn', 'strained', 'trained', 'cost', 'lost', 'strength', 'length', 'mine--no', 'blow', 'gun', 'one', 'lump', 'rump', 'bolder', 'shoulder', 'steed', 'heed', 'calls', 'falls', 'storm', 'form', 'hand', 'stand', 'done', 'won', 'say', 'men', 'again', 'surf', 'turf', 'years', 'peers']]['there']['care']['there']['care']['there']['air']['there']['care']['pain']['again']['rest']['breast']['breast']['rest']['rest']['breast']['breast']['rest']['cold']['gold']['cold']['gold']['cold']['gold']['old']['gold']['cold']['gold']['be']['me']['me']['me']['soul']['soul']['roll']['soul']['soul']['roll']['soul']['soul']\n",
"----- diversity: 0.5\n",
"----- Generating with seed: \"['strife', 'life', 'urge', 'surge', 'stride', 'beside', 'passed', 'on', 'fawn', 'strained', 'trained', 'cost', 'lost', 'strength', 'length', 'mine--no', 'blow', 'gun', 'one', 'lump', 'rump', 'bolder', 'shoulder', 'steed', 'heed', 'calls', 'falls', 'storm', 'form', 'hand', 'stand', 'done', 'won', 'say', 'men', 'again', 'surf', 'turf', 'years', 'peers']\"\n",
"['be', ['strife', 'life', 'urge', 'surge', 'stride', 'beside', 'passed', 'on', 'fawn', 'strained', 'trained', 'cost', 'lost', 'strength', 'length', 'mine--no', 'blow', 'gun', 'one', 'lump', 'rump', 'bolder', 'shoulder', 'steed', 'heed', 'calls', 'falls', 'storm', 'form', 'hand', 'stand', 'done', 'won', 'say', 'men', 'again', 'surf', 'turf', 'years', 'peers']]['place']['be']['me']['eye']['me']['be']['sea']['me']['me']['be']['me']['sea']['me']['be']['me']['be']['me']['sea']['me']['soul']['be']['me']['sea']['me']['me']['be']['me']['sea']['me']['sea']['be']['me']['sea']['be']['me']['sea']['be']['me']['thee']['sea']\n",
"----- diversity: 1.0\n",
"----- Generating with seed: \"['strife', 'life', 'urge', 'surge', 'stride', 'beside', 'passed', 'on', 'fawn', 'strained', 'trained', 'cost', 'lost', 'strength', 'length', 'mine--no', 'blow', 'gun', 'one', 'lump', 'rump', 'bolder', 'shoulder', 'steed', 'heed', 'calls', 'falls', 'storm', 'form', 'hand', 'stand', 'done', 'won', 'say', 'men', 'again', 'surf', 'turf', 'years', 'peers']\"\n",
"['be', ['strife', 'life', 'urge', 'surge', 'stride', 'beside', 'passed', 'on', 'fawn', 'strained', 'trained', 'cost', 'lost', 'strength', 'length', 'mine--no', 'blow', 'gun', 'one', 'lump', 'rump', 'bolder', 'shoulder', 'steed', 'heed', 'calls', 'falls', 'storm', 'form', 'hand', 'stand', 'done', 'won', 'say', 'men', 'again', 'surf', 'turf', 'years', 'peers']]['time']['roof']['sea']['high']['stone']['death']['shed']['winter']['word']['bread']['tell']['well']['fell']['fine']['arm']['corn']['be']['be']['child']['sea']['fright']['town']['walls']['ground']['milk']['divine']['tender']['shame']['years']['dawn']['dawn']['flow']['stands']['king']['sublime']['sun']['me']['keep']['plant']['die']\n",
"----- diversity: 1.2\n",
"----- Generating with seed: \"['strife', 'life', 'urge', 'surge', 'stride', 'beside', 'passed', 'on', 'fawn', 'strained', 'trained', 'cost', 'lost', 'strength', 'length', 'mine--no', 'blow', 'gun', 'one', 'lump', 'rump', 'bolder', 'shoulder', 'steed', 'heed', 'calls', 'falls', 'storm', 'form', 'hand', 'stand', 'done', 'won', 'say', 'men', 'again', 'surf', 'turf', 'years', 'peers']\"\n",
"['be', ['strife', 'life', 'urge', 'surge', 'stride', 'beside', 'passed', 'on', 'fawn', 'strained', 'trained', 'cost', 'lost', 'strength', 'length', 'mine--no', 'blow', 'gun', 'one', 'lump', 'rump', 'bolder', 'shoulder', 'steed', 'heed', 'calls', 'falls', 'storm', 'form', 'hand', 'stand', 'done', 'won', 'say', 'men', 'again', 'surf', 'turf', 'years', 'peers']]['rise']['glad']['revealed']['cheek']['each']['view']['state']['tried']['he']['now']['days']['hill']['gold']['go']['follow']['heaven']['night']['height']['light']['door']['more']['style']['heart']['her']['been']['slave']['do']['years']['end']['life']['been']['her']['she']['part']['me']['speak']['floor']['go']['alone']['away']\n",
"Epoch 7/20\n",
"34477/34477 [==============================] - 228s 7ms/step - loss: 6.1825\n",
"Epoch 8/20\n",
"34477/34477 [==============================] - 229s 7ms/step - loss: 5.9736\n",
"Epoch 9/20\n",
"34477/34477 [==============================] - 220s 6ms/step - loss: 5.7911\n",
"Epoch 10/20\n",
"34477/34477 [==============================] - 220s 6ms/step - loss: 5.6130\n",
"Epoch 11/20\n",
"34477/34477 [==============================] - 219s 6ms/step - loss: 5.4404\n",
"\n",
"----- Generating text after Epoch: 10\n",
"----- diversity: 0.2\n",
"----- Generating with seed: \"['place', 'undertone', 'drew', 'adieu', 'behest', 'stayed', 'land', 'hand', 'cloak', 'spoke', 'will', \"howe'er\", 'peer', 'alone', 'foundation-stone', 'own', 'grasp', 'clasp', 'fire', 'ire', 'said', 'spared', 'head', 'peer', 'here', 'pride', 'near', 'sword', 'defied', 'peer', 'here', 'near', 'lied', 'rage', 'age', 'then', 'den', 'hall', 'go', 'no']\"\n",
"['be', ['place', 'undertone', 'drew', 'adieu', 'behest', 'stayed', 'land', 'hand', 'cloak', 'spoke', 'will', \"howe'er\", 'peer', 'alone', 'foundation-stone', 'own', 'grasp', 'clasp', 'fire', 'ire', 'said', 'spared', 'head', 'peer', 'here', 'pride', 'near', 'sword', 'defied', 'peer', 'here', 'near', 'lied', 'rage', 'age', 'then', 'den', 'hall', 'go', 'no']]['died']['all']['eyes']['dead']['said']['head']['dead']['said']['head']['dead']['said']['head']['out']['about']['out']['about']['out']['about']['out']['about']['out']['about']['out']['about']['out']['about']['out']['about']['out']['about']['out']['about']['out']['about']['out']['about']['it']['again']['deep']['sleep']\n",
"----- diversity: 0.5\n",
"----- Generating with seed: \"['place', 'undertone', 'drew', 'adieu', 'behest', 'stayed', 'land', 'hand', 'cloak', 'spoke', 'will', \"howe'er\", 'peer', 'alone', 'foundation-stone', 'own', 'grasp', 'clasp', 'fire', 'ire', 'said', 'spared', 'head', 'peer', 'here', 'pride', 'near', 'sword', 'defied', 'peer', 'here', 'near', 'lied', 'rage', 'age', 'then', 'den', 'hall', 'go', 'no']\"\n",
"['be', ['place', 'undertone', 'drew', 'adieu', 'behest', 'stayed', 'land', 'hand', 'cloak', 'spoke', 'will', \"howe'er\", 'peer', 'alone', 'foundation-stone', 'own', 'grasp', 'clasp', 'fire', 'ire', 'said', 'spared', 'head', 'peer', 'here', 'pride', 'near', 'sword', 'defied', 'peer', 'here', 'near', 'lied', 'rage', 'age', 'then', 'den', 'hall', 'go', 'no']]['yet']['all']['dead']['hand']['stand']['hand']['land']['day']['lay']['eyes']['rise']['skies']['fled']['eyes']['shore']['name']['dead']['said']['shame']['came']['name']['shame']['name']['us']['me']['gay']['bright']['sky']['air']['there']['care']['prayer']['star']['me']['me']['be']['made']['shore']['more']['on']\n",
"----- diversity: 1.0\n",
"----- Generating with seed: \"['place', 'undertone', 'drew', 'adieu', 'behest', 'stayed', 'land', 'hand', 'cloak', 'spoke', 'will', \"howe'er\", 'peer', 'alone', 'foundation-stone', 'own', 'grasp', 'clasp', 'fire', 'ire', 'said', 'spared', 'head', 'peer', 'here', 'pride', 'near', 'sword', 'defied', 'peer', 'here', 'near', 'lied', 'rage', 'age', 'then', 'den', 'hall', 'go', 'no']\"\n",
"['be', ['place', 'undertone', 'drew', 'adieu', 'behest', 'stayed', 'land', 'hand', 'cloak', 'spoke', 'will', \"howe'er\", 'peer', 'alone', 'foundation-stone', 'own', 'grasp', 'clasp', 'fire', 'ire', 'said', 'spared', 'head', 'peer', 'here', 'pride', 'near', 'sword', 'defied', 'peer', 'here', 'near', 'lied', 'rage', 'age', 'then', 'den', 'hall', 'go', 'no']]['mother']['eyes']['way']['side']['beside']['king']['mind']['sight']['john']['sorrow']['with']['corn']['fame']['know']['relief']['gaze']['dress']['save']['went']['wall']['ended']['array']['dim']['success']['boy']['chin']['play']['see']['be']['faster']['breast']['rocked']['on']['true']['do']['trod']['me']['me']['earth']['arose']\n",
"----- diversity: 1.2\n",
"----- Generating with seed: \"['place', 'undertone', 'drew', 'adieu', 'behest', 'stayed', 'land', 'hand', 'cloak', 'spoke', 'will', \"howe'er\", 'peer', 'alone', 'foundation-stone', 'own', 'grasp', 'clasp', 'fire', 'ire', 'said', 'spared', 'head', 'peer', 'here', 'pride', 'near', 'sword', 'defied', 'peer', 'here', 'near', 'lied', 'rage', 'age', 'then', 'den', 'hall', 'go', 'no']\"\n",
"['be', ['place', 'undertone', 'drew', 'adieu', 'behest', 'stayed', 'land', 'hand', 'cloak', 'spoke', 'will', \"howe'er\", 'peer', 'alone', 'foundation-stone', 'own', 'grasp', 'clasp', 'fire', 'ire', 'said', 'spared', 'head', 'peer', 'here', 'pride', 'near', 'sword', 'defied', 'peer', 'here', 'near', 'lied', 'rage', 'age', 'then', 'den', 'hall', 'go', 'no']]['dream']['delight']['strong']['spell']['men']['eye']['slowly']['spot']['my']['find']['curls']['top']['enderby']['skies']['sea']['eyes']['lies']['play']['see']['smile']['must']['roam']['flee']['east']['mouse']['press']['sight']['ought']['school']['beast']['sextus']['mine']['power']['turn']['the']['of']['near']['ashore']['hands']['view']\n",
"Epoch 12/20\n",
"34477/34477 [==============================] - 220s 6ms/step - loss: 5.2854\n",
"Epoch 13/20\n",
"34477/34477 [==============================] - 221s 6ms/step - loss: 5.1560\n",
"Epoch 14/20\n",
"34477/34477 [==============================] - 224s 6ms/step - loss: 5.0554\n",
"Epoch 15/20\n",
"34477/34477 [==============================] - 231s 7ms/step - loss: 4.9645\n",
"Epoch 16/20\n",
"34477/34477 [==============================] - 235s 7ms/step - loss: 4.8694\n",
"\n",
"----- Generating text after Epoch: 15\n",
"----- diversity: 0.2\n",
"----- Generating with seed: \"['eyes', 'fell', 'own', 'blown', 'heart', 'in', 'part', 'sigh', 'why', 'tell', 'sell', 'away', 'day', 'fair', 'hair', 'white', 'to-night', 'cold', 'nigh', 'white', 'to-night', 'dart', 'hour', 'right', 'to-night', 'brow', 'vow', 'sigh', 'bright', 'to-night', 'door', 'before', 'aglow', 'fro', 'light', 'to-night', 'bell', 'hell', 'now', 'brow']\"\n",
"['be', ['eyes', 'fell', 'own', 'blown', 'heart', 'in', 'part', 'sigh', 'why', 'tell', 'sell', 'away', 'day', 'fair', 'hair', 'white', 'to-night', 'cold', 'nigh', 'white', 'to-night', 'dart', 'hour', 'right', 'to-night', 'brow', 'vow', 'sigh', 'bright', 'to-night', 'door', 'before', 'aglow', 'fro', 'light', 'to-night', 'bell', 'hell', 'now', 'brow']]['light']['to-night']['rest']['breast']['door']['floor']['more']['door']['shore']['more']['shore']['yore']['shore']['still']['shore']['more']['shore']['bay']['lay']['away']['bay']['town']['down']['town']['done']['down']['on']['all']['more']['shore']['bay']['it']['it']['to']['too']['you']['true']['too']['you']['blue']\n",
"----- diversity: 0.5\n",
"----- Generating with seed: \"['eyes', 'fell', 'own', 'blown', 'heart', 'in', 'part', 'sigh', 'why', 'tell', 'sell', 'away', 'day', 'fair', 'hair', 'white', 'to-night', 'cold', 'nigh', 'white', 'to-night', 'dart', 'hour', 'right', 'to-night', 'brow', 'vow', 'sigh', 'bright', 'to-night', 'door', 'before', 'aglow', 'fro', 'light', 'to-night', 'bell', 'hell', 'now', 'brow']\"\n",
"['be', ['eyes', 'fell', 'own', 'blown', 'heart', 'in', 'part', 'sigh', 'why', 'tell', 'sell', 'away', 'day', 'fair', 'hair', 'white', 'to-night', 'cold', 'nigh', 'white', 'to-night', 'dart', 'hour', 'right', 'to-night', 'brow', 'vow', 'sigh', 'bright', 'to-night', 'door', 'before', 'aglow', 'fro', 'light', 'to-night', 'bell', 'hell', 'now', 'brow']]['light']['to-night']['need']['more']['shore']['yore']['shore']['before']['shore']['yore']['child']['smiled']['too']['he']['door']['sea']['thing']['word']['day']['he']['be']['sea']['snow']['eyes']['free']['be']['along']['grace']['face']['me']['too']['sped']['me']['too']['do']['hand']['day']['sun']['gray']['white']\n",
"----- diversity: 1.0\n",
"----- Generating with seed: \"['eyes', 'fell', 'own', 'blown', 'heart', 'in', 'part', 'sigh', 'why', 'tell', 'sell', 'away', 'day', 'fair', 'hair', 'white', 'to-night', 'cold', 'nigh', 'white', 'to-night', 'dart', 'hour', 'right', 'to-night', 'brow', 'vow', 'sigh', 'bright', 'to-night', 'door', 'before', 'aglow', 'fro', 'light', 'to-night', 'bell', 'hell', 'now', 'brow']\"\n",
"['be', ['eyes', 'fell', 'own', 'blown', 'heart', 'in', 'part', 'sigh', 'why', 'tell', 'sell', 'away', 'day', 'fair', 'hair', 'white', 'to-night', 'cold', 'nigh', 'white', 'to-night', 'dart', 'hour', 'right', 'to-night', 'brow', 'vow', 'sigh', 'bright', 'to-night', 'door', 'before', 'aglow', 'fro', 'light', 'to-night', 'bell', 'hell', 'now', 'brow']]['light']['to-night']['speak']['red']['head']['of']['bad']['within']['sin']['springs']['low']['sat']['cat']['everywhere']['clear']['pride']['success']['waiting']['bend']['two']['men']['dead']['low']['it']['it']['wide']['place']['bare']['thee']['name']['mead']['sight']['sail']['hand']['band']['fro']['wood']['while']['eye']['fight']\n",
"----- diversity: 1.2\n",
"----- Generating with seed: \"['eyes', 'fell', 'own', 'blown', 'heart', 'in', 'part', 'sigh', 'why', 'tell', 'sell', 'away', 'day', 'fair', 'hair', 'white', 'to-night', 'cold', 'nigh', 'white', 'to-night', 'dart', 'hour', 'right', 'to-night', 'brow', 'vow', 'sigh', 'bright', 'to-night', 'door', 'before', 'aglow', 'fro', 'light', 'to-night', 'bell', 'hell', 'now', 'brow']\"\n",
"['be', ['eyes', 'fell', 'own', 'blown', 'heart', 'in', 'part', 'sigh', 'why', 'tell', 'sell', 'away', 'day', 'fair', 'hair', 'white', 'to-night', 'cold', 'nigh', 'white', 'to-night', 'dart', 'hour', 'right', 'to-night', 'brow', 'vow', 'sigh', 'bright', 'to-night', 'door', 'before', 'aglow', 'fro', 'light', 'to-night', 'bell', 'hell', 'now', 'brow']]['still']['stir']['pace']['face']['more']['done']['desire']['tongue']['kind']['future']['creed']['stay']['burning']['own']['me']['more']['delight']['queen']['thought']['glow']['grow']['high']['youth']['falling']['jaw']['clay']['last']['grasp']['harm']['roar']['knee']['takes']['quee']['flow']['there']['day']['lay']['court']['off']['eyes']\n",
"Epoch 17/20\n",
"34477/34477 [==============================] - 223s 6ms/step - loss: 4.7961\n",
"Epoch 18/20\n",
"34477/34477 [==============================] - 222s 6ms/step - loss: 4.7257\n",
"Epoch 19/20\n",
"34477/34477 [==============================] - 223s 6ms/step - loss: 4.6614\n",
"Epoch 20/20\n",
"34477/34477 [==============================] - 222s 6ms/step - loss: 4.6155\n"
]
},
{
"data": {
"text/plain": [
"<keras.callbacks.History at 0x7f420c0a1400>"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"\n",
"char_indices = dict((c, i) for i, c in enumerate(chars))\n",
"indices_char = dict((i, c) for i, c in enumerate(chars))\n",
"\n",
"# cut the text in semi-redundant sequences of maxlen characters\n",
"maxlen = 40\n",
"step = 1\n",
"sentences = []\n",
"next_chars = []\n",
"\n",
"\n",
"for i in range(0, len(text) - maxlen, step):\n",
" sentences.append(text[i: i + maxlen])\n",
" next_chars.append(text[i + maxlen])\n",
"print('nb sequences:', len(sentences))\n",
"\n",
"print('Vectorization...')\n",
"x = np.zeros((len(sentences), maxlen, len(chars)), dtype=np.bool)\n",
"y = np.zeros((len(sentences), len(chars)), dtype=np.bool)\n",
"for i, sentence in enumerate(sentences):\n",
" for t, char in enumerate(sentence):\n",
" x[i, t, char_indices[char]] = 1\n",
" y[i, char_indices[next_chars[i]]] = 1\n",
"\n",
"\n",
"# build the model: a single LSTM\n",
"print('Build model...')\n",
"model = Sequential()\n",
"model.add(LSTM(128, input_shape=(maxlen, len(chars))))\n",
"model.add(Dense(len(chars), activation='softmax'))\n",
"\n",
"optimizer = RMSprop(lr=0.01)\n",
"model.compile(loss='categorical_crossentropy', optimizer=optimizer)\n",
"\n",
"\n",
"def sample(preds, temperature=1.0):\n",
" # helper function to sample an index from a probability array\n",
" preds = np.asarray(preds).astype('float64')\n",
" preds = np.log(preds) / temperature\n",
" exp_preds = np.exp(preds)\n",
" preds = exp_preds / np.sum(exp_preds)\n",
" probas = np.random.multinomial(1, preds, 1)\n",
" return np.argmax(probas)\n",
"\n",
"\n",
"char_indices = dict((c, i) for i, c in enumerate(chars))\n",
"indices_char = dict((i, c) for i, c in enumerate(chars))\n",
"\n",
"# cut the text in semi-redundant sequences of maxlen characters\n",
"maxlen = 40\n",
"step = 1\n",
"sentences = []\n",
"next_chars = []\n",
"\n",
"\n",
"for i in range(0, len(text) - maxlen, step):\n",
" sentences.append(text[i: i + maxlen])\n",
" next_chars.append(text[i + maxlen])\n",
"print('nb sequences:', len(sentences))\n",
"\n",
"print('Vectorization...')\n",
"x = np.zeros((len(sentences), maxlen, len(chars)), dtype=np.bool)\n",
"y = np.zeros((len(sentences), len(chars)), dtype=np.bool)\n",
"for i, sentence in enumerate(sentences):\n",
" for t, char in enumerate(sentence):\n",
" x[i, t, char_indices[char]] = 1\n",
" y[i, char_indices[next_chars[i]]] = 1\n",
"\n",
"\n",
"# build the model: a single LSTM\n",
"print('Build model...')\n",
"model = Sequential()\n",
"model.add(LSTM(128, input_shape=(maxlen, len(chars))))\n",
"model.add(Dense(len(chars), activation='softmax'))\n",
"\n",
"optimizer = RMSprop(lr=0.01)\n",
"model.compile(loss='categorical_crossentropy', optimizer=optimizer)\n",
"\n",
"\n",
"def sample(preds, temperature=1.0):\n",
" # helper function to sample an index from a probability array\n",
" preds = np.asarray(preds).astype('float64')\n",
" preds = np.log(preds) / temperature\n",
" exp_preds = np.exp(preds)\n",
" preds = exp_preds / np.sum(exp_preds)\n",
" probas = np.random.multinomial(1, preds, 1)\n",
" return np.argmax(probas)\n",
"\n",
"\n",
"def on_epoch_end(epoch, _):\n",
" # Function invoked at end of each epoch. Prints generated text.\n",
" if epoch % 5 == 0:\n",
" print()\n",
" print('----- Generating text after Epoch: {}'.format(epoch + 1))\n",
"\n",
" start_index = random.randint(0, len(text) - maxlen - 1)\n",
" for diversity in [0.2, 0.5, 1.0, 1.2]:\n",
" print('----- diversity:', diversity)\n",
"\n",
" generated = []\n",
" sentence = text[start_index: start_index + maxlen]\n",
" generated += sentence\n",
" print('----- Generating with seed: \"{}\"'.format(sentence))\n",
" sys.stdout.write(\"{}\".format(generated))\n",
"\n",
" for i in range(40):\n",
" x_pred = np.zeros((1, maxlen, len(chars)))\n",
" for t, char in enumerate(sentence):\n",
" x_pred[0, t, char_indices[char]] = 1.\n",
"\n",
" preds = model.predict(x_pred, verbose=0)[0]\n",
" next_index = sample(preds, diversity)\n",
" next_char = indices_char[next_index]\n",
"\n",
" generated += [next_char]\n",
" sentence = sentence[1:] + [next_char]\n",
"\n",
" sys.stdout.write('{}'.format([next_char]))\n",
" sys.stdout.flush()\n",
" print()\n",
"\n",
"print_callback = LambdaCallback(on_epoch_end=on_epoch_end)\n",
"\n",
"model.fit(x, y,\n",
" batch_size=128,\n",
" epochs=20,\n",
"callbacks=[print_callback])"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"----- diversity: 0.2\n",
"----- Generating with seed: \"['because', 'pause', 'gray', 'play', 'say', 'day', 'sheridan', 'man', 'high', 'sky', 'fame', 'name', 'bright', 'fight', 'away', 'bethlehem', 'lie', 'sleep', 'by', 'shineth', 'light', 'years', 'to-night', 'mary', 'above', 'keep', 'love', 'together', 'birth', 'king', 'earth', 'silently', 'given', 'hearts', 'heaven', 'coming', 'sin', 'still', 'in', 'bethlehem']\"\n",
"['be', ['because', 'pause', 'gray', 'play', 'say', 'day', 'sheridan', 'man', 'high', 'sky', 'fame', 'name', 'bright', 'fight', 'away', 'bethlehem', 'lie', 'sleep', 'by', 'shineth', 'light', 'years', 'to-night', 'mary', 'above', 'keep', 'love', 'together', 'birth', 'king', 'earth', 'silently', 'given', 'hearts', 'heaven', 'coming', 'sin', 'still', 'in', 'bethlehem']]['pray']['in']['to-day']['all']['fall']['fall']['again']['all']['came']['strong']['wrong']['song']['rest']['too']['you']['strong']['song']['now']['now']['now']['now']['light']['sight']['flight']['air']['there']['air']['lay']['hand']['land']['air']['lay']['hand']['white']['go']['snow']['below']['blow']['go']['blow']\n",
"----- diversity: 0.5\n",
"----- Generating with seed: \"['because', 'pause', 'gray', 'play', 'say', 'day', 'sheridan', 'man', 'high', 'sky', 'fame', 'name', 'bright', 'fight', 'away', 'bethlehem', 'lie', 'sleep', 'by', 'shineth', 'light', 'years', 'to-night', 'mary', 'above', 'keep', 'love', 'together', 'birth', 'king', 'earth', 'silently', 'given', 'hearts', 'heaven', 'coming', 'sin', 'still', 'in', 'bethlehem']\"\n",
"['be', ['because', 'pause', 'gray', 'play', 'say', 'day', 'sheridan', 'man', 'high', 'sky', 'fame', 'name', 'bright', 'fight', 'away', 'bethlehem', 'lie', 'sleep', 'by', 'shineth', 'light', 'years', 'to-night', 'mary', 'above', 'keep', 'love', 'together', 'birth', 'king', 'earth', 'silently', 'given', 'hearts', 'heaven', 'coming', 'sin', 'still', 'in', 'bethlehem']]['pray']['in']['all']['earth']['birth']['heard']['day']['man']['can']['mind']['yet']['sight']['all']['flight']['fall']['end']['friend']['day']['lay']['all']['day']['way']['soul']['goal']['soul']['bear']['out']['about']['eyes']['skies']['eyes']['rise']['skies']['eyes']['rise']['eyes']['lies']['eyes']['men']['dead']\n",
"----- diversity: 1.0\n",
"----- Generating with seed: \"['because', 'pause', 'gray', 'play', 'say', 'day', 'sheridan', 'man', 'high', 'sky', 'fame', 'name', 'bright', 'fight', 'away', 'bethlehem', 'lie', 'sleep', 'by', 'shineth', 'light', 'years', 'to-night', 'mary', 'above', 'keep', 'love', 'together', 'birth', 'king', 'earth', 'silently', 'given', 'hearts', 'heaven', 'coming', 'sin', 'still', 'in', 'bethlehem']\"\n",
"['be', ['because', 'pause', 'gray', 'play', 'say', 'day', 'sheridan', 'man', 'high', 'sky', 'fame', 'name', 'bright', 'fight', 'away', 'bethlehem', 'lie', 'sleep', 'by', 'shineth', 'light', 'years', 'to-night', 'mary', 'above', 'keep', 'love', 'together', 'birth', 'king', 'earth', 'silently', 'given', 'hearts', 'heaven', 'coming', 'sin', 'still', 'in', 'bethlehem']]['sighed']['go']['home']['man']['measure']['fears']['pain']['roof']['swell']['pinion']['south']['smile']['wind']['calls']['grass']['walls']['rise']['round']['do']['sound']['world']['he']['thee']['flowers']['mill']['liberty']['between']['nose']['day']['clear']['sword']['word']['line']['fly']['high']['folks']['too']['knows']['hell']['ahead']\n",
"----- diversity: 1.2\n",
"----- Generating with seed: \"['because', 'pause', 'gray', 'play', 'say', 'day', 'sheridan', 'man', 'high', 'sky', 'fame', 'name', 'bright', 'fight', 'away', 'bethlehem', 'lie', 'sleep', 'by', 'shineth', 'light', 'years', 'to-night', 'mary', 'above', 'keep', 'love', 'together', 'birth', 'king', 'earth', 'silently', 'given', 'hearts', 'heaven', 'coming', 'sin', 'still', 'in', 'bethlehem']\"\n",
"['be', ['because', 'pause', 'gray', 'play', 'say', 'day', 'sheridan', 'man', 'high', 'sky', 'fame', 'name', 'bright', 'fight', 'away', 'bethlehem', 'lie', 'sleep', 'by', 'shineth', 'light', 'years', 'to-night', 'mary', 'above', 'keep', 'love', 'together', 'birth', 'king', 'earth', 'silently', 'given', 'hearts', 'heaven', 'coming', 'sin', 'still', 'in', 'bethlehem']]['apace']['remain']['night']['light']['meet']['klingle']['enough']['scorn']['prayer']['helpless']['bonnet']['late']['gate']['one']['deep'][\"'em\"]['song']['strong']['wrong']['universe']['give']['tears']['slow']['wait']['morning']['flame']['give']['upon']['dead.']['so']['wave']['mind']['din']['game']['lace']['bread']['hope']['that']['top']['bed']\n"
]
}
],
"source": [
"start_index = random.randint(0, len(text) - maxlen - 1)\n",
"for diversity in [0.2, 0.5, 1.0, 1.2]:\n",
" print('----- diversity:', diversity)\n",
"\n",
" generated = ['be']\n",
" sentence = text[start_index: start_index + maxlen]\n",
" generated += [sentence]\n",
" print('----- Generating with seed: \"{}\"'.format(sentence))\n",
" sys.stdout.write(\"{}\".format(generated))\n",
"\n",
" for i in range(40):\n",
" x_pred = np.zeros((1, maxlen, len(chars)))\n",
" for t, char in enumerate(sentence):\n",
" x_pred[0, t, char_indices[char]] = 1.\n",
"\n",
" preds = model.predict(x_pred, verbose=0)[0]\n",
" next_index = sample(preds, diversity)\n",
" next_char = indices_char[next_index]\n",
"\n",
" generated += [next_char]\n",
" sentence = sentence[1:] + [next_char]\n",
"\n",
" sys.stdout.write('{}'.format([next_char]))\n",
" sys.stdout.flush()\n",
" print()"
]
},
{
"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.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
+105
View File
@@ -0,0 +1,105 @@
from __future__ import print_function
from keras.callbacks import LambdaCallback
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM
from keras.optimizers import RMSprop
from keras.utils.data_utils import get_file
import numpy as np
import random
import sys
import io
path = 'rhymes.txt'
with open(path, encoding='utf-8') as f:
text = f.read().lower()
text = text.split()
chars = sorted(list(set(text))) # vocabulary of rhymes in the corpus
# transform words into integer
char_indices = dict((c, i) for i, c in enumerate(chars))
indices_char = dict((i, c) for i, c in enumerate(chars))
maxlen = 40
step = 1
sentences = []
next_chars = []
# creating input and output
for i in range(0, len(text) - maxlen, step):
sentences.append(text[i: i + maxlen])
next_chars.append(text[i + maxlen])
print('Vectorization...')
x = np.zeros((len(sentences), maxlen, len(chars)), dtype=np.bool)
y = np.zeros((len(sentences), len(chars)), dtype=np.bool)
for i, sentence in enumerate(sentences):
for t, char in enumerate(sentence):
x[i, t, char_indices[char]] = 1
y[i, char_indices[next_chars[i]]] = 1
# build the LSTM model
print('Build model...')
model = Sequential()
model.add(LSTM(128, input_shape=(maxlen, len(chars))))
model.add(Dense(len(chars), activation='softmax'))
optimizer = RMSprop(lr=0.01)
model.compile(loss='categorical_crossentropy', optimizer=optimizer)
def sample(preds, temperature=1.0):
# Generates the next word
preds = np.asarray(preds).astype('float64')
preds = np.log(preds) / temperature
exp_preds = np.exp(preds)
preds = exp_preds / np.sum(exp_preds)
probas = np.random.multinomial(1, preds, 1)
return np.argmax(probas)
def on_epoch_end(epoch, _):
# Function invoked at end of each epoch. Prints generated text.
if epoch % 5 == 0:
print()
print('----- Generating text after Epoch: {}'.format(epoch + 1))
start_index = random.randint(0, len(text) - maxlen - 1)
for diversity in [0.2, 0.5, 1.0, 1.2]:
print('----- diversity:', diversity)
generated = [rhyme]
sentence = text[start_index: start_index + maxlen]
generated += [sentence]
print('----- Generating from: "{}"'.format(sentence))
sys.stdout.write("{}".format(generated))
for i in range(40):
x_pred = np.zeros((1, maxlen, len(chars)))
for t, char in enumerate(sentence):
x_pred[0, t, char_indices[char]] = 1.
preds = model.predict(x_pred, verbose=0)[0]
next_index = sample(preds, diversity)
next_char = indices_char[next_index]
generated += [next_char]
sentence = sentence[1:] + [next_char]
sys.stdout.write('{}'.format([next_char]))
sys.stdout.flush()
print()
print_callback = LambdaCallback(on_epoch_end=on_epoch_end)
model.fit(x, y,
batch_size=128,
epochs=20,
callbacks=[print_callback])