|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": 1, |
| 6 | + "metadata": {}, |
| 7 | + "outputs": [ |
| 8 | + { |
| 9 | + "name": "stderr", |
| 10 | + "output_type": "stream", |
| 11 | + "text": [ |
| 12 | + "Using TensorFlow backend.\n", |
| 13 | + "c:\\users\\david\\appdata\\local\\programs\\python\\python37\\lib\\site-packages\\tensorflow\\python\\framework\\dtypes.py:516: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.\n", |
| 14 | + " _np_qint8 = np.dtype([(\"qint8\", np.int8, 1)])\n", |
| 15 | + "c:\\users\\david\\appdata\\local\\programs\\python\\python37\\lib\\site-packages\\tensorflow\\python\\framework\\dtypes.py:517: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.\n", |
| 16 | + " _np_quint8 = np.dtype([(\"quint8\", np.uint8, 1)])\n", |
| 17 | + "c:\\users\\david\\appdata\\local\\programs\\python\\python37\\lib\\site-packages\\tensorflow\\python\\framework\\dtypes.py:518: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.\n", |
| 18 | + " _np_qint16 = np.dtype([(\"qint16\", np.int16, 1)])\n", |
| 19 | + "c:\\users\\david\\appdata\\local\\programs\\python\\python37\\lib\\site-packages\\tensorflow\\python\\framework\\dtypes.py:519: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.\n", |
| 20 | + " _np_quint16 = np.dtype([(\"quint16\", np.uint16, 1)])\n", |
| 21 | + "c:\\users\\david\\appdata\\local\\programs\\python\\python37\\lib\\site-packages\\tensorflow\\python\\framework\\dtypes.py:520: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.\n", |
| 22 | + " _np_qint32 = np.dtype([(\"qint32\", np.int32, 1)])\n", |
| 23 | + "c:\\users\\david\\appdata\\local\\programs\\python\\python37\\lib\\site-packages\\tensorflow\\python\\framework\\dtypes.py:525: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.\n", |
| 24 | + " np_resource = np.dtype([(\"resource\", np.ubyte, 1)])\n", |
| 25 | + "c:\\users\\david\\appdata\\local\\programs\\python\\python37\\lib\\site-packages\\tensorboard\\compat\\tensorflow_stub\\dtypes.py:541: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.\n", |
| 26 | + " _np_qint8 = np.dtype([(\"qint8\", np.int8, 1)])\n", |
| 27 | + "c:\\users\\david\\appdata\\local\\programs\\python\\python37\\lib\\site-packages\\tensorboard\\compat\\tensorflow_stub\\dtypes.py:542: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.\n", |
| 28 | + " _np_quint8 = np.dtype([(\"quint8\", np.uint8, 1)])\n", |
| 29 | + "c:\\users\\david\\appdata\\local\\programs\\python\\python37\\lib\\site-packages\\tensorboard\\compat\\tensorflow_stub\\dtypes.py:543: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.\n", |
| 30 | + " _np_qint16 = np.dtype([(\"qint16\", np.int16, 1)])\n", |
| 31 | + "c:\\users\\david\\appdata\\local\\programs\\python\\python37\\lib\\site-packages\\tensorboard\\compat\\tensorflow_stub\\dtypes.py:544: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.\n", |
| 32 | + " _np_quint16 = np.dtype([(\"quint16\", np.uint16, 1)])\n", |
| 33 | + "c:\\users\\david\\appdata\\local\\programs\\python\\python37\\lib\\site-packages\\tensorboard\\compat\\tensorflow_stub\\dtypes.py:545: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.\n", |
| 34 | + " _np_qint32 = np.dtype([(\"qint32\", np.int32, 1)])\n", |
| 35 | + "c:\\users\\david\\appdata\\local\\programs\\python\\python37\\lib\\site-packages\\tensorboard\\compat\\tensorflow_stub\\dtypes.py:550: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.\n", |
| 36 | + " np_resource = np.dtype([(\"resource\", np.ubyte, 1)])\n" |
| 37 | + ] |
| 38 | + }, |
| 39 | + { |
| 40 | + "name": "stdout", |
| 41 | + "output_type": "stream", |
| 42 | + "text": [ |
| 43 | + "Downloading data from https://s3.amazonaws.com/text-datasets/nietzsche.txt\n", |
| 44 | + "606208/600901 [==============================] - 0s 1us/step\n", |
| 45 | + "corpus length: 600893\n", |
| 46 | + "total chars: 57\n", |
| 47 | + "nb sequences: 200285\n", |
| 48 | + "Vectorization...\n" |
| 49 | + ] |
| 50 | + }, |
| 51 | + { |
| 52 | + "name": "stderr", |
| 53 | + "output_type": "stream", |
| 54 | + "text": [ |
| 55 | + "WARNING: Logging before flag parsing goes to stderr.\n", |
| 56 | + "W0926 21:19:30.765121 9788 deprecation_wrapper.py:119] From c:\\users\\david\\appdata\\local\\programs\\python\\python37\\lib\\site-packages\\keras\\backend\\tensorflow_backend.py:74: The name tf.get_default_graph is deprecated. Please use tf.compat.v1.get_default_graph instead.\n", |
| 57 | + "\n" |
| 58 | + ] |
| 59 | + }, |
| 60 | + { |
| 61 | + "name": "stdout", |
| 62 | + "output_type": "stream", |
| 63 | + "text": [ |
| 64 | + "Build model...\n" |
| 65 | + ] |
| 66 | + }, |
| 67 | + { |
| 68 | + "name": "stderr", |
| 69 | + "output_type": "stream", |
| 70 | + "text": [ |
| 71 | + "W0926 21:19:31.060807 9788 deprecation_wrapper.py:119] From c:\\users\\david\\appdata\\local\\programs\\python\\python37\\lib\\site-packages\\keras\\backend\\tensorflow_backend.py:517: The name tf.placeholder is deprecated. Please use tf.compat.v1.placeholder instead.\n", |
| 72 | + "\n", |
| 73 | + "W0926 21:19:31.137966 9788 deprecation_wrapper.py:119] From c:\\users\\david\\appdata\\local\\programs\\python\\python37\\lib\\site-packages\\keras\\backend\\tensorflow_backend.py:4138: The name tf.random_uniform is deprecated. Please use tf.random.uniform instead.\n", |
| 74 | + "\n" |
| 75 | + ] |
| 76 | + }, |
| 77 | + { |
| 78 | + "ename": "TypeError", |
| 79 | + "evalue": "Unexpected keyword argument passed to optimizer: learning_rate", |
| 80 | + "output_type": "error", |
| 81 | + "traceback": [ |
| 82 | + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", |
| 83 | + "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", |
| 84 | + "\u001b[1;32m<ipython-input-1-f60c43a8adc2>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m 48\u001b[0m \u001b[0mmodel\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0madd\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mDense\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mlen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mchars\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mactivation\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m'softmax'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 49\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 50\u001b[1;33m \u001b[0moptimizer\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mRMSprop\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mlearning_rate\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m0.01\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 51\u001b[0m \u001b[0mmodel\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mcompile\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mloss\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m'categorical_crossentropy'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0moptimizer\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0moptimizer\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 52\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n", |
| 85 | + "\u001b[1;32mc:\\users\\david\\appdata\\local\\programs\\python\\python37\\lib\\site-packages\\keras\\optimizers.py\u001b[0m in \u001b[0;36m__init__\u001b[1;34m(self, lr, rho, epsilon, decay, **kwargs)\u001b[0m\n\u001b[0;32m 241\u001b[0m def __init__(self, lr=0.001, rho=0.9, epsilon=None, decay=0.,\n\u001b[0;32m 242\u001b[0m **kwargs):\n\u001b[1;32m--> 243\u001b[1;33m \u001b[0msuper\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mRMSprop\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m__init__\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 244\u001b[0m \u001b[1;32mwith\u001b[0m \u001b[0mK\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mname_scope\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m__class__\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m__name__\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 245\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mlr\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mK\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mvariable\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mlr\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mname\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m'lr'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", |
| 86 | + "\u001b[1;32mc:\\users\\david\\appdata\\local\\programs\\python\\python37\\lib\\site-packages\\keras\\optimizers.py\u001b[0m in \u001b[0;36m__init__\u001b[1;34m(self, **kwargs)\u001b[0m\n\u001b[0;32m 77\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mk\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mallowed_kwargs\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 78\u001b[0m raise TypeError('Unexpected keyword argument '\n\u001b[1;32m---> 79\u001b[1;33m 'passed to optimizer: ' + str(k))\n\u001b[0m\u001b[0;32m 80\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m__dict__\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mupdate\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 81\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mupdates\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", |
| 87 | + "\u001b[1;31mTypeError\u001b[0m: Unexpected keyword argument passed to optimizer: learning_rate" |
| 88 | + ] |
| 89 | + } |
| 90 | + ], |
| 91 | + "source": [ |
| 92 | + "from __future__ import print_function\n", |
| 93 | + "from keras.callbacks import LambdaCallback\n", |
| 94 | + "from keras.models import Sequential\n", |
| 95 | + "from keras.layers import Dense\n", |
| 96 | + "from keras.layers import LSTM\n", |
| 97 | + "from keras.optimizers import RMSprop\n", |
| 98 | + "from keras.utils.data_utils import get_file\n", |
| 99 | + "import numpy as np\n", |
| 100 | + "import random\n", |
| 101 | + "import sys\n", |
| 102 | + "import io\n", |
| 103 | + "\n", |
| 104 | + "path = get_file(\n", |
| 105 | + " 'nietzsche.txt',\n", |
| 106 | + " origin='https://s3.amazonaws.com/text-datasets/nietzsche.txt')\n", |
| 107 | + "with io.open(path, encoding='utf-8') as f:\n", |
| 108 | + " text = f.read().lower()\n", |
| 109 | + "print('corpus length:', len(text))\n", |
| 110 | + "\n", |
| 111 | + "chars = sorted(list(set(text)))\n", |
| 112 | + "print('total chars:', len(chars))\n", |
| 113 | + "char_indices = dict((c, i) for i, c in enumerate(chars))\n", |
| 114 | + "indices_char = dict((i, c) for i, c in enumerate(chars))\n", |
| 115 | + "\n", |
| 116 | + "# cut the text in semi-redundant sequences of maxlen characters\n", |
| 117 | + "maxlen = 40\n", |
| 118 | + "step = 3\n", |
| 119 | + "sentences = []\n", |
| 120 | + "next_chars = []\n", |
| 121 | + "for i in range(0, len(text) - maxlen, step):\n", |
| 122 | + " sentences.append(text[i: i + maxlen])\n", |
| 123 | + " next_chars.append(text[i + maxlen])\n", |
| 124 | + "print('nb sequences:', len(sentences))\n", |
| 125 | + "\n", |
| 126 | + "print('Vectorization...')\n", |
| 127 | + "x = np.zeros((len(sentences), maxlen, len(chars)), dtype=np.bool)\n", |
| 128 | + "y = np.zeros((len(sentences), len(chars)), dtype=np.bool)\n", |
| 129 | + "for i, sentence in enumerate(sentences):\n", |
| 130 | + " for t, char in enumerate(sentence):\n", |
| 131 | + " x[i, t, char_indices[char]] = 1\n", |
| 132 | + " y[i, char_indices[next_chars[i]]] = 1\n", |
| 133 | + "\n", |
| 134 | + "\n", |
| 135 | + "# build the model: a single LSTM\n", |
| 136 | + "print('Build model...')\n", |
| 137 | + "model = Sequential()\n", |
| 138 | + "model.add(LSTM(128, input_shape=(maxlen, len(chars))))\n", |
| 139 | + "model.add(Dense(len(chars), activation='softmax'))\n", |
| 140 | + "\n", |
| 141 | + "optimizer = RMSprop(learning_rate=0.01)\n", |
| 142 | + "model.compile(loss='categorical_crossentropy', optimizer=optimizer)\n", |
| 143 | + "\n", |
| 144 | + "\n", |
| 145 | + "def sample(preds, temperature=1.0):\n", |
| 146 | + " # helper function to sample an index from a probability array\n", |
| 147 | + " preds = np.asarray(preds).astype('float64')\n", |
| 148 | + " preds = np.log(preds) / temperature\n", |
| 149 | + " exp_preds = np.exp(preds)\n", |
| 150 | + " preds = exp_preds / np.sum(exp_preds)\n", |
| 151 | + " probas = np.random.multinomial(1, preds, 1)\n", |
| 152 | + " return np.argmax(probas)\n", |
| 153 | + "\n", |
| 154 | + "\n", |
| 155 | + "def on_epoch_end(epoch, _):\n", |
| 156 | + " # Function invoked at end of each epoch. Prints generated text.\n", |
| 157 | + " print()\n", |
| 158 | + " print('----- Generating text after Epoch: %d' % epoch)\n", |
| 159 | + "\n", |
| 160 | + " start_index = random.randint(0, len(text) - maxlen - 1)\n", |
| 161 | + " for diversity in [0.2, 0.5, 1.0, 1.2]:\n", |
| 162 | + " print('----- diversity:', diversity)\n", |
| 163 | + "\n", |
| 164 | + " generated = ''\n", |
| 165 | + " sentence = text[start_index: start_index + maxlen]\n", |
| 166 | + " generated += sentence\n", |
| 167 | + " print('----- Generating with seed: \"' + sentence + '\"')\n", |
| 168 | + " sys.stdout.write(generated)\n", |
| 169 | + "\n", |
| 170 | + " for i in range(400):\n", |
| 171 | + " x_pred = np.zeros((1, maxlen, len(chars)))\n", |
| 172 | + " for t, char in enumerate(sentence):\n", |
| 173 | + " x_pred[0, t, char_indices[char]] = 1.\n", |
| 174 | + "\n", |
| 175 | + " preds = model.predict(x_pred, verbose=0)[0]\n", |
| 176 | + " next_index = sample(preds, diversity)\n", |
| 177 | + " next_char = indices_char[next_index]\n", |
| 178 | + "\n", |
| 179 | + " sentence = sentence[1:] + next_char\n", |
| 180 | + "\n", |
| 181 | + " sys.stdout.write(next_char)\n", |
| 182 | + " sys.stdout.flush()\n", |
| 183 | + " print()\n", |
| 184 | + "\n", |
| 185 | + "print_callback = LambdaCallback(on_epoch_end=on_epoch_end)\n", |
| 186 | + "\n", |
| 187 | + "model.fit(x, y,\n", |
| 188 | + " batch_size=128,\n", |
| 189 | + " epochs=60,\n", |
| 190 | + " callbacks=[print_callback])" |
| 191 | + ] |
| 192 | + }, |
| 193 | + { |
| 194 | + "cell_type": "code", |
| 195 | + "execution_count": null, |
| 196 | + "metadata": {}, |
| 197 | + "outputs": [], |
| 198 | + "source": [] |
| 199 | + } |
| 200 | + ], |
| 201 | + "metadata": { |
| 202 | + "kernelspec": { |
| 203 | + "display_name": "Python 3", |
| 204 | + "language": "python", |
| 205 | + "name": "python3" |
| 206 | + }, |
| 207 | + "language_info": { |
| 208 | + "codemirror_mode": { |
| 209 | + "name": "ipython", |
| 210 | + "version": 3 |
| 211 | + }, |
| 212 | + "file_extension": ".py", |
| 213 | + "mimetype": "text/x-python", |
| 214 | + "name": "python", |
| 215 | + "nbconvert_exporter": "python", |
| 216 | + "pygments_lexer": "ipython3", |
| 217 | + "version": "3.7.4" |
| 218 | + } |
| 219 | + }, |
| 220 | + "nbformat": 4, |
| 221 | + "nbformat_minor": 2 |
| 222 | +} |
0 commit comments