I'm trying to implement the CNN described in A Framework of Hierarchical Deep Q-Network for Portfolio Management (see screenshot). In the paper, the author describes the first CNN layer as having a kernel of 1x3, taking in a price tensor with shape (2,10,4) and outputting 32 feature maps of size 2x5.

The actual quote is:

1st and 2nd CNN Layers: As shown in Fig. 1,t he first CNN layer receives the price tensor Ptwithdimension (2, 10, 4). The filter of this layer is in size of 1 × 3, and the activation function we use here isSelu which is defined in (Klambauer et al., 2017). In this layer, we obtain 32 feature maps and each one is in size of 2 × 5, and these feature maps are received by the next CNN layer. In the second CNN layer, the filters are of size 1 × 5 and 64 feature maps are produced.

The only way I can figure out how to do this using conv2D in PyTorch is by adding stride and padding. Is there something I am missing with regard to how to set up this neural network, or is it just implied that I have to add the appropriate stride and padding?

Moreover, is choosing the output size before the features/kernel putting the chicken before the egg, so to speak? Is it better to tailor the input of the next layer to the output of the previous layer rather than trying to finesse one layer's output to fit into a predetermined input on the next layer?


CNN Image


2 Answers 2


I agree - there appears to be something missing from the description of their model, and from the structure shown it looks like they must have used padding. However, they may not have used a stride > 1, as the same shapes could be obtained by including a pooling layer after the convolutional layer. IMHO the paper should have mentioned these details (especially the use of stride > 1 or pooling) as the model can't be reproduced without this information.

If you want to reproduce their model exactly and the authors don't provide the missing detail elsewhere in the paper or a link to their code, you would need to contact the lead/corresponding author and ask them to clarify. But if you're happy with a model that's similar to theirs but may not be exactly the same then I don't see any problem with adding the required stride to make the output shape of the first layer fit the stated input shape of the second layer. Obviously, you would need to test this to make sure your get reasonable results.


It looks like the first CNN layer in the paper you mentioned is a 1D convolutional layer, which is different from a 2D convolutional layer (conv2D) that is commonly used for image processing tasks. In a 1D convolutional layer, the kernel is applied along a single dimension (e.g., the time dimension in a time series), whereas in a 2D convolutional layer, the kernel is applied to a 2D region of the input feature map.

To implement the first CNN layer described in the paper using PyTorch, you can use the torch.nn.Conv1d module, which is designed for 1D convolutional layers. The Conv1d module takes in three main arguments: the input channel size, the output channel size, and the kernel size. You can set the input channel size to 4 (since the input tensor has 4 channels), the output channel size to 32 (as stated in the paper), and the kernel size to 3.

As for the output size of the first CNN layer, it is determined by the kernel size, the stride, and the padding. The output size is calculated as:

output_size = (input_size - kernel_size + 2 * padding) / stride + 1

Therefore, you can choose the stride and padding to make the output size of the first CNN layer match the desired size of 2x5. For example, if you set the stride to 2 and the padding to 0, the output size will be (10 - 3) / 2 + 1 = 4, which is not what you want. Instead, you can try setting the stride to 1 and the padding to 1, in which case the output size will be (10 - 3 + 2 * 1) / 1 + 1 = 8. This is larger than the desired output size of 2x5, but you can later use pooling or other techniques to reduce the size of the feature maps.

In general, it is usually a good idea to design the architecture of a neural network to match the characteristics of the input data and the desired output, rather than trying to fit the output of one layer into a predetermined input size of the next layer. For example, in this case, you can consider the size of the input tensor, the kernel size, the stride, and the padding to determine the output size of the first CNN layer. However, it is also important to consider the computational complexity and the capacity of the model, as well as the trade-off between the accuracy and the efficiency of the model.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.