tf函数总结(一)

来源:互联网 发布:php类与对象做出兔子 编辑:程序博客网 时间:2024/05/25 21:35

1、tf.concat

concat(    values,    axis,    name='concat')
将一组张量沿某一维度连接起来。

当axis=i时,如果values[i].shape=[d1,d2, ....,daxis[i], ..., dn],则连接后的张量维度为[d1,d2,..., Raxis,...,dn],其中Raxis=sum(daxis[i]),这就是输入的张量数据沿着某一维度连接。

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

2、tf.split

split(    value,    num_or_size_splits,    axis=0,    num=None,    name='split')
将一个张量分开成多个子张量

如果num_or_size_splits是一个整数为num_splits,则会将value沿着axis维度分为num_splits个子张量,如果不为整数可以是一个一维张量,将axis维度分为len(num_splits)个子张量,每个子张量在axis的维度上等于num_splits[i]

# 'value' is a tensor with shape [5, 30]# Split 'value' into 3 tensors with sizes [4, 15, 11] along dimension 1split0, split1, split2 = tf.split(value, [4, 15, 11], 1)tf.shape(split0) ==> [5, 4]tf.shape(split1) ==> [5, 15]tf.shape(split2) ==> [5, 11]# Split 'value' into 3 tensors along dimension 1split0, split1, split2 = tf.split(value, num_or_size_splits=3, axis=1)tf.shape(split0) ==> [5, 10] 
3、tf.expand_dims
expand_dims(    input,    axis=None,    name=None,    dim=None)
为一个张量添加一个维度

axis为增加维度的位置,0表示第一个位置,如果是负数,则从后面计算,如-1表示从最后位置增加一个维度。

# 't' is a tensor of shape [2]shape(expand_dims(t, 0)) ==> [1, 2]shape(expand_dims(t, 1)) ==> [2, 1]shape(expand_dims(t, -1)) ==> [2, 1]# '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.constant
constant(    value,    dtype=None,    shape=None,    name='Const',    verify_shape=False)
创建一个常亮的张量

其中value给出常亮的值,dtype和shape都是可选的,如果不指定则以value的准。

tensor = tf.constant([1, 2, 3, 4, 5, 6, 7]) => [1 2 3 4 5 6 7]

tensor = tf.constant(-1.0, shape=[2, 3]) => [[-1. -1. -1.] [-1. -1. -1.]]


5、tf.placeholder_with_default

placeholder_with_default(    input,    shape,    name=None)
创建一个placeholder操作,如果在输出时没有给feed,则以input为值

tf.placeholder_with_default(tf.constant(1.0),shape=[],  name='use_dropout')


6、tf.global_variables

返回去全局变量。全局变量指的是在分布式环境中可以跨机器进行共享的变量。通过Variable()构造函数或get_variable()创建的新变量会自动添加到图的集合GraphKeys.GLOBAL_VARIABLES中,这个函数可以方便的返回这个集合的内容。




原创粉丝点击