Created
February 6, 2020 02:57
-
-
Save mohdsanadzakirizvi/66231070f09f27df4add9bf914a7fc91 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# keras imports for the dataset and building our neural network | |
from keras.datasets import cifar10 | |
from keras.models import Sequential | |
from keras.layers import Dense, Dropout, Conv2D, MaxPool2D, Flatten | |
from keras.utils import np_utils | |
# loading the dataset | |
(X_train, y_train), (X_test, y_test) = cifar10.load_data() | |
# # building the input vector from the 32x32 pixels | |
X_train = X_train.reshape(X_train.shape[0], 32, 32, 3) | |
X_test = X_test.reshape(X_test.shape[0], 32, 32, 3) | |
X_train = X_train.astype('float32') | |
X_test = X_test.astype('float32') | |
# normalizing the data to help with the training | |
X_train /= 255 | |
X_test /= 255 | |
# one-hot encoding using keras' numpy-related utilities | |
n_classes = 10 | |
print("Shape before one-hot encoding: ", y_train.shape) | |
Y_train = np_utils.to_categorical(y_train, n_classes) | |
Y_test = np_utils.to_categorical(y_test, n_classes) | |
print("Shape after one-hot encoding: ", Y_train.shape) | |
# building a linear stack of layers with the sequential model | |
model = Sequential() | |
# convolutional layer | |
model.add(Conv2D(50, kernel_size=(3,3), strides=(1,1), padding='same', activation='relu', input_shape=(32, 32, 3))) | |
# convolutional layer | |
model.add(Conv2D(75, kernel_size=(3,3), strides=(1,1), padding='same', activation='relu')) | |
model.add(MaxPool2D(pool_size=(2,2))) | |
model.add(Dropout(0.25)) | |
model.add(Conv2D(125, kernel_size=(3,3), strides=(1,1), padding='same', activation='relu')) | |
model.add(MaxPool2D(pool_size=(2,2))) | |
model.add(Dropout(0.25)) | |
# flatten output of conv | |
model.add(Flatten()) | |
# hidden layer | |
model.add(Dense(500, activation='relu')) | |
model.add(Dropout(0.4)) | |
model.add(Dense(250, activation='relu')) | |
model.add(Dropout(0.3)) | |
# output layer | |
model.add(Dense(10, activation='softmax')) | |
# compiling the sequential model | |
model.compile(loss='categorical_crossentropy', metrics=['accuracy'], optimizer='adam') | |
# training the model for 10 epochs | |
model.fit(X_train, Y_train, batch_size=128, epochs=10, validation_data=(X_test, Y_test)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment