content: #  Copyright (c) ZenML GmbH 2022. All Rights Reserved.
  #
  #  Licensed under the Apache License, Version 2.0 (the "License");
  #  you may not use this file except in compliance with the License.
  #  You may obtain a copy of the License at:
  #
  #       https://www.apache.org/licenses/LICENSE-2.0
  #
  #  Unless required by applicable law or agreed to in writing, software
  #  distributed under the License is distributed on an "AS IS" BASIS,
  #  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
  #  or implied. See the License for the specific language governing
  #  permissions and limitations under the License.
  
  import os
  
  import numpy as np
  import tensorflow as tf
  
  from zenml import get_step_context, step
  
  
  @step(enable_cache=True)
  def trainer(
      X_train: np.ndarray,
      y_train: np.ndarray,
      epochs: int = 5,
      lr: float = 0.001,
  ) -> tf.keras.Model:
      """Train a neural net from scratch to recognize MNIST digits return our
      model or the learner."""
      model = tf.keras.Sequential(
          [
              tf.keras.layers.Flatten(input_shape=(28, 28)),
              tf.keras.layers.Dense(10),
          ]
      )
  
      context = get_step_context()
      log_dir = os.path.join(context.get_output_artifact_uri(), "logs")
      tensorboard_callback = tf.keras.callbacks.TensorBoard(
          log_dir=log_dir, histogram_freq=1
      )
  
      model.compile(
          optimizer=tf.keras.optimizers.Adam(lr),
          loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
          metrics=["accuracy"],
      )
  
      model.fit(
          X_train,
          y_train,
          epochs=epochs,
          callbacks=[tensorboard_callback],
      )
  
      return model
  