TensorFlow Note
TensorFlow study note #1.
One | Basic
Graph
Graph包含一系列操作(op)和张量(tensor,op输出的标识符),是一系列计算过程的合集。TensorFlow全局有一默认Graph,不需要手动创建。
1 | # 在默认Graph下 |
代码中默认Graph可表示为下图:tensor为边表示输出,op为节点表示操作。constant和add是op,constant创建了张量m1和m2,add将m1和m2相加并产生新张量。但Graph只定义了如何操作,并不会真正执行。
1 | graph LR |
Session
Session对话是定义tensor和执行op的环境,在Session中可指定要执行的Graph中的op。
1 | # 默认Session使用默认Graph |
Op
Op是Graph的节点,输入tensor输出tensor。
以下收集op笔记:
tf.constant
1 | tensor = tf.constant([1, 2, 3]) => [1 2 3] |
tf.placeholder
占位符,类似于声明。不指定shape可填充任意shape。
使用时必须用feed_dict选修填充数据。参考session
reduce_sum
通过求和降维。
判断要降哪一维:若M的shape为(6,7,8),即678的三维矩阵,则alis=0,1,2分别得到(78),(68),(6*7)的二维矩阵,alis=[1,2]则先把7去掉,再把8去掉,最终得到(6)的一维向量,若再alis=[1,2,0]则6也去掉,从向量变成一个点(一个数字)。
1 | # 官方例子 |
参数
- alis(reduction_indices) - 要降低的维度,支持多个如[0,1]
The dimensions to reduce. If None (the default), reduces all dimensions. Must be in the range [-rank(input_tensor), rank(input_tensor)).
tf.nn.softmax
归一化指数函数
使得每一个元素的范围都在(0,1)之间,并且所有元素的和为1。
Tensor
A Tensor is a symbolic handle to one of the outputs of an Operation. It does not hold the values of that operation’s output, but instead provides a means of computing those values in a TensorFlow tf.Session.
Tensor是对操作输出的标识处理,它并不代表输出值的本身,而是为Session提供计算那些值的手段。就是标识符的意思吧…
以下收集tensor笔记:
hint: 以下基本属于class,基于个人理解放在这一章。
tf.Variable
Variable表示值可变的tensor,存在于session.run()上下文之外。 (意思是全局变量?)
创建
W = tf.Variable(rng.randn(), name="weight")
在session中使用前需
sess.run(W.initializer)
或sess.run(tf.global_variables_initializer())
初始化值,会通过创建语句的值(这里是rng.randn()随机数)初始化变量。trainable = default-True
Optimizer
This class defines the API to add Ops to train a model.
优化器 基类,定义训练模型的接口和操作。
补充资料:http://ruder.io/optimizing-gradient-descent/
GradientDescentOptimizer
梯度下降优化器 tf.train.GradientDescentOptimizer
Args:
- learning_rate: A Tensor or a floating point value. 学习率
- use_locking: If True use locks for update operations. 更新锁
- name: Optional name prefix for the operations created when applying gradients. Defaults to “GradientDescent”.
.minimize() simply combines calls compute_gradients() and apply_gradients().
minimize包括了compute_gradients()和apply_gradients().
Gitalking ...