Assignment 6.2a.pdf

D

data science

Downloading data from https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz

170500096/170498071 [==============================] - 19s 0us/step

X_train original shape (50000, 32, 32, 3)

y_train original shape (50000, 1)

X_test original shape (10000, 32, 32, 3)

y_test original shape (10000, 1)

Text(0.5, 1.0, '[9]')
In [1]: import pandas as pd

import numpy as np



from keras.datasets import cifar10

from keras.utils import to_categorical

import matplotlib.pyplot as plt

In [2]: (x_train, y_train), (x_test, y_test) = cifar10.load_data()

In [5]: 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 [9]: plt.imshow(x_train[2], cmap='gray')

plt.title(y_train[2])

Out[9]:
In [10]: # Preprocess the data (these are NumPy arrays)

x_train = x_train.astype("float32") / 255

x_test = x_test.astype("float32") / 255



y_train = to_categorical(y_train)

y_test = to_categorical(y_test)



# allocating 10,000 samples for validation

x_val = x_train[-10000:]

y_val = y_train[-10000:]

x_train = x_train[:-10000]

y_train = y_train[:-10000]

In [13]: #instantiate the model

from keras import models

from keras import layers



model = models.Sequential()

model.add(layers.Conv2D(32, (3,3), activation='relu', input_shape=(32,32,3)))
Model: "sequential_2"

_________________________________________________________________

Layer (type) Output Shape Param # 

=================================================================

conv2d_6 (Conv2D) (None, 30, 30, 32) 896 

_________________________________________________________________

