基本上,我有一個由 生成的索引串列tf.random.uniform和一個名為 的張量tensor_big,現在我需要創建一個新的張量tensor_small,其中內部是主張量的所有元素,其中第二個坐標位于indices串列內。
例子:
指數 = [1, ......]
然后我需要[0,1], [1,1], [2,1]為每個索引創建一個具有位置等權重的新張量。
import tensorflow as tf
if __name__ == '__main__':
tensor_big = tf.random.uniform(
(3136,512), minval=0, maxval=None, dtype=tf.dtypes.float32, seed=None, name=None
)
indices = tf.random.uniform(shape=[410, ], minval=0, maxval=512, dtype=tf.dtypes.int32, seed=None, name=None)
for weight in tensor_big:
print(weight[1])
tensor_small = tf.reshape(tf.gather(tensor_big, WHERE_SECOND_COORDINATE_INSIDE_INDICES), (3136,410))
print(tensor_small)
uj5u.com熱心網友回復:
您可以使用tf.gather引數axis=1來選擇列:
tensor_small = tf.gather(tensor_big, indices, axis=1)
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/350058.html
