SlideShare a Scribd company logo
1 of 4
Download to read offline
X_train original shape (60000, 28, 28)

y_train original shape (60000,)

X_test original shape (10000, 28, 28)

y_test original shape (10000,)

Text(0.5, 1.0, '5')
(5, array([0., 0., 0., 0., 0., 1., 0., 0., 0., 0.], dtype=float32))
In [1]: import numpy as np

import pandas as pd

import matplotlib.pyplot as plt

%matplotlib inline

from keras.datasets import mnist

from keras.models import Sequential

from keras.layers import Dense, Dropout, Activation

from keras.utils import np_utils



np.random.seed(35)

In [2]: (X_train, y_train), (X_test, y_test) = mnist.load_data()

print("X_train original shape", X_train.shape)

print("y_train original shape", y_train.shape)

print("X_test original shape", X_test.shape)

print("y_test original shape", y_test.shape)

In [3]: plt.imshow(X_train[0], cmap='gray')

plt.title(y_train[0])

Out[3]:
In [4]: X_train = X_train.reshape(60000,784)

X_test = X_test.reshape(10000,784)



X_train = X_train.astype('float32')

X_test = X_test.astype('float32')



X_train/=255

X_test/=255

In [5]: number_of_classes = 10



Y_train = np_utils.to_categorical(y_train, number_of_classes)

Y_test = np_utils.to_categorical(y_test, number_of_classes)



y_train[0], Y_train[0]

Out[5]:
((50000, 784), (10000, 784))
Epoch 1/5

391/391 [==============================] - 59s 34ms/step - loss: 0.5052 - accuracy:
0.8394 - val_loss: 0.1180 - val_accuracy: 0.9640

Epoch 2/5

391/391 [==============================] - 12s 30ms/step - loss: 0.1169 - accuracy:
0.9646 - val_loss: 0.1011 - val_accuracy: 0.9705

Epoch 3/5

391/391 [==============================] - 12s 30ms/step - loss: 0.0794 - accuracy:
0.9744 - val_loss: 0.0913 - val_accuracy: 0.9741

Epoch 4/5

391/391 [==============================] - 12s 30ms/step - loss: 0.0661 - accuracy:
0.9792 - val_loss: 0.0850 - val_accuracy: 0.9780

Epoch 5/5

391/391 [==============================] - 12s 30ms/step - loss: 0.0545 - accuracy:
0.9824 - val_loss: 0.0875 - val_accuracy: 0.9783

In [6]: #shuffle the training set

for _ in range(5): 

indexes = np.random.permutation(len(X_train))



X_train = X_train[indexes]

Y_train = Y_train[indexes]



#set aside 10,000 for validation

val_images = X_train[:10000,:]

val_labels = Y_train[:10000,:]



# leave rest in training set

train_images = X_train[10000:,:]

train_labels = Y_train[10000:,:]



train_images.shape, val_images.shape

Out[6]:
In [7]: model = Sequential()



model.add(Dense(512, input_dim=784))

# An "activation" is just a non-linear function applied to the output

# of the layer above. Here, with a "rectified linear unit",

# we clamp all values below 0 to 0.

model.add(Activation('relu'))

# Dropout helps protect the model from memorizing or "overfitting" the training data
model.add(Dropout(0.2))



model.add(Dense(512))

model.add(Activation('relu'))

model.add(Dropout(0.2))



model.add(Dense(512))

model.add(Activation('relu'))

model.add(Dropout(0.2))



model.add(Dense(10))

# This special "softmax" activation among other things,

# ensures the output is a valid probaility distribution, that is

# that its values are all non-negative and sum to 1.

model.add(Activation('softmax'))

In [8]: model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'
In [9]: history = model.fit(train_images, train_labels, epochs=5, batch_size=128,

validation_data=(val_images, val_labels))

In [10]: train_loss = history.history['loss']
<Figure size 432x288 with 0 Axes>
val_loss = history.history['val_loss']

In [11]: epochs = range(1, len(history.history['loss']) + 1)

In [13]: plt.plot(epochs, train_loss, 'bo', label='Training loss')

plt.plot(epochs, val_loss, 'g', label='Validation loss')

