title: KeyError: '' in python/tensorflow
body: <p>I am trying to make a text generator code, but the following line keeps giving me an error:</p>
  <pre><code>X[0, (sequence_length - len(seed)) + t, char2int[char]] = 1
  </code></pre>
  <p>The error that this line gives me:</p>
  <pre><code>Traceback (most recent call last):
    File &quot;C:\Users\m8\Desktop\programing_stuff\python stuff\text generator\app.py&quot;, line 65, in &lt;module&gt;
      X[0, (sequence_length - len(seed)) + t, char2int[char]] = 1
  KeyError: '3'
  </code></pre>
  <p>It was working so far, and i didn't change the code so i don't know whats the issue.
  I think the keyError is coming from the randomly generated seed but i don't know how to fix the problem</p>
  <p>full code(note: this is only the code for the generation, the other script for creating the .pickle file is separate, but i don't use any other variables from it):</p>
  <pre><code>import numpy as np
  import pickle
  import tqdm
  from tensorflow.keras.models import Sequential
  from tensorflow.keras.layers import Dense, LSTM, Dropout, Activation
  import os
  import string
  import random
  import time
  from PIL import Image, ImageFont, ImageDraw
  import textwrap
  
  
  
  sequence_length = 100
  # dataset file path
  FILE_PATH = &quot;data/shrek.txt&quot;
  # FILE_PATH = &quot;data/python_code.py&quot;
  BASENAME = os.path.basename(FILE_PATH)
  
  
  S = random.randint(5, 20)
  ran = ''.join(random.choices(string.ascii_uppercase + string.digits, k = S))
  
  seed = str(ran).lower()
  
  # load vocab dictionaries
  char2int = pickle.load(open(f&quot;{BASENAME}-char2int.pickle&quot;, &quot;rb&quot;))
  int2char = pickle.load(open(f&quot;{BASENAME}-int2char.pickle&quot;, &quot;rb&quot;))
  vocab_size = len(char2int)
  
  
  # building the model
  model = Sequential([
      LSTM(256, input_shape=(sequence_length, vocab_size), return_sequences=True),
      Dropout(0.3),
      LSTM(256),
      Dense(vocab_size, activation=&quot;softmax&quot;),
  ])
  
  
  # load the optimal weights
  model.load_weights(f&quot;results/{BASENAME}-{sequence_length}.h5&quot;)
  
  
  s = seed
  n_chars = 120
  generated = &quot;&quot;
  for i in tqdm.tqdm(range(n_chars), &quot;Generating text&quot;):
      # make the input sequence
      X = np.zeros((1, sequence_length, vocab_size))
      for t, char in enumerate(seed):
          X[0, (sequence_length - len(seed)) + t, char2int[char]] = 1
      # predict the next character
      predicted = model.predict(X, verbose=0)[0]
      # converting the vector to an integer
      next_index = np.argmax(predicted)
      # converting the integer to a character
      next_char = int2char[next_index]
      # add the character to results
      generated += next_char
      # shift seed and the predicted character
      seed = seed[1:] + next_char
  
  print(&quot;Seed:&quot;, s)
  print(&quot;Generated text:&quot;)
  
  
  if generated[0] == &quot; &quot;:
      new_string = generated[1:]
      generated = new_string
  
  if generated[-2] == &quot; &quot;:
      new_string = generated[:-1]
      generated = new_string
  
  generated = ''.join(('&quot;',generated,'&quot;'))
  
  print(generated)
  </code></pre>