构造你自己的第一个神经网络

通过手势的图片识别图片比划的数字:
1) 现在用1080张64*64的图片作为训练集
2) 用120张图片作为测试集

 定义初始化值

  1. def load_dataset():
  2. train_dataset = h5py.File('datasets/train_signs.h5', "r")
  3. train_set_x_orig = np.array(train_dataset["train_set_x"][:]) # your train set features
  4. train_set_y_orig = np.array(train_dataset["train_set_y"][:]) # your train set labels
  5.  
  6. test_dataset = h5py.File('datasets/test_signs.h5', "r")
  7. test_set_x_orig = np.array(test_dataset["test_set_x"][:]) # your test set features
  8. test_set_y_orig = np.array(test_dataset["test_set_y"][:]) # your test set labels
  9.  
  10. classes = np.array(test_dataset["list_classes"][:]) # the list of classes
  11.  
  12. train_set_y_orig = train_set_y_orig.reshape((1, train_set_y_orig.shape[0]))
  13. test_set_y_orig = test_set_y_orig.reshape((1, test_set_y_orig.shape[0]))
  14.  
  15. return train_set_x_orig, train_set_y_orig, test_set_x_orig, test_set_y_orig, classes
  16.  
  17. X_train_orig, Y_train_orig, X_test_orig, Y_test_orig, classes = load_dataset()

小测:

  1. import matplotlib.pyplot as plt
  2. index = 0
  3. plt.imshow(X_train_orig[index])
  4. print(Y_train_orig)
  5. print ("y = " + str(np.squeeze(Y_train_orig[:, index])))

小测2:把矩阵降维为一维,并做分类映射

  1. # Flatten the training and test images
  2. X_train_flatten = X_train_orig.reshape(X_train_orig.shape[0], -1).T
  3. X_test_flatten = X_test_orig.reshape(X_test_orig.shape[0], -1).T
  4. # Normalize image vectors
  5. X_train = X_train_flatten/255.
  6. X_test = X_test_flatten/255.
  7. # Convert training and test labels to one hot matrices
  8. Y_train = convert_to_one_hot(Y_train_orig, 6)
  9. Y_test = convert_to_one_hot(Y_test_orig, 6)
  10.  
  11. print ("number of training examples = " + str(X_train.shape[1]))
  12. print ("number of test examples = " + str(X_test.shape[1]))
  13. print ("X_train shape: " + str(X_train.shape))
  14. print ("Y_train shape: " + str(Y_train.shape))
  15. print ("X_test shape: " + str(X_test.shape))
  16. print ("Y_test shape: " + str(Y_test.shape))
  17.  
  18. 结果:number of training examples = 1080
  19. number of test examples = 120
  20. X_train shape: (12288, 1080)
  21. Y_train shape: (6, 1080)
  22. X_test shape: (12288, 120)
  23. Y_test shape: (6, 120)

线性回归模型:LINEAR -> RELU -> LINEAR -> RELU -> LINEAR -> SOFTMAX.
Softmax 是判断哪个分类的概率最大

  1. 3.1 创建容器 存放变量
  1. def create_placeholders(n_x,n_y):
  2. X = tf.placeholder(tf.float32, shape=[n_x, None])
  3. Y = tf.placeholder(tf.float32, shape=[n_y, None])
  4. return X,Y

小测:

  1. X, Y = create_placeholders(12288, 6)
  2. print ("X = " + str(X))
  3. print ("Y = " + str(Y))
  1. 3.2 初始化参数
  1. tensorflow里有get_variable初始化参数,通过Xavier进行设置变量的权重
  1. W1 = tf.get_variable("W1", [25,12288], initializer = tf.contrib.layers.xavier_initializer(seed = 1))
  1. b1 = tf.get_variable("b1", [25,1], initializer = tf.zeros_initializer())
  1. def initialize_parameters():
  2. tf.set_random_seed(1) # so that your "random" numbers match ours
  3.  
  4. ### START CODE HERE ### (approx. 6 lines of code)
  5. W1 = tf.get_variable("W1", [25,12288], initializer = tf.contrib.layers.xavier_initializer(seed = 1))
  6. b1 = tf.get_variable("b1", [25,1], initializer = tf.zeros_initializer())
  7. W2 = tf.get_variable("W2", [12,25], initializer = tf.contrib.layers.xavier_initializer(seed = 1))
  8. b2 = tf.get_variable("b2", [12,1], initializer = tf.zeros_initializer())
  9. W3 = tf.get_variable("W3", [6,12], initializer = tf.contrib.layers.xavier_initializer(seed = 1))
  10. b3 = tf.get_variable("b3", [6,1], initializer = tf.zeros_initializer())
  11. ### END CODE HERE ###
  12.  
  13. parameters = {"W1": W1,
  14. "b1": b1,
  15. "W2": W2,
  16. "b2": b2,
  17. "W3": W3,
  18. "b3": b3}
  19.  
  20. return parameters

