一. Tensorflow 基本概念:
*用图(graphs)来表示计算任务
*graphs在会话Session中执行
*使用tensor表示数据
*通过变量Variable维护状态
*使用feed和fetch为任意操作赋值从中获取数据
二 tensorflow结构
tf.constant(value, dtype=None, shape=None, name='Const', verify_shape=False)
用案例解释上面的流程逻辑,比如我们计算一个一行两列和两行一列的向量的积,步骤如下。
第一步创建两个向量 m1 = tf.constant([[3, 3]]) m2 = tf.constant([ [2], [3] ])
第二步创建一个矩阵乘法操作 op = tf.matmul(m1, m2) - > 形成一个tensor
第三步定义会话启动默认图 with tf.Session() as sess:
result = sess.run(op)
print(result)
案例1
import tensorflow as tf m1 = tf.constant([[3, 3]]) m2 = tf.constant([[2], [5]]) operator = tf.matmul(m1, m2) with tf.Session() as sess: result = sess.run(operator) print(result)
三. 变量的用法: 1 定义变量 2 调用变量是要初始化
tf.Variable(initializer, name) 第一个参数是初始化器
案例2
import tensorflow as tf a = tf.Variable([4, 4]) #定义变量 b = tf.constant([7, 3]) sub = tf.subtract(a, b) add = tf.add(a, b) #注意变量再使用之前要再sess中做初始化 init = tf.global_variables_initializer() with tf.Session() as sess: sess.run(init) result1 = sess.run(sub) result2 = sess.run(add) print((result1, result2))
案例三: 计算一个数叠加
import tensorflow as tf first = tf.Variable(0, name = "count") cal = tf.add(first, 1) update = tf.assign(first, cal) #first加一后,就讲cal赋值给first init = tf.global_variables_initializer() with tf.Session() as sess: sess.run(init) for _ in range(5): sess.run(update) print(sess.run(first))
四. fetch,feed的用法
1. fetch就是在session中同时运行多个操作
比如案例二可以改成成以下
import tensorflow as tf a = tf.Variable([4, 4]) #定义变量 b = tf.constant([7, 3]) sub = tf.subtract(a, b) add = tf.add(a, b) #注意变量再使用之前要再sess中做初始化 init = tf.global_variables_initializer() with tf.Session() as sess: sess.run(init) result = sess.run([sub, add]) #这里[]就是fetch操作 print(result)
2. feed用法:先创建占位符,再用字典形式传入数据
案例四
import tensorflow as tf #创建占位符 a = tf.placeholder(tf.float32) b = tf.placeholder(tf.float32) output = tf.multiply(a, b) with tf.Session() as sess: #feed的数据以字典的形式传入 result = sess.run(output, feed_dict={a:[4.5], b:[3.7]}) print(result)
五. 线性函数的模型创建
创建模型的意思就是,给定已经知道的数据(x, y),创建一个符合该数据的模型,给定数据x能预测出y^与y接近甚至相同。
1. 随机生成样本数据(x1, x2......x100) 与之对应的 (y1, y2.....y100)
2. 建立y^ = k*x + b的模型,随机初始化k, b
3. 计算出y - y^的平方和的平均数,即为损失值loss,尽可能地让loss值最小,这样预测数据y^ 与y才能接近
案例五
import tensorflow as tf import numpy as np #获取样本x_data, y_data x_data = np.random.rand(100) #随机数 y_data = 0.23 * x_data + 0.15 #创建模型,初始化k, b k = tf.Variable(0.) b = tf.Variable(0.) y = k*x_data + b loss = tf.reduce_mean(tf.square(y_data - y)) #两数之差平方后取 平均值 即二次代价函数 optimizer = tf.train.GradientDescentOptimizer(0.15) #梯度下降法的优化器,0.15为学习率 train = optimizer.minimize(loss) #最小优化 init = tf.global_variables_initializer() with tf.Session() as sess: sess.run(init) for step in range(201): sess.run(train) if step%10 == 0: print(step, sess.run([k, b])) 结果: 0 [0.04511498, 0.07963798] 10 [0.13336873, 0.20012432] 20 [0.14958979, 0.19287243] 30 [0.16268793, 0.18589863] 40 [0.17364912, 0.18005294] 50 [0.18282536, 0.17515908] 60 [0.19050731, 0.17106216] 70 [0.19693834, 0.16763239] 80 [0.20232214, 0.1647611] 90 [0.20682922, 0.16235739] 100 [0.21060236, 0.1603451] 110 [0.21376106, 0.15866052] 120 [0.21640544, 0.15725023] 130 [0.21861918, 0.15606959] 140 [0.22047244, 0.15508123] 150 [0.22202392, 0.15425378] 160 [0.22332276, 0.15356109] 170 [0.2244101, 0.15298119] 180 [0.22532037, 0.15249573] 190 [0.2260824, 0.15208933] 200 [0.22672036, 0.15174909] Process finished with exit code 0