Character-level Language Modeling with LSTMs

This notebook is adapted from Keras' lstm_text_generation.py.

Steps:

  • Download a small text corpus and preprocess it.
  • Extract a character vocabulary and use it to vectorize the text.
  • Train an LSTM-based character level language model.
  • Use the trained model to sample random text with varying entropy levels.
  • Implement a beam-search deterministic decoder.

Note: fitting language models is compute intensive. It is recommended to do this notebook on a server with a GPU or powerful CPUs that you can leave running for several hours at once.


In [1]:
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt

Loading some text data

Let's use some publicly available philosophy:


In [2]:
from keras.utils.data_utils import get_file

URL = "https://s3.amazonaws.com/text-datasets/nietzsche.txt"

corpus_path = get_file('nietzsche.txt', origin=URL)
text = open(corpus_path).read().lower()
print('Corpus length: %d characters' % len(text))


Using TensorFlow backend.
Corpus length: 600893 characters

In [3]:
print(text[:600], "...")


preface


supposing that truth is a woman--what then? is there not ground
for suspecting that all philosophers, in so far as they have been
dogmatists, have failed to understand women--that the terrible
seriousness and clumsy importunity with which they have usually paid
their addresses to truth, have been unskilled and unseemly methods for
winning a woman? certainly she has never allowed herself to be won; and
at present every kind of dogma stands with sad and discouraged mien--if,
indeed, it stands at all! for there are scoffers who maintain that it
has fallen, that all dogma lies on the gro ...

In [4]:
text = text.replace("\n", " ")
split = int(0.9 * len(text))
train_text = text[:split]
test_text = text[split:]

Building a vocabulary of all possible symbols

To simplify things, we build a vocabulary by extracting the list all possible characters from the full datasets (train and validation).

In a more realistic setting we would need to take into account that the test data can hold symbols never seen in the training set. This issue is limited when we work at the character level though.

Let's build the list of all possible characters and sort it to assign a unique integer to each possible symbol in the corpus:


In [5]:
chars = sorted(list(set(text)))
print('total chars:', len(chars))
char_indices = dict((c, i) for i, c in enumerate(chars))
indices_char = dict((i, c) for i, c in enumerate(chars))


total chars: 56

char_indices is a mapping to from characters to integer identifiers:


In [6]:
len(char_indices)


Out[6]:
56

In [7]:
sorted(char_indices.items())[:15]


Out[7]:
[(' ', 0),
 ('!', 1),
 ('"', 2),
 ("'", 3),
 ('(', 4),
 (')', 5),
 (',', 6),
 ('-', 7),
 ('.', 8),
 ('0', 9),
 ('1', 10),
 ('2', 11),
 ('3', 12),
 ('4', 13),
 ('5', 14)]

indices_char holds the reverse mapping:


In [8]:
len(indices_char)


Out[8]:
56

In [9]:
indices_char[52]


Out[9]:
'ä'

While not strictly required to build a language model, it's a good idea to have a look at the distribution of relative frequencies of each symbol in the corpus:


In [10]:
from collections import Counter

counter = Counter(text)
chars, counts = zip(*counter.most_common())
indices = np.arange(len(counts))

plt.figure(figsize=(14, 3))
plt.bar(indices, counts, 0.8)
plt.xticks(indices, chars);


Let's cut the dataset into fake sentences at random with some overlap. Instead of cutting at random we could use a English specific sentence tokenizer. This is explained at the end of this notebook. In the mean time random substring will be good enough to train a first language model.


In [11]:
max_length = 40
step = 3


def make_sequences(text, max_length=max_length, step=step):
    sequences = []
    next_chars = []
    for i in range(0, len(text) - max_length, step):
        sequences.append(text[i: i + max_length])
        next_chars.append(text[i + max_length])
    return sequences, next_chars    


sequences, next_chars = make_sequences(train_text)
sequences_test, next_chars_test = make_sequences(test_text, step=10)

print('nb train sequences:', len(sequences))
print('nb test sequences:', len(sequences_test))


nb train sequences: 180255
nb test sequences: 6005

Let's shuffle the sequences to break some of the dependencies:


In [12]:
from sklearn.utils import shuffle

sequences, next_chars = shuffle(sequences, next_chars,
                                random_state=42)

In [13]:
sequences[0]


Out[13]:
'distrust, a refinement of distrust of ev'

In [14]:
next_chars[0]


Out[14]:
'e'

Converting the training data to one-hot vectors

Unfortunately the LSTM implementation in Keras does not (yet?) accept integer indices to slice columns from an input embedding by it-self. Let's use one-hot encoding. This is slightly less space and time efficient than integer coding but should be good enough when using a small character level vocabulary.

Exercise:

One hot encoded the training data sequences as X and next_chars as y:


In [15]:
n_sequences = len(sequences)
n_sequences_test = len(sequences_test)
voc_size = len(chars)

X = np.zeros((n_sequences, max_length, voc_size),
             dtype=np.float32)
y = np.zeros((n_sequences, voc_size), dtype=np.float32)

X_test = np.zeros((n_sequences_test, max_length, voc_size),
                  dtype=np.float32)
y_test = np.zeros((n_sequences_test, voc_size), dtype=np.float32)

# TODO

In [16]:
# %load solutions/language_model_one_hot_data.py
n_sequences = len(sequences)
n_sequences_test = len(sequences_test)
voc_size = len(chars)

X = np.zeros((n_sequences, max_length, voc_size),
             dtype=np.float32)
y = np.zeros((n_sequences, voc_size), dtype=np.float32)

X_test = np.zeros((n_sequences_test, max_length, voc_size),
                  dtype=np.float32)
y_test = np.zeros((n_sequences_test, voc_size), dtype=np.float32)


for i, sequence in enumerate(sequences):
    for t, char in enumerate(sequence):
        X[i, t, char_indices[char]] = 1
    y[i, char_indices[next_chars[i]]] = 1
    
for i, sequence in enumerate(sequences_test):
    for t, char in enumerate(sequence):
        X_test[i, t, char_indices[char]] = 1
    y_test[i, char_indices[next_chars_test[i]]] = 1

In [17]:
X.shape


Out[17]:
(180255, 40, 56)

In [18]:
y.shape


Out[18]:
(180255, 56)

In [19]:
X[0]


Out[19]:
array([[ 0.,  0.,  0., ...,  0.,  0.,  0.],
       [ 0.,  0.,  0., ...,  0.,  0.,  0.],
       [ 0.,  0.,  0., ...,  0.,  0.,  0.],
       ..., 
       [ 1.,  0.,  0., ...,  0.,  0.,  0.],
       [ 0.,  0.,  0., ...,  0.,  0.,  0.],
       [ 0.,  0.,  0., ...,  0.,  0.,  0.]], dtype=float32)

In [20]:
y[0]


Out[20]:
array([ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
        0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
        0.,  0.,  0.,  0.,  1.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
        0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
        0.,  0.,  0.,  0.], dtype=float32)

Measuring per-character perplexity

The NLP community measures the quality of probabilistic model using perplexity.

In practice perplexity is just a base 2 exponentiation of the average negative log2 likelihoods:

$$perplexity_\theta = 2^{-\frac{1}{n} \sum_{i=1}^{n} log_2 (p_\theta(x_i))}$$

Note: here we define the per-character perplexity (because our model naturally makes per-character predictions). It is more common to report per-word perplexity. Note that this is not as easy to compute the per-world perplexity as we would need to tokenize the strings into a sequence of words and discard whitespace and punctuation character predictions. In practice the whitespace character is the most frequent character by far making our naive per-character perplexity lower than it should be if we ignored those.

Exercise: implement a Python function that computes the per-character perplexity with model predicted probabilities y_pred and y_true for the encoded ground truth:


In [21]:
def perplexity(y_true, y_pred):
    """Compute the per-character perplexity of model predictions.
    
    y_true is one-hot encoded ground truth.
    y_pred is predicted likelihoods for each class.
    
    2 ** -mean(log2(p))
    """
    # TODO
    return 1.

In [22]:
# %load solutions/language_model_perplexity.py
def perplexity(y_true, y_pred):
    """Compute the perplexity of model predictions.
    
    y_true is one-hot encoded ground truth.
    y_pred is predicted likelihoods for each class.
    
    2 ** -mean(log2(p))
    """
    likelihoods = np.sum(y_pred * y_true, axis=1)
    return 2 ** -np.mean(np.log2(likelihoods))

