deeplearning.ai 第四課第一周, 卷積神經網路的tensorflow實現

來源:互聯網
上載者:User

1、載入需要模組和函數:

import mathimport numpy as npimport h5pyimport matplotlib.pyplot as pltimport scipyfrom PIL import Imagefrom scipy import ndimageimport tensorflow as tffrom tensorflow.python.framework import opsfrom cnn_utils import *%matplotlib inlinenp.random.seed(1)

2、載入資料及資料處理:

# Loading the data (signs)X_train_orig, Y_train_orig, X_test_orig, Y_test_orig, classes = load_dataset()X_train = X_train_orig/255.X_test = X_test_orig/255.Y_train = convert_to_one_hot(Y_train_orig, 6).TY_test = convert_to_one_hot(Y_test_orig, 6).Tprint ("number of training examples = " + str(X_train.shape[0]))print ("number of test examples = " + str(X_test.shape[0]))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))conv_layers = {}

二、模型定義開始:
1、定義place_holder創造函數:

# GRADED FUNCTION: create_placeholdersdef create_placeholders(n_H0, n_W0, n_C0, n_y):    """    Creates the placeholders for the tensorflow session.    Arguments:    n_H0 -- scalar, height of an input image    n_W0 -- scalar, width of an input image    n_C0 -- scalar, number of channels of the input    n_y -- scalar, number of classes    Returns:    X -- placeholder for the data input, of shape [None, n_H0, n_W0, n_C0] and dtype "float"    Y -- placeholder for the input labels, of shape [None, n_y] and dtype "float"    """    ### START CODE HERE ### (≈2 lines)    X = tf.placeholder(tf.float32,[None,n_H0,n_W0,n_C0])    Y = tf.placeholder(tf.float32,[None,n_y])    ### END CODE HERE ###    return X, Y

2、定義初始化函數:

# GRADED FUNCTION: initialize_parametersdef initialize_parameters():    """    Initializes weight parameters to build a neural network with tensorflow. The shapes are:                        W1 : [4, 4, 3, 8]                        W2 : [2, 2, 8, 16]    Returns:    parameters -- a dictionary of tensors containing W1, W2    """    tf.set_random_seed(1)                              # so that your "random" numbers match ours    ### START CODE HERE ### (approx. 2 lines of code)    W1 = tf.get_variable('W1',[4,4,3,8],initializer=tf.contrib.layers.xavier_initializer(seed=0))    W2 = tf.get_variable('W2',[2,2,8,16],initializer=tf.contrib.layers.xavier_initializer(seed=0))    ### END CODE HERE ###    parameters = {"W1": W1,                  "W2": W2}    return parameters

3、定義前向傳播函數(此處到全串連層,並無啟用函數)

# GRADED FUNCTION: forward_propagationdef forward_propagation(X, parameters):    """    Implements the forward propagation for the model:    CONV2D -> RELU -> MAXPOOL -> CONV2D -> RELU -> MAXPOOL -> FLATTEN -> FULLYCONNECTED    Arguments:    X -- input dataset placeholder, of shape (input size, number of examples)    parameters -- python dictionary containing your parameters "W1", "W2"                  the shapes are given in initialize_parameters    Returns:    Z3 -- the output of the last LINEAR unit    """    # Retrieve the parameters from the dictionary "parameters"     W1 = parameters['W1']    W2 = parameters['W2']    ### START CODE HERE ###    # CONV2D: stride of 1, padding 'SAME'    Z1 = tf.nn.conv2d(X,W1,strides=[1,1,1,1],padding='SAME')    # RELU    A1 = tf.nn.relu(Z1)    # MAXPOOL: window 8x8, sride 8, padding 'SAME'    P1 = tf.nn.max_pool(A1,ksize=[1,8,8,1],strides=[1,8,8,1],padding='SAME')    # CONV2D: filters W2, stride 1, padding 'SAME'    Z2 = tf.nn.conv2d(P1,W2,strides=[1,1,1,1],padding='SAME')    # RELU    A2 = tf.nn.relu(Z2)    # MAXPOOL: window 4x4, stride 4, padding 'SAME'    P2 = tf.nn.max_pool(A2,ksize=[1,4,4,1],strides=[1,4,4,1],padding='SAME')    # FLATTEN    P2 = tf.contrib.layers.flatten(P2)    # FULLY-CONNECTED without non-linear activation function (not not call softmax).    # 6 neurons in output layer. Hint: one of the arguments should be "activation_fn=None"     Z3 = tf.contrib.layers.fully_connected(P2,6,activation_fn=None)    ### END CODE HERE ###    return Z3

4、定義代價Function Compute:

# GRADED FUNCTION: compute_cost def compute_cost(Z3, Y):    """    Computes the cost    Arguments:    Z3 -- output of forward propagation (output of the last LINEAR unit), of shape (6, number of examples)    Y -- "true" labels vector placeholder, same shape as Z3    Returns:    cost - Tensor of the cost function    """    ### START CODE HERE ### (1 line of code)    cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=Z3,labels=Y))    ### END CODE HERE ###    return cost

5、模型定義:模型中建立應按照如下步驟:
create placeholders
initialize parameters
forward propagate
compute the cost
create an optimizer

# GRADED FUNCTION: modeldef model(X_train, Y_train, X_test, Y_test, learning_rate = 0.009,          num_epochs = 100, minibatch_size = 64, print_cost = True):    """    Implements a three-layer ConvNet in Tensorflow:    CONV2D -> RELU -> MAXPOOL -> CONV2D -> RELU -> MAXPOOL -> FLATTEN -> FULLYCONNECTED    Arguments:    X_train -- training set, of shape (None, 64, 64, 3)    Y_train -- test set, of shape (None, n_y = 6)    X_test -- training set, of shape (None, 64, 64, 3)    Y_test -- test set, of shape (None, n_y = 6)    learning_rate -- learning rate of the optimization    num_epochs -- number of epochs of the optimization loop    minibatch_size -- size of a minibatch    print_cost -- True to print the cost every 100 epochs    Returns:    train_accuracy -- real number, accuracy on the train set (X_train)    test_accuracy -- real number, testing accuracy on the test set (X_test)    parameters -- parameters learnt by the model. They can then be used to predict.    """    ops.reset_default_graph()                         # to be able to rerun the model without overwriting tf variables    tf.set_random_seed(1)                             # to keep results consistent (tensorflow seed)    seed = 3                                          # to keep results consistent (numpy seed)    (m, n_H0, n_W0, n_C0) = X_train.shape                 n_y = Y_train.shape[1]                                costs = []                                        # To keep track of the cost    # Create Placeholders of the correct shape    ### START CODE HERE ### (1 line)    X, Y = create_placeholders(n_H0,n_W0,n_C0,n_y)    ### END CODE HERE ###    # Initialize parameters    ### START CODE HERE ### (1 line)    parameters = initialize_parameters()    ### END CODE HERE ###    # Forward propagation: Build the forward propagation in the tensorflow graph    ### START CODE HERE ### (1 line)    Z3 = forward_propagation(X,parameters)    ### END CODE HERE ###    # Cost function: Add cost function to tensorflow graph    ### START CODE HERE ### (1 line)    cost = compute_cost(Z3,Y)    ### END CODE HERE ###    # Backpropagation: Define the tensorflow optimizer. Use an AdamOptimizer that minimizes the cost.    ### START CODE HERE ### (1 line)    optimizer = tf.train.AdamOptimizer(learning_rate).minimize(cost)    ### END CODE HERE ###    # Initialize all the variables globally    init = tf.global_variables_initializer()    # Start the session to compute the tensorflow graph    with tf.Session() as sess:        # Run the initialization        sess.run(init)        # Do the training loop        for epoch in range(num_epochs):            minibatch_cost = 0.            num_minibatches = int(m / minibatch_size) # number of minibatches of size minibatch_size in the train set            seed = seed + 1            minibatches = random_mini_batches(X_train, Y_train, minibatch_size, seed)            for minibatch in minibatches:                # Select a minibatch                (minibatch_X, minibatch_Y) = minibatch                # IMPORTANT: The line that runs the graph on a minibatch.                # Run the session to execute the optimizer and the cost, the feedict should contain a minibatch for (X,Y).                ### START CODE HERE ### (1 line)                _ , temp_cost = sess.run([optimizer,cost],feed_dict={X:minibatch_X,Y:minibatch_Y})                ### END CODE HERE ###                minibatch_cost += temp_cost / num_minibatches            # Print the cost every epoch            if print_cost == True and epoch % 5 == 0:                print ("Cost after epoch %i: %f" % (epoch, minibatch_cost))            if print_cost == True and epoch % 1 == 0:                costs.append(minibatch_cost)        # plot the cost        plt.plot(np.squeeze(costs))        plt.ylabel('cost')        plt.xlabel('iterations (per tens)')        plt.title("Learning rate =" + str(learning_rate))        plt.show()        # Calculate the correct predictions        predict_op = tf.argmax(Z3, 1)        correct_prediction = tf.equal(predict_op, tf.argmax(Y, 1))        # Calculate accuracy on the test set        accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))        print(accuracy)        train_accuracy = accuracy.eval({X: X_train, Y: Y_train})        test_accuracy = accuracy.eval({X: X_test, Y: Y_test})        print("Train Accuracy:", train_accuracy)        print("Test Accuracy:", test_accuracy)        return train_accuracy, test_accuracy, parameters

6、模型訓練:

_, _, parameters = model(X_train, Y_train, X_test, Y_test)

7、結果如下:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.