16-4-RNN-Generate-city-name-LSTM.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. from datasets import Datasets
  2. from exts import sample
  3. import tensorflow as tf
  4. import random
  5. import numpy as np
  6. def main():
  7. # 导入数据
  8. maxlen = 20
  9. x, y, char_idx, file_lines = Datasets.load_us_cities(maxlen=20)
  10. # 建立模型
  11. model = tf.keras.Sequential([
  12. tf.keras.layers.Input(shape=[maxlen, len(char_idx)]),
  13. tf.keras.layers.LSTM(128, return_sequences=True),
  14. tf.keras.layers.Dropout(0.5),
  15. tf.keras.layers.LSTM(128),
  16. tf.keras.layers.Dropout(0.5),
  17. tf.keras.layers.Dense(len(char_idx), activation="softmax")
  18. ])
  19. # 编译模型
  20. model.compile(
  21. optimizer="adam",
  22. loss="categorical_crossentropy",
  23. metrics=["acc"],
  24. )
  25. char_indices = dict((c, i) for i, c in enumerate(char_idx))
  26. indices_char = dict((i, c) for i, c in enumerate(char_idx))
  27. for epoch in range(40):
  28. rand_index = random.randint(0, len(file_lines) - maxlen - 1)
  29. seed = file_lines[rand_index: rand_index + maxlen]
  30. # 训练
  31. model.fit(x, y, epochs=1, batch_size=128)
  32. for diversity in [0.2, 0.5, 1.0, 1.2]:
  33. print("[*]Diversity:", diversity)
  34. generated = ""
  35. for i in range(30):
  36. x_pred = np.zeros((1, maxlen, len(char_idx)))
  37. for t, char in enumerate(seed):
  38. x_pred[0, t, char_indices[char]] = 1.0
  39. preds = model.predict(x_pred, verbose=0)[0]
  40. next_index = sample(preds, diversity)
  41. next_char = indices_char[next_index]
  42. seed = seed[1:] + next_char
  43. generated += next_char
  44. print("[*]Generated: ", generated)
  45. print()
  46. if __name__ == "__main__":
  47. main()