Tensorflow 常用语法(自用)

1、Tensorflow layers.fully_connected参数

  def fully_connected(inputs,
                    num_outputs,
                    activation_fn=nn.relu,
                    normalizer_fn=None,
                    normalizer_params=None,
                    weights_initializer=initializers.xavier_initializer(),
                    weights_regularizer=None,
                    biases_initializer=init_ops.zeros_initializer(),
                    biases_regularizer=None,
                    reuse=None,
                    variables_collections=None,
                    outputs_collections=None,
                    trainable=True,
                    scope=None):

inputs: A tensor of at least rank 2 and static value for the last dimension; i.e. `[batch_size, depth]`, `[None, None, None, channels]`.
至少有两层张量且最后一维是静态值。例如[batch_size,depth],[None, None, None, channels].
num_outputs: Integer or long, the number of output units in the layer.
int型整数或者是long型,是层中输出单元的个数
activation_fn: Activation function. The default value is a ReLU function.Explicitly set it to None to skip it and maintain a linear activation.
激活函数,默认值是ReLU函数,如果将它设为None则会跳过且保持线性激活
normalizer_fn: Normalization function to use instead of `biases`. If `normalizer_fn` is provided then `biases_initializer` and
`biases_regularizer` are ignored and `biases` are not created nor added.default set to None for no normalizer function 
正则化函数用来代替偏置,如果设置了正则化函数,则biases_initializer和biases_regularizer将被忽略且biases不会被创建。
默认设置None,不设置正则化函数
normalizer_params: Normalization function parameters.
正则化函数参数
weights_initializer: An initializer for the weights.
权重初始化
weights_regularizer: Optional regularizer for the weights.
对权重的可选的正则化项,正则化:https://www.zhihu.com/question/20924039
biases_initializer: An initializer for the biases. If None skip biases.
偏置初始化,如果为None则跳过
biases_regularizer: Optional regularizer for the biases.
对偏置可选的正则化项
reuse: Whether or not the layer and its variables should be reused. To be able to reuse the layer scope must be given.
是否应该重用该层及其变量,能够重新使用的层的范围一定是确定的
variables_collections: Optional list of collections for all the variables or a dictionary containing a different list of collections per variable.
可选的所有变量的集合列表或包含每个变量集合的不同列表的字典。 
outputs_collections: Collection to add the outputs.收集去添加到输出
trainable: If `True` also add variables to the graph collection `GraphKeys.TRAINABLE_VARIABLES` (see tf.Variable).如果我们是微调网络,有时候需要冻结某一层的参数,则设置为False。
scope: Optional scope for variable_scope.可变范围的可选范围 

2、tf.reduce_sum
看了些回答有的讲按行按列,有的只给例子,不容易被新手理解。下面结合自己的理解举个多维tensor例子简单说明。下面是个234的tensor。
[[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]]
[[13 14 15 16]
[17 18 19 20]
[21 22 23 24]]]
如果计算tf.reduce_sum(tensor, axis=0),axis=0说明是按第一个维度进行求和,也就是说把
[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]

[[13 14 15 16]
[17 18 19 20]
[21 22 23 24]]相加,所以第一个维度(也就是2)抹去,求和结束得到的tensor_ans是34(之前tensor是234)。显然tensor_ans的元素分别是1+13;2+14;3+15……;12+24。即:
[[1+13 2+14 3+15 4+16]
[5+17 6+18 7+19 8+20]
[9+21 10+22 11+23 12+24]]。
依次类推,如果axis=1,那么求和结果shape是2
4,即:
[[ 1 + 5 + 9 2 + 6+10 3 + 7+11 4 + 8+12]
[13+17+21 14+18+22 15+19+23 16+20+24]]
如果axis=2,那么求和结果shape是2*3,即:
[[1+2+3+4 5+6+7+8 9+10+11+12]
[13+14+15+16 17+18+19+20 21+22+23+24]]

3、tf.expand_dims()与tf.reshape()相同功能,增加维度

 't2' is a tensor of shape [2, 3, 5]
shape(expand_dims(t2, 0)) ==> [1, 2, 3, 5]
shape(expand_dims(t2, 2)) ==> [2, 3, 1, 5]
shape(expand_dims(t2, 3)) ==> [2, 3, 5, 1]

4、tf.equal()

A = [[1,2,3,4,5,6]]
B = [[1,2,3,4,8,9]]
with tf.Session() as sess:
print sess.run(tf.equal(A,B))
输出结果
[[ True  True  True  True False False]]

5、tf.where()
where(condition, x=None, y=None, name=None)
condition, x, y 相同维度,condition是bool型值,True/False
1、where(condition)的用法
返回值,是condition中元素为True对应的索引

import tensorflow as tf
a = [[1,2,3],[4,5,6]]
b = [[1,0,3],[1,5,1]]
condition1 = [[True,False,False],
             [False,True,True]]
condition2 = [[True,False,False],
             [False,True,False]]
with tf.Session() as sess:
    print(sess.run(tf.where(condition1)))
    print(sess.run(tf.where(condition2)))
结果1:
[[0 0]
 [1 1]
 [1 2]]
结果2:
[[0 0]
 [1 1]]

