Question

In: Computer Science

write a fully-connected neural network to work with MNIST and Tensorflow 2.x. The labels will be...

write a fully-connected neural network to work with MNIST and Tensorflow 2.x. The labels will be input and MNIST image vectors as output labels. use tf.GradientTape instead fit. show the 28x28 image with the input vector.

PLEASE WRITE CODE AND DO NOT SENT ME DIFFERENT PARTS TO A WEBSITE.

It is clear, it's a homework assignment.  

Solutions

Expert Solution

#loading library
import tensorflow as tf
from tensorflow.keras.layers import Conv2D, MaxPooling2D, BatchNormalization
from tensorflow.keras.layers import Dropout, Flatten, Input, Dense

#loading dataset
fashion_mnist = tf.keras.datasets.fashion_mnist
(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()

#Normalization of dataset
train_images = train_images / 255.0
test_images = test_images / 255.0


#creating the model
def create_model():
    
    def add_conv_block(model, num_filters):
        
        model.add(Conv2D(num_filters, 3, activation='relu', padding='same'))
        model.add(BatchNormalization())
        model.add(Conv2D(num_filters, 3, activation='relu', padding='valid'))
        model.add(MaxPooling2D(pool_size=2))
        model.add(Dropout(0.2))

        return model
    
    model = tf.keras.models.Sequential()
    model.add(Input(shape=(28, 28, 3)))
    
    model = add_conv_block(model, 32)
    model = add_conv_block(model, 64)
    model = add_conv_block(model, 128)

    #flattening the model
    model.add(Flatten())
    model.add(Dense(3, activation='softmax'))

    #compiling the model
    model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
    return model

model = create_model()
model.summary()

#fitting the training dataset
model.fit(train_images, train_labels, epochs=10)

test_loss, test_acc = model.evaluate(test_images,  test_labels, verbose=2)
print('\nTest accuracy:', test_acc)

Related Solutions

JAVA Program: Train a 3 layer neural network to recognize MNIST handwritten digit set. The network...
JAVA Program: Train a 3 layer neural network to recognize MNIST handwritten digit set. The network must use back propagation and Stochastic Gradient algorithm. NO third party libraries allowed(from scratch). Assume the mnist data is in two csv files: mnist_train.csv and mnist_test.csv. After each training epoch, The program should (1) print out statistics for each of the ten digits, the number of recognized inputs over total number of occurences of that digit (2) the overall accuracy considering all ten digits.
Write a neural network in python for multiclass classification of an imbalanced dataset that makes a...
Write a neural network in python for multiclass classification of an imbalanced dataset that makes a successful model that both trains and evaluates the model and prints the Accuracy, Precision, Recall, and F1 score. Data will be in the form of a CSV file with 600,000 samples (or rows in the CSV) of 15 classes (for y_train and y_test) and 78 input dimensions/features. The imbalance will have some classes that have a low of only 8 samples taken. Your job...
Consider a Convolutional Neural Network which accepts a 120 x 120 CMYK image as input. The...
Consider a Convolutional Neural Network which accepts a 120 x 120 CMYK image as input. The network has a series of 5 convolutional layers, where the parameters in conv-layer 3 and conv-layer 5 are shared. Each conv-layer has 20 3x3 filters. The output of the last conv-layer is flattened and passed through a fully connected layer with 30 neurons, which is then passed through another fully connected layer of 10 neurons. Each neuron in the fully connected layers and each...
In this assignment you will start with Python code that builds a 2 host network connected...
In this assignment you will start with Python code that builds a 2 host network connected by a legacy router. You will modify it such that the two hosts can send packets to each other. It requires that you understand subnet addressing and the function of a router. The network built using Miniedit on a Mininet virtual machine: python mininet/examples/miniedit.py. We need to also make 6 static routes to make the program work. This is the work that I have...
Create the appropriate 2 x 2 table for the data. Include appropriate row and column labels and complete the table with information provided in the scenario.
A. Researchers conducted a case control study to identify the association between alcohol consumption and esophageal cancer among men. They enrolled 187 men with esophageal cancer and 715 men without esophageal cancer. The men were asked about their regular alcohol consumption and the men were categorized as either drinking ≥80 g/day (exposed) or
Write out the addition and multiplication tables for the ring Z2[x]/(x^2 + x). Is Z2[x]/(x^2 +...
Write out the addition and multiplication tables for the ring Z2[x]/(x^2 + x). Is Z2[x]/(x^2 + x) a field?
Q.  Show that the ellipse x^2/4+ y^2/9= 1 in R2 is connected, and show that the function...
Q.  Show that the ellipse x^2/4+ y^2/9= 1 in R2 is connected, and show that the function f(x, y) = 3 x^3− 5y^3 − 4 has a zero on the above ellipse.
Find the equation of the tangent line at x=2 to the graph of y= x^2-x-7 Write...
Find the equation of the tangent line at x=2 to the graph of y= x^2-x-7 Write your answer as a simplified slope-intercept equation y=mx+b. For example   y=7x-8
1. Write the set { x | x ∈ R, x2 = 4 or x 2...
1. Write the set { x | x ∈ R, x2 = 4 or x 2 = 9} in list form. 2. {x: x is a real number between 1 and 2} is an a) finite set b) empty set c) infinite set 3. Write set {1, 5, 15, 25,…} in set-builder form. 4. What is the cardinality of each of these sets? a) {{a}} b) {a, {a}} c) {a, {a}, {a, {a}}} d) {∅} e) {∅, {∅}, {∅, {∅}}}...
x+1 men will do the work in x+1 days, find the number of days that x+2 men can do the same work in.
x+1 men will do the work in x+1 days, find the number of days that x+2 men can do the same work in.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT