Q:

Error when checking target: expected dense to have 3 dimensions, but got array with shape (32, 200)

I'm trying to modify the example on https://www.tensorflow.org/tutorials/sequences/text_generation for character based text generation.
The code on the example uses Tensorflow Eager Execution (via tensorflow.enable_eager_execution)and runs fine with it, but if I disable eager execution, i start getting this error:

Error when checking target: expected dense to have 3 dimensions, but got array with shape (32, 200)

Why is that so? Shouldn't the code work identically with or without Eager enabled? 
I tried flattening the output of the LSTM layer, but i get a similar error: 

ValueError: Error when checking target: expected dense to have shape (1,) but got array with shape (200,)

The simplest code i could make is as follows:
import tensorflow as tf
import numpy as np

# tf.enable_eager_execution()

def get_input():
    path_to_file = tf.keras.utils.get_file(
        'shakespeare.txt',
        'https://storage.googleapis.com/download.tensorflow.org/data/shakespeare.txt'
    )
    with open(path_to_file) as f:
        text = f.read()
    return text

def get_dataset(text_as_indexes, sequence_size, sequences_per_batch):
    def split_input(sequence):
        return sequence[:-1], sequence[1:]

    data_set = tf.data.Dataset.from_tensor_slices(text_as_indexes)
    data_set = data_set.batch(sequence_size + 1, drop_remainder=True)
    data_set = data_set.map(split_input)
    data_set = data_set.shuffle(10000).batch(sequences_per_batch, drop_remainder=True)
    return data_set

if __name__ == '__main__':
    sequences_len = 200
    batch_size = 32
    embeddings_size = 64
    rnn_units = 128

    text = get_input()
    vocab = sorted(set(text))
    vocab_size = len(vocab)

    char2int = {c: i for i, c in enumerate(vocab)}
    int2char = np.array(vocab)
    text_as_int = np.array([char2int[c] for c in text])

    dataset = get_dataset(text_as_int, sequences_len, batch_size)
    steps_per_epoch = len(text_as_int) // sequences_len // batch_size

    model = tf.keras.models.Sequential()
    model.add(tf.keras.layers.Embedding(
        input_dim=vocab_size,
        output_dim=embeddings_size,
        input_length=sequences_len))

    model.add(tf.keras.layers.LSTM(
        units=rnn_units,
        return_sequences=True))

    model.add(tf.keras.layers.Dense(units=vocab_size, activation='softmax'))

    model.compile(optimizer=tf.train.AdamOptimizer(),
                  loss='sparse_categorical_crossentropy')

    model.summary()
    model.fit(
        x=dataset.repeat(),
        batch_size=batch_size,
        steps_per_epoch=steps_per_epoch)

A:

When using sparse_categorical_crossentropy your labels should be of shape (batch_size, sequence_length, 1) instead of simply (batch_size, sequence_length). You can fix this by
reshaping the labels in your split_input() function as follows:
def split_input(sequence):
    return sequence[:-1], tf.reshape(sequence[1:], (-1,1))

The above code works for both eager execution and normal execution.

