摘要

本文是学习Ng深度学习课程第四部分——卷积神经网络。看完课程做了一下作业,用TensorFlow实现了一个简单的CNN网络,完成了手势0~5数字识别。基本就是课件代码copy,比较遗憾的是numpy实现的同样结构CNN有问题,损失不下降,还出现梯度爆炸。
程序地址:https://github.com/ConstellationBJUT/Coursera-DL-Study-Notes/blob/master/class4_week1_cnn/tf_cnn_classfier.py

网络结构

在这里插入图片描述
图片->conv1->relu->pool1 -> conv2->relu->pool2 -> flatten ->FC->softmax

程序结构

  1. 数据:两个.h5文件,分别是train,X(1080, 64, 64, 3),一共1080个样本,每个样本图片是64643,label(1080, 6)。dev,X(120,64,64,3),label(120, 6)
  2. 程序文件:tf_cnn_classfier.py,包括CNN网络,分类试验等

主程序

用tf主要完成正向传播网络的构建,用框架不用考虑反向传播,还是相当方便的。

def 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']

    # CONV2D: stride of 1, padding 'SAME'
    Z1 = tf.nn.conv2d(X, filter=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, filter=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)

    return Z3

实验结果

直接copy,代码一样,结果也是一样的,老没意思了
在这里插入图片描述

总结

numpy实现的CNN还在查错误,发现实现这种网络结构真的好难纠错啊。这波课程5部分——DNN、改善神经网络(Adam等优化梯度算法)、结构化机器学习(经验之谈)、CNN、RNN(GRU、LSTM),就算刷完了。有些语音和翻译的实验没做,感觉还不错

Logo

技术共进,成长同行——讯飞AI开发者社区

更多推荐