tf函数

来源:互联网 发布:java中final和static 编辑:程序博客网 时间:2024/05/22 10:29

tf.slice(inputs,begin,size,name=”)

    import tensorflow as tf      import numpy as np      x=[[1,2,3],[4,5,6]]      y=np.arange(24).reshape([2,3,4])      sess=tf.Session()      begin_x=[1,0]           #第一个1,决定了从x的第二行[4,5,6]开始,第二个0,决定了从[4,5,6] 中的4开始抽取      size_x=[1,2]          # 第一个1决定了,从第二行以起始位置抽取1行,也就是只抽取[4,5,6] 这一行,在这一行中从4开始抽取2个元素      out=tf.slice(x,begin_x,size_x)      print sess.run(out)  #  结果:[[4 5]]      begin_y=[1,0,0]      size_y=[1,2,3]      out=tf.slice(y,begin_y,size_y)         print sess.run(out)  # 结果:[[[12 13 14] [16 17 18]]]  

tf.concat
tf.concat(concat_dim, values, name=’concat’)

如果concat_dim是0,那么在某一个shape的第一个维度上连,对应到实际,就是叠放到列上

[python]

t1 = [[1, 2, 3], [4, 5, 6]]  t2 = [[7, 8, 9], [10, 11, 12]]  tf.concat(0, [t1, t2]) == > [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]  

如果concat_dim是1,那么在某一个shape的第二个维度上连

[python]

t1 = [[1, 2, 3], [4, 5, 6]]  t2 = [[7, 8, 9], [10, 11, 12]]  tf.concat(1, [t1, t2]) ==> [[1, 2, 3, 7, 8, 9], [4, 5, 6, 10, 11, 12  

tf.split
tf.split(split_dim, num_split, value, name=’split’)
着某一维度将tensor分离为num_split tensors

# ‘value’ is a tensor with shape [5, 30]# Split ‘value’ into 3 tensors along dimension 1split0, split1, split2 = tf.split(1, 3, value)tf.shape(split0) ==> [5, 10]

tf.equal(A, B)
对比这两个矩阵或者向量的相等的元素,如果是相等的那就返回True,反正返回False

tf.gradient

tensorflow中有一个计算梯度的函数tf.gradients(ys, xs),要注意的是,xs中的x必须要与ys相关,不相关的话,会报错。
代码中定义了两个变量w1, w2, 但res只与w1相关

import tensorflow as tfw1 = tf.Variable([[1,2]])w2 = tf.Variable([[3,4]])res = tf.matmul(w1, [[2],[1]])grads = tf.gradients(res,[w1,w2])with tf.Session() as sess:    tf.global_variables_initializer().run()    re = sess.run(grads)    print(re)

错误信息
TypeError: Fetch argument None has invalid type

tf.select(condition, t, e, name=None)
https://www.tensorflow.org/versions/r0.10/api_docs/python/control_flow_ops/comparison_operators#select
Selects elements from t or e, depending on condition.

a=2b=3sess.run(tf.select(True,a,b))#outuput 2sess.run(tf.select(False,a,b))#output 3

tf.select
tf.select(condition,a,b)
a :一个张量tensor,shape与condition一致,类型一般为float32, float64, int32, int64.
b :一个张量tensor,类型和shape与a一致。
举例:

import tensorflow as tf  sess=tf.Session()  condition=[[True,False],[True,False]]  a=[[1,2],[3,4]]  b=[[5,6],[7,8]]  c=tf.select(condition,a,b)  print(sess.run(c))  

输出:

[[1,6],[3,8]]  

如果把condition改成[[True,True],[True,False]]
输出变为:

[[1,2],[3,8]]  

tf.squeeze
tf.squeeze(input, squeeze_dims=None, name=None)

Removes dimensions of size 1 from the shape of a tensor.
从tensor中删除所有大小是1的维度

Given a tensor input, this operation returns a tensor of the same type with all dimensions of size 1 removed. If you don’t want to remove all size 1 dimensions, you can remove specific size 1 dimensions by specifying squeeze_dims.
如果不想删除所有大小是1的维度,可以通过squeeze_dims指定。

For example:

# 't' is a tensor of shape [1, 2, 1, 3, 1, 1]shape(squeeze(t)) ==> [2, 3]Or, to remove specific size 1 dimensions:# 't' is a tensor of shape [1, 2, 1, 3, 1, 1]shape(squeeze(t, [2, 4])) ==> [1, 2, 3, 1]
原创粉丝点击