Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 随机去除张量_Python_Tensorflow - Fatal编程技术网

Python 随机去除张量

Python 随机去除张量,python,tensorflow,Python,Tensorflow,我试图从张量中随机删除一行。到目前为止,我看到的最简单的方法如下(参考): 我想传递给“del”一个基于张量运算的随机整数。但当我使用: ran = tf.random_uniform((1,), minval=0, maxval=val, dtype=tf.int32) 我得到一个数组,del不接受数组。另外,如果有更简单的方法从阵列中删除,请告诉我。您可以使用任何大小和形状的tf.boolean\u mask。使用np.random.choice #your_shape = int or

我试图从张量中随机删除一行。到目前为止,我看到的最简单的方法如下(参考):

我想传递给“del”一个基于张量运算的随机整数。但当我使用:

ran = tf.random_uniform((1,), minval=0, maxval=val, dtype=tf.int32)

我得到一个数组,del不接受数组。另外,如果有更简单的方法从阵列中删除,请告诉我。

您可以使用任何大小和形状的
tf.boolean\u mask
。使用
np.random.choice

#your_shape = int or array
a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='a')
ran = tf.random_uniform((1,), minval=0, maxval=2, dtype=tf.int32)
a_vecs = tf.unstack(a, axis=1)
rm = np.random.choice([True, False], your_shape)
a_new = tf.boolean_mask(a_vecs, rm)
或者您可以使用
np.asscalar
将数组转换为标量,但它需要在会话中运行

a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='a')
ran = tf.random_uniform((1,), minval=0, maxval=2, dtype=tf.int32)
a_vecs = tf.unstack(a, axis=1)
with tf.Session() as sess:
    r = ran.eval()
    val = np.asscalar(r)
    del a_vecs[val]
    a_new = tf.stack(a_vecs, 1)

您可以使用任何大小和形状的
tf.boolean\u mask
。使用
np.random.choice

#your_shape = int or array
a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='a')
ran = tf.random_uniform((1,), minval=0, maxval=2, dtype=tf.int32)
a_vecs = tf.unstack(a, axis=1)
rm = np.random.choice([True, False], your_shape)
a_new = tf.boolean_mask(a_vecs, rm)
或者您可以使用
np.asscalar
将数组转换为标量,但它需要在会话中运行

a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='a')
ran = tf.random_uniform((1,), minval=0, maxval=2, dtype=tf.int32)
a_vecs = tf.unstack(a, axis=1)
with tf.Session() as sess:
    r = ran.eval()
    val = np.asscalar(r)
    del a_vecs[val]
    a_new = tf.stack(a_vecs, 1)

感谢您的帮助,但您的解决方案会随机删除,甚至删除多行。我只想随机删除一行。另外,您的解决方案假设我知道所需的形状(单位为numpy),我需要一个张量值。不管布尔掩码可能有什么帮助,我都会检查出来谢谢你的帮助,但是你的解决方案会随机删除,甚至会删除多行。我只想随机删除一行。另外,您的解决方案假设我知道所需的形状(单位为numpy),我需要一个张量值。但是布尔掩码可能会有所帮助,我将检查它