2、 where(condition, x=None, y=None, name=None)的用法
condition, x, y 相同维度,condition是bool型值,True/False
返回值是对应元素,condition中元素为True的元素替换为x中的元素,为False的元素替换为y中对应元素
x只负责对应替换True的元素,y只负责对应替换False的元素,x,y各有分工
由于是替换,返回值的维度,和condition,x , y都是相等的。
看个例子:

import tensorflow as tf
x = [[1,2,3],[4,5,6]]
y = [[7,8,9],[10,11,12]]
condition3 = [[True,False,False],
             [False,True,True]]
condition4 = [[True,False,False],
             [True,True,False]]
with tf.Session() as sess:
    print(sess.run(tf.where(condition3,x,y)))
    print(sess.run(tf.where(condition4,x,y)))  
结果:
1, [[ 1  8  9]
    [10  5  6]]
2, [[ 1  8  9]
    [ 4  5 12]]

6、tf.slice(input_, begin, size, name = None)
解释 :
这个函数的作用是从输入数据input中提取出一块切片
切片的尺寸是size,切片的开始位置是begin。
切片的尺寸size表示输出tensor的数据维度,其中size[i]表示在第i维度上面的元素个数。
开始位置begin表示切片相对于输入数据input_的每一个偏移量,比如数据input是

[[[1, 1, 1], [2, 2, 2]], 
[[33, 3, 3], [4, 4, 4]], 
[[5, 5, 5], [6, 6, 6]]],

begin为[1, 0, 0],那么数据的开始位置是33。因为,第一维偏移了1,其余几位都没有偏移,所以开始位置是33。

操作满足:

size[i] = input.dim_size(i) - begin[i] 
0 <= begin[i] <= begin[i] + size[i] <= Di for i in [0, n]
import tensorflow as tf

sess = tf.Session()
input = tf.constant([[[1, 1, 1], [2, 2, 2]],
                     [[3, 3, 3], [4, 4, 4]],
                     [[5, 5, 5], [6, 6, 6]]])
data = tf.slice(input, [1, 0, 0], [1, 1, 3])
print(sess.run(data))

"""[1,0,0]表示第一维偏移了1
则是从[[[3, 3, 3], [4, 4, 4]],[[5, 5, 5], [6, 6, 6]]]中选取数据
然后选取第一维的第一个,第二维的第一个数据,第三维的三个数据"""
 [[[3 3 3]]]
data = tf.slice(input, [1, 0, 0], [1, 2, 3])
print(sess.run(data))
 [[[3 3 3]
   [4 4 4]]]
data = tf.slice(input, [1, 0, 0], [2, 1, 3])
print(sess.run(data))
 [[[3 3 3]]

  [[5 5 5]]]
data = tf.slice(input, [1, 0, 0], [2, 2, 2])
print(sess.run(data))
 [[[3 3]
   [4 4]]

  [[5 5]
   [6 6]]]
"""输入参数:
  ● input_: 一个Tensor。
  ● begin: 一个Tensor,数据类型是int32或者int64。
  ● size: 一个Tensor,数据类型是int32或者int64。
  ● name:(可选)为这个操作取一个名字。
输出参数:
  ● 一个Tensor,数据类型和input_相同。"""

7、tf.concat([a,b],axis=)

concat()是将tensor沿着指定维度连接起来。其中tensorflow1.3版中是这样定义的:
concat(values,axis,name=‘concat’)
一、对于2维来说,0表示行,1表示列

t1 = [[1, 2, 3], [4, 5, 6]]
t2 = [[7, 8, 9], [10, 11, 12]]
with tf.Session() as sess:
    print(sess.run(tf.concat([t1, t2], 0) ))
结果为:[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]
t1 = [[1, 2, 3], [4, 5, 6]]
t2 = [[7, 8, 9], [10, 11, 12]]
with tf.Session() as sess:
    print(sess.run(tf.concat([t1, t2], 1) ))
结果为:[[1, 2, 3, 7, 8, 9], [4, 5, 6, 10, 11, 12]]

二、 对于3维来说 0表示纵向,1表示行,2表示列

t1 = [[[1, 1, 1],[2, 2, 2]],[[3, 3, 3],[4, 4, 4]]]
t2 = [[[5, 5, 5],[6, 6, 6]],[[7, 7, 7],[8, 8, 8]]]
with tf.Session() as sess:
    print(sess.run(tf.concat([t1, t2], 0) ))
结果:[[[1 1 1],[2 2 2]] , [[3 3 3],[4 4 4]] , [[5 5 5],[6 6 6]] ,  [[7 7 7],[8 8 8]]]
Tensor("concat_30:0", shape=(4, 2, 3), dtype=int32)
axis=1的结果如下:
Tensor("concat_31:0", shape=(2, 4, 3), dtype=int32)
[[[1 1 1], [2 2 2],[5 5 5],[6 6 6]], [[3 3 3], [4 4 4],[7 7 7], [8 8 8]]]
axis=2的结果如下:
Tensor("concat_32:0", shape=(2, 2, 6), dtype=int32)
[[[1 1 1 5 5 5],[2 2 2 6 6 6]], [[3 3 3 7 7 7], [4 4 4 8 8 8]]]

你可能感兴趣的:(tensorflow)