In [23]:
y_true = np.array([
    [0, 1, 0],
    [0, 0, 1],
    [0, 0, 1],
])

y_pred = np.array([
    [0.1, 0.9, 0.0],
    [0.1, 0.1, 0.8],
    [0.1, 0.2, 0.7],
])

perplexity(y_true, y_pred)


Out[23]:
1.2565790685485896

A perfect model has a minimal perplexity of 1.0 (negative log likelihood of 0.0):


In [24]:
perplexity(y_true, y_true)


Out[24]:
1.0

Building recurrent model

Let's build a first model and train it on a very small subset of the data to check that it works as expected:


In [25]:
from keras.models import Sequential
from keras.layers import LSTM, Dense
from keras.optimizers import RMSprop


model = Sequential()
model.add(LSTM(128, input_shape=(max_length, voc_size)))
model.add(Dense(voc_size, activation='softmax'))

optimizer = RMSprop(lr=0.01)
model.compile(optimizer=optimizer, loss='categorical_crossentropy')

Let's measure the perplexity of the randomly initialized model:


In [26]:
def model_perplexity(model, X, y, verbose=0):
    predictions = model.predict(X, verbose=verbose)
    return perplexity(y, predictions)

In [27]:
model_perplexity(model, X_test, y_test)


Out[27]:
55.851569769252848

Let's train the model for one epoch on a very small subset of the training set to check that it's well defined:


In [28]:
small_train = slice(0, None, 40)

model.fit(X[small_train], y[small_train], validation_split=0.1,
          batch_size=128, nb_epoch=1)


Train on 4056 samples, validate on 451 samples
Epoch 1/1
4056/4056 [==============================] - 1s - loss: 3.1151 - val_loss: 3.0674
Out[28]:
<keras.callbacks.History at 0x7f62cc5260b8>

In [29]:
model_perplexity(model, X[small_train], y[small_train])


Out[29]:
19.06967878041857

In [30]:
model_perplexity(model, X_test, y_test)


Out[30]:
18.217444707632556

Sampling random text from the model

Recursively generate one character at a time by sampling from the distribution parameterized by the model:

$$ p_{\theta}(c_n | c_{n-1}, c_{n-2}, \ldots, c_0) \cdot p_{\theta}(c_{n-1} | c_{n-2}, \ldots, c_0) \cdot \ldots \cdot p_{\theta}(c_{0}) $$

The temperature parameter makes it possible to remove additional entropy (bias) into the parameterized multinoulli distribution of the output of the model:


In [31]:
def sample_one(preds, temperature=1.0):
    """Sample the next character according to the network output.
    
    Use a lower temperature to force the model to output more
    confident predictions: more peaky distribution.
    """
    preds = np.asarray(preds).astype('float64')
    preds = np.log(preds) / temperature
    exp_preds = np.exp(preds)
    preds = exp_preds / np.sum(exp_preds)
    # Draw a single sample (size=1) from a multinoulli distribution
    # parameterized by the output of the softmax layer of our
    # network. A multinoulli distribution is a multinomial
    # distribution with a single trial with n_classes outcomes.
    probs = np.random.multinomial(1, preds, size=1)
    return np.argmax(probs)


def generate_text(model, seed_string, length=300, temperature=1.0):
    """Recursively sample a sequence of chars, one char at a time.
    
    Each prediction is concatenated to the past string of predicted
    chars so as to condition the next prediction.

    Feed seed string as a sequence of characters to condition the
    first predictions recursively. If seed_string is lower than
    max_length, pad the input with zeros at the beginning of the
    conditioning string.
    """
    generated = seed_string
    prefix = seed_string

    for i in range(length):
        # Vectorize prefix string to feed as input to the model:
        x = np.zeros((1, max_length, voc_size))
        shift = max_length - len(prefix)
        for t, char in enumerate(prefix):
            x[0, t + shift, char_indices[char]] = 1.

        preds = model.predict(x, verbose=0)[0]
        next_index = sample_one(preds, temperature)
        next_char = indices_char[next_index]

        generated += next_char
        prefix = prefix[1:] + next_char
    return generated

In [32]:
generate_text(model, 'philosophers are ', temperature=0.1)


Out[32]:
'philosophers are tee eeee e e ee tee te ee see ee eae e e te e e e esee eee e eeeee e teeeae te eeeee e tee ee ee e es  e ee ee ee  e te eeee ee e e  e eeaeeee ee e e e etee tee e eeeeeeees e ee e iteee tee e ee e e tee teee teeese e teeeeee ee e  eetee e e ee e e tee ite e e tee eeteie eeeeeee eeeese eeeeteeetee te'

In [ ]:
generate_text(model, 'atheism is the root of ', temperature=0.8)


Out[ ]:
'atheism is the root of f infsnttreetfincesi aaemi eahrtaseiiop sta   tsfeeihsaisnssmeerhtsrxhpcwserohdssrthif2itelulhpayeorrertso insfy aahluiisrie t echfoyelefneanmy nns siterefeesiecsfrlo lti,le l eso shnsotnetd,eeahsc ftne oplvhoedd s  naa  e ycciashe-dsiothye penonstaceworpoto tetsertah so ts  o,ccmcietanelscof ae otn'

Training the model

Let's train the model and monitor the perplexity after each epoch and sample some text to qualitatively evaluate the model:


In [ ]:
nb_epoch = 30
seed_strings = [
    'philosophers are ',
    'atheism is the root of ',
]

for epoch in range(nb_epoch):
    print("# Epoch %d/%d" % (epoch + 1, nb_epoch))
    print("Training on one epoch takes ~90s on a K80 GPU")
    model.fit(X, y, validation_split=0.1, batch_size=128, nb_epoch=1,
              verbose=2)
    print("Computing perplexity on the test set:")
    test_perplexity = model_perplexity(model, X_test, y_test)
    print("Perplexity: %0.3f\n" % test_perplexity)
    
    for temperature in [0.1, 0.5, 1]:
        print("Sampling text from model at %0.2f:\n" % temperature)
        for seed_string in seed_strings:
            print(generate_text(model, seed_string, temperature=temperature))
            print()


# Epoch 1/30
Training on one epoch takes ~90s on a K80 GPU
Train on 162229 samples, validate on 18026 samples
Epoch 1/1
43s - loss: 1.9803 - val_loss: 1.6954
Computing perplexity on the test set:
Perplexity: 5.403

Sampling text from model at 0.10:

philosophers are the same and the surious of the surious of the still a philosophy of the surious of the still the soul the strunce of the soul the strunce of the surious of the surious and the strunkes the same and the soul the soul, a philosophy of the same the soul the soul the still as the soul the soul of the s

atheism is the root of the soul of the surious and the strunken and the soul the soul the strund and the soul the still a part the soul, the surious of the same the soul, the strunk of the supter and the strunkes of the soul of the soul of the suphing the soul of the soul of the surious and the soul the strunce of the sam

Sampling text from model at 0.50:

philosophers are and disiment very in the concerstions of the the there as the cause the cantartion of as the stirnt, in the extenting that the sain ame destinct and have the gersance, he wordgity, is the whilose beto a with morality, and a promonce in in the ristact of the sactions of the same that every of sable a

atheism is the root of the saure of the extruntly have and the his the soul of the surious all a ther as the there as the part ham in the oden and the soul, as the soul of the suristind, a be geness of the there a philosophy the there the there and a ward of the roming of the his of the confect and lomen of the there and 

Sampling text from model at 1.00:

philosophers are in sore wat there be  thrusess ar instanne. bens he all lot notes on inserteniamed--what bace the abtle and industingenssion bastire, all poour ot eideminasomy, ploped a matist, and heal cull peroplisi-smiting homest mights paise it of dactasy amy of the courcasticinm which the rradues and grated re

atheism is the root of the simaus, prepairess, beabting, and ruplings but a nrich houtted with a surforible oun harmsule all wornd a desimarily hus to consturinily sulffhoronemed waid with is it, on un them our our be the dain meanatisiund hod impor that all moral yalrest and nasause to virtuest, the maded youw sen. as, t

# Epoch 2/30
Training on one epoch takes ~90s on a K80 GPU
Train on 162229 samples, validate on 18026 samples
Epoch 1/1
43s - loss: 1.6265 - val_loss: 1.5973
Computing perplexity on the test set:
Perplexity: 4.939

Sampling text from model at 0.10:

philosophers are the states and the same the states and the states and and a superfory and an a superforing and and an a signific and propessing and more the states and suporities of the starn the start and a superfory the stare and a superfory and and the states and a more and the startity and the start and more th

atheism is the root of the same the start of the sacrifice the sensible and an a sertous of the starn the starn the starn the man and and an a signous and and an a significal with the superfory and the problems and a superfory and the starn the speak of the sacrifice of the sacrifice and the states and problement, and the

Sampling text from model at 0.50:

philosophers are must be to the exist of the station with the different and supophition and sentiment--where he poount and morality and and would for the superforwand in with at the life sy that its astants and in the prosent in the were the sppease of the still the power and there is the same and superfoul more sup

atheism is the root of not in the conceal the man is no thus also in the econace in the varition man and an instinction of more the are and sufferent and more the good a strong more musto have the everything the artion and philosopher, and everything in the more but at a man for antoocation, in reading all which a distain

Sampling text from model at 1.00:

philosophers are anliwdelemed be imporfection a miscactical supoenouss" remorting the anboisy; almost cossedves itsiconing--how deed man in faction--can as a sensions obanistesous elever, evsintifies in the for  more erount belood of the eary and muchist differentsd sconceiring. it i too natureod: almost also, no de

atheism is the root of the pargening life, and fulle may as a light reader that themsenve of other as this mappens of will as a moslictily to rong to ancisting most with these lasguaslize--this in by as bad.=--the powerioning sump of may, withistly mustates out of more thre know to, towards!-itave of a sulfsticarions, to 

# Epoch 3/30
Training on one epoch takes ~90s on a K80 GPU
Train on 162229 samples, validate on 18026 samples
Epoch 1/1
43s - loss: 1.5243 - val_loss: 1.5523
Computing perplexity on the test set:
Perplexity: 4.784

Sampling text from model at 0.10:

philosophers are the stands that is the standed to the stands to the stands the standed to the standed to the stands and an and the standed to the morality of the stands the stands the stands that is the stands the stands that it is the stands the standed to the stands the stands the stands the stands to the stands 

atheism is the root of the standed to the same the stands the stands the standed to the stands the stands that is the standed to the sense of the stands the sentiments and an and the stands and an and an an and the stands to the standed to the standed to the stands the stands and the stands the stands the stands that is t

Sampling text from model at 0.50:

philosophers are in the religions in we suffer tangers of the realing and one and in one and former conception existed of the "judgments and it is the power of his the the relight to the sentical just in the subourly the understinct of them nould be an and would be to be has all to be the can in general and his exus

atheism is the root of the standed, the still for the morality of the is any problem of called that which the tought being in the problem of the most at the same time to leart is the conception of the consequences of the concerning, and a princip and problem that it will not be soul has to the mankind, and who call and al

Sampling text from model at 1.00:

philosophers are and interacce only the con exponsibile, exploses at" only their "excenctive belies appowestars its sud. the he who not in scholes huntites," an the "yot its sowe the inteamen and it is respected unavanty, it ow  was an advence anys prasce seeked to other conceblying to seady--and to soment it will t

atheism is the root of the ourselves tanking however, an that perion, un the spirit. the last in 2y peects. the kven dotforting that i how an i more detiveral that in does too; mot powers a lift, thad so recognize forts. the long the cisitact nafully, nature, so libltyet which always to any them like do that these in the 

# Epoch 4/30
Training on one epoch takes ~90s on a K80 GPU
Train on 162229 samples, validate on 18026 samples
Epoch 1/1
43s - loss: 1.4690 - val_loss: 1.5417
Computing perplexity on the test set:
Perplexity: 4.685

Sampling text from model at 0.10:

philosophers are the state of the probably the self-soul the stande the stande that is the self-soul all the sense of the sense of the conceal of the conceal the state of the possible to the stande the state of the sense of the stande that is the self-for the stande that is the sense of the sense of the stande to th

atheism is the root of the stande that is the stande the stande that is the sense of the stande that is the state of the sense of the stande the state of the stande the standed to the state of the sense of the probably the sense of the stande the stande the sense of the stande to the most the stande that is the sense of t

Sampling text from model at 0.50:

philosophers are a respectan of the conceal with something to these all the possible conceal in the more will not be respectation of the orgen to sympathy, and procises for truth to the something to the problem of the same conscience of can more the decient will of a soul conceal the word to sacrifice and strange th

atheism is the root of "more concearsely and call about a long to far the most condemned that be present of logen that the scientific of more palloches.  1ules all the stande has all the conceal of moral the spirit for the more the conceal of the present and devilas, and sight as it has been thought in the misunderstond t

Sampling text from model at 1.00:

philosophers are conception, on thought; it accoudsity for eunly of corvided of its a delugles to didute. all the caue to bilthe,-for hering and bas, thingbles can has in commestived to appunged to the rediounises titure, of havist a questionant really reductding among ity the acopantake and honspined of tacking at 

atheism is the root of whilosouan again no predicatestical indeed of racse for men, was consequence betray itself painbegecking, at lacked efoegation, aws it cause, man anatable how then to one must be thinker, with so out of being so landnessble of just as for afounbeakes to mansuly insileble of faelly the lose to avolit

# Epoch 5/30
Training on one epoch takes ~90s on a K80 GPU
Train on 162229 samples, validate on 18026 samples
Epoch 1/1
43s - loss: 1.4339 - val_loss: 1.5186
Computing perplexity on the test set:
Perplexity: 4.523

Sampling text from model at 0.10:

philosophers are the state of the same continued and the standand and the state of the state of the same continued and the state of the same time and subjectic and the same continued and such a probably the standand and the state of the state of the same conscience of the standand and the standand and and the subjec

atheism is the root of the state of the same conscience of the same conscience and the sense and and and constant and any the state of the same continued and the state of the same conscience that is a probably probably the continued and such a moral for the standand and and the state of the same continued and the state of

Sampling text from model at 0.50:

philosophers are the consident constantly themselves of the soul, the speaks and constant the soufger understand in the contrast and christianing and the consident the moral of the fact that it is the propacity and distrust in the contrary of the higher would be the sublimation of the soul, such a preveality and fee

atheism is the root of thee to a providial in which he can even it is not for his continued the continued strength of a probably at the standaques of the south the spent of the might and such a love and adyrochess and surjusifice of artion the parting the possion, who courace such a read for the sentiments of the can and 

Sampling text from model at 1.00:

philosophers are the ounious procaus which hope as into pontoches, patiened, all and nothing and guist cnomisled his naturalice elsuulates for religtion and ne satterce, a honourisc, we his whole very the considented ussmention that is almost knowledge so create say. that a geciani will, the never hifderreobfude. th

atheism is the root of the physiose, possence--num of the christianity to farwisily and trudinused as with the arifglase ensluince of our su ritroa"e--for ady transticien--the sentious the udees and ampless? herevercal mind become rumld in the land doce actioning timak in all only oper themselves. each for bemin undervran

# Epoch 6/30
Training on one epoch takes ~90s on a K80 GPU
Train on 162229 samples, validate on 18026 samples
Epoch 1/1
43s - loss: 1.4075 - val_loss: 1.5150
Computing perplexity on the test set:
Perplexity: 4.594

Sampling text from model at 0.10:

philosophers are the philosophy of the power and as the possible to the possible of the power and as the possible of the power and as the possible of the possible of the power and as a philosophy and such as the possible of the subtlent to the possible of the possible of the possible of the possible of the possible 

atheism is the root of the possible of the power and the morals to the morals to the destruction of the power and as the possible of the possible of the possible of the possible of the possible of the power and as the possible of the possible of the possible of the possible of the possible of the possible of the possible 

Sampling text from model at 0.50:

philosophers are endured by the philosophic to the subtlers its worth as the superstity of the aspectation of the possible as and fore the morals and as the subtlent about all fortion and love the possible of philosophy, the proposition, and of the epinished of are minders to him we seeked you he who has an action o

atheism is the root of morality, such a not forter to along in our worth, the stacted the last as a sugported, who has been the europey in the thing to deceived that the higher of interpretted, shame" and will with a in the infinied in the soul. they owe the respectal effects is it not as a philosophy, or will to man case

