<jupyter_start><jupyter_text>**Author:** <NAME>  Imports<jupyter_code>import tensorflow as tf
print("TensorFlow version:", tf.__version__)
import matplotlib.pyplot as plt
from sklearn.model_selection import KFold
import numpy as np<jupyter_output><empty_output><jupyter_text>Loading Data<jupyter_code>mnist = tf.keras.datasets.fashion_mnist

(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
print('Train: X=%s, y=%s' % (x_train.shape, y_train.shape))
print('Test: X=%s, y=%s' % (x_test.shape, y_test.shape))<jupyter_output>Train: X=(60000, 28, 28), y=(60000,)
Test: X=(10000, 28, 28), y=(10000,)<jupyter_text>EDA<jupyter_code>for i in range(9):
	plt.subplot(330 + 1 + i)
	plt.imshow(x_train[i], cmap=plt.get_cmap('gray'))<jupyter_output><empty_output><jupyter_text>Define Model<jupyter_code>model = tf.keras.models.Sequential([
  tf.keras.layers.Flatten(input_shape=(28, 28)),
  tf.keras.layers.Dense(128, activation='relu'),
  tf.keras.layers.Dense(64, activation='relu'),
  tf.keras.layers.Dense(10),
  tf.keras.layers.Softmax() # is this fine?
])
model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])<jupyter_output><empty_output><jupyter_text>Cross Validation Loop and Model Training<jupyter_code>def cross_val(x, y, n_folds=5):
  scores, histories = list(), list()
  # prepare cross validation
  kfold = KFold(n_folds, shuffle=True, random_state=1)
  counter = 0

  for train_ix, test_ix in kfold.split(x):
    # select rows for train and test
    x_train, y_train, x_test, y_test = x[train_ix], y[train_ix], x[test_ix], y[test_ix]
    # fit model
    history = model.fit(x_train, y_train, epochs=5, validation_data=(x_test, y_test), verbose=2)
    # evaluate model
    _, acc = model.evaluate(x_test, y_test, verbose=2)
    print('\n------------------------------Fold #{counter} Validation Acc {acc}------------------------------\n'.format(counter = counter + 1, acc = acc * 100.0))
    # append scores
    scores.append(acc)
    histories.append(history)
    counter += 1

  return scores, histories
scores, histories = cross_val(x_train, y_train)
test = model.evaluate(x_test,  y_test, verbose=2)<jupyter_output>313/313 - 1s - loss: 0.3727 - accuracy: 0.8897 - 659ms/epoch - 2ms/step<jupyter_text>Metrics<jupyter_code>for i in range(len(histories)):
  # plot loss
  plt.title('Cross Entropy Loss')
  plt.plot(histories[i].history['loss'], color='green', label='train')
  plt.plot(histories[i].history['val_loss'], color='red', label='val')
  plt.legend(['train', 'val'], loc='upper left')
  plt.xlabel('epoch')
  plt.ylabel('loss')
plt.show()
for i in range(len(histories)):
  # plot accuracy
  plt.title('Classification Accuracy')
  plt.plot(histories[i].history['accuracy'], color='green', label='train')
  plt.plot(histories[i].history['val_accuracy'], color='red', label='val')
  plt.legend(['train', 'val'], loc='upper left')
  plt.xlabel('epoch')
  plt.ylabel('acc')
plt.show()<jupyter_output><empty_output><jupyter_text>Analyzing Model Predictions<jupyter_code>def plot_img(i, pred, ground_truth, img):
  pred, ground_truth, img = pred[i], ground_truth[i], img[i]
  plt.grid(False)
  plt.xticks([])
  plt.yticks([])
  
  plt.imshow(img, cmap=plt.cm.binary)

  predicted_label = np.argmax(pred)
  if predicted_label == ground_truth:
    color = 'green'
  else:
    color = 'red'
  
  plt.xlabel("{} {:2.0f}% ({})".format(class_names[predicted_label],
                                100*np.max(pred),
                                class_names[ground_truth]),
                                color=color)
def plot_probas(i, pred, ground_truth):
    pred, ground_truth = pred[i], ground_truth[i]
    plt.grid(False)
    plt.xticks([])
    plt.yticks([])
    plot= plt.bar(range(10), pred, color="black")
    plt.ylim([0,1])
    predicted_label = np.argmax(pred)
    
    plot[predicted_label].set_color('red')
    plot[ground_truth].set_color('blue')
predictions = model.predict(x_test)

class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat', 'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']
num_rows = 5
num_cols = 5
num_images = num_rows*num_cols

plt.figure(figsize=(2*2*num_cols, 2*num_rows))
plt.title("Predictions on test set, DNN")

for i in range(num_images):
  plt.subplot(num_rows, 2*num_cols, 2*i+1)
  plot_img(i, predictions, y_test, x_test)
  plt.subplot(num_rows, 2*num_cols, 2*i+2)
  plot_probas(i, predictions, y_test)<jupyter_output><empty_output>