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 tfkeras
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 different classes already incorporated in Keras:
#load the database from keras
fashionmnist keras.datasets.fashionmnist
trainimages, trainlabelstestimages, testlabels fashionmnist.loaddata
classnames Tshirttop 'Trouser', 'Pullover', 'Dress', 'Coat','Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot'
#Let's check the size of our data:
printtrainimages.shape, trainlabels.shape, testimages.shape, testlabels.shape
#We have x images to learn and images to test after training. Let's have a look at some of the images:
trainimages trainimages
testimages testimages
pltfigurefigsize
for i in range:
pltsubploti
pltxticks
pltyticks
pltgridFalse
pltimshowtrainimagesi cmappltcmbinary
pltxlabelclassnamestrainlabelsi
# Please explain this code:
trlabelsnpzeros
tlabelsnpzeros
for i in rangetrlabels.shape:
trlabelsitrainlabelsi
for i in rangetlabels.shape:
tlabelsitestlabelsi
# Your explanation here
#Now let's create a model in keras:
model keras.Sequential
keras.layers.Flatteninputshape
keras.layers.Dense activationtfnnrelu
keras.layers.Dense activationtfnnsoftmax
#Your explanation of the above code here.
#Let's compile our model:
model.compileoptimizertfoptimizers.Adam
loss'meansquarederror',
metricsaccuracy
#Your explanation of the above code here.
#Let's train our model:
model.fittrainimages, trlabels, epochs
#Your explanation of the above code here.
#Let's test our model:
testloss, testacc model.evaluatetestimages, tlabels
printTest accuracy: testacc
#Your explanation of the above code here.
#Let's make predictions with our model:
predictions model.predicttestimages
#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 plotimagei predictionsarray, truelabel, img:
predictionsarray, truelabel, img predictionsarrayi truelabeli imgi
pltgridFalse
pltxticks
pltyticks
pltimshowimg cmappltcmbinary
predictedlabel npargmaxpredictionsarray
if predictedlabel truelabel:
color 'blue'
else:
color 'red'
pltxlabel:fformatclassnamespredictedlabel
npmaxpredictionsarray
classnamestruelabel
colorcolor
def plotvaluearrayi predictionsarray, truelabel:
predictionsarray, truelabel predictionsarrayi truelabeli
pltgridFalse
pltxticks
pltyticks
thisplot pltbarrange predictionsarray, color#
pltylim
predictedlabel npargmaxpredictionsarray
thisplotpredictedlabelsetcolorred
thisplottruelabelsetcolorblue
# Plot the first X test images, their predicted label, and the true label
# Color correct predictions in blue, incorrect predictions in red
numrows
numcols
numimages numrowsnumcols
pltfigurefigsizenumcols, numrows
for i in rangenumimages:
pltsubplotnumrows, numcols, i
plotimagei predictions, testlabels, testimages
pltsubplotnumrows, numcols, i
plotvaluearrayi predictions, testlabels
#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
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
