In [1]:
import numpy as np
import matplotlib.pyplot as plt
from sgd_utils import *

# for auto-reloading extenrnal modules
# see http://stackoverflow.com/questions/1907993/autoreload-of-modules-in-ipython
%load_ext autoreload
%autoreload 2

Load and normalize data


In [4]:
train_set_x_orig, train_set_y_orig, test_set_x_orig, test_set_y_orig = load_data()
X = (train_set_x_orig.reshape(train_set_x_orig.shape[0], -1) / 255.).T
Y = (train_set_y_orig.reshape(train_set_y_orig.shape[0], 1)).T
X_test = (test_set_x_orig.reshape(test_set_x_orig.shape[0], -1) / 255.).T
Y_test = (test_set_y_orig.reshape(test_set_y_orig.shape[0], 1)).T

print('X shape: ', X.shape)
print('Y shape: ', Y.shape)
print('X_test shape: ', X_test.shape)
print('Y_test shape: ', Y_test.shape)


X shape:  (12288, 209)
Y shape:  (1, 209)
X_test shape:  (12288, 50)
Y_test shape:  (1, 50)

Run model with only stochastic gradient descent


In [5]:
layer_dims = [X.shape[0], 5, 3, 1]
parameters = model(X, Y, layer_dims, None, learning_rate = 0.0075)


cost after 0 epochs is: 0.693216477038589
cost after 100 epochs is: 0.6775789784829873
cost after 200 epochs is: 0.6227154309734987
cost after 300 epochs is: 0.5825571562638366
cost after 400 epochs is: 0.6848407529965058
cost after 500 epochs is: 0.5029002128536287
cost after 600 epochs is: 0.6493045364097539
cost after 700 epochs is: 0.5746040703467326
cost after 800 epochs is: 0.6113457984671772
cost after 900 epochs is: 0.6111416955247834
cost after 1000 epochs is: 0.6494321192079329
cost after 1100 epochs is: 0.8389141797580372
cost after 1200 epochs is: 0.5735576854910422
cost after 1300 epochs is: 0.5359951303463659
cost after 1400 epochs is: 0.6113967454504097
cost after 1500 epochs is: 0.6113097575993741
cost after 1600 epochs is: 0.6493879540960793
cost after 1700 epochs is: 0.6493865377626988
cost after 1800 epochs is: 0.7623199981994113
cost after 1900 epochs is: 0.574422916896221
cost after 2000 epochs is: 0.649375993174188
cost after 2100 epochs is: 0.6493817744208286
cost after 2200 epochs is: 0.6116980366625351
cost after 2300 epochs is: 0.5352230091832202
cost after 2400 epochs is: 0.6113249530128401
cost after 2500 epochs is: 0.80206616832342
cost after 2600 epochs is: 0.7626593249450475
cost after 2700 epochs is: 0.6118008726099501
cost after 2800 epochs is: 0.6114739261882802
cost after 2900 epochs is: 0.6872022434783368

Run model with stochastic gradient descent + momentum


In [6]:
layer_dims = [X.shape[0], 5, 3, 1]
parameters = model(X, Y, layer_dims, 'momentum', learning_rate = 0.0075)


cost after 0 epochs is: 0.6913984292798777
cost after 100 epochs is: 0.6078892166787597
cost after 200 epochs is: 0.31191747245005286
cost after 300 epochs is: 0.2799559239580461
cost after 400 epochs is: 0.23446881902819905
cost after 500 epochs is: 0.20720077248928398
cost after 600 epochs is: 0.13553496446194846
cost after 700 epochs is: 0.1656973862523841
cost after 800 epochs is: 0.17603799734096295
cost after 900 epochs is: 0.05496891389241829
cost after 1000 epochs is: 0.1603193240284766
cost after 1100 epochs is: 0.0424952091525869
cost after 1200 epochs is: 0.027597631136612455
cost after 1300 epochs is: 0.0661010608876735
cost after 1400 epochs is: 0.031865373840745434
cost after 1500 epochs is: 0.015383947881821236
cost after 1600 epochs is: 0.16571448496486327
cost after 1700 epochs is: 0.05184188668254035
cost after 1800 epochs is: 0.16743956071135976
cost after 1900 epochs is: 0.1745679920825049
cost after 2000 epochs is: 0.1697878798323619
cost after 2100 epochs is: 0.032800584043302515
cost after 2200 epochs is: 0.025966019251597413
cost after 2300 epochs is: 0.3098942713443068
cost after 2400 epochs is: 0.029661557892054456
cost after 2500 epochs is: 0.16467917261818663
cost after 2600 epochs is: 0.17031026947768904
cost after 2700 epochs is: 0.0138126856983264
cost after 2800 epochs is: 0.022590060280183774
cost after 2900 epochs is: 0.030766552556810555

