title: Convert a string to one hot encoding matrix and then feed to neural network
body: <p>I have many DNA sequence data, which has been read into <em>xtrain</em>. Each sample has a label (classification problem), which has been read into <em>ytrain</em>.</p>
  <pre class="lang-py prettyprint-override"><code>tokenizer = keras.preprocessing.text.Tokenizer(char_level=True, lower=True)
  tokenizer.fit_on_texts(&quot;ATCGN&quot;)
  # number of distinct characters, should be 5 in this case
  max_id = len(tokenizer.word_index) 
  print(tokenizer.word_index)
  </code></pre>
  <p>{'a': 1, 't': 2, 'c': 3, 'g': 4, 'n': 5}</p>
  <p>one sequence data looks like: &quot;---ATCGATN---&quot;.</p>
  <p>I want to split each sequence into fixed length (e.g., 4) sub-seq. Take the seq above as an example: &quot;ATCG&quot;, &quot;TCGA&quot;, &quot;CGAT&quot;, &quot;GATN&quot;. Each seq will be represented by one row in the matrix. Then using one hot encoding to represent each character. So, &quot;A&quot; is something like [0,0,0,0,1], &quot;T&quot; is something like [0,0,0,1,0]. Concatenating all encodings for each chars in the sequence gives us the encoding for the sub-seq. So, &quot;ATCG&quot; will be something like [0,0,0,0,1,0,0,0,1,0,0,0,1,0,0,...]
  In this way, each sequence will be turned into a matrix of size (number_of_sub-seq, len_of_sub-seq * 5), where 5 comes from tokenizer.word_index.</p>
  <p>The following code tries to accomplish this. I am pretty new to Tensorflow, so I cannot figure out how to convert each type into others or print out the real values of tensors. For the code <code>[x_encoded] = np.array(tokenizer.texts_to_sequences([x])) - 1</code> gives me an error of AttributeError: 'Tensor' object has no attribute 'lower'.</p>
  <pre class="lang-py prettyprint-override"><code>def seq2mat(x, y):
    x = tf.strings.regex_replace(x, &quot;-&quot;, &quot;&quot;)
    x = tf.strings.regex_replace(x, 'K', 'N')
  
    [x_encoded] = np.array(tokenizer.texts_to_sequences([x])) - 1
    x_dataset = x_dataset.window(kmer_len, shift=3, drop_remainder=True)
    x_flat = x_dataset.flat_map(lambda window: window.batch(kmer_len))
    x_1hot = x_flat.map(lambda kmer: tf.one_hot(kmer, depth=max_id))
    
    # try to stack them into a matrix
    x_np_mat = []
    for item in x_1hot:
      line = np.array(item)
      x_np_mat.append(line.flatten())
    x_np_mat = np.array(x_np_mat)
    return x_np_mat
  
  batch_size = 8
  kmer_len = 8
  train_dataset = tf.data.Dataset.from_tensor_slices((xtrain, ytrain))
  train_data = train_dataset.shuffle(buffer_size=1000, seed=1)
  train_data = train_data.map(seq2mat)
  train_data = train_data.batch(batch_size).prefetch(1)
  </code></pre>