tensorflow常用函数

tf.gather:用一个一维的索引数组,将张量中对应索引的向量提取出来

 1 import tensorflow as tf 
 2  
 3 a = tf.Variable([[1,2,3,4,5], [6,7,8,9,10], [11,12,13,14,15]])
 4 index_a = tf.Variable([0,2])
 5  
 6 b = tf.Variable([1,2,3,4,5,6,7,8,9,10])
 7 index_b = tf.Variable([2,4,6,8])
 8  
 9 with tf.Session() as sess:
10     sess.run(tf.global_variables_initializer())
11     print(sess.run(tf.gather(a, index_a)))
12     print(sess.run(tf.gather(b, index_b)))
13  
14 #  [[ 1  2  3  4  5]
15 #   [11 12 13 14 15]]
16  
17 #  [3 5 7 9]

np.stack()

axis参数指定新轴在结果尺寸中的索引。指定的索引,是新结果shape的第n个位置的值。例如,如果axis=0,它将是第一个维度,如果axis=-1,它将是最后一个维度。

  • 参数: 数组:array_like的序列每个数组必须具有相同的形状。axis:int,可选输入数组沿其堆叠的结果数组中的轴。
  • 返回: 堆叠:ndarray堆叠数组比输入数组多一个维。
>>> a = np.array([1, 2, 3])

>>> b = np.array([2, 3, 4])

>>> a.shape

(3,)

>>> b.shape

(3,)

>>> np.stack((a, b), axis=0).shape

(2, 3)

>>> np.stack((a, b), axis=1).shape

(3, 2)

tf.stack()和np.stack()道理差不多

tf.concat是沿某一维度拼接shape相同的张量,拼接生成的新张量维度不会增加。而tf.stack是在新的维度上拼接,拼接后维度加1

参考连接:

https://www.cnblogs.com/estragon/p/9809154.html

https://blog.csdn.net/Gai_Nothing/article/details/88416782

https://blog.csdn.net/feifeiyechuan/article/details/89388103

import tensorflow as tf
 
a = tf.Variable([[1,2,3,4,5], [6,7,8,9,10], [11,12,13,14,15]])
index_a = tf.Variable([0,2])
 
b = tf.Variable([1,2,3,4,5,6,7,8,9,10])
index_b = tf.Variable([2,4,6,8])
 
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    print(sess.run(tf.gather(a, index_a)))
    print(sess.run(tf.gather(b, index_b)))
 
#  [[ 1  2  3  4  5]
#   [11 12 13 14 15]]
 
#  [3 5 7 9]
原文地址:https://www.cnblogs.com/ywheunji/p/11802001.html