tf.random_uniform((4, 4), minval=low,maxval=high,dtype=tf.float32)))返回4*4的矩阵,产生于low和high之间,产生的值是均匀分布的。
创建一个所有的参数为0的tensor对象
This operation returns a tensor of type dtype with shape shape and all elements set to zero.
这个操作会返回一个类型为dtype,并且维度为sharp的tensor,并且所有的参数均为0.
参数: shape: 用于表示维度,通常为一个int32类型数组,或者一个一维(1-D)的tf.int32数字.注意不能直接使用数字dtype: 所要创建的tensor对象的数据类型name: 一个该操作的别名(可选的). 返回:所有参数都为0的tensor对象
# 'x' is [[1., 2.]# [3., 4.]]
x是一个2维数组,分别调用reduce_*函数如下:
首先求平均值:
tf.reduce_mean(x) ==> 2.5 #如果不指定第二个参数,那么就在所有的元素中取平均值tf.reduce_mean(x, 0) ==> [2., 3.] #指定第二个参数为0,则第一维的元素取平均值,即每一列求平均值tf.reduce_mean(x, 1) ==> [1.5, 3.5] #指定第二个参数为1,则第二维的元素取平均值,即每一行求平均值
同理,还可用tf.reduce_max()求最大值等。
这个类是实现梯度下降算法的优化器。(结合理论可以看到,这个构造函数需要的一个学习率就行了)
__init__(learning_rate, use_locking=False,name=’GradientDescent’)
作用:创建一个梯度下降优化器对象 参数: learning_rate: A Tensor or a floating point value. 要使用的学习率 use_locking: 要是True的话,就对于更新操作(update operations.)使用锁 name: 名字,可选,默认是”GradientDescent”.
compute_gradients(loss,var_list=None,gate_gradients=GATE_OP,aggregation_method=None,colocate_gradients_with_ops=False,grad_loss=None)
作用:对于在变量列表(var_list)中的变量计算对于损失函数的梯度,这个函数返回一个(梯度,变量)对的列表,其中梯度就是相对应变量的梯度了。这是minimize()函数的第一个部分, 参数: loss: 待减小的值 var_list: 默认是在GraphKey.TRAINABLE_VARIABLES. gate_gradients: How to gate the computation of gradients. Can be GATE_NONE, GATE_OP, or GATE_GRAPH. aggregation_method: Specifies the method used to combine gradient terms. Valid values are defined in the class AggregationMethod. colocate_gradients_with_ops: If True, try colocating gradients with the corresponding op. grad_loss: Optional. A Tensor holding the gradient computed for loss.
apply_gradients(grads_and_vars,global_step=None,name=None)
作用:把梯度“应用”(Apply)到变量上面去。其实就是按照梯度下降的方式加到上面去。这是minimize()函数的第二个步骤。 返回一个应用的操作。 参数: grads_and_vars: compute_gradients()函数返回的(gradient, variable)对的列表 global_step: Optional Variable to increment by one after the variables have been updated. name: 可选,名字
get_name()
minimize(loss,global_step=None,var_list=None,gate_gradients=GATE_OP,aggregation_method=None,colocate_gradients_with_ops=False,name=None,grad_loss=None)
作用:非常常用的一个函数 通过更新var_list来减小loss,这个函数就是前面compute_gradients() 和apply_gradients().的结合
for i in range(n):表示在i循环n次,
例如for i in range(4): 从i=0开始,到i=3结束,循环四次,相当于for(i=0,i++,i<4)
tf.assign(state,new_value),赋值语句,将new_value的值传给state