plt.title('Training and Validation Losses')

plt.xlabel('Epochs')

plt.ylabel('Loss')

plt.legend()



plt.show()

plt.savefig('Results/6_1_lossplot.png')

In [14]: train_accuracy = history.history['accuracy']

val_accuracy = history.history['val_accuracy']

In [15]: epochs = range(1, len(history.history['accuracy']) + 1)

In [16]: plt.plot(epochs, train_accuracy, 'bo', label='Training Accuracy')

plt.plot(epochs, val_accuracy, 'g', label='Validation Accuracy')

plt.title('Training and Validation Accuracy')

plt.xlabel('Epochs')

plt.ylabel('Accuracy')

plt.legend()



plt.show()

plt.savefig('results/6_1_accuracyplot.png')
<Figure size 432x288 with 0 Axes>
313/313 [==============================] - 2s 5ms/step - loss: 0.0770 - accuracy: 0.
9792

Test accuracy: 0.979200005531311

Actual Predictions

0 7 7

1 2 2

2 1 1

3 0 0

4 4 4

... ... ...

9995 2 2

9996 3 3

9997 4 4

9998 5 5

9999 6 6

[10000 rows x 2 columns]

In [17]: score = model.evaluate(X_test, Y_test)

print()

print('Test accuracy: ', score[1])

In [18]: predictions = np.argmax(model.predict(X_test), axis=1)


predictions = list(predictions)

actuals = list(y_test)



pred_res = pd.DataFrame({'Actual': actuals, 'Predictions': predictions})

pred_res.to_csv('results/6_1_predictions.csv', index=False)

print (pred_res)

In [19]: # save model

model.save('results/6_1_model.h5')

In [20]: #Metrics output

with open('results/6_1_metrics.txt', 'w') as f:
f.write('Training Loss: {}'.format(str(history.history['loss'])))

f.write('nTraining Accuracy: {}'.format(str(history.history['accuracy'])))

f.write('nTest Loss: {}'.format(score[0]))

f.write('nTest Accuracy: {}'.format(score[1]))

In [ ]:

More Related Content

Similar to Assignment 6.1.pdf

Image classification using cnn
Image classification using cnnImage classification using cnn
Image classification using cnnDebarko De
 
maXbox starter69 Machine Learning VII
maXbox starter69 Machine Learning VIImaXbox starter69 Machine Learning VII
maXbox starter69 Machine Learning VIIMax Kleiner
 
Assignment 6.2a.pdf
Assignment 6.2a.pdfAssignment 6.2a.pdf
Assignment 6.2a.pdfdash41
 
How to use SVM for data classification
How to use SVM for data classificationHow to use SVM for data classification
How to use SVM for data classificationYiwei Chen
 
The Ring programming language version 1.2 book - Part 79 of 84
The Ring programming language version 1.2 book - Part 79 of 84The Ring programming language version 1.2 book - Part 79 of 84
The Ring programming language version 1.2 book - Part 79 of 84Mahmoud Samir Fayed
 
AIML4 CNN lab256 1hr (111-1).pdf
AIML4 CNN lab256 1hr (111-1).pdfAIML4 CNN lab256 1hr (111-1).pdf
AIML4 CNN lab256 1hr (111-1).pdfssuserb4d806
 
Intelligent System Optimizations
Intelligent System OptimizationsIntelligent System Optimizations
Intelligent System OptimizationsMartin Zapletal
 
From Tensorflow Graph to Tensorflow Eager
From Tensorflow Graph to Tensorflow EagerFrom Tensorflow Graph to Tensorflow Eager
From Tensorflow Graph to Tensorflow EagerGuy Hadash
 
Testing in those hard to reach places
Testing in those hard to reach placesTesting in those hard to reach places
Testing in those hard to reach placesdn
 
I want you to add the output of the F1 score- Precision- ROC AUC- and.pdf
I want you to add the output of the F1 score- Precision- ROC AUC- and.pdfI want you to add the output of the F1 score- Precision- ROC AUC- and.pdf
I want you to add the output of the F1 score- Precision- ROC AUC- and.pdfGordonF2XPatersonh
 
The Ring programming language version 1.7 book - Part 91 of 196
The Ring programming language version 1.7 book - Part 91 of 196The Ring programming language version 1.7 book - Part 91 of 196
The Ring programming language version 1.7 book - Part 91 of 196Mahmoud Samir Fayed
 
Competition 1 (blog 1)
Competition 1 (blog 1)Competition 1 (blog 1)
Competition 1 (blog 1)TarunPaparaju
 
Deep Learning with Julia1.0 and Flux
Deep Learning with Julia1.0 and FluxDeep Learning with Julia1.0 and Flux
Deep Learning with Julia1.0 and FluxSatoshi Terasaki
 
The Ring programming language version 1.9 book - Part 99 of 210
The Ring programming language version 1.9 book - Part 99 of 210The Ring programming language version 1.9 book - Part 99 of 210
The Ring programming language version 1.9 book - Part 99 of 210Mahmoud Samir Fayed
 
maXbox starter67 machine learning V
maXbox starter67 machine learning VmaXbox starter67 machine learning V
maXbox starter67 machine learning VMax Kleiner
 
Training course lect2
Training course lect2Training course lect2
Training course lect2Noor Dhiya
 

Similar to Assignment 6.1.pdf (20)

Image classification using cnn
Image classification using cnnImage classification using cnn
Image classification using cnn
 
maXbox starter69 Machine Learning VII
maXbox starter69 Machine Learning VIImaXbox starter69 Machine Learning VII
maXbox starter69 Machine Learning VII
 
Assignment 6.2a.pdf
Assignment 6.2a.pdfAssignment 6.2a.pdf
Assignment 6.2a.pdf
 
How to use SVM for data classification
How to use SVM for data classificationHow to use SVM for data classification
How to use SVM for data classification
 
The Ring programming language version 1.2 book - Part 79 of 84
The Ring programming language version 1.2 book - Part 79 of 84The Ring programming language version 1.2 book - Part 79 of 84
The Ring programming language version 1.2 book - Part 79 of 84
 
AIML4 CNN lab256 1hr (111-1).pdf
AIML4 CNN lab256 1hr (111-1).pdfAIML4 CNN lab256 1hr (111-1).pdf
AIML4 CNN lab256 1hr (111-1).pdf
 
Intelligent System Optimizations
Intelligent System OptimizationsIntelligent System Optimizations
Intelligent System Optimizations
 
From Tensorflow Graph to Tensorflow Eager
From Tensorflow Graph to Tensorflow EagerFrom Tensorflow Graph to Tensorflow Eager
From Tensorflow Graph to Tensorflow Eager
 
Naïve Bayes.pptx
Naïve Bayes.pptxNaïve Bayes.pptx
Naïve Bayes.pptx
 
Testing in those hard to reach places
Testing in those hard to reach placesTesting in those hard to reach places
Testing in those hard to reach places
 
I want you to add the output of the F1 score- Precision- ROC AUC- and.pdf
I want you to add the output of the F1 score- Precision- ROC AUC- and.pdfI want you to add the output of the F1 score- Precision- ROC AUC- and.pdf
I want you to add the output of the F1 score- Precision- ROC AUC- and.pdf
 
The Ring programming language version 1.7 book - Part 91 of 196
The Ring programming language version 1.7 book - Part 91 of 196The Ring programming language version 1.7 book - Part 91 of 196
The Ring programming language version 1.7 book - Part 91 of 196
 
Gans
GansGans
Gans
 
GANs
GANsGANs
GANs
 
Xgboost
XgboostXgboost
Xgboost
 
Competition 1 (blog 1)
Competition 1 (blog 1)Competition 1 (blog 1)
Competition 1 (blog 1)
 
Deep Learning with Julia1.0 and Flux
Deep Learning with Julia1.0 and FluxDeep Learning with Julia1.0 and Flux
Deep Learning with Julia1.0 and Flux
 
The Ring programming language version 1.9 book - Part 99 of 210
The Ring programming language version 1.9 book - Part 99 of 210The Ring programming language version 1.9 book - Part 99 of 210
The Ring programming language version 1.9 book - Part 99 of 210
 
