TensorFlow 基础知识(笔记)

来源:互联网 发布:fps耳机测试软件 编辑:程序博客网 时间:2024/06/05 02:42

  1. TensorFlow 是一种采用数据流图,用于数值计算的开源软件库。其中Tensor代表传递的数据为张量(多维数组),Flow代表使用计算图进行运算。数据流图用「结点」(nodes)和「边」(edges)组成的有向图来描述数学运算。「结点」一般用来表示施加的数学操作,但也可以表示数据输入的起点和输出的终点,或者是读取/写入持久变量(persistent variable)的终点。边表示结点之间的输入/输出关系。这些数据边可以传送维度可动态调整的多维数据数组,即张量(tensor)。
    在 Tensorflow 中,所有不同的变量和运算都是储存在计算图,在我们构建完模型所需要的图之后,还需要打开一个会话(Session)来运行整个计算图。
note1 = tf.constant(3,tf.float32)note2 = tf.constant(4,tf.float32)note3 = tf.add(note1,note2)print(note1,note2)print(note3)sess = tf.Session()print('note1,note2 = ',sess.run([note1,note2]))print('note3 = ',sess.run(note3))

运行结果

Tensor("Const:0",shape=(),dtype=float32) Tensor("Const_1:0",shape=(),dtype=float32)Tensor("Add:0", shape=(), dtype=float32)note1,note2 =  [3.0, 4.0]note3 =  7.0

2.TensorFlow基本单位
2.1 常量(Constant)
常量定义后值和维度不可变,在神经网络中,常量可储存超参数或其他结构信息的。
tf.constant(value, dtype=None, shape=None, name=”Const”, verify_shape=False)
2.2 变量(Variable)
变量定义后值可变而维度不可变。在神经网络中,变量一般可作为储存权重和其他信息的矩阵。

#variablex=tf.Variable(initial_value=[[1,1],[1,1]],dtype=tf.float32)init_op=tf.global_variables_initializer()with tf.Session() as sess:    sess.run(init_op)    print('x=',sess.run(x))    x.load(value=[[1,2],[3,4]])    print(x)    print(x.eval())

运行结果

运行结果:x= [[ 1.  1.]    [ 1.  1.]]x= <tf.Variable 'Variable:0' shape=(2, 2) dtype=float32_ref>x= [[ 1.  2.]    [ 3.  4.]]

2.3 占位符(Placeholder)和 feed_dict
占位符并没有初始值,它只会分配必要的内存。在会话中,占位符可以使用 feed_dict 馈送数据。
在训练神经网络时需要每次提供一个批量的训练样本,如果每次迭代选取的数据要通过常量表示,那么TensorFlow的计算图会非常大。因为每增加一个常量,TensorFlow 都会在计算图中增加一个结点,而占位符只会拥有占位符这一个结点。

#placeholdera = tf.placeholder(tf.float32)b = tf.placeholder(tf.float32)add = a+bwith tf.Session() as sess:    print(sess.run(add,feed_dict={a:1,b:2}))    print(sess.run(add, feed_dict={a:[1,2], b:[3,4]}))

运行结果

3.0[ 4.  6.]

3.张量
在 TensorFlow 中,张量是计算图执行运算的基本载体,我们需要计算的数据都以张量的形式储存或声明。

维度 数学意义 例子 0 标量数字(只表示大小) a=8 1 向量(表示大小和方向) b=[1,2],c=[1,2,3] 2 矩阵 d=[[1],[2]],e=[[1,1],[2,2],[3,3]] n …… ……

4.TensorFlow模型构建
TensorFlow 机器学习模型流程,即构建计算图、传入数据、更新数据、返回输出值。
在构建计算图时,我们需要构建整个模型的架构。例如在神经网络模型中,我们需要从输入层开始构建整个神经网络的架构,包括隐藏层的数量、每一层神经元的数量、层级之间连接的情况与权重、整个网络每个神经元使用的激活函数等内容。此外,我们还需要配置整个训练、验证与测试的过程。例如在神经网络中,定义整个正向传播的过程与参数并设定学习率、正则化率和批量大小等各类训练超参数。
5.例子
5.1 线性回归
这里写图片描述
其中[×]为数据点,(x1,y1)=(1,1),(x2,y2)=(2,2),(x3,y3)=(3,3),
找出一条最逼近的线,并预测x=2.5时,y=?
1)构建目标函数
目标函数即 H(x)=Wx+b,其中x为特征向量,W权重、b 为偏置项。

x = tf.placeholder(tf.float32)y = tf.placeholder(tf.float32)W = tf . Variable ( tf . random_normal ([ 1 ]), name = 'weight' )b = tf . Variable ( tf . random_normal ([ 1 ]), name = 'bias' )hx = x * W + b#目标函数

2)构建损失函数
这里写图片描述

cost = tf . reduce_mean ( tf . square ( hx - y ))

3)梯度下降更新权重

optimizer = tf . train . GradientDescentOptimizer ( learning_rate = 0.01 )train = optimizer . minimize ( cost )

4)打开对话,计算图,训练并预测

