Question: PLEASE HELP IN PYTHON I need to answere the questions in the code explaining how the sections in the code work. Thank you # TensorFlow

PLEASE HELP IN PYTHON
I need to answere the questions in the code explaining how the sections in the code work. Thank you
# TensorFlow and tf.keras
import tensorflow as tf
from tensorflow import keras
import numpy as np
import matplotlib.pyplot as plt
#We want to implement a neural network to solve a particular classification problem using high level libraries (Tensorflow, keras). Please refer to any tutorial about Keras (on the web) to explain the chunks of code below, every time you are asked to leave an explanation.
#For this problem let's load the database Fashion (with 10 different classes) already incorporated in Keras:
#load the database from keras
fashion_mnist = keras.datasets.fashion_mnist
(train_images, train_labels),(test_images, test_labels)= fashion_mnist.load_data()
class_names =['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat','Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']
#Let's check the size of our data:
print(train_images.shape, train_labels.shape, test_images.shape, test_labels.shape)
#We have 60000028x28 images to learn and 100000 images to test after training. Let's have a look at some of the images:
train_images = train_images /255.0
test_images = test_images /255.0
plt.figure(figsize=(10,10))
for i in range(25):
plt.subplot(5,5,i+1)
plt.xticks([])
plt.yticks([])
plt.grid(False)
plt.imshow(train_images[i], cmap=plt.cm.binary)
plt.xlabel(class_names[train_labels[i]])
# Please explain this code:
tr_labels=np.zeros((60000,10))
t_labels=np.zeros((10000,10))
for i in range(tr_labels.shape[0]):
tr_labels[i,train_labels[i]]=1
for i in range(t_labels.shape[0]):
t_labels[i,test_labels[i]]=1
# Your explanation here
#Now let's create a model in keras:
model = keras.Sequential([
keras.layers.Flatten(input_shape=(28,28)),
keras.layers.Dense(128, activation=tf.nn.relu),
keras.layers.Dense(10, activation=tf.nn.softmax)
])
#Your explanation of the above code here.
#Let's compile our model:
model.compile(optimizer=tf.optimizers.Adam(),
loss='mean_squared_error',
metrics=['accuracy'])
#Your explanation of the above code here.
#Let's train our model:
model.fit(train_images, tr_labels, epochs=5)
#Your explanation of the above code here.
#Let's test our model:
test_loss, test_acc = model.evaluate(test_images, t_labels)
print('Test accuracy:', test_acc)
#Your explanation of the above code here.
#Let's make predictions with our model:
predictions = model.predict(test_images)
#Ready! This is how to train a Neural Network, using TensorFlow and Keras. Now let's see graphically the results of our predictions. So, the following code has nothing to do with Neural Network just with plotting the results. Do not worry about it!
def plot_image(i, predictions_array, true_label, img):
predictions_array, true_label, img = predictions_array[i], true_label[i], img[i]
plt.grid(False)
plt.xticks([])
plt.yticks([])
plt.imshow(img, cmap=plt.cm.binary)
predicted_label = np.argmax(predictions_array)
if predicted_label == true_label:
color = 'blue'
else:
color = 'red'
plt.xlabel("{}{:2.0f}%({})".format(class_names[predicted_label],
100*np.max(predictions_array),
class_names[true_label]),
color=color)
def plot_value_array(i, predictions_array, true_label):
predictions_array, true_label = predictions_array[i], true_label[i]
plt.grid(False)
plt.xticks([])
plt.yticks([])
thisplot = plt.bar(range(10), predictions_array, color="#777777")
plt.ylim([0,1])
predicted_label = np.argmax(predictions_array)
thisplot[predicted_label].set_color('red')
thisplot[true_label].set_color('blue')
# Plot the first X test images, their predicted label, and the true label
# Color correct predictions in blue, incorrect predictions in red
num_rows =5
num_cols =3
num_images = num_rows*num_cols
plt.figure(figsize=(2*2*num_cols, 2*num_rows))
for i in range(num_images):
plt.subplot(num_rows, 2*num_cols, 2*i+1)
plot_image(i, predictions, test_labels, test_images)
plt.subplot(num_rows, 2*num_cols, 2*i+2)
plot_value_array(i, predictions, test_labels)
#Your explanation (interpretation) of the above results here!

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!