maXbox starter67 machine learning V
maXbox starter67 machine learning VmaXbox starter67 machine learning V
maXbox starter67 machine learning V
 
Training course lect2
Training course lect2Training course lect2
Training course lect2
 

More from dash41

Assignment7.pdf
Assignment7.pdfAssignment7.pdf
Assignment7.pdfdash41
 
Assignment 6.3.pdf
Assignment 6.3.pdfAssignment 6.3.pdf
Assignment 6.3.pdfdash41
 
Assignment 5.3.pdf
Assignment 5.3.pdfAssignment 5.3.pdf
Assignment 5.3.pdfdash41
 
Assignment 5.2.pdf
Assignment 5.2.pdfAssignment 5.2.pdf
Assignment 5.2.pdfdash41
 
Assignment 5.1.pdf
Assignment 5.1.pdfAssignment 5.1.pdf
Assignment 5.1.pdfdash41
 
Assignment 4.pdf
Assignment 4.pdfAssignment 4.pdf
Assignment 4.pdfdash41
 
Assignment 3.pdf
Assignment 3.pdfAssignment 3.pdf
Assignment 3.pdfdash41
 
rdbms.pdf
rdbms.pdfrdbms.pdf
rdbms.pdfdash41
 
documentsdb.pdf
documentsdb.pdfdocumentsdb.pdf
documentsdb.pdfdash41
 

More from dash41 (9)

Assignment7.pdf
Assignment7.pdfAssignment7.pdf
Assignment7.pdf
 
Assignment 6.3.pdf
Assignment 6.3.pdfAssignment 6.3.pdf
Assignment 6.3.pdf
 
Assignment 5.3.pdf
Assignment 5.3.pdfAssignment 5.3.pdf
Assignment 5.3.pdf
 
Assignment 5.2.pdf
Assignment 5.2.pdfAssignment 5.2.pdf
Assignment 5.2.pdf
 
Assignment 5.1.pdf
Assignment 5.1.pdfAssignment 5.1.pdf
Assignment 5.1.pdf
 
Assignment 4.pdf
Assignment 4.pdfAssignment 4.pdf
Assignment 4.pdf
 
Assignment 3.pdf
Assignment 3.pdfAssignment 3.pdf
Assignment 3.pdf
 
rdbms.pdf
rdbms.pdfrdbms.pdf
rdbms.pdf
 
documentsdb.pdf
documentsdb.pdfdocumentsdb.pdf
documentsdb.pdf
 

Recently uploaded

Progress Report - Oracle Database Analyst Summit
Progress  Report - Oracle Database Analyst SummitProgress  Report - Oracle Database Analyst Summit
Progress Report - Oracle Database Analyst SummitHolger Mueller
 
Call Girls Pune Just Call 9907093804 Top Class Call Girl Service Available
Call Girls Pune Just Call 9907093804 Top Class Call Girl Service AvailableCall Girls Pune Just Call 9907093804 Top Class Call Girl Service Available
Call Girls Pune Just Call 9907093804 Top Class Call Girl Service AvailableDipal Arora
 
Vip Dewas Call Girls #9907093804 Contact Number Escorts Service Dewas
Vip Dewas Call Girls #9907093804 Contact Number Escorts Service DewasVip Dewas Call Girls #9907093804 Contact Number Escorts Service Dewas
Vip Dewas Call Girls #9907093804 Contact Number Escorts Service Dewasmakika9823
 
Monte Carlo simulation : Simulation using MCSM
Monte Carlo simulation : Simulation using MCSMMonte Carlo simulation : Simulation using MCSM
Monte Carlo simulation : Simulation using MCSMRavindra Nath Shukla
 
The Coffee Bean & Tea Leaf(CBTL), Business strategy case study
The Coffee Bean & Tea Leaf(CBTL), Business strategy case studyThe Coffee Bean & Tea Leaf(CBTL), Business strategy case study
The Coffee Bean & Tea Leaf(CBTL), Business strategy case studyEthan lee
 
Catalogue ONG NƯỚC uPVC - HDPE DE NHAT.pdf
Catalogue ONG NƯỚC uPVC - HDPE DE NHAT.pdfCatalogue ONG NƯỚC uPVC - HDPE DE NHAT.pdf
Catalogue ONG NƯỚC uPVC - HDPE DE NHAT.pdfOrient Homes
 
Lucknow 💋 Escorts in Lucknow - 450+ Call Girl Cash Payment 8923113531 Neha Th...
Lucknow 💋 Escorts in Lucknow - 450+ Call Girl Cash Payment 8923113531 Neha Th...Lucknow 💋 Escorts in Lucknow - 450+ Call Girl Cash Payment 8923113531 Neha Th...
Lucknow 💋 Escorts in Lucknow - 450+ Call Girl Cash Payment 8923113531 Neha Th...anilsa9823
 
VIP Call Girls In Saharaganj ( Lucknow ) 🔝 8923113531 🔝 Cash Payment (COD) 👒
VIP Call Girls In Saharaganj ( Lucknow  ) 🔝 8923113531 🔝  Cash Payment (COD) 👒VIP Call Girls In Saharaganj ( Lucknow  ) 🔝 8923113531 🔝  Cash Payment (COD) 👒
VIP Call Girls In Saharaganj ( Lucknow ) 🔝 8923113531 🔝 Cash Payment (COD) 👒anilsa9823
 
DEPED Work From Home WORKWEEK-PLAN.docx
DEPED Work From Home  WORKWEEK-PLAN.docxDEPED Work From Home  WORKWEEK-PLAN.docx
DEPED Work From Home WORKWEEK-PLAN.docxRodelinaLaud
 
Call Girls In Panjim North Goa 9971646499 Genuine Service
Call Girls In Panjim North Goa 9971646499 Genuine ServiceCall Girls In Panjim North Goa 9971646499 Genuine Service
Call Girls In Panjim North Goa 9971646499 Genuine Serviceritikaroy0888
 
Ensure the security of your HCL environment by applying the Zero Trust princi...
Ensure the security of your HCL environment by applying the Zero Trust princi...Ensure the security of your HCL environment by applying the Zero Trust princi...
Ensure the security of your HCL environment by applying the Zero Trust princi...Roland Driesen
 
Catalogue ONG NUOC PPR DE NHAT .pdf
Catalogue ONG NUOC PPR DE NHAT      .pdfCatalogue ONG NUOC PPR DE NHAT      .pdf
Catalogue ONG NUOC PPR DE NHAT .pdfOrient Homes
 
MONA 98765-12871 CALL GIRLS IN LUDHIANA LUDHIANA CALL GIRL
MONA 98765-12871 CALL GIRLS IN LUDHIANA LUDHIANA CALL GIRLMONA 98765-12871 CALL GIRLS IN LUDHIANA LUDHIANA CALL GIRL
MONA 98765-12871 CALL GIRLS IN LUDHIANA LUDHIANA CALL GIRLSeo
 
The CMO Survey - Highlights and Insights Report - Spring 2024
The CMO Survey - Highlights and Insights Report - Spring 2024The CMO Survey - Highlights and Insights Report - Spring 2024
The CMO Survey - Highlights and Insights Report - Spring 2024christinemoorman
 
Call Girls Navi Mumbai Just Call 9907093804 Top Class Call Girl Service Avail...
Call Girls Navi Mumbai Just Call 9907093804 Top Class Call Girl Service Avail...Call Girls Navi Mumbai Just Call 9907093804 Top Class Call Girl Service Avail...
Call Girls Navi Mumbai Just Call 9907093804 Top Class Call Girl Service Avail...Dipal Arora
 
Call Girls in Gomti Nagar - 7388211116 - With room Service
Call Girls in Gomti Nagar - 7388211116  - With room ServiceCall Girls in Gomti Nagar - 7388211116  - With room Service
Call Girls in Gomti Nagar - 7388211116 - With room Servicediscovermytutordmt
 
Sales & Marketing Alignment: How to Synergize for Success
Sales & Marketing Alignment: How to Synergize for SuccessSales & Marketing Alignment: How to Synergize for Success
Sales & Marketing Alignment: How to Synergize for SuccessAggregage
 

Recently uploaded (20)

Progress Report - Oracle Database Analyst Summit
Progress  Report - Oracle Database Analyst SummitProgress  Report - Oracle Database Analyst Summit
Progress Report - Oracle Database Analyst Summit
 
Call Girls Pune Just Call 9907093804 Top Class Call Girl Service Available
Call Girls Pune Just Call 9907093804 Top Class Call Girl Service AvailableCall Girls Pune Just Call 9907093804 Top Class Call Girl Service Available
Call Girls Pune Just Call 9907093804 Top Class Call Girl Service Available
 
Vip Dewas Call Girls #9907093804 Contact Number Escorts Service Dewas
Vip Dewas Call Girls #9907093804 Contact Number Escorts Service DewasVip Dewas Call Girls #9907093804 Contact Number Escorts Service Dewas
Vip Dewas Call Girls #9907093804 Contact Number Escorts Service Dewas
 
Monte Carlo simulation : Simulation using MCSM
Monte Carlo simulation : Simulation using MCSMMonte Carlo simulation : Simulation using MCSM
Monte Carlo simulation : Simulation using MCSM
 
The Coffee Bean & Tea Leaf(CBTL), Business strategy case study
The Coffee Bean & Tea Leaf(CBTL), Business strategy case studyThe Coffee Bean & Tea Leaf(CBTL), Business strategy case study
The Coffee Bean & Tea Leaf(CBTL), Business strategy case study
 
Catalogue ONG NƯỚC uPVC - HDPE DE NHAT.pdf
Catalogue ONG NƯỚC uPVC - HDPE DE NHAT.pdfCatalogue ONG NƯỚC uPVC - HDPE DE NHAT.pdf
Catalogue ONG NƯỚC uPVC - HDPE DE NHAT.pdf
 
Lucknow 💋 Escorts in Lucknow - 450+ Call Girl Cash Payment 8923113531 Neha Th...
Lucknow 💋 Escorts in Lucknow - 450+ Call Girl Cash Payment 8923113531 Neha Th...Lucknow 💋 Escorts in Lucknow - 450+ Call Girl Cash Payment 8923113531 Neha Th...
Lucknow 💋 Escorts in Lucknow - 450+ Call Girl Cash Payment 8923113531 Neha Th...
 
VIP Call Girls In Saharaganj ( Lucknow ) 🔝 8923113531 🔝 Cash Payment (COD) 👒
VIP Call Girls In Saharaganj ( Lucknow  ) 🔝 8923113531 🔝  Cash Payment (COD) 👒VIP Call Girls In Saharaganj ( Lucknow  ) 🔝 8923113531 🔝  Cash Payment (COD) 👒
VIP Call Girls In Saharaganj ( Lucknow ) 🔝 8923113531 🔝 Cash Payment (COD) 👒
 
DEPED Work From Home WORKWEEK-PLAN.docx
DEPED Work From Home  WORKWEEK-PLAN.docxDEPED Work From Home  WORKWEEK-PLAN.docx
DEPED Work From Home WORKWEEK-PLAN.docx
 
Nepali Escort Girl Kakori \ 9548273370 Indian Call Girls Service Lucknow ₹,9517
Nepali Escort Girl Kakori \ 9548273370 Indian Call Girls Service Lucknow ₹,9517Nepali Escort Girl Kakori \ 9548273370 Indian Call Girls Service Lucknow ₹,9517
Nepali Escort Girl Kakori \ 9548273370 Indian Call Girls Service Lucknow ₹,9517
 
Call Girls In Panjim North Goa 9971646499 Genuine Service
Call Girls In Panjim North Goa 9971646499 Genuine ServiceCall Girls In Panjim North Goa 9971646499 Genuine Service
Call Girls In Panjim North Goa 9971646499 Genuine Service
 
KestrelPro Flyer Japan IT Week 2024 (English)
KestrelPro Flyer Japan IT Week 2024 (English)KestrelPro Flyer Japan IT Week 2024 (English)
KestrelPro Flyer Japan IT Week 2024 (English)
 
