# PROBLEM- You are provided a Python program- This program downloads 600.docx

22. Feb 2023
1 von 4

### PROBLEM- You are provided a Python program- This program downloads 600.docx

• 1. PROBLEM: You are provided a Python program. This program downloads 60000 images of different types of clothing. The program learns the general features of each clothing type so that when it encounters a clothing item that it has never seen before, it is able to put it in the correct category.The measure of whether it has learned well is done by removing some of the images from learning and using them to check if it can categorize them correctly after learning (validation). The two tests of whether the learning is good are accuracy and loss. Accuracy means it has categorized correctly. Loss means it has categorized incorrectly.Install TensorFlow to use with your Python IDE and run the program. Alternatively, you may create a Google CoLab account and run this program in CoLab. Submit the results (matplotlibcharts) of running the program together with a record of interactive run session.Perform Google searches of installing TensorFlow or creating a CoLab account TURN-IN: Accuracy and Loss charts produced by your program and a record of interactive program run. Python Program: import numpy as np import matplotlib.pyplot as plt import tensorflow as tf from tensorflow import keras from tensorflow.keras import layers from tensorflow.keras import initializers # Model / data parameters num_classes = 10 input_shape = (28, 28, 1) # the data, split between train and test sets (x_train, y_train), (x_test, y_test) = keras.datasets.fashion_mnist.load_data() #x_train = x_train[:5000] #y_train = y_train[:5000] # Scale images to the [0, 1] range x_train = x_train.astype("float32") / 255 x_test = x_test.astype("float32") / 255 # Make sure images have shape (28, 28, 1) x_train = np.expand_dims(x_train, -1) x_test = np.expand_dims(x_test, -1) print("x_train shape:", x_train.shape) print(x_train.shape[0], "train samples") print(x_test.shape[0], "test samples")
• 2. # convert class vectors to binary class matrices y_train = keras.utils.to_categorical(y_train, num_classes) y_test_categorical = keras.utils.to_categorical(y_test, num_classes) print(x_train.shape) print(x_test.shape) x_train = x_train[:,:,:,0] x_test = x_test[:,:,:,0] plt.imshow(x_train[0]) plt.show() fig, axes = plt.subplots(3,3) counter = 0 for x in range(3): for y in range(3): axes[x,y].imshow(x_train[counter]) counter += 1 plt.show() initializer = tf.keras.initializers.GlorotUniform model = tf.keras.models.Sequential([ tf.keras.layers.Flatten(), # converts 28x28 image to a 784 vector tf.keras.layers.Dense(512, activation=tf.nn.relu, kernel_initializer=initializer), tf.keras.layers.Dense(512, activation=tf.nn.relu, kernel_initializer=initializer), #tf.keras.layers.Dense(10, activation=None) tf.keras.layers.Dense(10, activation='softmax', kernel_initializer=initializer) ]) # NOTE comment the following two lines to make the below ones work sample_image = np.expand_dims(x_train[0], 0) print('Predicted label: ', model(sample_image).numpy()) # You can only call model.summary() once you pass something through the model model.summary() for W in model.variables: print(W.name, W.shape) batch_size = 128 epochs = 5 opt=keras.optimizers.Adam(learning_rate=0.001) model.compile( optimizer=opt,
• 3. loss="categorical_crossentropy", metrics=["accuracy"], ) history=model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, validation_split=0.1) # Accuracy plot plt.plot(history.history['accuracy']) plt.plot(history.history['val_accuracy']) plt.title('model accuracy') plt.ylabel('accuracy') plt.xlabel('epoch') plt.legend(['train', 'validation'], loc='upper left') plt.show() # Loss plot plt.plot(history.history['loss']) plt.plot(history.history['val_loss']) plt.title('MODEL LOSS') plt.ylabel('loss') plt.xlabel('epoch') plt.legend(['train', 'validation'], loc='upper left') plt.show() y_pred = [] y_true = [] test_dataset = tf.data.Dataset.from_tensor_slices((x_test, y_test)) test_dataset = test_dataset.batch(32) for images, labels in test_dataset: logits = model(images).numpy() predictions = np.argmax(logits, axis=-1) y_pred.extend(predictions) y_true.extend(labels.numpy()) from sklearn.metrics import accuracy_score acc = accuracy_score(y_true=y_true, y_pred=y_pred) print('Accuracy: %f' % acc) # Visualize predictions that the model got wrong y_pred = np.array(y_pred) y_true = np.array(y_true)
• 4. wrong_idxs = np.nonzero(y_pred - y_true)[0] wrong_idxs = np.random.choice(wrong_idxs, size=9) # select a subset count = 0 fig, axes = plt.subplots(3,3) for x in range(3): for y in range(3): wrong_idx = wrong_idxs[count] count += 1 axes[x,y].imshow(x_test[wrong_idx]) axes[x,y].set_title('True:%d | Pred:%d' % (y_true[wrong_idx], y_pred[wrong_idx])) plt.tight_layout() plt.show() score = model.evaluate(x_test, y_test_categorical, verbose=0) print("Test loss:", score[0]) print("Test accuracy:", score[1])