This file contains hidden or 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
| import gzip | |
| import numpy as np | |
| import matplotlib.pyplot as plt | |
| import struct | |
| import time | |
| import torch | |
| import torchvision | |
| from torch import nn | |
| import torch.nn.functional as F | |
| from torch.utils.tensorboard import SummaryWriter |
This file contains hidden or 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
| accuracy = history.history['accuracy'] | |
| val_accuracy = history.history['val_accuracy'] | |
| loss = history.history['loss'] | |
| val_loss = history.history['val_loss'] |
This file contains hidden or 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
| def evaluate_score(model, test_x, test_y): | |
| score = model.evaluate(test_x,test_y,verbose=0) | |
| print('Test Loss : {:.4f}'.format(score[0])) | |
| print('Test Accuracy : {:.4f}'.format(score[1])) | |
| evaluate_score(model, x_test, y_test) |
This file contains hidden or 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
| history = compile_model(model, x_train, y_train, x_validate, y_validate, batch_size=512, no_epochs=15) |
This file contains hidden or 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
| def load_trained_model(filepath): | |
| new_model = load_model(filepath) | |
| return model | |
| model = load_trained_model("models/best_model.hdf5") |
This file contains hidden or 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
| def run_model(model, train_x, train_y, validate_x, validate_y, batch_size, no_epochs): | |
| datetime_now = datetime.datetime.now() | |
| datetime_now = datetime_now.strftime("%Y_%m_%d_%H_%M_%S") | |
| print(datetime_now) | |
| os.mkdir('models/' + str(datetime_now)) | |
| filepath = 'models/' + str(datetime_now) + '/model-{epoch:02d}-{val_accuracy:.2f}.hdf5' | |
| checkpoint = ModelCheckpoint(filepath, monitor='loss', verbose=1, | |
| save_best_only=False, mode='auto', period=1) |
This file contains hidden or 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
| def keras_cnn(input_shape, num_classes): | |
| model = Sequential() | |
| model.add(Conv2D(filters=32, kernel_size=3, activation='relu', input_shape = input_shape)) | |
| model.add(MaxPooling2D(pool_size=2) ) | |
| model.add(Conv2D(filters=64, kernel_size=3, activation='relu')) | |
| model.add(Dropout(0.2)) | |
| model.add(Flatten()) | |
| model.add(Dense(32, activation='relu')) | |
| model.add(Dense(num_classes, activation = 'softmax')) | |
This file contains hidden or 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
| y_train = keras.utils.to_categorical(train_y, num_classes) | |
| y_test = keras.utils.to_categorical(test_y, num_classes) | |
| x_train, x_validate, y_train, y_validate = train_test_split(train_x, y_train, test_size = 0.2, random_state = 16) | |
| input_shape = (28, 28, 1) | |
| x_train = x_train.reshape(x_train.shape[0], *input_shape) | |
| x_validate = x_validate.reshape(x_validate.shape[0], *input_shape) | |
| x_test = test_x.reshape(test_x.shape[0], *input_shape) |
This file contains hidden or 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
| num_classes = 10 | |
| target_names = ['top', 'trouser', 'pullover', 'dress', 'coat', 'sandal', 'shirt', 'sneaker', 'bag', 'boot'] |
This file contains hidden or 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
| train_x = train_x/255 | |
| test_x = test_x/255 |
NewerOlder