Ensure the security of your HCL environment by applying the Zero Trust princi...
Ensure the security of your HCL environment by applying the Zero Trust princi...Ensure the security of your HCL environment by applying the Zero Trust princi...
Ensure the security of your HCL environment by applying the Zero Trust princi...
 
Catalogue ONG NUOC PPR DE NHAT .pdf
Catalogue ONG NUOC PPR DE NHAT      .pdfCatalogue ONG NUOC PPR DE NHAT      .pdf
Catalogue ONG NUOC PPR DE NHAT .pdf
 
MONA 98765-12871 CALL GIRLS IN LUDHIANA LUDHIANA CALL GIRL
MONA 98765-12871 CALL GIRLS IN LUDHIANA LUDHIANA CALL GIRLMONA 98765-12871 CALL GIRLS IN LUDHIANA LUDHIANA CALL GIRL
MONA 98765-12871 CALL GIRLS IN LUDHIANA LUDHIANA CALL GIRL
 
The CMO Survey - Highlights and Insights Report - Spring 2024
The CMO Survey - Highlights and Insights Report - Spring 2024The CMO Survey - Highlights and Insights Report - Spring 2024
The CMO Survey - Highlights and Insights Report - Spring 2024
 
Call Girls Navi Mumbai Just Call 9907093804 Top Class Call Girl Service Avail...
Call Girls Navi Mumbai Just Call 9907093804 Top Class Call Girl Service Avail...Call Girls Navi Mumbai Just Call 9907093804 Top Class Call Girl Service Avail...
Call Girls Navi Mumbai Just Call 9907093804 Top Class Call Girl Service Avail...
 
Call Girls in Gomti Nagar - 7388211116 - With room Service
Call Girls in Gomti Nagar - 7388211116  - With room ServiceCall Girls in Gomti Nagar - 7388211116  - With room Service
Call Girls in Gomti Nagar - 7388211116 - With room Service
 
Sales & Marketing Alignment: How to Synergize for Success
Sales & Marketing Alignment: How to Synergize for SuccessSales & Marketing Alignment: How to Synergize for Success
Sales & Marketing Alignment: How to Synergize for Success
 
Forklift Operations: Safety through Cartoons
Forklift Operations: Safety through CartoonsForklift Operations: Safety through Cartoons
Forklift Operations: Safety through Cartoons
 

