class simple_cnn_model(object): def __init__(self, epochs, batch_size, lr): self.epochs = epochs self.batch_size = batch_size self.lr = lr def load_data(self): # load data from cifar100 folder (x_train, y_train), (x_test, y_test) = cifar100(1211506319) return x_train, y_train, x_test, y_test def train_model(self, layers, loss_metrics, x_train, y_train): # build model self.model = Sequential(layers, loss_metrics) # train the model loss = self.model.fit(x_train, y_train, self.epochs, self.lr, self.batch_size, print_output=True) avg_loss = np.mean(np.reshape(loss, (self.epochs, -1)), axis=1) return avg_loss def test_model(self, x_test, y_test): # make a prediction pred_result = self.model.predict(x_test) accuracy = np.mean(pred_result == y_test) return accuracy
from layers import Sequential, Dense import numpy as np from utils import load_ionosphere (X_train, y_train), (X_test, y_test) = load_ionosphere(0.7, normalize=True, shuffled=True) model = Sequential() model.add(Dense(15, input_shape=(X_train.shape[1], ))) model.add(Dense(15)) model.add(Dense(y_train.shape[1])) model.fit(X_train, y_train, reg_factor=0.0, epochs=1000, learning_rate=0.1, batch_size=32, validation_data=(X_test, y_test), verbose=True) model.plot_error() # model.plot_train_error()
# Please make sure that cifar-100-python is present in the same folder as dataset.py (x_train, y_train), (x_test, y_test) = cifar100(1212356299) from layers import (FullLayer, ReluLayer, SoftMaxLayer, CrossEntropyLayer, Sequential) model = Sequential(layers=(FullLayer(3072, 500), ReluLayer(), FullLayer(500, 4), SoftMaxLayer()), loss=CrossEntropyLayer()) lr_accuracies = np.zeros((3, )) loss1 = model.fit(x_train, y_train, lr=0.01, epochs=15) y_predict = model.predict(x_test) count = 0 for i in range(np.size(y_test)): if y_predict[i] == y_test[i]: count += 1 lr_accuracies[0] = (100.0 * count) / np.shape(y_predict)[0] loss2 = model.fit(x_train, y_train, lr=0.1, epochs=15) y_predict = model.predict(x_test) count = 0 for i in range(np.size(y_test)):
(x_train, y_train), (x_test, y_test) = cifar100(1337) model = Sequential(layers=(ConvLayer(3, 16, 3), ReluLayer(), MaxPoolLayer(), ConvLayer(16, 32, 3), ReluLayer(), MaxPoolLayer(), FlattenLayer(), FullLayer(8 * 8 * 32, 4), SoftMaxLayer()), loss=CrossEntropyLayer()) start_time = time.clock() lr_vals = [0.1] losses_train = list() losses_test = list() test_acc = np.zeros(len(lr_vals)) for j in range(len(lr_vals)): train_loss, test_loss = model.fit(x_train, y_train, x_test, y_test, epochs=8, lr=lr_vals[j], batch_size=128) losses_train.append(train_loss) losses_test.append(test_loss) print("--- RUN TIME %s seconds --- \n" % (time.clock() - start_time)) # print("Losses are: ",losses) plt.plot(range(1, 9), losses_train[j], label='Train loss') plt.plot(range(1, 9), losses_test[j], label='Test Loss') predictions_train = model.predict(x_train) y_train_class = np.array( [np.argmax(y_train[i]) for i in range(y_train.shape[0])]) print("Train accuracy is: ", np.mean(predictions_train == y_train_class))
(x_train, y_train), (x_test, y_test) = cifar100(1213268041) test_accuracy = [] epochs = 15 finalloss = [] testloss = [] model = Sequential(layers=(ConvLayer(3, 16, 3), ReluLayer(), MaxPoolLayer(2), ConvLayer(16, 32, 3), ReluLayer(), MaxPoolLayer(2), FlattenLayer(), FullLayer(8 * 8 * 32, 4), SoftMaxLayer()), loss=CrossEntropyLayer()) train_loss, valid_loss = model.fit(x_train, y_train, x_test, y_test, epochs=15, lr=0.1, batch_size=128) y_pred = model.predict(x_test) accuracy = (np.mean(y_test == onehot(y_pred))) print('Accuracy: %.2f' % accuracy) #np.append(test_accuracy, accuracy) plt.plot(range(len(train_loss)), train_loss, label='Training loss') plt.plot(range(len(valid_loss)), valid_loss, label='Testing loss') plt.xlabel('epochs') plt.ylabel('Training & Testing loss') plt.title('Training & Testing loss Vs Epochs for the CIFAR100 data set') plt.legend() plt.show() # plt.plot(lr, test_accuracy)