Wayne H Nixalo - 13 June 2017
Practical Deep Learning I
Lesson 5 - RNNs, NLP
Code along of char-rnn.ipynb
In [1]:
import theano
In [2]:
%matplotlib inline
import os, sys
sys.path.insert(1, os.path.join('utils'))
import utils; reload(utils)
from utils import *
from __future__ import print_function, division
In [3]:
from keras.layers import TimeDistributed, Activation
# https://keras.io/layers/wrappers/
# [Doc:TimeDistributed] this wrapper allows to apply a layer to every temporal slice of an input
# https://keras.io/activations/
# [Doc:Activation] activations can be used through an Activation layer
from numpy.random import choice
In [4]:
path = get_file('nietzsche.txt', origin='https://s3.amazonaws.com/text-datasets/nietzsche.txt')
text = open(path).read().lower()
print('corpus length:', len(text))
In [5]:
!tail {path} -n25
In [6]:
# unique characters
chars = sorted(list(set(text)))
vocab_size = len(chars) + 1
print('total chars:', vocab_size)
In [7]:
chars.insert(0, "\0")
In [8]:
# the unique characters (in UpLoCase corpus, add 26)
''.join(chars[1:-6])
Out[8]:
In [9]:
# create a mapping from char to index in which it appears
char_indices = dict((c, i) for i, c in enumerate(chars))
# create a mapping from index to char
indices_char = dict((i, c) for i, c in enumerate(chars))
^ This allows us to take the text & convert into a list of numbers, where the number represents the index in which the char appears in the unique-char list.
In [10]:
idx = [char_indices[c] for c in text]
idx[:10]
Out[10]:
In [11]:
''.join(indices_char[i] for i in idx[:70])
Out[11]:
In [12]:
maxlen = 40
sentences = []
next_chars = []
for i in xrange(0, len(idx) - maxlen + 1):
sentences.append(idx[i: i + maxlen])
next_chars.append(idx[i + 1: i + maxlen + 1])
print('nb sequences:', len(sentences))
In [13]:
sentences = np.concatenate([[np.array(o)] for o in sentences[:-2]])
next_chars = np.concatenate([[np.array(o)] for o in next_chars[:-2]])
In [14]:
sentences.shape, next_chars.shape
Out[14]:
In [15]:
n_fac = 24
In Lesson 6: improved: an RNN feeding into an RNN. See lecture at ~ 1:10:00
In [16]:
model = Sequential([
Embedding(vocab_size, n_fac, input_length=maxlen),
LSTM(512, input_dim=n_fac, return_sequences=True, dropout_U=0.2, dropout_W=0.2,
consume_less='gpu'),
Dropout(0.2),
LSTM(512, return_sequences=True, dropout_U=0.2, dropout_W=0.2,
consume_less='gpu'),
Dropout(0.2),
TimeDistributed(Dense(vocab_size)),
Activation('softmax')
])
In [17]:
model.compile(loss='sparse_categorical_crossentropy', optimizer=Adam())
In [18]:
def print_example():
seed_string="ethics is a basic foundation of all that"
for i in range(320):
x=np.array([char_indices[c] for c in seed_string[-40:]])[np.newaxis,:]
preds = model.predict(x, verbose=0)[0][-1]
preds = preds/np.sum(preds)
next_char = choice(chars, p=preds)
seed_string = seed_string + next_char
print(seed_string)
In [19]:
model.fit(sentences, np.expand_dims(next_chars, -1), batch_size=64, nb_epoch=1)
Out[19]:
In [20]:
print_example()
In [21]:
model.fit(sentences, np.expand_dims(next_chars, -1), batch_size=64, nb_epoch=1)
Out[21]:
In [22]:
print_example()
In [23]:
model.optimizer.lr=1e-3
model.fit(sentences, np.expand_dims(next_chars, -1), batch_size=128, nb_epoch=1)
Out[23]:
In [24]:
print_example()
In [25]:
model.optimizer.lr=1e-4
model.fit(sentences, np.expand_dims(next_chars, -1), batch_size=256, nb_epoch=1)
Out[25]:
In [26]:
print_example()
In [27]:
%mkdir -p 'data/char_rnn/'
model.save_weights('data/char_rnn.h5')
In [28]:
model.optimizer.lr=1e-5
model.fit(sentences, np.expand_dims(next_chars, -1), batch_size=256, nb_epoch=1)
Out[28]:
In [29]:
print_example()
In [30]:
model.fit(sentences, np.expand_dims(next_chars, -1), batch_size=128, nb_epoch=1)
Out[30]:
In [31]:
print_example()
In [32]:
print_example()
In [33]:
model.save_weights('data/char_rnn.h5')
In [34]:
def print_example(seed_string=''):
if not seed_string:
seed_string="ethics is a basic foundation of all that"
for i in range(320):
x=np.array([char_indices[c] for c in seed_string[-40:]])[np.newaxis,:]
preds = model.predict(x, verbose=0)[0][-1]
preds = preds/np.sum(preds)
next_char = choice(chars, p=preds)
seed_string = seed_string + next_char
print(seed_string)
In [40]:
text ='so um first i was afraid i was petrified'
print_example(text)
In [ ]: