Guest User

Untitled

a guest
Jan 11th, 2018
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.15 KB | None | 0 0
  1. from __future__ import print_function
  2. from keras.callbacks import LambdaCallback
  3. from keras.models import Sequential
  4. from keras.layers import Dense, Activation
  5. from keras.layers import LSTM
  6. from keras.optimizers import RMSprop
  7. from keras.utils.data_utils import get_file
  8. import numpy as np
  9. import random
  10. import sys
  11. import io
  12.  
  13. path = get_file('isokyro.txt', origin='https://punkka.net/.cloud/sananparsi/isokyro.txt')
  14. text = io.open(path, encoding='utf-8').read().lower()
  15. print('corpus length:', len(text))
  16.  
  17. chars = sorted(list(set(text)))
  18. print('total chars:', len(chars))
  19. char_indices = dict((c, i) for i, c in enumerate(chars))
  20. indices_char = dict((i, c) for i, c in enumerate(chars))
  21.  
  22. # cut the text in semi-redundant sequences of maxlen characters
  23. maxlen = 9
  24. step = 1
  25. sentences = []
  26. next_chars = []
  27. for i in range(0, len(text) - maxlen, step):
  28. sentences.append(text[i: i + maxlen])
  29. next_chars.append(text[i + maxlen])
  30. print('nb sequences:', len(sentences))
  31.  
  32. print('Vectorization...')
  33. x = np.zeros((len(sentences), maxlen, len(chars)), dtype=np.bool)
  34. y = np.zeros((len(sentences), len(chars)), dtype=np.bool)
  35. for i, sentence in enumerate(sentences):
  36. for t, char in enumerate(sentence):
  37. x[i, t, char_indices[char]] = 1
  38. y[i, char_indices[next_chars[i]]] = 1
  39.  
  40.  
  41. # build the model: a single LSTM
  42. print('Build model...')
  43. model = Sequential()
  44. model.add(LSTM(128, input_shape=(maxlen, len(chars))))
  45. model.add(Dense(len(chars)))
  46. model.add(Activation('softmax'))
  47.  
  48. optimizer = RMSprop(lr=0.01)
  49. model.compile(loss='categorical_crossentropy', optimizer=optimizer)
  50.  
  51.  
  52. def sample(preds, temperature=1.0):
  53. # helper function to sample an index from a probability array
  54. preds = np.asarray(preds).astype('float64')
  55. preds = np.log(preds) / temperature
  56. exp_preds = np.exp(preds)
  57. preds = exp_preds / np.sum(exp_preds)
  58. probas = np.random.multinomial(1, preds, 1)
  59. return np.argmax(probas)
  60.  
  61.  
  62. def on_epoch_end(epoch, logs):
  63. # Function invoked at end of each epoch. Prints generated text.
  64. print()
  65. print('----- Generating text after Epoch: %d' % epoch)
  66.  
  67. start_index = random.randint(0, len(text) - maxlen - 1)
  68. for diversity in [0.2, 0.5, 1.0, 1.2]:
  69. print('----- diversity:', diversity)
  70.  
  71. generated = ''
  72. sentence = text[start_index: start_index + maxlen]
  73. generated += sentence
  74. print('----- Generating with seed: "' + sentence + '"')
  75. sys.stdout.write(generated)
  76.  
  77. for i in range(400):
  78. x_pred = np.zeros((1, maxlen, len(chars)))
  79. for t, char in enumerate(sentence):
  80. x_pred[0, t, char_indices[char]] = 1.
  81.  
  82. preds = model.predict(x_pred, verbose=0)[0]
  83. next_index = sample(preds, diversity)
  84. next_char = indices_char[next_index]
  85.  
  86. generated += next_char
  87. sentence = sentence[1:] + next_char
  88.  
  89. sys.stdout.write(next_char)
  90. sys.stdout.flush()
  91. print()
  92.  
  93. print_callback = LambdaCallback(on_epoch_end=on_epoch_end)
  94.  
  95. model.fit(x, y,
  96. batch_size=128,
  97. epochs=60,
  98. callbacks=[print_callback])
Advertisement
Add Comment
Please, Sign In to add comment