使用张量索引对张量进行切片 [英] Slice a tensor using tensor indices
本文介绍了使用张量索引对张量进行切片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在TF中有一个表示图像的张量img
,其形状为(n_channels, img_height, img_width)
.
I have a tensor img
in TF representing an image, whose shape is (n_channels, img_height, img_width)
.
我还有几个整数张量,h_start
、h_end
、w_start
、w_end
.
I also have a couple of integer tensors, h_start
, h_end
, w_start
, w_end
.
我想提取对应于 numpy 中的图像部分 img[:, :, h_start:h_end, w_start:w_end]
.
I want to extract the part of the image that corresponds to what in numpy would be img[:, :, h_start:h_end, w_start:w_end]
.
我该怎么做?
推荐答案
您可以使用 tf.Tensor.__getitem__
很像 NumPy 索引:
You can use tf.Tensor.__getitem__
pretty much like with NumPy indexing:
img[:, h_start:h_end, w_start:w_end]
或者,使用 tf.slice
:
Alternatively, use tf.slice
:
sliced_img = tf.slice(img, [0, h_start, w_start], [-1, h_end - h_start, w_end-w_start]
这篇关于使用张量索引对张量进行切片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文