Yongxing Tensorflow notes -3 neural network development foundation

Here Insert Picture Description
Learning together is strongly recommended that this chapter and lesson 4

Click to open

First, what is the model?

TensorFlow model calculation = calculation chart
concept diagram of computing:
TensorFlow name has been explained in the two most important concepts --Tensor (tensor) and Flow (flow). TensorFlow is expressed in the form of a calculation calculated by the programming system of FIG. TensorFlow calculated for each of all the computing nodes of a graph, edge describes dependencies between the nodes is calculated. If an operation is dependent on the output of another input operation, the two operations have dependencies.
The figure, a and b two constants do not depend on any other calculations, add the calculated value of the two constants dependent read. So in this figure is obtained from a side of an edge and to add to add from the b. No dependence add computing nodes, so there is no point to add the node to other nodes edges. TensorFlow programs can be represented by the computation graph form similar to that shown in FIG.
Here Insert Picture Description
FIG calculated using the
TensorFlow procedures may be generally divided into two stages:
Here Insert Picture Description

  • The first stage in FIG define computation all operations;
  • The second stage of the operation is performed

Second, the model is how the birth?

1, the basic meaning of the model of content:
the standard model has an input node, an output of three parts, how to make these three parts together to learn the rules of communication and can be calculated, which is Tensorflow doing things.
Tensorflow upper computing nodes, and the relationship between the intermediate node (OPS) which defines FIG calculated only by the session (the session), specific operations.

  • Calculated figure is static, no matter what the operational definition, they just take the relationship together, will not carry out operations.
  • Session is dynamic, data will flow only after the session calculation map, in accordance with the diagram, the final calculation result from the drawing start flowing.

Tensorflow separate definition and performing calculations.
Construction of a calculation map, it is necessary:

  • Inlet input node, i.e. the FIG.
  • 用于训练中的模型参数(学习参数),是连接各个节点的路径
  • 模型中的节点(OP):OP可以代表模型中的中间节点,是网络中的真正结构。

这三种结构组成计算图的静态网络结构模型。在实际开发过程中,通过动态的会话将图中的各个节点按照静态的规则联系起来,每一次迭代都会对图中的学习参数(比如 权重 w 和 偏置 b ) , 通过一定的迭代后所形成的计算图,便是可以使用的模型。
在会话中,任何一个节点都可以通过会话的run函数进行计算,得到该节点的数值。
2、模型内部的数据流向:

  • 正向
    正向,则是数据从输入开始,依次进行各节点的运算,最后得到输出,是模型最基本的数据流向。
  • 反向
    反向只有在训练的场景下,才会用到。
    即先从正向的最后一个节点开始,计算此时 结果 和 真实值 的误差,这样会形成一个用学习参数表示误差的方程,比如 梯度下降方法: 会对方程中的每个参数进行求导,得到梯度修正值,同时修正权重等学习参数,然后反推出上一层的误差,并接着计算上一层的修正值,直到传到正向的第一个节点。

三、Tensorflow开发基本步骤:

  • 定义tensorlfow输入节点
  • 定义 学习参数 的变量
  • 定义运算
  • 优化函数
  • 初始化变量
  • 迭代更新到最优解
  • 测试模型
  • 使用模型

四、基本开发步骤讲解:

1、定义输入节点:

  • 通过占位符定义:
    由placeholder方法创建,其也是一种常量,但是由用户在调用run方法是传递的,也可以将placeholder理解为一种形参。即其不像constant那样直接可以使用,需要用户传递常数值.
tf.placeholder(dtype=tf.float32, shape=[144, 10], name='X')
  • 参数:
    • dtype:数据类型,必填,默认为value的数据类型,传入参数为tensorflow下的枚举值(float32,float64…)
    • shape:数据形状,选填,不填则随传入数据的形状自行变动,可以在多次调用中传入不同形状的数据
    • name:常量名,选填,默认值不重复,建议根据创建顺序为(Placeholder,Placeholder_1,Placeholder_2…)
  • 通过字典定义:
    比如:
inputDict = {"x":tf.placeholder(tf.float32)}
  • 直接定义输入节点(用的比较少)
    比如:
train_X = np.linspace(-1,1,100)

2、定义 学习参数 的变量:

  • 直接定义:
    比如:
w = tf.Variable(tf.random_normal([1]),name="weight")
  • 通过字典定义:
    比如:
paradict = {
	"w":tf.Variable(tf.random_normal([1])),
	"b":tf.Variable(tf.zeros([1])
	}

3、定义运算:
定义运算的过程是建立模型的核心过程,直接决定的模型的效果。

  • 1、定义正向传播运算:
    即定义前向传播的运算,一般问题越复杂,前向神经网络越复杂,后面我会向大家介绍 卷积神经网络、循环神经网络、GooLeNet等

  • 2、损失函数:
    损失函数就是 输出值 和 真实值 之间的误差,配合反向传播使用,不断迭代中找到最小值,达到模型的最好效果。

  • 3、优化函数:
    在 正向传播 和 损失函数 的基础上 , 我们使用优化函数对学习参数进行优化,这个过程在反向传播中实现。
    反向传播的过程,就是沿着正向传播的过程将误差向反方向传递过去。
    之后我们会涉及到:正则化、随机梯度下降等

  • 4、初始化变量:
    session 创建好后第一件事就是初始化变量!!!

  • 5、迭代更新参数到最优解
    我们一般通过 一次会话 来完成,常常使用with愈发,可以在session结束后自动关闭

  • 6、测试模型:
    是模型验证节点,一般通过输入模型没有训练过的数据和真实结果,来测试和验证模型的效果。

  • 7, using the model:
    and test models, but not necessarily true results were compared, also on behalf of this neural network is used to solve practical problems.

Published 45 original articles · won praise 28 · views 10000 +

Guess you like

Origin blog.csdn.net/m0_43505377/article/details/103879603