def load_dataset ( ) :
with h5py. File( './dataset4/train.h5' ) as f:
classes = np. array( f[ 'list_classes' ] )
x_train = np. array( f[ 'train_set_x' ] )
y_train = np. array( f[ 'train_set_y' ] )
with h5py. File( './dataset4/test.h5' ) as f:
x_test = np. array( f[ 'test_set_x' ] )
y_test = np. array( f[ 'test_set_y' ] )
return x_train, y_train, x_test, y_test, classes
def convert2onehot ( y, c) :
return np. eye( c) [ y] . T
x_train_orig, y_train_orig, x_test_orig, y_test_orig, classes = load_dataset( )
x_train_flatten = x_train_orig. reshape( x_train_orig. shape[ 0 ] , - 1 ) . T
x_test_flatten = x_test_orig. reshape( x_test_orig. shape[ 0 ] , - 1 ) . T
x_train = x_train_flatten/ 255 .
x_test = x_test_flatten/ 255 .
y_train = convert2onehot( y_train_orig, classes. size)
y_test = convert2onehot( y_test_orig, classes. size)
print ( "number of training examples = " + str ( x_train. shape[ 1 ] ) )
print ( "number of test examples = " + str ( x_test. shape[ 1 ] ) )
print ( "X_train shape: " + str ( x_train. shape) )
print ( "Y_train shape: " + str ( y_train. shape) )
print ( "X_test shape: " + str ( x_test. shape) )
print ( "Y_test shape: " + str ( y_test. shape) )
def initialize_parameters ( ) :
tf. set_random_seed( 1 )
w1 = tf. get_variable( "w1" , [ 100 , 12288 ] , initializer = tf. contrib. layers. variance_scaling_initializer( seed = 1 ) )
b1 = tf. get_variable( "b1" , [ 100 , 1 ] , initializer = tf. zeros_initializer( ) )
w2 = tf. get_variable( "w2" , [ 50 , 100 ] , initializer = tf. contrib. layers. variance_scaling_initializer( seed = 1 ) )
b2 = tf. get_variable( "b2" , [ 50 , 1 ] , initializer = tf. zeros_initializer( ) )
w3 = tf. get_variable( "w3" , [ 6 , 50 ] , initializer = tf. contrib. layers. variance_scaling_initializer( seed = 1 ) )
b3 = tf. get_variable( "b3" , [ 6 , 1 ] , initializer = tf. zeros_initializer( ) )
params = {
'w1' : w1,
'b1' : b1,
'w2' : w2,
'b2' : b2,
'w3' : w3,
'b3' : b3,
}
return params
def forward_propagation ( x, params) :
w1 = params[ 'w1' ]
b1 = params[ 'b1' ]
w2 = params[ 'w2' ]
b2 = params[ 'b2' ]
w3 = params[ 'w3' ]
b3 = params[ 'b3' ]
z1 = tf. add( tf. matmul( w1, x) , b1)
a1 = tf. nn. relu( z1)
z2 = tf. add( tf. matmul( w2, a1) , b2)
a2 = tf. nn. relu( z2)
z3 = tf. add( tf. matmul( w3, a2) , b3)
return z3
def compute_cost ( z3, y) :
logits = tf. transpose( z3)
labels = tf. transpose( y)
cost = tf. reduce_mean( tf. nn. softmax_cross_entropy_with_logits( logits = logits, labels = labels) )
return cost
def create_placeholders ( n_x, n_y) :
x = tf. placeholder( tf. float32, shape= ( n_x, None ) )
y = tf. placeholder( tf. float32, shape= ( n_y, None ) )
return x, y
def random_mini_batches ( x, y, mini_batch_size = 64 , seed = 0 ) :
m = x. shape[ 1 ]
mini_batches = [ ]
np. random. seed( seed)
permutation = list ( np. random. permutation( m) )
shuffled_x = x[ : , permutation]
shuffled_y = y[ : , permutation]
num_minibatches = int ( m/ mini_batch_size)
for k in range ( num_minibatches) :
mini_batch_x = shuffled_x[ : , k * mini_batch_size : k * mini_batch_size + mini_batch_size]
mini_batch_y = shuffled_y[ : , k * mini_batch_size : k * mini_batch_size + mini_batch_size]
mini_batch = ( mini_batch_x, mini_batch_y)
mini_batches. append( mini_batch)
if m % mini_batch_size != 0 :
mini_batch_x = shuffled_x[ : , num_minibatches * mini_batch_size : m]
mini_batch_y = shuffled_y[ : , num_minibatches * mini_batch_size : m]
mini_batch = ( mini_batch_x, mini_batch_y)
mini_batches. append( mini_batch)
return mini_batches
def model ( x_train, y_train, x_test, y_test, learning_rate = 0.0001 , num_epochs = 1500 , minibatch_size = 64 ) :
tf. reset_default_graph( )
tf. set_random_seed( 1 )
seed = 3
( n_x, m) = x_train. shape
n_y = y_train. shape[ 0 ]
costs = [ ]
x, y = create_placeholders( n_x, n_y)
params = initialize_parameters( )
z3 = forward_propagation( x, params)
cost = compute_cost( z3, y)
optimizer = tf. train. GradientDescentOptimizer( learning_rate = learning_rate) . minimize( cost)
init = tf. global_variables_initializer( )
with tf. Session( ) as sess:
sess. run( init)
for epoch in range ( num_epochs) :
epoch_cost = 0 .
num_minibatches = int ( m / minibatch_size)
seed = seed + 1
minibatches = random_mini_batches( x_train, y_train, minibatch_size, seed)
for minibatch in minibatches:
( minibatch_x, minibatch_y) = minibatch
_ , minibatch_cost = sess. run( [ optimizer, cost] , feed_dict= { x: minibatch_x, y: minibatch_y} )
epoch_cost += minibatch_cost / num_minibatches
if epoch % 100 == 0 :
print ( "Cost after epoch %i: %f" % ( epoch, epoch_cost) )
if epoch % 5 == 0 :
costs. append( epoch_cost)
correct_prediction = tf. equal( tf. argmax( z3) , tf. argmax( y) )
accuracy = tf. reduce_mean( tf. cast( correct_prediction, "float" ) )
print ( "Train Accuracy:" , accuracy. eval ( { x: x_train, y: y_train} ) )
print ( "Test Accuracy:" , accuracy. eval ( { x: x_test, y: y_test} ) )