3.3 向前传播 训练集训练

常用到的tensorflow函数:
tf.add(…,..)
tf.matmul(..,..) 矩阵阶乘
tf.nn.relu(..) Relu激活函数

  1. def forward_propagation(X, parameters):
  2. # Retrieve the parameters from the dictionary "parameters"
  3. print(X.shape)
  4. W1 = parameters['W1']
  5. b1 = parameters['b1']
  6. W2 = parameters['W2']
  7. b2 = parameters['b2']
  8. W3 = parameters['W3']
  9. b3 = parameters['b3']
  10.  
  11. ### START CODE HERE ### (approx. 5 lines) # Numpy Equivalents:
  12. Z1 = tf.add(tf.matmul(W1, X), b1) # Z1 = np.dot(W1, X) + b1
  13. A1 = tf.nn.relu(Z1) # A1 = relu(Z1)
  14. Z2 = tf.add(tf.matmul(W2, A1), b2) # Z2 = np.dot(W2, a1) + b2
  15. A2 = tf.nn.relu(Z2) # A2 = relu(Z2)
  16. Z3 = tf.add(tf.matmul(W3, A2), b3) # Z3 = np.dot(W3,Z2) + b3
  17. ### END CODE HERE ###
  18.  
  19. return Z3

小测:

  1. tf.reset_default_graph()
  2. With tf.Session() as sess:
  3. X,Y = create_placeholders(12888,6)
  4. Parameters = initialize_parameters()
  5. Z3 = forward_propagation(X,parameters)
  6. Print(“Z3=”+str(Z3))

 3.4 计算损失函数(成本函数 Cost function)

在tensorflow 函数里 有tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=…,labels=…)) 其中

softmax_cross_entropy_with_logits是计算softmax函数

  1. def conpute_cost(Z3,Y)
  2. logits = tf.transpose(Z3) ##向量的转置
  3. labels = tf.transpose(Y) ##向量的转置
  4.  
  5. cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits,labels=labels))
  6. return cost

3.5 向后传播 求导 参数更新

向后传播 主要是通过求导来进行梯度下降 然后优化参数模型

其根本就是对损失函数求最小值

优化函数:

Optimizer = tf.train.GrandientDescentOptimizer(learning_rate = learning_rate).minimize(cost)

执行函数:

_,c=sess.run([optimizer,cost],feed_dict={X:minibatch_X,Y:minibatch_Y})