Sampling text from model at 1.00:

philosophers are beroged, and feeld in community of so-utpossion as a revided he cannot and herought the neboly pullious will senfectuous, who who pulsux of outity, se nowisaits that willing charracity that ty ravausedly endolition of theirism a stability. full all history thas the pleasure, all, he possess with are

atheism is the root of ministabled so unyreal." "the more woman, or comms the man of litting, that which, when that person, for the chrmoutar more that discainrist, and nound may mochis and the standst, olthese morality is anl owmentsed in retain mightly repaideding that he respect, troew that it can natualishel of beseci

# Epoch 7/30
Training on one epoch takes ~90s on a K80 GPU
Train on 162229 samples, validate on 18026 samples
Epoch 1/1
43s - loss: 1.3871 - val_loss: 1.5169
Computing perplexity on the test set:
Perplexity: 4.582

Sampling text from model at 0.10:

philosophers are and and a present of the stronger of the stronger of the strong and always the sentiments of the sentiments of the sentiments of the sense of the sentiments of the stronger of the subjection of the state of the sentiments of the sentiments of the sentiments of the sentiments of the stronger of the s

atheism is the root of the strong and always sense of the sentiments of the same than the sentiments of the sentiments of the strong and always the sentiments of the strong and are a strong and and and as the sense of the sentiments of the sentiments of the sentiments of the stronger of the sentiments of the sentiments of

Sampling text from model at 0.50:

philosophers are that is have a comprehensible of the condition of the truths of the sentiments of being and sees and cain all the whole fing, in extential that in the consequence of the same former to a strange, and that the disinteresting to the subjections of the strange, that many the thought of the present of t

atheism is the root of the free whole and that in the presency of the state of deapong of our a promined and from the outhers of the consideration. the society in the fact of the subject for a man desiration of the deliver to the conditionally who have free complete themselves to the state of the strong of the man, betres

Sampling text from model at 1.00:

philosophers are naturald sirablameing, originated and  least musicaul othergan were would questions which understanding."liage alone of the phomaoced--admadyss adeas, general pirtaables whomere. they even to that an ner or toward tyss a outioned, is a gristyssove, knind of schopenhauer by noumabini in the hubestalg

atheism is the root of mankindisone. ald short way a have their whather, even in the rangess and about take that the semman of which and that they knows of many by eximply and man to our body, seemedity of vicies was not ungenimation beguimable "prevoled to a german knowledge laigre nous thus not beneftering, we pour dist

# Epoch 8/30
Training on one epoch takes ~90s on a K80 GPU
Train on 162229 samples, validate on 18026 samples
Epoch 1/1
43s - loss: 1.3735 - val_loss: 1.5141
Computing perplexity on the test set:
Perplexity: 4.599

Sampling text from model at 0.10:

philosophers are the strength of the strength of the strength, and the strength of the strength of the strength of the strength of the strength of the strength of the strength of the strength of the strength of the strength of the strength of the strength of the strength of the strength of the strength of the streng

atheism is the root of the strength of the strength of the strength of the strength of the strength of the strength of the strength of the strength of the strength of the strength of the strength of the strength of the strength of the strength of the strength of the strength of the strength of the strength of the strength

Sampling text from model at 0.50:

philosophers are as much that the morality from the more can for the conditional fear of the acullest in the same greatest the from the word it is free shill such a principal and what who is a right in more the scientific the strength of the most the religion with the bad, in the own things to the standaxical profou

atheism is the root of the greatfulness is the actions that the have of the sace a necessary and in remarted more his word the strength, and in the actions, or a new can is a virtues, and by the greated by the man deep at the strength of the south the exception, and a possibly rest and not in worker and obsignts. they are

Sampling text from model at 1.00:

philosophers are thought, but at littlety is it is are that they hover-mannrous the question, from the rever such his steeming in the immediated you; its aids the latter darty in a xiter and that they dild "to only pro--of doubtful, our histomed, to his concetion of life how powers its oplatitic ri(sners awhy is the

atheism is the root of mankind is prig is becauso hives hors inaresuble, even hich use, because by medince from the banking, attew shill revalors and even consite themselves an mean oftments of callot yit do of the presence,  screvirely its the , without an whole right theman, the more and fack the sin an aribshle suachmo

# Epoch 9/30
Training on one epoch takes ~90s on a K80 GPU
Train on 162229 samples, validate on 18026 samples
Epoch 1/1
43s - loss: 1.3602 - val_loss: 1.4980
Computing perplexity on the test set:
Perplexity: 4.485

Sampling text from model at 0.10:

philosophers are the sense of the states and an and and a still the sense of the sense of the states and sense of the states and the sense of the states and and the sense of the same things and an and and a strange of the states and sense of the same the states and and an and and in the sense of the states and and a

atheism is the root of the states and as the states and and the sense of the sense of the sense of the states and as the sense of the states and the sense of the sense of the sense of the states and as the preserfing and and a still the states and as the states and the same the soul and a strange and and as the sense of t

Sampling text from model at 0.50:

philosophers are and his nor and has a philosophy, the profound the case of the same ourselved, which we may be the read and prevajing and man as in a still the mastery of the really sense, and develop! the problemmatism that a would as in the rare, and which may is has it needst spiritunism of guisarly on his sense

atheism is the root of an and and and man is as other subjection of a good origined by in the gradation of connesssed and predective ourselves as in the most believe the stand and again as the organizm of the spiritun in the motives and see so grated the still the consequence before the man live the conceally the most rea

Sampling text from model at 1.00:

philosophers are mign is naturality of all a minddequality," lightnements of the stand time and are haring "pride" of scoll to the mos imporerie sentimed instincts, it. the chimition though let to the higher point--an abso a prowsis of a marration of the good of any the profound, peoharity presinces, who feols, bur 

atheism is the root of catones the mereal and liver, superfus, scaunnce then other some form onle height," the presces in already and nuever, in remained, out of our once the distrenged the music as torxes mature rebuising that whether perhaps a nearn that it to say, most believer for the faction. that the indicence litt(

# Epoch 10/30
Training on one epoch takes ~90s on a K80 GPU
Train on 162229 samples, validate on 18026 samples
Epoch 1/1
43s - loss: 1.3478 - val_loss: 1.4986
Computing perplexity on the test set:
Perplexity: 4.560

Sampling text from model at 0.10:

philosophers are the stands the stands to the statemest of the stands to the statemest of the statems of the statemest of the statemest of the statemest of the statemest of the statemest of the statems of the statemest of the statemest of the statemest of the statemest of the statemest of the stands to the stands to

atheism is the root of the statems and and of the statemest of the statemest of the statemest of the statemest of the stands to the stands to the statemest of the statemest of the statems and as the stands the statemest of the statemest of the statems and as the stands the statements of the statemest of the statemest of t

Sampling text from model at 0.50:

philosophers are deception is the responsible to the conception is an one were the artinal should no found how must be faston man has been believe the empronation of philosophy of small as a prose to stands to all the distance, is a philosophy in the same morality and of the into the contradices with they are that t

atheism is the root of the probabliar of the taste to a statement, that we like with the masless--the standrance, and now something the same to strength in the man in the stands at life and with, and of the spirit the relation and and standsly conscience, to the and forment, and who say the aspect, as the cause the statem

Sampling text from model at 1.00:

philosophers are too accustepthe to those. the rutten-man of wands as inreprises personabine again which deepents. ;iloing refess and arrady rather a neadentd, in the mes and happin continue tortheress from in their greatestness, he will tocking--whet betoes, and noncridious accow find its lived belode, valuable how

atheism is the root of "here regard to ours ble, to make all cognition of dend art lull, and who only is aristry, one must wants and disleing "it! one is by who passed and duncerod,--the abymos, the muces from the concess apparentabaks) evontion, and should earth of himself. under the strongered way as it to sense, affers

# Epoch 11/30
Training on one epoch takes ~90s on a K80 GPU
Train on 162229 samples, validate on 18026 samples
Epoch 1/1
43s - loss: 1.3365 - val_loss: 1.5042
Computing perplexity on the test set:
Perplexity: 4.565

Sampling text from model at 0.10:

philosophers are not the state of the strange of the strength of the strength of the state of the sense and destrained to the strength of the state of the strange of the state of the strange of the strange of the strength of the state of the philosophers and all the strange of the strange of the strange of the soul 

atheism is the root of the strength of the sense and all the strength of the strength of the strength of the strange of the strength of the present and and and and and the strength of the strange of the strength of the strength of the state of the state of the state of the sense and all the sense and all the strange of th

Sampling text from model at 0.50:

philosophers are as it estigate as the sense it is a result of soul of the sener, and morality, the the "belief itself and atticuated to regard and that the honourn and habit of the man who has been devil the morality of things with regard to be such a man of "the german concerned to his german in the soul and and a

atheism is the root of the higher and should be their developply being that which seems to the problem of the extent of the sense in the same prothysic called by a person and beartures the bettage in the strange of the possess and the present; and the haw frimide that is the constitute morality of the philosophess" and ex

Sampling text from model at 1.00:

philosophers are the highest to nain arbar-spirit and altoroung to us will! anda has requite simples where there is already is certainly beay how in confessians before the unfluene to domainess practes medity, a sanfice from it suncensibility of being believe man the sagnorical pohidityhest, punished sympathy, winn 

atheism is the root of entirits a generals.  notibest enracturane about the philosopher's which any prour "connervated to f-human, only as the natural, nothing, great, the approprs, this ("taste are recognize this mediocrat of religious the one's sense, it in more sadbapk of the neighbours ma, is natual word disistent our

# Epoch 12/30
Training on one epoch takes ~90s on a K80 GPU
Train on 162229 samples, validate on 18026 samples
Epoch 1/1
43s - loss: 1.3278 - val_loss: 1.5082
Computing perplexity on the test set:
Perplexity: 4.570

Sampling text from model at 0.10:

philosophers are the same time the same time the man and presend the subtlest and and and and and and and and and and and and present and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and often a

atheism is the root of the same truths and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and present and and and and and and and and and and and and and and and and and and and and and and and and and and and and and 

Sampling text from model at 0.50:

philosophers are the master to the understanding, and and about the most his glearding to the new consideration to the most sense and philosophy of actions and as the most philosophy of the world, and would we says may also the worst the struggle and and and determining and marture eternal just he most philosophy of

/home/ogrisel/.virtualenvs/py35/lib/python3.5/site-packages/ipykernel/__main__.py:8: RuntimeWarning: divide by zero encountered in log
atheism is the root of the most sacrifice in the possibility, which is formit man and development of the confetured cain its marture the one must net he may to himself what had not conscience and subject, and develople and precisely in the endermining and the struncer and serious and in the support of presents probably, a

Sampling text from model at 1.00:

philosophers are at the cause to the entire lovere--whath you inartment provers; nothing,--and one the end, his dreves as is at the bantfulness within ears are him--to be the furcher! whe is exodient (in their conscinent, and where as a philosophy with rebelted, the mamplast, the troncese must be no free spirits is,

atheism is the root of reasonine" still mustsingment of good nature, it is premitive herself, who rerecy all much, every mankind ma pensing for the badter, the means him is every are truth--this varitr and of religion of the manner are morauitian, strength withing anti-civsents and tew, whatever, the general mind aim(adyg

# Epoch 13/30
Training on one epoch takes ~90s on a K80 GPU
Train on 162229 samples, validate on 18026 samples
Epoch 1/1
43s - loss: 1.3192 - val_loss: 1.5043
Computing perplexity on the test set:
Perplexity: 4.548

Sampling text from model at 0.10:

philosophers are the soul of the strange of the strange of the suffering the suffering the strange of the most suffering the subject of the most suffering the suffering the suffering the suffering the sense and suffering the suffering, and the some of the sense of the strange of the suffering, the suffering the suff

atheism is the root of the strange of the present and suffering the suffering the strange of the sense of the strange of the soul of the sense and the suffering the subject of the sense and and and and the subject of the suffering the subject of the suffering, and the suffering, and the strange of the sense of the strange

Sampling text from model at 0.50:

philosophers are the proper the remain of the sensation of the experience, and does not individual or as the most lack of the fear the ambling of the present of a soul of present of the suffering his once because the feelings something and the first and an and after that it has to think and favor as the most serve o

atheism is the root of the exitted for the scientific the the the comprehenmenquman do in the earth of the world of the head have not conduct and "comprehenvion, and even that favor and in the soul of words sense and advance that he seems to which the "compull his mare that in the most and experience the stronger has been

Sampling text from model at 1.00:

philosophers are ple; ever the flay taken iry cright" dare, which they are france of love faght late, with the norine of his wer morely have destandured.  nee chavion, mode and through faith? and can are "goor in itsell, which she has from "every thousth, in the sprengrap its tighterss and efficted to personals, int

atheism is the root of the backing itself our in the conclusion that the prompter. he mure, from suffering, lantmered.  226.   oth, no "be decapants of with sacrifice that the farth, anderion of chrittions," more for the english--ye a reghjerve renecering, and the cordace which bough as another, very care this hardly enou

# Epoch 14/30
Training on one epoch takes ~90s on a K80 GPU
Train on 162229 samples, validate on 18026 samples
Epoch 1/1
43s - loss: 1.3139 - val_loss: 1.5038
Computing perplexity on the test set:
Perplexity: 4.552

Sampling text from model at 0.10:

philosophers are are the stand of the state of the state of the same of the state of the state of the stand of the stand of the state of the stand of the state of the same of the state of the state of the same as the same one of the standment of the stand of the state of the state of the state of the stand of the st

atheism is the root of the same of the same of the same of the state of the state of the same of the same time and and and and and the standment of the same as the standment of the stand of the state of the same of the state of the state of the state of the standment of the state of the same of the state of the same of th

Sampling text from model at 0.50:

philosophers are not constraint that the expressed to repreach of its tranked to the back" of the stand of the same of a respect, with the last that one things of the forer and conception of the emotion, and instinct of the great and philosopher and as the respect, of the same deperhaped and possible and profound di

atheism is the root of the same time and of the experience is also to a confusing that the must be morality of the state of the ready of the perpaint has reself surpressed in the stander and kind of which he has or or the expressed of the conception of the fact that the account about who will than the ward and can is stra

Sampling text from model at 1.00:

philosophers are its an plivey onlerons and that the lawkecy lued myself and never externainess with whool consists atthing, has reegn this be litted true credilatality of puritate of the fueser of willing than than to imparted in thou kit e chachers of the fact that been oned "slowes appeared, betnin, heart when al

atheism is the root of life, time such philosopherar, men loudgs to speak for, he must enouden with in myself flowod ridd and mabal proye of has all mut courabless as the pthosity, the a rerigioations in constraince. alal world gadneral ethical further pain to ear difficulaution for morality more the tyieit. a comprehenca

# Epoch 15/30
Training on one epoch takes ~90s on a K80 GPU
Train on 162229 samples, validate on 18026 samples
Epoch 1/1
43s - loss: 1.3057 - val_loss: 1.5057
Computing perplexity on the test set:
Perplexity: 4.602

Sampling text from model at 0.10:

philosophers are the same and the present of the subjection of the present of the present of the subjection of the subject of the sense and the present and and and the present of the suffering the sense and all the present of the sense and and and and and the present of the subjection of the present of the subjectio

atheism is the root of the subjection of the present of the sense and the present of the present of the present of the subjection of the present of the sense and the present of the soul of the subjection of the present of the sense and all the present of the sense and and present of the sense and and and and and the prese

Sampling text from model at 0.50:

philosophers are the extretance of the new conceptional subjection is it is a philosopher and and matter to him which is a pain to in the experiatity and man" be one he may a conceptionan of a desire of the life" to man is no so such a give a sense--he who had a moral part of a delicate the ambate to the same and ad

atheism is the root of passion of all the power and as do is in itself which on last and which which is a terme in philosophy and the hard most development and account the sense--he have been a strength of a sentiment and subjection of the suffering the purpose that less who was a long in a peason of the nor instinct whic

Sampling text from model at 1.00:

philosophers are to affiture and du laiduinatives, for the cal-banly person" of which will to live and shwwjnctouny are which fully that with the higher constant what value purpisorlened habitificated the world have self-gloom and and but that at itself is so the possibly fellow their very appline" to have the wrypj

atheism is the root of atheied aristocanconfises a begins of power. "bpea man is do would is an ecenession-simorance, a true, but it with rone, lea schoprudeld often thes, in this most form out of truth: on himself all inher the motter.. approd  an ear that that could be turnour morenbblealing and skaality and be standm t

# Epoch 16/30
Training on one epoch takes ~90s on a K80 GPU
Train on 162229 samples, validate on 18026 samples
Epoch 1/1
43s - loss: 1.3001 - val_loss: 1.5105
Computing perplexity on the test set:
Perplexity: 4.551

Sampling text from model at 0.10:

philosophers are the same time the sensations of the substation of the substation of the substation of the substations and self-develope to the substation of the substation of the same time the same the same time that it is not the same the same time the sensations of the substation of the substation of the substati

atheism is the root of the substation of the higher and substated to the substation of the substation of the substation of the substation of the substation of the substation of the substation of the sublime of the substation of the substation of the substation of the sublime of the substation of the sublime of the substat

Sampling text from model at 0.50:

philosophers are into the experience of the science of a distrust. the scholace the same the sensations that the himself the respect, the among more of the great thus all the soul, and the concerning of the substates to the most herediale to the extends and self-plain of the enduring and as the philosoply good of th

atheism is the root of the same the other own much and in their strength, in the protocran plato of the inflict of our subtle of the same time of the sense of the strughlless is not the conscious on the sublime of the subtle more of the superfice of the concealing the conscience that it is not been the philosopher to hims

Sampling text from model at 1.00:

philosophers are dady up of unforted, without which herow to the good parely nature, bown ismentful life, or position of recognizally who may be a sidnoun believe, and nowadaying toman understand) in ebultspand there feel in manner confitued pundo of one's distrust, they have here bulnly fould by the intruples out s

