예제 #1
0
    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
예제 #2
0
# 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)):
    if y_predict[i] == y_test[i]:
예제 #3
0
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))

    predictions_test = model.predict(x_test)
    y_test_class = np.array(
        [np.argmax(y_test[i]) for i in range(y_test.shape[0])])
    test_acc[j] = np.mean(predictions_test == y_test_class)
    print("Test accuracy is: ", np.mean(predictions_test == y_test_class))

plt.xlabel('Epoch Number --->')
plt.ylabel('Loss value --->')
plt.title('Loss v/s epochs curve')
plt.legend()
plt.show()
예제 #4
0
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)
# plt.title('Test Accuracy Vs Learning Rate for Modified NN')
# plt.xlabel('Learning rate')
# plt.ylabel('Testing Accuracy')
# plt.show()