3.6 一个完整的例子 (把上面的代码块汇总成功能)

  1. def model(X_train, Y_train, X_test, Y_test, learning_rate = 0.0001,
  2. num_epochs = 1500, minibatch_size = 32, print_cost = True):
  3. """
  4. Implements a three-layer tensorflow neural network: LINEAR->RELU->LINEAR->RELU->LINEAR->SOFTMAX.
  5.  
  6. Arguments:
  7. X_train -- training set, of shape (input size = 12288, number of training examples = 1080)
  8. Y_train -- test set, of shape (output size = 6, number of training examples = 1080)
  9. X_test -- training set, of shape (input size = 12288, number of training examples = 120)
  10. Y_test -- test set, of shape (output size = 6, number of test examples = 120)
  11. learning_rate -- learning rate of the optimization
  12. num_epochs -- number of epochs of the optimization loop
  13. minibatch_size -- size of a minibatch
  14. print_cost -- True to print the cost every 100 epochs
  15.  
  16. Returns:
  17. parameters -- parameters learnt by the model. They can then be used to predict.
  18. """
  19.  
  20. ops.reset_default_graph() # to be able to rerun the model without overwriting tf variables
  21. tf.set_random_seed(1) # to keep consistent results
  22. seed = 3 # to keep consistent results
  23. (n_x, m) = X_train.shape # (n_x: input size, m : number of examples in the train set)
  24. n_y = Y_train.shape[0] # n_y : output size
  25. costs = [] # To keep track of the cost
  26.  
  27. # Create Placeholders of shape (n_x, n_y)
  28. ### START CODE HERE ### (1 line)
  29. X, Y = create_placeholders(n_x, n_y)
  30. ### END CODE HERE ###
  31.  
  32. # Initialize parameters
  33. ### START CODE HERE ### (1 line)
  34. parameters = initialize_parameters()
  35. ### END CODE HERE ###
  36.  
  37. # Forward propagation: Build the forward propagation in the tensorflow graph
  38. ### START CODE HERE ### (1 line)
  39. Z3 = forward_propagation(X, parameters)
  40. ### END CODE HERE ###
  41.  
  42. # Cost function: Add cost function to tensorflow graph
  43. ### START CODE HERE ### (1 line)
  44. cost = compute_cost(Z3, Y)
  45. ### END CODE HERE ###
  46.  
  47. # Backpropagation: Define the tensorflow optimizer. Use an AdamOptimizer.
  48. ### START CODE HERE ### (1 line)
  49. optimizer = tf.train.AdamOptimizer(learning_rate = learning_rate).minimize(cost)
  50. ### END CODE HERE ###
  51.  
  52. # Initialize all the variables
  53. init = tf.global_variables_initializer()
  54.  
  55. # Start the session to compute the tensorflow graph
  56. with tf.Session() as sess:
  57.  
  58. # Run the initialization
  59. sess.run(init)
  60.  
  61. # Do the training loop
  62. for epoch in range(num_epochs):
  63.  
  64. epoch_cost = 0. # Defines a cost related to an epoch
  65. num_minibatches = int(m / minibatch_size) # number of minibatches of size minibatch_size in the train set
  66. seed = seed + 1
  67. minibatches = random_mini_batches(X_train, Y_train, minibatch_size, seed)
  68.  
  69. for minibatch in minibatches:
  70.  
  71. # Select a minibatch
  72. (minibatch_X, minibatch_Y) = minibatch
  73.  
  74. # IMPORTANT: The line that runs the graph on a minibatch.
  75. # Run the session to execute the "optimizer" and the "cost", the feedict should contain a minibatch for (X,Y).
  76. ### START CODE HERE ### (1 line)
  77. _ , minibatch_cost = sess.run([optimizer, cost], feed_dict={X: minibatch_X, Y: minibatch_Y})
  78. ### END CODE HERE ###
  79.  
  80. epoch_cost += minibatch_cost / num_minibatches
  81.  
  82. # Print the cost every epoch
  83. if print_cost == True and epoch % 100 == 0:
  84. print ("Cost after epoch %i: %f" % (epoch, epoch_cost))
  85. if print_cost == True and epoch % 5 == 0:
  86. costs.append(epoch_cost)
  87.  
  88. # plot the cost
  89. plt.plot(np.squeeze(costs))
  90. plt.ylabel('cost')
  91. plt.xlabel('iterations (per tens)')
  92. plt.title("Learning rate =" + str(learning_rate))
  93. plt.show()
  94.  
  95. # lets save the parameters in a variable
  96. parameters = sess.run(parameters)
  97. print ("Parameters have been trained!")
  98.  
  99. # Calculate the correct predictions
  100. correct_prediction = tf.equal(tf.argmax(Z3), tf.argmax(Y))
  101.  
  102. # Calculate accuracy on the test set
  103. accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
  104.  
  105. print ("Train Accuracy:", accuracy.eval({X: X_train, Y: Y_train}))
  106. print ("Test Accuracy:", accuracy.eval({X: X_test, Y: Y_test}))
  107.  
  108. return parameters

我们执行:

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

得到结果:

tensorflow的 函数库很多,这里是冰山一角,还有很多需要我们去学习。后面有时间,就把图像识别的卷积的tensorflow例子给搬出研究一下。

我的大都内容来自吴恩达的公益视频和教案,特此鸣谢。

参考:吴恩达网易课程

  1.  