Assignment 6.1.pdf

  • 1. X_train original shape (60000, 28, 28) y_train original shape (60000,) X_test original shape (10000, 28, 28) y_test original shape (10000,) Text(0.5, 1.0, '5') (5, array([0., 0., 0., 0., 0., 1., 0., 0., 0., 0.], dtype=float32)) In [1]: import numpy as np import pandas as pd import matplotlib.pyplot as plt %matplotlib inline from keras.datasets import mnist from keras.models import Sequential from keras.layers import Dense, Dropout, Activation from keras.utils import np_utils np.random.seed(35) In [2]: (X_train, y_train), (X_test, y_test) = mnist.load_data() print("X_train original shape", X_train.shape) print("y_train original shape", y_train.shape) print("X_test original shape", X_test.shape) print("y_test original shape", y_test.shape) In [3]: plt.imshow(X_train[0], cmap='gray') plt.title(y_train[0]) Out[3]: In [4]: X_train = X_train.reshape(60000,784) X_test = X_test.reshape(10000,784) X_train = X_train.astype('float32') X_test = X_test.astype('float32') X_train/=255 X_test/=255 In [5]: number_of_classes = 10 Y_train = np_utils.to_categorical(y_train, number_of_classes) Y_test = np_utils.to_categorical(y_test, number_of_classes) y_train[0], Y_train[0] Out[5]:
  • 2. ((50000, 784), (10000, 784)) Epoch 1/5 391/391 [==============================] - 59s 34ms/step - loss: 0.5052 - accuracy: 0.8394 - val_loss: 0.1180 - val_accuracy: 0.9640 Epoch 2/5 391/391 [==============================] - 12s 30ms/step - loss: 0.1169 - accuracy: 0.9646 - val_loss: 0.1011 - val_accuracy: 0.9705 Epoch 3/5 391/391 [==============================] - 12s 30ms/step - loss: 0.0794 - accuracy: 0.9744 - val_loss: 0.0913 - val_accuracy: 0.9741 Epoch 4/5 391/391 [==============================] - 12s 30ms/step - loss: 0.0661 - accuracy: 0.9792 - val_loss: 0.0850 - val_accuracy: 0.9780 Epoch 5/5 391/391 [==============================] - 12s 30ms/step - loss: 0.0545 - accuracy: 0.9824 - val_loss: 0.0875 - val_accuracy: 0.9783 In [6]: #shuffle the training set for _ in range(5): indexes = np.random.permutation(len(X_train)) X_train = X_train[indexes] Y_train = Y_train[indexes] #set aside 10,000 for validation val_images = X_train[:10000,:] val_labels = Y_train[:10000,:] # leave rest in training set train_images = X_train[10000:,:] train_labels = Y_train[10000:,:] train_images.shape, val_images.shape Out[6]: In [7]: model = Sequential() model.add(Dense(512, input_dim=784)) # An "activation" is just a non-linear function applied to the output # of the layer above. Here, with a "rectified linear unit", # we clamp all values below 0 to 0. model.add(Activation('relu')) # Dropout helps protect the model from memorizing or "overfitting" the training data model.add(Dropout(0.2)) model.add(Dense(512)) model.add(Activation('relu')) model.add(Dropout(0.2)) model.add(Dense(512)) model.add(Activation('relu')) model.add(Dropout(0.2)) model.add(Dense(10)) # This special "softmax" activation among other things, # ensures the output is a valid probaility distribution, that is # that its values are all non-negative and sum to 1. model.add(Activation('softmax')) In [8]: model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy' In [9]: history = model.fit(train_images, train_labels, epochs=5, batch_size=128, validation_data=(val_images, val_labels)) In [10]: train_loss = history.history['loss']
  • 3. <Figure size 432x288 with 0 Axes> val_loss = history.history['val_loss'] In [11]: epochs = range(1, len(history.history['loss']) + 1) In [13]: plt.plot(epochs, train_loss, 'bo', label='Training loss') plt.plot(epochs, val_loss, 'g', label='Validation loss') plt.title('Training and Validation Losses') plt.xlabel('Epochs') plt.ylabel('Loss') plt.legend() plt.show() plt.savefig('Results/6_1_lossplot.png') In [14]: train_accuracy = history.history['accuracy'] val_accuracy = history.history['val_accuracy'] In [15]: epochs = range(1, len(history.history['accuracy']) + 1) In [16]: plt.plot(epochs, train_accuracy, 'bo', label='Training Accuracy') plt.plot(epochs, val_accuracy, 'g', label='Validation Accuracy') plt.title('Training and Validation Accuracy') plt.xlabel('Epochs') plt.ylabel('Accuracy') plt.legend() plt.show() plt.savefig('results/6_1_accuracyplot.png')
  • 4. <Figure size 432x288 with 0 Axes> 313/313 [==============================] - 2s 5ms/step - loss: 0.0770 - accuracy: 0. 9792 Test accuracy: 0.979200005531311 Actual Predictions 0 7 7 1 2 2 2 1 1 3 0 0 4 4 4 ... ... ... 9995 2 2 9996 3 3 9997 4 4 9998 5 5 9999 6 6 [10000 rows x 2 columns] In [17]: score = model.evaluate(X_test, Y_test) print() print('Test accuracy: ', score[1]) In [18]: predictions = np.argmax(model.predict(X_test), axis=1) predictions = list(predictions) actuals = list(y_test) pred_res = pd.DataFrame({'Actual': actuals, 'Predictions': predictions}) pred_res.to_csv('results/6_1_predictions.csv', index=False) print (pred_res) In [19]: # save model model.save('results/6_1_model.h5') In [20]: #Metrics output with open('results/6_1_metrics.txt', 'w') as f: f.write('Training Loss: {}'.format(str(history.history['loss']))) f.write('nTraining Accuracy: {}'.format(str(history.history['accuracy']))) f.write('nTest Loss: {}'.format(score[0])) f.write('nTest Accuracy: {}'.format(score[1])) In [ ]: