input: Write a function to implement a Machine Learning model in Python that can recognize handwritten digits from 0 to 9. The function should load the MNIST dataset, preprocess the data, define and compile a neural network model, and then fit the model. The function should return the trained model.
  assert train_digit_recognizer().history['val_accuracy'][-1] >= 0.92
  assert train_digit_recognizer().history['accuracy'][-1] >= 0.95
  assert train_digit_recognizer().history['val_loss'][-1] < 0.2
  assert train_digit_recognizer().history['loss'][-1] < 0.1
output: def train_digit_recognizer():
      from tensorflow.keras.datasets import mnist
      from tensorflow.keras.utils import to_categorical
      from tensorflow.keras.models import Sequential
      from tensorflow.keras.layers import Dense, Flatten
      from tensorflow.keras.optimizers import Adam
  
      (x_train, y_train), (x_test, y_test) = mnist.load_data()
      x_train = x_train.reshape((x_train.shape[0], 28, 28, 1)).astype('float32') / 255
      x_test = x_test.reshape((x_test.shape[0], 28, 28, 1)).astype('float32') / 255
      y_train = to_categorical(y_train)
      y_test = to_categorical(y_test)
  
      model = Sequential()
      model.add(Flatten(input_shape=(28, 28, 1)))
      model.add(Dense(128, activation='relu'))
      model.add(Dense(10, activation='softmax'))
      model.compile(optimizer=Adam(), loss='categorical_crossentropy', metrics=['accuracy'])
  
      history = model.fit(x_train, y_train, validation_data=(x_test, y_test), epochs=10, batch_size=128)
      return history