max_pooling2d_6 (MaxPooling2 (None, 15, 15, 32) 0 

_________________________________________________________________

conv2d_7 (Conv2D) (None, 13, 13, 64) 18496 

_________________________________________________________________

max_pooling2d_7 (MaxPooling2 (None, 6, 6, 64) 0 

_________________________________________________________________

conv2d_8 (Conv2D) (None, 4, 4, 128) 73856 

_________________________________________________________________

max_pooling2d_8 (MaxPooling2 (None, 2, 2, 128) 0 

_________________________________________________________________

flatten_2 (Flatten) (None, 512) 0 

_________________________________________________________________

dense_4 (Dense) (None, 128) 65664 

_________________________________________________________________

dense_5 (Dense) (None, 10) 1290 

=================================================================

Total params: 160,202

Trainable params: 160,202

Non-trainable params: 0

_________________________________________________________________

model.add(layers.MaxPooling2D(2,2))

model.add(layers.Conv2D(64, (3,3), activation='relu'))

model.add(layers.MaxPooling2D(2,2))

model.add(layers.Conv2D(128, (3,3), activation='relu'))

model.add(layers.MaxPooling2D(2,2))

model.add(layers.Flatten())

model.add(layers.Dense(128, activation='relu'))

model.add(layers.Dense(10, activation='softmax'))



model.summary()

In [14]: model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'
In [17]: history = model.fit(x_train, y_train, epochs=100, batch_size=128,

validation_data=(x_val, y_val),verbose=0)

In [18]: train_loss = history.history['loss']

val_loss = history.history['val_loss']



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

In [19]: plt.plot(epochs, train_loss, 'bo', label='Loss_Training')

plt.plot(epochs, val_loss, 'b', label='Loss_Validation')

plt.title('Training and Validation Losses')

plt.xlabel('Epochs')

plt.ylabel('Loss')

plt.legend()



plt.show()

plt.savefig('Results/6_2a_lossplot.png')
<Figure size 432x288 with 0 Axes>
<Figure size 432x288 with 0 Axes>
313/313 [==============================] - 1s 4ms/step - loss: 3.9177 - accuracy: 0.
6841

Test accuracy: 0.6840999722480774
In [20]: train_loss = history.history['accuracy']

val_loss = history.history['val_accuracy']



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

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

plt.plot(epochs, val_loss, 'b', label='Validation accuracy')

plt.title('Training and Validation Accuracy')

plt.xlabel('Epochs')

plt.ylabel('Accuracy')

plt.legend()



plt.show()

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

In [22]: model.save('Results/6_2a_model.h5')

In [23]: score = model.evaluate(x_test, y_test)



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

In [26]: predictions = np.argmax(model.predict(x_test), axis=1)
Actual Predictions

0 [0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, ... 3

1 [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, ... 8

2 [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, ... 8

3 [1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, ... 0

4 [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, ... 6

... ... ...

9995 [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, ... 3

9996 [0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, ... 5

9997 [0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, ... 5

9998 [0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, ... 4

9999 [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, ... 7

[10000 rows x 2 columns]



predictions = list(predictions)

actuals = list(y_test)



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

pred_res.to_csv('Results/6_2a_predictions.csv', index=False)

print (pred_res)

In [27]: #Metrics output

with open('Results/6_2a_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]))

Recomendados

Assignment 5.2.pdf von
Assignment 5.2.pdfAssignment 5.2.pdf
Assignment 5.2.pdfdash41
4 views7 Folien
Pydata DC 2018 (Skorch - A Union of Scikit-learn and PyTorch) von
Pydata DC 2018 (Skorch - A Union of Scikit-learn and PyTorch)Pydata DC 2018 (Skorch - A Union of Scikit-learn and PyTorch)
Pydata DC 2018 (Skorch - A Union of Scikit-learn and PyTorch)Thomas Fan
575 views66 Folien
Machine Learning Algorithms von
Machine Learning AlgorithmsMachine Learning Algorithms
Machine Learning Algorithmshichem felouat
249 views148 Folien
logistic regression with python and R von
logistic regression with python and Rlogistic regression with python and R
logistic regression with python and RAkhilesh Joshi
1.1K views27 Folien
svm classification von
svm classificationsvm classification
svm classificationAkhilesh Joshi
849 views43 Folien
Assignment 6.1.pdf von
Assignment 6.1.pdfAssignment 6.1.pdf
Assignment 6.1.pdfdash41
14 views4 Folien

Más contenido relacionado

Similar a Assignment 6.2a.pdf

SCIPY-SYMPY.pdf von
SCIPY-SYMPY.pdfSCIPY-SYMPY.pdf
SCIPY-SYMPY.pdfFreddyGuzman19
2 views11 Folien
Chainer ui v0.3 and imagereport von
Chainer ui v0.3 and imagereportChainer ui v0.3 and imagereport
Chainer ui v0.3 and imagereportPreferred Networks
1.8K views21 Folien
knn classification von
knn classificationknn classification
knn classificationAkhilesh Joshi
1.2K views23 Folien
Nyc open-data-2015-andvanced-sklearn-expanded von
Nyc open-data-2015-andvanced-sklearn-expandedNyc open-data-2015-andvanced-sklearn-expanded
Nyc open-data-2015-andvanced-sklearn-expandedVivian S. Zhang
2.6K views98 Folien
Regression Tree.pdf von
Regression Tree.pdfRegression Tree.pdf
Regression Tree.pdfIvanHartana4
5 views12 Folien
Converting Scikit-Learn to PMML von
Converting Scikit-Learn to PMMLConverting Scikit-Learn to PMML
Converting Scikit-Learn to PMMLVillu Ruusmann
27.8K views32 Folien

Similar a Assignment 6.2a.pdf(20)

Nyc open-data-2015-andvanced-sklearn-expanded von Vivian S. Zhang
Nyc open-data-2015-andvanced-sklearn-expandedNyc open-data-2015-andvanced-sklearn-expanded
Nyc open-data-2015-andvanced-sklearn-expanded
Vivian S. Zhang2.6K views
Converting Scikit-Learn to PMML von Villu Ruusmann
Converting Scikit-Learn to PMMLConverting Scikit-Learn to PMML
Converting Scikit-Learn to PMML
Villu Ruusmann27.8K views
Python 03-parameters-graphics.pptx von TseChris
Python 03-parameters-graphics.pptxPython 03-parameters-graphics.pptx
Python 03-parameters-graphics.pptx
TseChris10 views
Deep learning study 3 von San Kim
Deep learning study 3Deep learning study 3
Deep learning study 3
San Kim52 views
Pandas+postgre sql 實作 with code von Tim Hong
Pandas+postgre sql 實作 with codePandas+postgre sql 實作 with code
Pandas+postgre sql 實作 with code
Tim Hong150 views
Informatics Practices (new) solution CBSE 2021, Compartment, improvement ex... von FarhanAhmade
Informatics Practices (new) solution CBSE  2021, Compartment,  improvement ex...Informatics Practices (new) solution CBSE  2021, Compartment,  improvement ex...
Informatics Practices (new) solution CBSE 2021, Compartment, improvement ex...
FarhanAhmade331 views
Introduction to deep learning using python von Lino Coria
Introduction to deep learning using pythonIntroduction to deep learning using python
Introduction to deep learning using python
Lino Coria144 views
Python profiling von dreampuf
Python profilingPython profiling
Python profiling
dreampuf1.7K views
ridgeandlasso - Jupyter Notebook.pdf von PiyushBorhade1
ridgeandlasso - Jupyter Notebook.pdfridgeandlasso - Jupyter Notebook.pdf
ridgeandlasso - Jupyter Notebook.pdf
PiyushBorhade119 views
Linear Regression (Machine Learning) von Omkar Rane
Linear Regression (Machine Learning)Linear Regression (Machine Learning)
Linear Regression (Machine Learning)
Omkar Rane122 views
AIML4 CNN lab256 1hr (111-1).pdf von ssuserb4d806
AIML4 CNN lab256 1hr (111-1).pdfAIML4 CNN lab256 1hr (111-1).pdf
AIML4 CNN lab256 1hr (111-1).pdf
ssuserb4d8067 views
Hybrid quantum classical neural networks with pytorch and qiskit von Vijayananda Mohire
Hybrid quantum classical neural networks with pytorch and qiskitHybrid quantum classical neural networks with pytorch and qiskit
Hybrid quantum classical neural networks with pytorch and qiskit
Vijayananda Mohire268 views

Más de dash41

Assignment7.pdf von
Assignment7.pdfAssignment7.pdf
Assignment7.pdfdash41
37 views6 Folien
Assignment 6.3.pdf von
Assignment 6.3.pdfAssignment 6.3.pdf
Assignment 6.3.pdfdash41
16 views2 Folien
Assignment 6.2b.pdf von
Assignment 6.2b.pdfAssignment 6.2b.pdf
Assignment 6.2b.pdfdash41
9 views4 Folien
Assignment 5.3.pdf von
Assignment 5.3.pdfAssignment 5.3.pdf
Assignment 5.3.pdfdash41
19 views4 Folien
Assignment 5.1.pdf von
Assignment 5.1.pdfAssignment 5.1.pdf
Assignment 5.1.pdfdash41
3 views9 Folien
Assignment 4.pdf von
Assignment 4.pdfAssignment 4.pdf
Assignment 4.pdfdash41
29 views10 Folien

Más de dash41(9)

Assignment7.pdf von dash41
Assignment7.pdfAssignment7.pdf
Assignment7.pdf
dash4137 views
Assignment 6.3.pdf von dash41
Assignment 6.3.pdfAssignment 6.3.pdf
Assignment 6.3.pdf
dash4116 views
Assignment 6.2b.pdf von dash41
Assignment 6.2b.pdfAssignment 6.2b.pdf
Assignment 6.2b.pdf
dash419 views
Assignment 5.3.pdf von dash41
Assignment 5.3.pdfAssignment 5.3.pdf
Assignment 5.3.pdf
dash4119 views
Assignment 5.1.pdf von dash41
Assignment 5.1.pdfAssignment 5.1.pdf
Assignment 5.1.pdf
dash413 views
Assignment 4.pdf von dash41
Assignment 4.pdfAssignment 4.pdf
Assignment 4.pdf
dash4129 views
Assignment 3.pdf von dash41
Assignment 3.pdfAssignment 3.pdf
Assignment 3.pdf
dash4132 views
rdbms.pdf von dash41
rdbms.pdfrdbms.pdf
rdbms.pdf
dash412 views
documentsdb.pdf von dash41
documentsdb.pdfdocumentsdb.pdf
documentsdb.pdf
dash412 views

Último

apple.pptx von
apple.pptxapple.pptx
apple.pptxhoneybeeqwe
6 views15 Folien
Listed Instruments Survey 2022.pptx von
Listed Instruments Survey  2022.pptxListed Instruments Survey  2022.pptx
Listed Instruments Survey 2022.pptxsecretariat4
130 views12 Folien
Lack of communication among family.pptx von
Lack of communication among family.pptxLack of communication among family.pptx
Lack of communication among family.pptxahmed164023
16 views10 Folien
Data Journeys Hard Talk workshop final.pptx von
Data Journeys Hard Talk workshop final.pptxData Journeys Hard Talk workshop final.pptx
Data Journeys Hard Talk workshop final.pptxinfo828217
11 views18 Folien
AvizoImageSegmentation.pptx von
AvizoImageSegmentation.pptxAvizoImageSegmentation.pptx
AvizoImageSegmentation.pptxnathanielbutterworth1
7 views14 Folien
Oral presentation (1).pdf von
Oral presentation (1).pdfOral presentation (1).pdf
Oral presentation (1).pdfreemalmazroui8
6 views10 Folien

Último(20)

Listed Instruments Survey 2022.pptx von secretariat4
Listed Instruments Survey  2022.pptxListed Instruments Survey  2022.pptx
Listed Instruments Survey 2022.pptx
secretariat4130 views
Lack of communication among family.pptx von ahmed164023
Lack of communication among family.pptxLack of communication among family.pptx
Lack of communication among family.pptx
ahmed16402316 views
Data Journeys Hard Talk workshop final.pptx von info828217
Data Journeys Hard Talk workshop final.pptxData Journeys Hard Talk workshop final.pptx
Data Journeys Hard Talk workshop final.pptx
info82821711 views
OPPOTUS - Malaysians on Malaysia 3Q2023.pdf von Oppotus
OPPOTUS - Malaysians on Malaysia 3Q2023.pdfOPPOTUS - Malaysians on Malaysia 3Q2023.pdf
OPPOTUS - Malaysians on Malaysia 3Q2023.pdf
Oppotus34 views
Product Research sample.pdf von AllenSingson
Product Research sample.pdfProduct Research sample.pdf
Product Research sample.pdf
AllenSingson35 views
GDG Cloud Community Day 2022 - Managing data quality in Machine Learning von SARADINDU SENGUPTA
GDG Cloud Community Day 2022 -  Managing data quality in Machine LearningGDG Cloud Community Day 2022 -  Managing data quality in Machine Learning
GDG Cloud Community Day 2022 - Managing data quality in Machine Learning
CRM stick or twist workshop von info828217
CRM stick or twist workshopCRM stick or twist workshop
CRM stick or twist workshop
info82821714 views
Customer Data Cleansing Project.pptx von Nat O
Customer Data Cleansing Project.pptxCustomer Data Cleansing Project.pptx
Customer Data Cleansing Project.pptx
Nat O6 views
6498-Butun_Beyinli_Cocuq-Daniel_J.Siegel-Tina_Payne_Bryson-2011-259s.pdf von 10urkyr34
6498-Butun_Beyinli_Cocuq-Daniel_J.Siegel-Tina_Payne_Bryson-2011-259s.pdf6498-Butun_Beyinli_Cocuq-Daniel_J.Siegel-Tina_Payne_Bryson-2011-259s.pdf
6498-Butun_Beyinli_Cocuq-Daniel_J.Siegel-Tina_Payne_Bryson-2011-259s.pdf
10urkyr347 views
Analytics Center of Excellence | Data CoE |Analytics CoE| WNS Triange von RNayak3
Analytics Center of Excellence | Data CoE |Analytics CoE| WNS TriangeAnalytics Center of Excellence | Data CoE |Analytics CoE| WNS Triange
Analytics Center of Excellence | Data CoE |Analytics CoE| WNS Triange
RNayak35 views

Assignment 6.2a.pdf

  • 1. Downloading data from https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz 170500096/170498071 [==============================] - 19s 0us/step X_train original shape (50000, 32, 32, 3) y_train original shape (50000, 1) X_test original shape (10000, 32, 32, 3) y_test original shape (10000, 1) Text(0.5, 1.0, '[9]') In [1]: import pandas as pd import numpy as np from keras.datasets import cifar10 from keras.utils import to_categorical import matplotlib.pyplot as plt In [2]: (x_train, y_train), (x_test, y_test) = cifar10.load_data() In [5]: 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 [9]: plt.imshow(x_train[2], cmap='gray') plt.title(y_train[2]) Out[9]: In [10]: # Preprocess the data (these are NumPy arrays) x_train = x_train.astype("float32") / 255 x_test = x_test.astype("float32") / 255 y_train = to_categorical(y_train) y_test = to_categorical(y_test) # allocating 10,000 samples for validation x_val = x_train[-10000:] y_val = y_train[-10000:] x_train = x_train[:-10000] y_train = y_train[:-10000] In [13]: #instantiate the model from keras import models from keras import layers model = models.Sequential() model.add(layers.Conv2D(32, (3,3), activation='relu', input_shape=(32,32,3)))
  • 2. Model: "sequential_2" _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= conv2d_6 (Conv2D) (None, 30, 30, 32) 896 _________________________________________________________________ max_pooling2d_6 (MaxPooling2 (None, 15, 15, 32) 0 _________________________________________________________________ conv2d_7 (Conv2D) (None, 13, 13, 64) 18496 _________________________________________________________________ max_pooling2d_7 (MaxPooling2 (None, 6, 6, 64) 0 _________________________________________________________________ conv2d_8 (Conv2D) (None, 4, 4, 128) 73856 _________________________________________________________________ max_pooling2d_8 (MaxPooling2 (None, 2, 2, 128) 0 _________________________________________________________________ flatten_2 (Flatten) (None, 512) 0 _________________________________________________________________ dense_4 (Dense) (None, 128) 65664 _________________________________________________________________ dense_5 (Dense) (None, 10) 1290 ================================================================= Total params: 160,202 Trainable params: 160,202 Non-trainable params: 0 _________________________________________________________________ model.add(layers.MaxPooling2D(2,2)) model.add(layers.Conv2D(64, (3,3), activation='relu')) model.add(layers.MaxPooling2D(2,2)) model.add(layers.Conv2D(128, (3,3), activation='relu')) model.add(layers.MaxPooling2D(2,2)) model.add(layers.Flatten()) model.add(layers.Dense(128, activation='relu')) model.add(layers.Dense(10, activation='softmax')) model.summary() In [14]: model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy' In [17]: history = model.fit(x_train, y_train, epochs=100, batch_size=128, validation_data=(x_val, y_val),verbose=0) In [18]: train_loss = history.history['loss'] val_loss = history.history['val_loss'] epochs = range(1, len(history.history['loss']) + 1) In [19]: plt.plot(epochs, train_loss, 'bo', label='Loss_Training') plt.plot(epochs, val_loss, 'b', label='Loss_Validation') plt.title('Training and Validation Losses') plt.xlabel('Epochs') plt.ylabel('Loss') plt.legend() plt.show() plt.savefig('Results/6_2a_lossplot.png')
  • 3. <Figure size 432x288 with 0 Axes> <Figure size 432x288 with 0 Axes> 313/313 [==============================] - 1s 4ms/step - loss: 3.9177 - accuracy: 0. 6841 Test accuracy: 0.6840999722480774 In [20]: train_loss = history.history['accuracy'] val_loss = history.history['val_accuracy'] epochs = range(1, len(history.history['accuracy']) + 1) In [21]: plt.plot(epochs, train_loss, 'bo', label='Training accuracy') plt.plot(epochs, val_loss, 'b', label='Validation accuracy') plt.title('Training and Validation Accuracy') plt.xlabel('Epochs') plt.ylabel('Accuracy') plt.legend() plt.show() plt.savefig('Results/6_2a_accplot.png') In [22]: model.save('Results/6_2a_model.h5') In [23]: score = model.evaluate(x_test, y_test) print('Test accuracy: ', score[1]) In [26]: predictions = np.argmax(model.predict(x_test), axis=1)
  • 4. Actual Predictions 0 [0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, ... 3 1 [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, ... 8 2 [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, ... 8 3 [1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, ... 0 4 [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, ... 6 ... ... ... 9995 [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, ... 3 9996 [0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, ... 5 9997 [0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, ... 5 9998 [0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, ... 4 9999 [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, ... 7 [10000 rows x 2 columns] predictions = list(predictions) actuals = list(y_test) pred_res = pd.DataFrame({'Actual': actuals, 'Predictions': predictions}) pred_res.to_csv('Results/6_2a_predictions.csv', index=False) print (pred_res) In [27]: #Metrics output with open('Results/6_2a_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]))