How to get a random sub-tensor from a tensor?

本文探讨了在TensorFlow中从张量中随机抽取固定形状子张量的方法。提供了使用tf.gather_nd和NumPy库实现的两种解决方案,适用于不同场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

摘录一下之前项目遇到这个问题时,在Stack Overflow上提问获得的几种解决方案。

问题

I want to get a random sub-tensor from a tensor, and the shape is fixed. For example, I need to get the right tensor from the left tensor, and index is random for every row, just like this:

[[1 4 3]         [[3]     [[4]
 [3 2 1]  ----->  [2]  or  [1] (generate randomly)
 [0 3 4]]         [3]]     [0]]

I tried tf.slice and tf.gather, It doesn't work. And I tried to write a code testcase like this:

import random
import tensorflow as tf

a = tf.convert_to_tensor([[[1, 4, 3]],
                          [[3, 2, 1]],
                          [[0, 3, 4]]])

T = a.get_shape().as_list()[0]

result_list = []

for i in range(T):
    idx = random.randint(0, 2)  # get a random idx
    result_list.append(a[i][0][idx])

y_hat = tf.reshape(tf.convert_to_tensor(result_list), shape=(T, 1))

with tf.Session() as sess:
    print(sess.run(y_hat))

    # y_hat: [[4]
    #         [1]
    #         [4]]

In this testcase, It worked. But in a real environment, 'a'.shape=(None, 3), so
'T = a.get_shape().as_list()[0]' is not a int value, i can't iterate T by range(T). For example:

import random
import tensorflow as tf

a = tf.placeholder(shape=(None, 3), dtype=tf.int32)

result_list = []
T = a.get_shape().as_list()[0]

for i in range(T):
    idx = random.randint(0, 2)  # get a random idx
    result_list.append(a[i][0][idx])

y_hat = tf.reshape(tf.convert_to_tensor(result_list), shape=(T, 1))

with tf.Session() as sess:

    a_instance = [[[1, 4, 3]],
                  [[3, 2, 1]],
                  [[0, 3, 4]]]

    print(sess.run(y_hat, feed_dict={a: a_instance}))

In this case, it doesn't work. Who can tell me what should i do?

回答1

This is how you can do that with tf.gather_nd:

import tensorflow as tf

with tf.Graph().as_default(), tf.Session() as sess:
    tf.random.set_random_seed(0)
    a = tf.constant([[1, 4, 3],
                     [3, 2, 1],
                     [0, 3, 4]])
    s = tf.shape(a)
    rows, cols = s[0], s[1]
    # Row indices
    ii = tf.expand_dims(tf.range(rows), 1)
    # Column indices
    jj = tf.random.uniform((rows, 1), 0, cols, dtype=ii.dtype)
    # Gather result
    result = tf.gather_nd(a, tf.stack([ii, jj], axis=-1))
    # Print some results
    print(sess.run(result))
    # [[3]
    #  [2]
    #  [4]]
    print(sess.run(result))
    # [[4]
    #  [1]
    #  [0]]
    print(sess.run(result))
    # [[3]
    #  [2]
    #  [0]]

回答2

I normally use the numpy library to do this.

import numpy as np

a_instance = np.array([[1,4,3],[3,2,1],[0,3,4]])
a_instance = a_instance.T # transpose the matrix
np.random.shuffle(a_instance) # it performs the shuffle of the rows
a_instance = a_instance.T

Then you can get one column as wanted with the following code:

a_column = a_instance[:, 0]

In this way you have the wanted random column as numpy array that you can then use with tensorflow as showed:

...
print(sess.run(y_hat, feed_dict={a: [a_column.tolist()]}))

Remember also to use a copy of "a_instance" with the shuffle method if you do not want to modify the "a_instance" matrix permanently.

 

原文地址:How to get a random sub-tensor from a tensor?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值