API 查询网址
中国官网(tensorfly):
http://www.tensorfly.cn/tfdoc/api_docs/python/math_ops.html
w3cschool
https://www.w3cschool.cn/tensorflow_python/tensorflow_python-61ue2ocp.html
API官网:
https://devdocs.io/tensorflow~python/tf/app
apimirror网站(很全):
https://apimirror.com/tensorflow~python/tf/contrib/framework/list_variables
数据类型
tf.cast(variable, tf_dtype)
作用:转换数据类型,将变量variable转换为tf_dtype类型。
tf.to_float
XXXXXX
常见数据类型有:
tf.float32 tf.float16 .......(待补充)
维度相关
tf.reduce_mean(x)
作用:tf.reduce_mean函数的作用是求平均值。第一个参数是一个集合,可以是列表、二维数组和多维数组。第二个参数指定在哪个维度上面求平均值。默认对所有的元素求平均。
例子:下面是对所有元素求平均值:
x = tf.constant([[1., 1.], [2., 2.]])
tf.reduce_mean(x) # 1.512345
当指定第二个参数的时候,指定axis=0,表示沿着‘跨行’的方向求平均。
m1 = tf.reduce_mean(x, axis=0)
结果为:[1.5, 1.5]
第二个参数不仅可以是一个数,也可以是一个数字,里面的数字表示指定所有的的轴的方向。 比如
xx = tf.constant([[[1., 1, 1],
[2., 2, 2]],
[[3, 3, 3],
[4, 4, 4]]])
m3 = tf.reduce_mean(xx, [0, 1]) # [2.5 2.5 2.5]
上面是一个三维数组, xx的shape为(2,2,3),可以想象为三个2x2的二维数组叠加在一起形成一个2x2x3的立体,也就是三个面叠加。现在的axis为[0, 1],表示对第1和第2轴的方向求平均值, 也就是分别对每一个面求平均。
第一个面为: [[1., 2], [3., 4]] ,平均值为2.5。
第二个面为 [[1., 2], [3., 4]] ,平均值为2.5。
第三个面一样
数学运算
tf.add(x,y,name)
作用:x+y
参数:
x: A `Tensor`. Must be one of the following types: `bfloat16`, `half`, `float32`, `float64`, `uint8`, `int8`, `int16`, `int32`, `int64`, `complex64`, `complex128`, `string`.
y: A `Tensor`. Must have the same type as `x`.
name: A name for the operation (optional).
注意:x和y的数据类型必须相同。
tf.add_n
注意:使用tf.add_n对列表元素进行相加时,列表内元素类型必须一致,否则会报错。
更多:见tf.add_to_collection例子,进一步了解如何使用。
tf.sub(x, y, name=None)
Returns x - y element-wise.
Args:
x: ATensor. Must be one of the following types:float32,float64,int32,complex64,int64.
y: ATensor. Must have the same type asx.
name: A name for the operation (optional).
Returns: ATensor. Has the same type asx.
tf.mul(x, y, name=None)
Returns x * y element-wise.
Args:
x: ATensor. Must be one of the following ypes:float32,float64,int8,int16,int32,complex64,int64.
y: ATensor. Must have the same type asx.
name: A name for the operation (optional).
Returns: ATensor. Has the same type as x.
tf.div(x, y, name=None)
Returns x / y element-wise.
Args:
x: ATensor. Must be one of the following types:float32,float64,int32,complex64,int64.
y: ATensor. Must have the same type asx.
name: A name for the operation (optional).
Returns: ATensor. Has the same type asx.
tf.mod(x, y, name=None)
Returns element-wise remainder of division.
Args:
x: ATensor. Must be one of the following types:int32,int64,float32,float64.
y: ATensor. Must have the same type asx.
name: A name for the operation (optional).
Returns: ATensor. Has the same type asx.
tf.argmax()
作用:首先,明确一点,tf.argmax可以认为就是np.argmax。tensorflow使用numpy实现的这个API。简单的说,tf.argmax就是返回最大的那个数值所在的下标。这个很好理解,只是tf.argmax()的参数让人有些迷惑,比如,tf.argmax(array, 1)和tf.argmax(array, 0)有啥区别呢?这里面就涉及到一个概念:axis。上面例子中的1和0就是axis。我先笼统的解释这个问题,设置axis的主要原因是方便我们进行多个维度的计算。在实例面前,再多的语言都是苍白的呀,上例子!
例子:test = np.array([[1, 2, 3], [2, 3, 4], [5, 4, 3], [8, 7, 2]])
np.argmax(test, 0) #输出:array([3, 3, 1]
np.argmax(test, 1) #输出:array([2, 2, 0, 0]
tensorlayer.layers.ElementwiseLayer(prev_layer, combine_fn, act=None, name)
作用:将多个子网络(在combine_fn里面),进行操作,操作方法由act指定。比如要将两个形状完全一样的网络进行加和,则act=tf.add即可。
参数: prev_layer (list of Layer) -- The list of layers to combine.
combine_fn (a TensorFlow element-wise combine function) -- e.g. AND is tf.minimum ; OR is tf.maximum ; ADD is tf.add ; MUL is tf.multiply and so on. See TensorFlow Math API .
act (activation function) -- The activation function of this layer.
name (str) -- A unique layer name.
例子:
变量
tf.Variable
tf.get_variable
注意:tf.get_variable函数不受tf.name_scope的影响。
我们从代码看下这句话的具体意思。
首先是tf.variable_scope:
with tf.variable_scope('foo'):
a = tf.get_variable('bar',[1])
print(a.name)#结果为foo/bar:0
再看tf.name_scope:
with tf.name_scope('a'):
a=tf.Variable([1])
print(a.name)#结果为a/Variable:0
b=tf.get_variable('b',[1])
print(b.name)#结果为b:0
从这个结果中,我们能很清晰地看到,tf.get_variable创建的变量并不是a/b:0,而是b:0。这就表示了在tf.name_scope函数下,tf.get_variable不受其约束。
更多:关于tf.Variable、tf.get_variable、tf.variable_scope以及tf.name_scope异同,见https://blog.csdn.net/qq_22522663/article/details/78729029
tf.get_variable_scope()
说明:tf.get_variable_scope().reuse_variables() returns None
def reuse_variables(self):
"""Reuse variables in this scope."""
self._reuse = True
Actually, it just switches the _reuse on.
举例:
def my_image_filter(input_images):
conv1_weights = tf.get_variable(tf.random_normal([5, 5, 32, 32]),
name="conv1_weights")
conv1_biases = tf.get_variable(tf.zeros([32]), name="conv1_biases")
conv1 = tf.nn.conv2d(input_images, conv1_weights,
strides=[1, 1, 1, 1], padding='SAME')
return tf.nn.relu(conv1 + conv1_biases)
with tf.variable_scope("image_filters") as scope:
result1 = my_image_filter(image1)
scope.reuse_variables() # or
#tf.get_variable_scope().reuse_variables()
result2 = my_image_filter(image2)
tf.variable_scope()
注意1:该变量的reuse参数,默认是None.
注意2:variable scope的Initializers可以创递给子空间和tf.get_variable()函数,除非中间有函数改变,否则不变。详见【更多】。
reuse:可以是True、None或tf.AUTO_REUSE;如果是True,则我们进入此范围的重用模式以及所有子范围;如果是tf.AUTO_REUSE,则我们创建变量(如果它们不存在),否则返回它们;如果是None,则我们继承父范围的重用标志。当启用紧急执行时,该参数总是被强制为tf.AUTO_REUSE
更多:https://www.jianshu.com/p/ab0d38725f88
tf.global_variables()
作用: 获取tf.Variable或 tf.get_variable创建的变量的列表。
import tensorflow as tf
import numpy as np
v = tf.Variable(tf.constant(0.0, shape =[1],dtype = tf.float32), name = 'v')
v1 = tf.get_variable("v1", [1])
variables = tf.global_variables()
print(variables[0].name)
print(variables[1].name)
输出:
#v:0
#v1:0
tf.global_variables_initializer()
pooling层
tf.nn.avg_pool
padding层
tf.pad(input, paddings, name=None)
input : 代表输入张量
padding : 代表加的边界
name : 代表此操作的名字
说明: padding它必须是 [N,2] 形式,N代表张量的阶, 2代表必须是2列,比如
padding = [ [1,1], [2,2] ] 或者padding = [ [1,1], [2,2], [3,3] ]
具体根据需要设定,但列必须为2,不然报错
首先,假定 一个3x1的一个张量input = [[1],[2],[3]] , padding = [[1,1], [2,2]]
input = [[1], [2], [3]]
padding = [[1,2],[1,2]]## [ [上1行,下2行],[左1行,右2行]]
print(sess.run(tf.pad(input,padding)))
[[0 0 0 0]
[0 1 0 0]
[0 2 0 0]
[0 3 0 0]
[0 0 0 0]
[0 0 0 0]]
input = [[1], [2], [3]]
padding = [[3,1],[3,5]]· [上3行,下1行],[左3行,右5行]]
print(sess.run(tf.pad(input,padding)))
[[000000000]
[000000000]
[000000000]
[000100000]
[000200000]
[000300000]
[000000000]]
Tensorboard、Summary类相关
tf.summary.scalar
作用: Add a tensorboard summary
举例: tf.summary.scalar('Regularization Loss', reg_loss)
GraphKeys类相关
常见GraphKeys
GLOBAL_VARIABLES: 该collection默认加入所有的Variable对象,并且在分布式环境中共享。一般来说,TRAINABLE_VARIABLES包含在MODEL_VARIABLES中,MODEL_VARIABLES包含在GLOBAL_VARIABLES中。
LOCAL_VARIABLES: 与GLOBAL_VARIABLES不同的是,它只包含本机器上的Variable,即不能在分布式环境中共享。
MODEL_VARIABLES: 顾名思义,模型中的变量,在构建模型中,所有用于正向传递的Variable都将添加到这里。
TRAINALBEL_VARIABLES: 所有用于反向传递的Variable,即可训练(可以被optimizer优化,进行参数更新)的变量。
SUMMARIES: 跟Tensorboard相关,这里的Variable都由tf.summary建立并将用于可视化。
QUEUE_RUNNERS: theQueueRunnerobjects that are used to produce input for a computation.
MOVING_AVERAGE_VARIABLES: the subset ofVariableobjects that will also keep moving averages.
REGULARIZATION_LOSSES: regularization losses collected during graph construction.
UPDATE_OPS:
VARIABLES:
tf.add_to_collection(‘list_name’, element)
作用:将元素element添加到列表list_name中
举例:
import tensorflow as tf
tf.add_to_collection('losses', tf.constant(2.2))
tf.add_to_collection('losses', tf.constant(3.))
with tf.Session() as sess:
print(sess.run(tf.get_collection('losses')))
print(sess.run(tf.add_n(tf.get_collection('losses'))
结果:[2.2,3.0]
5.2
注意: 使用tf.add_n对列表元素进行相加时,列表内元素类型必须一致,否则会报错。
tf.get_collection
作用:返回名称为list_name的列表
举例1: regularization_losses = tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES)
举例2: grads = opt.compute_gradients(total_loss, var_list= tf.get_collection \
(tf.GraphKeys.VARIABLES, scope='output'))#这个例子中,用scope限制了收集的范围。
更多:见tf.add_to_collection例子,进一步了解如何使用。
Train相关
tf.train.piecewise_constant(x, boundaries, values, name=None)
作用:Piecewise constant from boundaries and interval values.
参数:
x: A 0-D scalarTensor. Must be one of the following types: float32, float64, uint8, int8, int16, int32, int64.
boundaries: A list ofTensors orints orfloats with strictly increasing entries, and with all elements having the same type asx.
values: A list ofTensors or floats orints that specifies the values for the intervals defined byboundaries. It should have one more element thanboundaries`, and all elements should have the same type.
name: A string. Optional name of the operation. Defaults to 'PiecewiseConstant'.
返回值:A 0-D Tensor. Its value is values[0] when x <= boundaries[0], values[1] when x > boundaries[0] and x <= boundaries[1], ..., and values[-1] when x > boundaries[-1].
例子:use a learning rate that's 1.0 for the first 100000 steps, 0.5 for steps 100001 to 110000, and 0.1 for any additional steps.
例1:global_step=tf.Variable(0,trainable=False)
boundaries=[100000,110000]
values=[1.0,0.5,0.1]
learning_rate=tf.train.piecewise_constant(global_step,boundaries,values)
# Later, whenever we perform an optimization step, we increment global_step.
例2:#epoch number
epoch_number = tf.get_variable('epoch_number', [], dtype= tf.int32,
initializer= tf.constant_initializer(0), trainable= False)
# Decay the learning rate
lr = tf.train.piecewise_constant(epoch_number, args.LR_steps,
args.LR_values, name= 'LearningRate')
# Weight Decay policy
wd = tf.train.piecewise_constant(epoch_number, args.WD_steps,
args.WD_values, name= 'WeightDecay')
tf.train.exponential_decay(learning_rate,global_step,decay_steps,
decay_rate, staircase=False, name=None)
作用:用于learning_rate或weight_decay指数速率下降。he function returns the decayed learning rate. It is computed as:
decayed_learning_rate=learning_rate*decay_rate^(global_step/decay_steps)
If the argument staircase is True, then global_step / decay_steps is an integer division and the decayed learning rate follows a staircase function.
参数:learning_rate: A scalarfloat32orfloat64Tensoror a Python number. The initial learning rate.
global_step: A scalarint32orint64Tensoror a Python number. Global step to use for the decay computation. Must not be negative.
decay_steps: A scalar int32 or int64Tensor or a Python number. Must be positive. See the decay computation above.
decay_rate: A scalarfloat32orfloat64Tensoror a Python number. The decay rate.
staircase: Boolean. IfTruedecay the learning rate at discrete intervals
name: String. Optional name of the operation. Defaults to 'ExponentialDecay'.
例子:
Example: decay every 100000 steps with a base of 0.96:
...
global_step=tf.Variable(0,trainable=False)
starter_learning_rate=0.1
learning_rate=tf.train.exponential_decay(starter_learning_rate,global_step,
100000,0.96,staircase=True)
# Passing global_step to minimize() will increment it at each step.
learning_step=(tf.train.GradientDescentOptimizer(learning_rate).minimize(...my
loss...,global_step=global_step))
优化及梯度
tf.train.MomentumOptimizer
例子:tf.train.MomentumOptimizer(learning_rate, 0.9)
属性:具有如下几个成员函数:
1) tf.train.MomentumOptimizer.compute_gradients()
返回值::A list of (gradient, variable) pairs,即[(grad_1,var_1),..., (grad_N,var_N)]。
checkpoint相关
tf.contrib.framework.list_variables(checkpoint_dir)
作用:Returns list of all variables in the latest checkpoint.
参数:checkpoint_dir: Directory with checkpoints file or path to checkpoint.
返回值: List of tuples (name, shape)
例子:for var_name,_ in tf.contrib.framework.list_variables(args.ckpt_path):
# Load the variable
var=tf.contrib.framework.load_variable(args.ckpt_path,var_name)
# Set the new namenew_name=var_name
tf.contrib.framework.load_variable( checkpoint_dir, name)
作用:Returns a Tensor with the contents of the given variable in the checkpoint.
参数:checkpoint_dir: Directory with checkpoints file or path to checkpoint.
name: Name of the tensor to return.
返回值:Tensor object.
tf.train.get_checkpoint_state(checkpoint_dir,latest_filename=None)
作用:该函数返回的是checkpoint文件CheckpointState proto类型的内容,返回值有model_checkpoint_path和all_model_checkpoint_paths两个属性。其中model_checkpoint_path保存了最新的tensorflow模型文件的文件名,all_model_checkpoint_paths则有未被删除的所有tensorflow模型文件的文件名。
举例:下图是在训练过程中生成的几个模型文件列表:
【详解(https://blog.csdn.net/changeforeve/article/details/80268522)】
以下是测试程序里的部分代码:
在上面代码中,通过tf.train.get_checkpoint_state函数得到的相关模型文件名如下:
对所有模型进行测试,得到:
其它
tf.group()
作用:用于创造一个操作,可以将传入参数的所有操作进行分组。
举例:ops = tf.group(tensor1, tensor2,...)
一旦ops完成了,那么传入的tensor1,tensor2,...等等都会完成了,经常用于组合一些训练节点,如在Cycle GAN中的多个训练节点,例子如:
generator_train_op = tf.train.AdamOptimizer(g_loss, ...)
discriminator_train_op = tf.train.AdamOptimizer(d_loss,...)
train_ops = tf.groups(generator_train_op ,discriminator_train_op)
with tf.Session() as sess:
sess.run(train_ops)
# 一旦运行了train_ops,那么里面的generator_train_op和discriminator_train_op都将被调用。
注意:注意的是,tf.group()返回的是个操作,而不是值,如果你想下面一样用,返回的将不是值。
a = tf.Variable([5])
b = tf.Variable([6])
c = a+b
d = a*b
e = a/b
ops = tf.group(c,d,e)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
ee = sess.run(ops)
返回的将不是c,d,e的运算结果,而是一个None,就是因为这个是一个操作,而不是一个张量。如果需要返回结果,请参考tf.tuple()
tf.tuple()
tf.tuple()用于组合多个张量输入组成的列表[tensor1,tensor2,...],然后返回一个计算过后的张量列表[cal_tensor1,cal_tensor2,...],这点和tf.group()是不同的,API手册如:
tf.tuple(
tensors,
name=None,
control_inputs=None
)
ops = tf.tuple([tensor1,tensor2,...],control_inputs=c_ops)
其中tensors是由多个tensor组成的列表,其中的control_inputs是添加额外的控制输入,添加的输入c_ops必须在整个ops完成之前得到执行,但是c_ops的输出是不会返回的。
API上描述,这个可以作为提供一种并行处理的机制,所有的输入的tensor可以并行计算,但是所有tensor的计算出来的值将会以tuple的形式返回,并且这个只能在并行计算完成之后得到。(This can be used as a “join” mechanism for parallel computations: all the argument tensors can be computed in parallel, but the values of any tensor returned by tuple are only available after all the parallel computations are done.)
使用例子:
a = tf.Variable([5])
b = tf.Variable([6])
c = a+b
d = a*b
e = a/b
ops = tf.tuple([c,d,e])
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
ee = sess.run(ops)
print(ee)
输出
[array([11], dtype=int32), array([30], dtype=int32), array([0.83333333])]
可以和tf.group()用于组合多个操作的例子进行对比。