从 Tensorflow 中的张量中删除一组张量 [英] Remove a set of tensors from a tensor in Tensorflow
问题描述
我正在寻找一种简单的方法来从 Tensorflow 中的当前张量中删除一组张量,但我有一个困难的合理解决方案.
I'm looking for an easy way to remove a set of tensors from a current tensor in Tensorflow and I'm having a difficult reasonable solution.
例如,假设我有以下当前张量:
For example, let's say that I have the below current tensor:
a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='a')
而且我想从这个张量(2.0 和 5.0)中删除两个项目.
And that I want to remove, say, two items from this tensor (2.0 and 5.0).
在创建后将这个张量转换为 [1.0, 3.0, 4.0, 6.0] 的最佳方法是什么?
What would be the best way to transform this tensor into [1.0, 3.0, 4.0, 6.0] after it has been created?
非常感谢.
推荐答案
您可以调用 tf.unstack
来获取子张量列表.然后你可以修改列表并调用 tf.stack
从列表中构造一个张量.例如,以下代码从 a 中删除 [2.0, 5.0] 列:
You can call tf.unstack
to obtain a list of sub-tensors. Then you can modify the list and call tf.stack
to construct a tensor from the list. For example, the following code removes the [2.0, 5.0] column from a:
a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='a')
a_vecs = tf.unstack(a, axis=1)
del a_vecs[1]
a_new = tf.stack(a_vecs, 1)
这篇关于从 Tensorflow 中的张量中删除一组张量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!