用keras切片张量
我有一个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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您熟悉常规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, andarr[:, :, :-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)
.