tensorflow基础总结

先贴出参考资料

1.Tensorflowshe链社区ttp://www.tensorfly.cn/

2.<Tensorflow学习指南>(朱小虎 李紫辉翻译)

3.莫烦tensorflow:https://morvanzhou.github.io/tutorials/machine-learning/tensorflow/


DeepLearning简单学了一遍简化数学知识体系后,决定下一步学会实践使用,再回过头再去详细得啃那本花书。接下来抽时间更新DeepLearning的学习日志,用以巩固知识体系,也用以自我督促。


使用 TensorFlow, 你必须明白的几个概念:

  • 使用计算图 (graph) 来表示计算任务.
  • 在被称之为会话(Session)上下文 (context) 中执行图.
  • 使用 tensor 表示数据.
  • 通过变量(Variables)维护状态.
  • 使用 feed fetch 可以为任意的操作(arbitrary operation) 赋值或者从其中获取数据.

1 图graph 

使用tensorflow的过程可以概括为两个主要步骤:构建图和使用图。

例如,通常在构建阶段创建一个图来表示和训练神经网络, 然后在执行阶段反复执行图中的训练 op。

1.1 什么是计算图,节点

先说官方一些的解释:图是相互连接的实体的集合。通常称为节点node或者certice,这些节点通过边edgexian相互连接。在数据流图zh中,边edge可以让数据从一个节点根据指定方向流向另一个节点。

通俗一点解释给大家:自从导入tensorflow后:

import tensorflow as tf

我们就已经经构建了一个空白的默认图了。随后我们在之后敲下的操作语句,比如使用tf创建一个常量:

matrix1 = tf.constant([[3,3]])  # constant是在tf中表示常量

这个操作称之为节点op(operation 的缩写)。

1.2 Tensor

tensorflow中数据都是用tensor来表示,每个 Tensor 是一个类型化的多维数组,所有节点(操作)都会产生0个及以上的tensor。

1.3 Session

官方一些的解释:TensorFlow 图描述了计算的过程.,为了进行计算,,图必须在会话session里被启动。会话session将图的 op 分发到诸如 CPU 或 GPU上,同时提供执行 op 的方法.。这些方法执行后,将产生的 tensor 返回。在 Python 语言中,返回的 tensor 是 numpy的ndrrary对象。

通俗点儿说:我们在tensorflow图中敲下的所有操作语句,比如两个矩阵相加。都需要用加载到会话Session中才能执行操作,比如:

matrix1 = tf.constant([[3,3]])  # constant是在tf中表示常量
matrix2 = tf.constant([[2],
                      [2]])
product = tf.matmul(matrix1,matrix2)  # 矩阵乘法, 相当于np中 np.dot(m1,m2)

sess = tf.Session() # 创建一个会话session

result = sess.run(product) # 使用会话执行某个op
print(result2) 

2 常量,随机量变量

2.1常量constant

上面已经提到了tf.constantyong用于创建chan常量,当然也可以用numpy去创建常量矩阵。

2.1随机量random与变量Variable

a = tf.Variable(tf.random_normal([size1,size2]))

 3 placehloder与feed

TensorFlow 还提供了 feed 机制,该机制 可以临时替代图中的任意操作中的 tensor 。

feed 使用一个 tensor 值临时替换一个操作的输出结果. 你可以提供 feed 数据作为 run() 调用的参数. feed 只在调用它的方法内有效, 方法结束, feed 就会消失. 最常见的用例是将某些特殊的操作指定为 "feed" 操作, 标记的方法是使用 tf.placeholder() 为这些操作创建占位符.

这两个操作通常使用,相当于于先用placeholder去创建一个待使用的数据,用某名称表示,以方便我们创建某些歌op时使用,当用会话去执行某个op操作的时候,去用feed操作去对其赋值。例如:

input1 = tf.placeholder(tf.float32) # placeholder为占个空位的操作
input2 = tf.placeholder(tf.float32)
output = tf.multiply(input1, input2)

with tf.Session() as sess:
  print (sess.run([output], feed_dict={input1:[7], input2:[2]})) 

其中input1,input2shiy使用placeholder占位操作,创建待赋值的空位,用以在随后的op中表示被操作的数据。

在实际操作执行阶段,用feed操作,为其赋值。

4 fetch

为了取回操作的输出内容,可以在使用 Session 对象的 run() 去执行op时,定义一些 tensor, 这些 tensor 会帮助你取回结果. 在下面的两个小例子中,第一个我们只取回了单个节点 state,第二个一次可以取回多个 tensor。

# 创建一个变量,命名为‘counter’ 初始化为标量 0.
state = tf.Variable(0, name="counter")
# 创建一个 op, 其作用是使 state 增加 1
one = tf.constant(1)
new_value = tf.add(state, one)
update = tf.assign(state, new_value)

# 启动图后, 变量必须先经过`初始化` (init) op 初始化,
# 首先必须增加一个`初始化` op 到图中.
init_op = tf.initialize_all_variables()

# 启动图, 运行 op
with tf.Session() as sess:
  # 运行 'init' op
  sess.run(init_op)
  # 打印 'state' 的初始值
  print sess.run(state)
  # 运行 op, 更新 'state', 并打印 'state'
  for _ in range(3):
    sess.run(update)
    print sess.run(state)
input1 = tf.constant(3.0)
input2 = tf.constant(2.0)
input3 = tf.constant(5.0)
intermed = tf.add(input2, input3)
mul = tf.mul(input1, intermed)

with tf.Session():
  result = sess.run([mul, intermed])
  print result

5 例子

最后贴上tensorflow社区里的一段对完整包括以上基础内容的程序。

import tensorflow as tf
import numpy as np

# 使用 NumPy 生成假数据(phony data), 总共 100 个点.
x_data = np.float32(np.random.rand(2, 100)) # 随机输入
y_data = np.dot([0.100, 0.200], x_data) + 0.300

# 构造一个线性模型
# 
b = tf.Variable(tf.zeros([1]))
W = tf.Variable(tf.random_uniform([1, 2], -1.0, 1.0))
y = tf.matmul(W, x_data) + b

# 最小化方差
loss = tf.reduce_mean(tf.square(y - y_data))
optimizer = tf.train.GradientDescentOptimizer(0.5)
train = optimizer.minimize(loss)

# 初始化变量
init = tf.initialize_all_variables()

# 启动图 (graph)
sess = tf.Session()
sess.run(init)

# 拟合平面
for step in xrange(0, 201):
    sess.run(train)
    if step % 20 == 0:
        print step, sess.run(W), sess.run(b)

# 得到最佳拟合结果 W: [[0.100  0.200]], b: [0.300]

猜你喜欢

转载自blog.csdn.net/weixin_42483560/article/details/84798225
今日推荐