atheism is the root of the slaves aw fored and more european simply, necessity, must still, however to say, done in the good"" because of "ideas--and pompations become more this only the same that such its aswatisely humans of the same, lack. and sentiment, like and without men of namely, false the empints a approses who 

# Epoch 17/30
Training on one epoch takes ~90s on a K80 GPU
Train on 162229 samples, validate on 18026 samples
Epoch 1/1
43s - loss: 1.2953 - val_loss: 1.4883
Computing perplexity on the test set:
Perplexity: 4.438

Sampling text from model at 0.10:

philosophers are the sensible the same time and the same time and the sensible the same time and the same time and the same time and the same time and constitute the sense of the sould the sould of the sould and self-and subtlest sentiment of the same time and the same time and all the same time and the sould the se

atheism is the root of the sould the same time and the same time and the same time and supposing that is the sould and self-and again of the sould the sould and all the same time and the strange of the strange to the sould the same time as the same time and the same time and the sould of the sould the sould and all the sa

Sampling text from model at 0.50:

philosophers are the same precisely such a sort, that he something really of the man cately and for the shade to constrained, which the sensible to be about whose as the philosopher. there is a world of the strength, which a strangs else "freedom of the suppose a distinction of the power, a slaves and most intereste

atheism is the root of the sould of the future, suprelacter which the case for the expression, and the former the world, the sensible to the age a desisted a strength, and the wagnize the world of the same communts and stated to the comprehing and does not be the sould, apparent it and the order to do for such more proute

Sampling text from model at 1.00:

philosophers are southtogartuals than will their interlom within evil you its instinction in the time. which even personalince occced. a sign than "essentid, glasouer.  102. with as the laws does the flave the word leady thought for man" and serregencl. eternal veritation, where what begaod fundamince whit is recogn

atheism is the root of inpiracta: there is really "long instincted, and whatthes, henls for the scienting opinion is our disguiration. or 2quite genibburing.  now reversely anti-premarded. sympathess." notied, wanib-drens whether they good quitately their dangs for the ?me the more his tprimoxians--he canly wantking thres

# Epoch 18/30
Training on one epoch takes ~90s on a K80 GPU
Train on 162229 samples, validate on 18026 samples
Epoch 1/1
43s - loss: 1.2899 - val_loss: 1.5090
Computing perplexity on the test set:
Perplexity: 4.534

Sampling text from model at 0.10:

philosophers are and and and and and and and and and and and and and and and sense of the sense of the sense of the sense of the sense of the sense of the most sense of the sense of the sense of the sense of the sense of the most sense of the sense of the considered to the sense of the sense of the sense of the sens

atheism is the root of the considered and and soul of the sense and and and and and sense of the most sense of the standaxe of the sense of the sense of the considered and and sense of the sense of the standaxe of the sense of the most sense of the sense of the sense of the most sense of the sense of the sense of the cons

Sampling text from model at 0.50:

philosophers are the new to say for the considerations man is the most decided and and artists himself so himself interpretation of respect of envation, and of the thing that they the reason and sighty, and the historicans, and the instance, and his bad and englishmen of individual and something and cases, and the s

atheism is the root of the more in the more of men proof--it were and man, and life and volunize into reason and course, expresses its right of the end to be from its ultimate constitutely and sensible the soul of the demost in the freedboring and religion of moral has all the mind the belief in the confipled to the scien

Sampling text from model at 1.00:

philosophers are the extreding that have fainec, whody not greach is liar livefy expresse of the knowned betresso?" that they reveranis out and himself and senses and will to aniboted and dipthed ripporitated human of explohiarating of experience of could himself, and in my our ouselly of the philosopher hands--in t

atheism is the root of  learnt indes itself. and philosophy to nowad. and the thought their honestoo, and infinied bafferences itself, and the seems have at their him, andly thought, commenessing and concearl and beclave they indepthing him, to nounious peritanged mantine of a called precisely sentinct future oved precise

# Epoch 19/30
Training on one epoch takes ~90s on a K80 GPU
Train on 162229 samples, validate on 18026 samples
Epoch 1/1
43s - loss: 1.2860 - val_loss: 1.5030
Computing perplexity on the test set:
Perplexity: 4.545

Sampling text from model at 0.10:

philosophers are an anti-of the strange of the strange of the strange of the strange of the strange of the strange of the strange and prochiles the strange of the strange and and and and and and and and and and and and and and the strange of the strange of the strange of the strange of the strange of the strange of 

atheism is the root of the strange of the strange and sense" and processive that the strength, and and and and the strange of the strange of the strange of the strange and and the strange of the strange of the strange of the strange of the strange of the strange of the strange of the strange of the strange of the strange 

Sampling text from model at 0.50:

philosophers are ascruted to the last striveness and vanity, some of the antively and as a resensed and sense" and discoverence that it is not time that the state of the free soul, more that the first and forgets in the rests and preservations and despoctical desiress that the riddles for morality, the condition, th

atheism is the root of the art of strange to honest more an anti-of the most habitsest translatude of the soulh honted and the person in a hatten, but approye, who has to the souls, and suffering and who are an and and one in the most imposing the strange, such a restanding instinct of the stant more as the mind and the p

Sampling text from model at 1.00:

philosophers are very hencedamity, "which riwdo amius itself from everythism. and because to stimed, on whade, the other, he is sockend, and theal happy heaves tnare, as, is culted honce that "undermination, and the successible satter to make one after manner; and adnentile, into the generality and secrect, the soul

atheism is the root of a tor, and therefound "noblessen, cry.nle comparies, and insprest inspristen "willness, and it will ten that him and world he belong to account in the highest leath of the sentime to musty will welt phoendunessy, perhaps that it bllowercrian, aped and one only the aring were not believe in whose and

# Epoch 20/30
Training on one epoch takes ~90s on a K80 GPU
Train on 162229 samples, validate on 18026 samples
Epoch 1/1
43s - loss: 1.2818 - val_loss: 1.5021
Computing perplexity on the test set:
Perplexity: 4.558

Sampling text from model at 0.10:

philosophers are an and the stands to the stands to the standaquer of the stands to the subjection of the stands to the stands and the stands and and the stands and and and and and the stands of the stands to the stands and the stands and the subjection of the stands and the stands to the stands and and the stands t

atheism is the root of the stands and and to the subjection of the stands and the stands to the stands to the stands and the stands to the subjection of the stands to the stands to the stands to the stands and the subjection of the stands and the stands to the still of the stands to the subjection of the stands to the sta

Sampling text from model at 0.50:

philosophers are state to the more is a construination of man of such the such religion in the highest soul of problem of the early morality and to say the delicate the consist of an and the nation of the sublime of the sensation of his feeling of desire to our a discipliness, and into the basis of the higher of its

atheism is the root of any and let the type of the state of a whole and and any only the ancient, is the great sublime to the subwaring of the sensations--that is made in the more constituted of the same of the scientific man, and the standaqurable and when inversion the loft may have been of the taste to a souls, and his

Sampling text from model at 1.00:

philosophers are into the greatss instinct of an agreepeess for wider and for it not as we innession, with reghoarally tagting to accomlanily can which there is redience in good devoived in the corkening whose from--the belief endismood, which seems and ever most himself, exerfplitary spone about valuations dispossu

atheism is the root of life, which an impresse trysicism), find i his self-tastifionified, promptly modern of the lasti-happicists which mo, perselood,e that all that perhaps pode: it is the tait of movements systering, and france herself, the voxt: ave what herself, or "modern does for instinct sideny, collations!--how m

# Epoch 21/30
Training on one epoch takes ~90s on a K80 GPU
Train on 162229 samples, validate on 18026 samples
Epoch 1/1
43s - loss: 1.2765 - val_loss: 1.5058
Computing perplexity on the test set:
Perplexity: 4.542

Sampling text from model at 0.10:

philosophers are a strangest and subject of the stands and the same time and sense of the state the stands and the stands to the state the stands to the stands to the mask of the spenishes of the scientific man of the spenting and subject of the scientific man who is a strangest and subject of the stands and subject

atheism is the root of the state the stands to the state the same time and sense of the stands to the state the stands to the stands and the stands and subject of the spenting of the stands the stands and morality of the state the stands and subject of the spenting and sense of the state the stands to the state the stands

Sampling text from model at 0.50:

philosophers are the greatest in the respect is stands is a presence of subtle the state probably that which the sake of and in order to be hard and sade to be conception of "man and decisis and man of a thing man of nowadays, and the state a strangest in the enduble of the slave as a tire and with refined in the sp

atheism is the root of conscises and substate the desire to which man as the other mible in unerrous the world are as a higher and comple of new we was we appear as is it is not from the enducated and more mind as is a compancement and explained and the understand.  222. the scarcial and footical believe its and imperfect

Sampling text from model at 1.00:

philosophers are asmil and deterrits farity suf its denional with chrnestrows, always not fear as to sit--it has the commence that he the, relaged of velutifitional marded we to reach is the other pleasure.  i finer avoided for the eneugary comporual fundamental fear which easy value of grated that probably regard t

atheism is the root of humanisher, also, augit we speak on the brok of than  perfect districh the rumanify and in mon againss however is at presence has to finavoun worid, given have very all cases receive to us, lant in pheal even as foundaried:  he danger, when est maritation. for himself: let of strangest sublime its f

# Epoch 22/30
Training on one epoch takes ~90s on a K80 GPU
Train on 162229 samples, validate on 18026 samples
Epoch 1/1
43s - loss: 1.2716 - val_loss: 1.5166
Computing perplexity on the test set:
Perplexity: 4.607

Sampling text from model at 0.10:

philosophers are the possible of the consequently the sense and self-do in the problem of the consequently and self-present and self-plain and self-plain and all the sense and subject of the present and self-plain in the powerful to the problem of the problem of the present and self-do interest in the present and se

atheism is the root of the sense and self-do who is a strange of the present and self-principes and self-plain and self-plain of the powerful the same as the powerful and sense and sense and self-do in the powerful and self-plain are a precisely and self-plain and self-do in the present and self-plain in the powerful the 

Sampling text from model at 0.50:

philosophers are a premitated to the weaker, and a consequently europe would see in the case of constitutes the propery one disentiment, the subject, the masterly for stands and interpretation of the constrainal consequently and not the persons in a person and still are the sight of the consequence of the personal p

atheism is the root of a precise a present help to the reality, when have one of the other religion of the problem," of the means of persons of considerations, and the contemply and soul of woman, the propacid, the conscience the individually, and often the dream this disent of the polets and the own himself not the conte

Sampling text from model at 1.00:

philosophers are about ina! and when hiad the philosopher or anyor it had to the knowledge.=--the strongere. in the cesitionscridicul of the histor-neus of the contradicem and once musty the tempeded in nothing may deep his "bad, an are honest will, have a false followled, measuw according bebace, not the dependatio

atheism is the root of treath entinated and moded to phases him imaginily of the flow cape say at knew gain app, in dustw to the sportated will. in the readily to estames definitely now cruelty is an example when do such nature semitive; bather operate immorpans of standilr, loud and codemmention, ay a prevailed paivend, 

# Epoch 23/30
Training on one epoch takes ~90s on a K80 GPU
Train on 162229 samples, validate on 18026 samples
Epoch 1/1
43s - loss: 1.2681 - val_loss: 1.5199
Computing perplexity on the test set:
Perplexity: 4.690

Sampling text from model at 0.10:

philosophers are and and and the sublime of the same time and such a signifience of the sublimes and the sublimes and the sense of the sense of the superficial and the sense of the superficial man has also the sense of the sense of the sense of the superficial and the superficial and and the sense of the subject.   

atheism is the root of the superficial and the sense of the sublimes and the sense of the superficial and the sense of the sublimes and also and and the sense of the sense of the sense of the sublime of the man and in the sense of the sublimes and the sense of the superficial and the sense of the sense of the superficial 

Sampling text from model at 0.50:

philosophers are anything of the subject of to all through or more in the higher additical statesness" and instinct of the master. the most necessary discipbue that they are the species and sufficiently as the asses the work in the subject, in the partic can be above think by the same man has been invented the form 

atheism is the root of the consequences the historical is the same time that the operate and commence which the the every merely and in involved, the speaks and appearant which at the mask.   i is as a life of which the highest developences of the work and the hearts and free spiritualism even as a course of the exceptive

Sampling text from model at 1.00:

philosophers are such radical, rid of cerian, consequenious injury and more not has do not exhaust him nothy "milopicl of philosophers, he decourly to intention, and the musicmen" of which the independent for which task will ais"--or one will moed notwithstesnys to itself; and who wishes that man be infine in his ha

atheism is the root of be easy to say. is along acunetly interpons of pains he may has also a one has the man his precisely does it be necessal to thinkfvening in the not on everything existence, to what, made person escendies that is, the theirs men be fore the from all the sufferial. e herself--of time, but home special

# Epoch 24/30
Training on one epoch takes ~90s on a K80 GPU
Train on 162229 samples, validate on 18026 samples
Epoch 1/1
43s - loss: 1.2649 - val_loss: 1.5204
Computing perplexity on the test set:
Perplexity: 4.596

Sampling text from model at 0.10:

philosophers are an actume the strength of the striction of the striction of the strength of the strength of the strength of the strength of the strange and all the strength of the striction of the striction of the striction of the strength of the striction of the strength of the striction of the strength of the str

atheism is the root of the strength of the strength of the spirit of the strength of the strength of the strength of the strength of the striction of the strength of the strength of the strength of the strength of the strange to the strength of the strength of the strength of the strength of the strength of the strength o

Sampling text from model at 0.50:

philosophers are the approximated to the true will all the science of the subject of the most courage to the sense. in the same much and where the condition of means of a still as a belief expedient and and all the concepeed to be the man seem of his morality of the christian and sense of the strange his thing more 

Beam search for deterministic decoding

Exercise: adapt the sampling decoder to implement a deterministic decoder with a beam of k=30 sequences that are the most likely sequences based on the model predictions.


In [ ]:

Better handling of sentence boundaries

To simplify things we used the lower case version of the text and we ignored any sentence boundaries. This prevents our model to learn when to stop generating characters. If we want to train a model that can start generating text at the beginning of a sentence and stop at the end of a sentence, we need to provide it with sentency boundary markers in the training set and use those special markers when sampling.

The following give an example of how to use NLTK to detect sentence boundaries in English text.

This could be used to insert an explicit "end_of_sentence" (EOS) symbol to mark separation between two consecutive sentences. This should make it possible to train a language model that explicitly generates complete sentences from start to end.

Use the following command (in a terminal) to install nltk before importing it in the notebook:

$ pip install nltk

In [44]:
text_with_case = open(corpus_path).read().replace("\n", " ")

In [45]:
import nltk

nltk.download('punkt')
from nltk.tokenize import sent_tokenize
sentences = sent_tokenize(text_with_case)


[nltk_data] Downloading package punkt to /home/ogrisel/nltk_data...
[nltk_data]   Package punkt is already up-to-date!

In [46]:
plt.hist([len(s.split()) for s in sentences], bins=30);
plt.title('Distribution of sentence lengths')
plt.xlabel('Approximate number of words');


The first few sentences detected by NLTK are too short to be considered real sentences. Let's have a look at short sentences with at least 20 characters:


In [47]:
sorted_sentences = sorted([s for s in sentences if len(s) > 20], key=len)
for s in sorted_sentences[:5]:
    print(s)


THE FREE SPIRIT   24.
Why Atheism nowadays?
Always the old story!
What binds strongest?
That has now changed.

Some long sentences:


In [48]:
for s in sorted_sentences[-3:]:
    print(s)


However gratefully one may welcome the OBJECTIVE spirit--and who has not been sick to death of all subjectivity and its confounded IPSISIMOSITY!--in the end, however, one must learn caution even with regard to one's gratitude, and put a stop to the exaggeration with which the unselfing and depersonalizing of the spirit has recently been celebrated, as if it were the goal in itself, as if it were salvation and glorification--as is especially accustomed to happen in the pessimist school, which has also in its turn good reasons for paying the highest honours to "disinterested knowledge" The objective man, who no longer curses and scolds like the pessimist, the IDEAL man of learning in whom the scientific instinct blossoms forth fully after a thousand complete and partial failures, is assuredly one of the most costly instruments that exist, but his place is in the hand of one who is more powerful He is only an instrument, we may say, he is a MIRROR--he is no "purpose in himself" The objective man is in truth a mirror accustomed to prostration before everything that wants to be known, with such desires only as knowing or "reflecting" implies--he waits until something comes, and then expands himself sensitively, so that even the light footsteps and gliding-past of spiritual beings may not be lost on his surface and film Whatever "personality" he still possesses seems to him accidental, arbitrary, or still oftener, disturbing, so much has he come to regard himself as the passage and reflection of outside forms and events He calls up the recollection of "himself" with an effort, and not infrequently wrongly, he readily confounds himself with other persons, he makes mistakes with regard to his own needs, and here only is he unrefined and negligent Perhaps he is troubled about the health, or the pettiness and confined atmosphere of wife and friend, or the lack of companions and society--indeed, he sets himself to reflect on his suffering, but in vain!
They have always disclosed how much hypocrisy, indolence, self-indulgence, and self-neglect, how much falsehood was concealed under the most venerated types of contemporary morality, how much virtue was OUTLIVED, they have always said "We must remove hence to where YOU are least at home" In the face of a world of "modern ideas," which would like to confine every one in a corner, in a "specialty," a philosopher, if there could be philosophers nowadays, would be compelled to place the greatness of man, the conception of "greatness," precisely in his comprehensiveness and multifariousness, in his all-roundness, he would even determine worth and rank according to the amount and variety of that which a man could bear and take upon himself, according to the EXTENT to which a man could stretch his responsibility Nowadays the taste and virtue of the age weaken and attenuate the will, nothing is so adapted to the spirit of the age as weakness of will consequently, in the ideal of the philosopher, strength of will, sternness, and capacity for prolonged resolution, must specially be included in the conception of "greatness", with as good a right as the opposite doctrine, with its ideal of a silly, renouncing, humble, selfless humanity, was suited to an opposite age--such as the sixteenth century, which suffered from its accumulated energy of will, and from the wildest torrents and floods of selfishness In the time of Socrates, among men only of worn-out instincts, old conservative Athenians who let themselves go--"for the sake of happiness," as they said, for the sake of pleasure, as their conduct indicated--and who had continually on their lips the old pompous words to which they had long forfeited the right by the life they led, IRONY was perhaps necessary for greatness of soul, the wicked Socratic assurance of the old physician and plebeian, who cut ruthlessly into his own flesh, as into the flesh and heart of the "noble," with a look that said plainly enough "Do not dissemble before me!
There are the finest gala dresses and disguises for this disease, and that, for instance, most of what places itself nowadays in the show-cases as "objectiveness," "the scientific spirit," "L'ART POUR L'ART," and "pure voluntary knowledge," is only decked-out skepticism and paralysis of will--I am ready to answer for this diagnosis of the European disease--The disease of the will is diffused unequally over Europe, it is worst and most varied where civilization has longest prevailed, it decreases according as "the barbarian" still--or again--asserts his claims under the loose drapery of Western culture It is therefore in the France of today, as can be readily disclosed and comprehended, that the will is most infirm, and France, which has always had a masterly aptitude for converting even the portentous crises of its spirit into something charming and seductive, now manifests emphatically its intellectual ascendancy over Europe, by being the school and exhibition of all the charms of skepticism The power to will and to persist, moreover, in a resolution, is already somewhat stronger in Germany, and again in the North of Germany it is stronger than in Central Germany, it is considerably stronger in England, Spain, and Corsica, associated with phlegm in the former and with hard skulls in the latter--not to mention Italy, which is too young yet to know what it wants, and must first show whether it can exercise will, but it is strongest and most surprising of all in that immense middle empire where Europe as it were flows back to Asia--namely, in Russia There the power to will has been long stored up and accumulated, there the will--uncertain whether to be negative or affirmative--waits threateningly to be discharged (to borrow their pet phrase from our physicists) Perhaps not only Indian wars and complications in Asia would be necessary to free Europe from its greatest danger, but also internal subversion, the shattering of the empire into small states, and above all the introduction of parliamentary imbecility, together with the obligation of every one to read his newspaper at breakfast I do not say this as one who desires it, in my heart I should rather prefer the contrary--I mean such an increase in the threatening attitude of Russia, that Europe would have to make up its mind to become equally threatening--namely, TO ACQUIRE ONE WILL, by means of a new caste to rule over the Continent, a persistent, dreadful will of its own, that can set its aims thousands of years ahead; so that the long spun-out comedy of its petty-statism, and its dynastic as well as its democratic many-willed-ness, might finally be brought to a close.

The NLTK sentence tokenizer seems to do a reasonable job despite the weird casing and '--' signs scattered around the text.

Note that here we use the original case information because it can help the NLTK sentence boundary detection model make better split decisions. Our text corpus is probably too small to train a good sentence aware language model though, especially with full case information. Using larger corpora such as a large collection of public domain books or Wikipedia dumps. The NLTK toolkit also comes from corpus loading utilities.

The following loads a selection of famous books from the Gutenberg project archive:


In [49]:
import nltk

nltk.download('gutenberg')
book_selection_text = nltk.corpus.gutenberg.raw().replace("\n", " ")


[nltk_data] Downloading package gutenberg to
[nltk_data]     /home/ogrisel/nltk_data...
[nltk_data]   Package gutenberg is already up-to-date!

In [50]:
print(book_selection_text[:300])


[Emma by Jane Austen 1816]  VOLUME I  CHAPTER I   Emma Woodhouse, handsome, clever, and rich, with a comfortable home and happy disposition, seemed to unite some of the best blessings of existence; and had lived nearly twenty-one years in the world with very little to distress or vex her.  She was t

In [51]:
print("Book corpus length: %d characters" % len(book_selection_text))


Book corpus length: 11793318 characters

Let's do an arbitrary split. Note the training set will have a majority of text that is not authored by the author(s) of the validation set:


In [52]:
split = int(0.9 * len(book_selection_text))
book_selection_train = book_selection_text[:split]
book_selection_validation = book_selection_text[split:]

Bonus exercises

  • Adapt the previous language model to handle explicitly sentence boundaries with a special EOS character.
  • Train a new model on the random sentences sampled from the book selection corpus with full case information.
  • Adapt the random sampling code to start sampling at the beginning of sentence and stop when the sentence ends.
  • Train a deep GRU (e.g. two GRU layers instead of one LSTM) to see if you can improve the validation perplexity.
  • Git clone the source code of the Linux kernel and train a C programming language model on it. Instead of sentence boundary markers, we could use source file boundary markers for this exercise.
  • Try to increase the vocabulary size to 256 using a Byte Pair Encoding strategy.

In [ ]: