用keras切片张量

发布于 2025-01-21 16:24:25 字数 600 浏览 1 评论 0原文

我有一个3D张量。它不是图像,而是为了使用与Keras相同的约定,我将认为它是图像,即高度x宽度x Channels

我已经对该张量应用了2D卷积,它是一系列层的输入。但是,我想单独保留最后一个频道,以稍后将其连接。也许在代码中更容易看到:

l = [64, 32, 16, 8]
inputs = Input(shape=(x,y,z))
we_are_here = Sequential([Conv2D(z, (x, y), padding='same') for _ in l])(inputs)
last_channel = SomethingSomething(we_are_here) # How do I do this?
long_path = Sequential(lots_of_layers)(we_are_here)
res = Concatenate([Flatten()(layer) for layer in [long_path, last_channel]]
outputs = Sequential([Dense(i) for i in l])(res)

我找不到一种分开最后一个频道的方法。切片,分裂,裁剪等有多项操作。但似乎没有任何帮助。

I have a 3D tensor. It is not an image, but for the sake of using the same conventions as Keras I will consider it an image, i.e. height x width x channels.

I have applied a 2D convolution to this tensor, and it is the input for a sequence of layers. However, I would like to keep the last channel separately, to concatenate it later on. Perhaps it is easier to see in code:

l = [64, 32, 16, 8]
inputs = Input(shape=(x,y,z))
we_are_here = Sequential([Conv2D(z, (x, y), padding='same') for _ in l])(inputs)
last_channel = SomethingSomething(we_are_here) # How do I do this?
long_path = Sequential(lots_of_layers)(we_are_here)
res = Concatenate([Flatten()(layer) for layer in [long_path, last_channel]]
outputs = Sequential([Dense(i) for i in l])(res)

I could not find a way to separate the last channel. There are several operations for slicing, splitting, cropping, etc. but none seems to help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

农村范ル 2025-01-28 16:24:25

如果您熟悉常规python中的列表切片,则类似数组的类型支持相同的行为,但被推广到其他维度。 arr [:,,:,-1]将在第三维处获得最后一个切片,而arr [:,:,:,: - 1]将获得所有最后一个。片。

请注意,其中arr.Shape(A,B,C)形状(a,b)。如果需要保留维度,则arr [:,,:,[-1]]将返回形状(a,b,1)的数组。 arr [:,:,: - 1]将返回形状(a,b,c -1)

If you're familiar with list slicing in regular Python, array-like types support the same behavior, but generalized to additional dimensions. arr[:, :, -1] would get the last slice along the third dimension, and arr[:, :, :-1] would get all but that last slice.

Note that where arr.shape is (a, b, c), arr[:, :, -1] will return an array of shape (a, b). If the dimensionality needs to be preserved, arr[:, :, [-1]] will return an array of shape (a, b, 1). arr[:, :, :-1] will return an array of shape (a, b, c - 1).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文