Run model with stochastic gradient descent + adam


In [3]:
layer_dims = [X.shape[0], 5, 3, 1]
parameters = model(X, Y, layer_dims, 'adam', learning_rate = 0.0075)


cost after 0 epochs is: 0.8968467383358543
cost after 100 epochs is: 0.649382835174094
cost after 200 epochs is: 0.277304559574315
cost after 300 epochs is: 0.11229801924113753
cost after 400 epochs is: 0.06764350361379798
cost after 500 epochs is: 0.18512450421163743
cost after 600 epochs is: 0.01671263394377294
cost after 700 epochs is: 0.038485583946276596
cost after 800 epochs is: 0.4104652076415669
cost after 900 epochs is: 0.17731910347787813
cost after 1000 epochs is: 0.1468499849580865
cost after 1100 epochs is: 0.27985916529282745
cost after 1200 epochs is: 0.5792480536719409
cost after 1300 epochs is: 0.6108670022599494
cost after 1400 epochs is: 0.612076459631792
cost after 1500 epochs is: 0.7265646785405604
cost after 1600 epochs is: 0.6495517760532757
cost after 1700 epochs is: 0.6495945406221988
cost after 1800 epochs is: 0.765743203553134
cost after 1900 epochs is: 0.5383221803230559
cost after 2000 epochs is: 0.5761038475623664
cost after 2100 epochs is: 0.6121136582051357
cost after 2200 epochs is: 0.5341311338645961
cost after 2300 epochs is: 0.5311875059038412
cost after 2400 epochs is: 0.6874878245032744
cost after 2500 epochs is: 0.6494441275778778
cost after 2600 epochs is: 0.6884041750231994
cost after 2700 epochs is: 0.6113048766379944
cost after 2800 epochs is: 0.6882320174888693
cost after 2900 epochs is: 0.6856976627223782

In [ ]:
layer_dims = [X.shape[0], 5, 3, 1]
parameters = model_with_batch_norm(X, Y, layer_dims, learning_rate = 0.0075)


cost after 0 epochs is: 0.8112514523654798
cost after 100 epochs is: 0.050734615282211015
cost after 200 epochs is: 0.029029921495605026
cost after 300 epochs is: 0.008348192206005392
cost after 400 epochs is: 0.2077440175844491
cost after 500 epochs is: 0.041949710517007334
cost after 600 epochs is: 0.18954437281435682
cost after 700 epochs is: 0.5515385652239283
cost after 800 epochs is: 0.04103201538813437
cost after 900 epochs is: 0.0009737271174689137
cost after 1000 epochs is: 0.003946917272065924
cost after 1100 epochs is: 0.054644518598327384
cost after 1200 epochs is: 0.0017228859540665531
cost after 1300 epochs is: 0.00732630279731838
cost after 1400 epochs is: 0.18984970036519486
cost after 1500 epochs is: 0.0008343984551257967
cost after 1600 epochs is: 0.0008491969058700982
cost after 1700 epochs is: 0.0005019724819177994
cost after 1800 epochs is: 0.0003856637681342541
cost after 1900 epochs is: 0.0033222441894589596
cost after 2000 epochs is: 0.034120376311843496
cost after 2100 epochs is: 0.001370990803610152
cost after 2200 epochs is: 0.04293940881706621
cost after 2300 epochs is: 0.0008458689090053744
cost after 2400 epochs is: 0.0014236790043322804
cost after 2500 epochs is: 0.0010304503890907998
cost after 2600 epochs is: 0.018490072341974924
cost after 2700 epochs is: 0.00015695742149765774
cost after 2800 epochs is: 0.0012762409368516242
cost after 2900 epochs is: 0.0028308407315349475

In [ ]: