SHARE
TWEET

1

a guest Sep 16th, 2019 99 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import numpy as np
  2. import tensorflow as tf
  3.  
  4. batch_size = 1
  5.  
  6. char_encodings = [
  7.     [1, 0, 0, 0, 0, 0, 0, 0],  # ' '
  8.     [0, 1, 0, 0, 0, 0, 0, 0],  # 'h'
  9.     [0, 0, 1, 0, 0, 0, 0, 0],  # 'e'
  10.     [0, 0, 0, 1, 0, 0, 0, 0],  # 'l'
  11.     [0, 0, 0, 0, 1, 0, 0, 0],  # 'o'
  12.     [0, 0, 0, 0, 0, 1, 0, 0],  # 'w'
  13.     [0, 0, 0, 0, 0, 0, 1, 0],  # 'r'
  14.     [0, 0, 0, 0, 0, 0, 0, 1],  # 'd'
  15. ]
  16. encoding_size = np.shape(char_encodings)[1]
  17.  
  18. index_to_char = [' ', 'h', 'e', 'l', 'o', 'w', 'r', 'd']
  19. char_to_index = dict((char, i) for i, char in enumerate(index_to_char))
  20.  
  21. x_train = [[[char_encodings[0], char_encodings[1], char_encodings[2], char_encodings[3], char_encodings[3], char_encodings[4], char_encodings[0], char_encodings[5], char_encodings[4], char_encodings[6], char_encodings[3], char_encodings[7]]]]  # ' hello world'
  22. y_train = [[[char_encodings[1], char_encodings[2], char_encodings[3], char_encodings[3], char_encodings[4], char_encodings[0], char_encodings[5], char_encodings[4], char_encodings[6], char_encodings[3], char_encodings[7], char_encodings[0]]]]  # 'hello world '
  23.  
  24. model = tf.keras.Sequential()
  25. model.add(tf.keras.layers.LSTM(128, input_shape=(None, encoding_size), return_sequences=True))
  26. model.add(tf.keras.layers.Dense(encoding_size, activation='softmax'))
  27.  
  28. optimizer = tf.keras.optimizers.RMSprop(learning_rate=0.05)
  29.  
  30. model.compile(loss=tf.keras.losses.categorical_crossentropy, optimizer=optimizer)
  31.  
  32.  
  33. def on_epoch_end(epoch, data):
  34.     if epoch % 10 == 9:
  35.         print("epoch", epoch)
  36.         print("loss", data['loss'])
  37.  
  38.         # Generate text from the initial text ' h'
  39.         text = ' h'
  40.         for i in range(150):
  41.             x = np.zeros((1, i + 2, encoding_size))
  42.             for t, char in enumerate(text):
  43.                 x[0, t, char_to_index[char]] = 1
  44.             y = model.predict(x)[0][-1]
  45.             text += index_to_char[y.argmax()]
  46.         print(text)
  47.  
  48.  
  49. model.fit(x_train, y_train, batch_size=batch_size, epochs=500, verbose=False, callbacks=[tf.keras.callbacks.LambdaCallback(on_epoch_end=on_epoch_end)])
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top