tensorflow常用函数记录

来源:互联网 发布:263网络通信 编辑:程序博客网 时间:2024/06/06 02:03
1.tf.range():产生一等差数列
示例代码如下:
# 'start' is 3
# 'limit' is 18 
# 'delta' is 3 
tf.range(start, limit, delta) ==> [3, 6, 9, 12,15] 

# 'limit' is 5 
tf.range(limit) ==> [0, 1, 2, 3, 4]

2.    tf.reshape()
示例代码如下:
# tensor 't' is [1, 2, 3, 4, 5, 6, 7, 8, 9]
# tensor 't' has shape [9]
reshape(t, [3, 3]) ==> [[1, 2, 3]
                     [4, 5,6]
                     [7, 8,9]]

# tensor 't' is [[[1, 1], [2, 2]]
#              [[3, 3], [4, 4]]]
# tensor 't' has shape [2, 2, 2]
reshape(t, [2, 4]) ==> [[1, 1, 2, 2]
                     [3, 3, 4,4]]

# tensor 't' is [[[1, 1, 1],
#               [2, 2, 2]],
#              [[3, 3, 3],
#               [4, 4, 4]],
#              [[5, 5, 5],
#               [6, 6, 6]]]
# tensor 't' has shape [3, 2, 3]
# pass '[-1]' to flatten 't'
reshape(t, [-1]) ==> [1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4,5, 5, 5, 6, 6, 6]
# -1 can also be used with higher dimensional shapes
reshape(t, [2, -1]) ==> [[1, 1, 1, 2, 2, 2, 3, 3, 3],
                     [4, 4, 4, 5, 5, 5, 6, 6, 6]]

# tensor 't' is [7]
# shape `[]` reshapes to a scalar
reshape(t, []) ==> 7

3.     tf.concat()
示例代码如下:
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]]
tf.concat(1, [t1, t2]) ==> [[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(0, [t3, t4])) ==> [4, 3]
tf.shape(tf.concat(1, [t3, t4])) ==> [2, 6]

4.      


1 0