tensorflow 从入门到摔掉肋骨 教程二的更多相关文章

  1. 给深度学习入门者的Python快速教程 - numpy和Matplotlib篇

    始终无法有效把word排版好的粘贴过来,排版更佳版本请见知乎文章: https://zhuanlan.zhihu.com/p/24309547 实在搞不定博客园的排版,排版更佳的版本在: 给深度学习入 ...

  2. SpringBoot入门教程(二)CentOS部署SpringBoot项目从0到1

    在之前的博文<详解intellij idea搭建SpringBoot>介绍了idea搭建SpringBoot的详细过程, 并在<CentOS安装Tomcat>中介绍了Tomca ...

  3. 基于tensorflow的MNIST手写数字识别(二)--入门篇

    http://www.jianshu.com/p/4195577585e6 基于tensorflow的MNIST手写字识别(一)--白话卷积神经网络模型 基于tensorflow的MNIST手写数字识 ...

  4. Tensorflow高速入门2--实现手写数字识别

    Tensorflow高速入门2–实现手写数字识别 环境: 虚拟机ubuntun16.0.4 Tensorflow 版本号:0.12.0(仅使用cpu下) Tensorflow安装见: http://b ...

  5. 给深度学习入门者的Python快速教程

    给深度学习入门者的Python快速教程 基础篇 numpy和Matplotlib篇 本篇部分代码的下载地址: https://github.com/frombeijingwithlove/dlcv_f ...

  6. TensorFlow从入门到实战资料汇总 2017-02-02 06:08 | 数据派

    TensorFlow从入门到实战资料汇总 2017-02-02 06:08 | 数据派 来源:DataCastle数据城堡 TensorFlow是谷歌基于DistBelief进行研发的第二代人工智能学 ...

  7. 【0】TensorFlow光速入门-序

    本文地址:https://www.cnblogs.com/tujia/p/13863181.html 序言: 对于我这么一个技术渣渣来说,想学习TensorFlow机器学习,实在是太难了: 百度&qu ...

  8. 【2】TensorFlow光速入门-数据预处理(得到数据集)

    本文地址:https://www.cnblogs.com/tujia/p/13862351.html 系列文章: [0]TensorFlow光速入门-序 [1]TensorFlow光速入门-tenso ...

  9. 无废话ExtJs 入门教程二十一[继承:Extend]

    无废话ExtJs 入门教程二十一[继承:Extend] extjs技术交流,欢迎加群(201926085) 在开发中,我们在使用视图组件时,经常要设置宽度,高度,标题等属性.而这些属性可以通过“继承” ...

随机推荐

  1. hadoop配置文件详解,安装及相关操作

    一.      Hadoop伪分布配置 1. 在conf/hadoop-env.sh文件中增加:export JAVA_HOME=/home/Java/jdk1.6  2.  在conf/core-s ...

  2. Java面向对象 Main函数 静态的应用 单例设计模式

     Java面向对象 Main函数 静态的应用与单例设计模式 知识概要             (1)Main函数的细解 (2)静态的应用,静态变量,静态代码块,静态函数 (3)单例设计模式 1.M ...

  3. Bootstrap文本排版基础--Bootsrap

    1.排版前的基础 (1)移动设备优先 <meta name="viewport" content="width=device-width, initial-scal ...

  4. 【学习】js学习笔记:对象的遍历和封装特性

    1.对象的属性访问: 对象.属性 对象[属性],但中括号中必须是字符串 2.属性的遍历: for in方法举例: var ren={}; ren.name="名字"; ren.ea ...

  5. AlexNet 网络详解及Tensorflow实现源码

    版权声明:本文为博主原创文章,未经博主允许不得转载. 1. 图片数据处理 2. 卷积神经网络 2.1. 卷积层 2.2. 池化层 2.3. 全链层 3. AlexNet 4. 用Tensorflow搭 ...

  6. Golang:使用 httprouter 构建 API 服务器

    https://medium.com/@gauravsingharoy/build-your-first-api-server-with-httprouter-in-golang-732b7b01f6 ...

  7. SQL 表结构操作

    数据库知识总结(表结构操作) 1.创建表Scores 1 create table Scores --表名 2 (Id int identity(1,1) primary key,--设置主键,并且行 ...

  8. 填个小坑,Vue不支持IE8及以下,跨域ajax不支持IE9

    这特么就尴尬了,说好的Vue支持IE8及以下的呢,引入jquery,测试IE个浏览器,IE9仍然显示不正常, 然而命令行测试Vue仍然存在, 数据回不来!数据回不来!数据回不来! 好吧  肉包子打狗$ ...

  9. UVa816,Ordering Tasks,WA

    #include <iostream> #include <cstdio> #include <string> #include <cstring> # ...

  10. LINUX 笔记-tee命令

    作用:把输出的一个副本输送到标准输出,另一个副本拷贝到相应的文件中 格式:tee filename 例:who | tee who.out 使用who命令,结果输出到屏幕上,同时保存在who.out文 ...