首页 > 解决方案 > 控制张量流中权重/内核中的通道数

问题描述

要实现特定功能,我需要在我的层中使用“input_channels”数量的内核,每个内核只有一个通道深度,而不是 depth = “input_channels”。我需要将一个内核与一个输入通道进行卷积,因此该层的输出将具有“input_channels”数量的内核。

哪个 python/numpy/tensorflow 卷积函数可以允许内核中的通道数不能总是等于“input_channels”而可以为 1 的卷积?

提前感谢您的帮助。

图表供参考

(如果有人想知道我已经尝试过什么,

在 tensorflow 的 conv2d 函数中,如果我指定内核数 = 1 来执行此操作,那么它将对所有 input_channels 求和,并且 output_channels 数将为 1,因为它始终初始化内核深度 =“input_channels”。

另一种选择是在 conv2d 函数中指定内核数 = input_channels,但这会创建深度为“input_channels”的内核数“input_channels”,从而增加了我的层的很多复杂性和不正确的实现。

我尝试的另一件事是初始化一个体积内核(kernel_height、kernel_width、input_channels)并在第三维上循环以仅将单个输入通道与单个内核进行卷积。但是 tensorflow conv2d 函数需要 4 级内核才能工作并给出以下错误 -

ValueError: Shape must be rank 4 but is rank 3 for 'generic_act_func_4/Conv2D' (op: 'Conv2D') with input shapes: [?,28,28], [28,28]. )

标签: pythontensorflow

解决方案


As I see it, you're trying to learn a separate model for each dimension in the input. Thus you will need 2D convolution filters with a filter depth of 1.

I believe there should be an easier way, but most logical to me would be to create a model consisting of a number of submodels equal to the depth of your input (32). Thus 32 models containing a single convolutional filter, receiving only one dimension of your input. Stacking the output from all models would then give the results as you require.

Another solution which would be interesting (but I'm not sure whether it will work, have not tried it myself) would be to do separable convolutions on the input.

A link to an article describing these operations: https://towardsdatascience.com/a-basic-introduction-to-separable-convolutions-b99ec3102728

You essentially want to perform only the 1st part of the separable convolution operation, which is exactly what the DepthwiseConv2D layer in keras/tensorflow does. So I would have a look at that if I where you. Would be interested to know whether this works out for you!


推荐阅读