with tf.Session() as sess:    # Initializes global variables in the graph .    sess.run ( tf.global_variables_initializer ())    # Fit the line    x_train = [ 1 , 2 , 3 ]    y_train = [ 1 , 2 , 3 ]    for step in range ( 2001 ):        sess.run ( train ,feed_dict={x:x_train,y:y_train})        if step % 500 == 0 :            print( step , sess.run(cost,feed_dict={x:x_train,y:y_train}),sess.run(W),sess.run(b))    print(sess.run(hx,feed_dict={x:[2.5]}))

5)运行结果

0 cost: 19.578 W: [-0.73227513] b: [ 0.35236371]500 cost: 0.00593153 W: [ 0.93590212] b: [ 0.18845552]1000 cost: 0.000295787 W: [ 0.9856863] b: [ 0.04208388]1500 cost: 1.47498e-05 W: [ 0.99680364] b: [ 0.00939764]2000 cost: 7.3561e-07 W: [ 0.99928612] b: [ 0.00209878]y = [ 2.500314]

5.2逻辑回归
分类问题是逻辑回归中的一类问题,x_data = [[1, 2], [2, 3], [3, 4], [4, 3], [5, 3], [6, 2]],y_data = [[0], [0], [0], [1], [1], [1]]
这里写图片描述
1)构建分类函数

X = tf.placeholder(tf.float32, shape=[None, 2])Y = tf.placeholder(tf.float32, shape=[None, 1])W = tf.Variable(tf.random_normal([2, 1]), name='weight')b = tf.Variable(tf.random_normal([1]), name='bias')hx = tf.sigmoid(tf.matmul(X, W) + b)

2)构建损失函数和梯度下降函数

cost = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=hx, labels=Y))train = tf.train.GradientDescentOptimizer(learning_rate=0.01).minimize(cost)predicted = tf.cast(hx > 0.5, dtype=tf.float32)accuracy = tf.reduce_mean(tf.cast(tf.equal(predicted, Y), dtype=tf.float32))

3)训练及测试

with tf.Session() as sess:   sess.run(tf.global_variables_initializer())   for step in range(10001):       cost_val, _ = sess.run([cost, train], feed_dict={X: x_data, Y: y_data})       if step % 1000 == 0:            print(step, cost_val)   h, c, a = sess.run([hx, predicted, accuracy],                      feed_dict={X: x_data, Y: y_data})   print("\nHx: ", h, "\nCorrect (Y): ", c, "\nAccuracy: ", a)   print('y', sess.run(predicted, feed_dict={X: [[4, 2]]}))

4)运行结果

Hx:  [ [ 0.02937358]      [ 0.03644223]      [ 0.04513283]      [ 0.85326111]      [ 0.98632032]      [ 0.99988723]] Correct (Y):  [[ 0.]            [ 0.]            [ 0.]            [ 1.]            [ 1.]            [ 1.]] Accuracy:  1.0y  [[ 1.]]

5.3 异或问题

输入 [ 0 , 0 ] [ 0 , 1 ] [ 1 , 0 ] [ 1 , 1 ] 输出 0 1 1 0

这里写图片描述
1)构建计算图

x = tf.placeholder("float", shape = [None,2])y = tf.placeholder("float",shape=[None,1])weights = {    'w1':tf.Variable(tf.random_normal([2,16])),    'w2':tf.Variable(tf.random_normal([16,1]))}biases = {    'b1':tf.Variable(tf.random_normal([16])),    'b2':tf.Variable(tf.random_normal([1]))}def nn(_X,_weights,_biases):    d1 = tf.matmul(_X, _weights['w1'])+_biases['b1']    d1 = tf.nn.relu(d1)    d2 = tf.matmul(d1,_weights['w2'])+_biases['b2']    d2 = tf.nn.sigmoid(d2)    return d2pred = nn(x, weights, biases)

2)构建损失函数和梯度下降函数

cost = tf.reduce_mean(tf.square(y-pred))optimizer = tf.train.AdamOptimizer(learning_rate = 0.01).minimize(cost)

3)训练及测试

with tf.Session() as sess:     step = 1for _ in range(500):        batch_xs = tf.reshape(x_data,shape=[-1,2])        batch_ys = tf.reshape(y_data,shape=[-1,1])        loss = sess.run(cost,feed_dict = {x:sess.run(x_data),y:sess.run(y_data)})        step += 1        if(step%100==0):            print("Step "+str(step)+"    loss "+"{:.6f}".format(loss))            print(sess.run(pred,feed_dict={x:sess.run(batch_xs)}))

4)运行结果

Step 100    loss 0.009775       Step 200    loss 0.002646[[ 0.11685876]                  [[ 0.06230327] [ 0.89292878]                   [ 0.94678855] [ 0.08430476]                   [ 0.04467924] [ 0.91710973]]                  [ 0.95668757]]Step 400    loss 0.000721       Step 500    loss 0.000474[[ 0.0330624 ]                  [[ 0.02693252] [ 0.97232604]                   [ 0.97759455] [ 0.02277627]                   [ 0.01835815] [ 0.97747266]]                  [ 0.98175263]] Optimization Finished!