How could I define layer contains parallel custom layers

Hey,

I defined a custom convolution let call it “custom_conv2d”, which takes n channels input and outputs fix 8 channels.

How do I define a layer “custom_conv2d_layer()”, which contains several of my custom conv2d layers in parallel?

For example, “custom_conv2d_layer()”, take 64 input channels, and couput 128 channels, that means “custom_conv2d_layer()” contain 128/8 = 16 “custom_conv2d” layers parallelly.

Thanks in advance!

Hi Deponce!

I don’t fully understand your use case, but take a look at torch.nn.Module.

You could package your “custom_conv2d” as a Module. (You don’t
have to, but you could.) Then you could define “custom_cond2d_layer”
also as a Module. Your “custom_cond2d_layer” Module could contain
16 “custom_cond2d” Modules as “properties.”

Your “custom_cond2d_layer” Module's forward() function would apply
each “custom_cond2d” to the appropriate part of the input and then, for
example, cat() together the results of the 16 “custom_cond2ds” to
form the output returned by forward().

Best.

K. Frank

Thanks for your reply KFrank!
Yeah, I thought you fully understand my question. But my question is how could I implement such ‘custom_cond2d_layer’ Module, there are 16 'custom_conv2d’s, I could define them manually, but what if there are more than 1000 'custom_conv2d’s? Is there a method to automatically initiate the 'custom_conv2d’s by a for loop?

Thanks in advance!

Hi Deponce!

Yes, you can write a method that will “automatically” create and
initialize a large number of custom_conv2ds with a for loop.

When you define your custom_cond2d_layer you will be defining a
new class (that is a subclass of Module). You will define for this
class an __init__() function that gets called every time you create
a new instance of the custom_cond2d_layer class. This function
initializes the new instance. (In other languages such a function is
often called the class’s constructor.)

You have to write the code, but this __init__() can run a loop that
creates (instantiates) 1000 (or however many) instances of your
custom_conv2d class and puts (references to) those instances into
a list (or other container) that is a property of the new instance of
your custom_conv2d_layer class.

(During the forward pass, the forward() function of your
custom_conv2d_layer could iterate over the list (or other container)
of custom_conv2ds, applying them to the appropriate “chunks” of the
input, and then cat() together those results to create the final result
returned by forward().)

Best.

K. Frank