Answers.Body: <p>First of all, I don't think that the two approaches are the same as @lev1248 claims. When using a 3D convolution the 3d filters have depth equal to <code>kernel_size</code> and you slide your filter along the <span class="math-container">$D$</span> dimension as well.</p>
  <p>I think that Approach 1 is better because you want to pay attention to <strong>all</strong> of the channels while sliding along the <code>14x14</code> plane. Note that the convolutional layer was designed to take advantage of translational invariance. So neurons are activated if they see something (plant, terrain, etc.) regardless of its location. The sliding kernel will slide along the <code>14x14</code> plane and whenever it &quot;sees&quot; something it will activate. Now regarding the channel dimension, you actually have a one-hot encoding of what exactly is present at a given location, as I understand 10 different options. You wouldn't want to slide a kernel along this dimension and output the same activation regardless of where it sees the <span class="math-container">$1$</span>. You want to take the contents of the entire channel into consideration.</p>
  <p>If you set <code>groups=1</code> in the <code>Conv2d</code> layer initializer (which is the default option) then you get the standard normal convolution. This means that <strong>each</strong> of the output channels contains the information from <strong>all</strong> of the input channels. The kernel is convolved with the input, channels are not convolved. So, yes, this captures the interaction.</p>
  <p>The other extreme would be to set <code>groups=in_channels</code> (which is 10 in your case). Then you split your input tensor <code>(10, 14, 14)</code> into 10 input tensors of size <code>(1, 14, 14)</code>, you perform the convolution along each one separately and you concatenate the results. In this case you do not capture the interaction between the channels. Usually, when you do this, you add an additional <code>1x1</code> convolutional layer after that that will intermix the channels. This approach is called a depthwise-separable convolution and was introduced in the <a href="https://arxiv.org/abs/1704.04861" rel="nofollow noreferrer">MobileNet paper</a>. This will harm the accuracy of the model, but greatly reduces the number of parameters and is the preferred approach for models that will be deployed on mobile phones and other edge devices.</p>
  <p>You can read more about splitting into groups here: <a href="https://pi-tau.github.io/posts/res-nets/#the-resnext-block-going-wider-instead-deeper" rel="nofollow noreferrer">https://pi-tau.github.io/posts/res-nets/#the-resnext-block-going-wider-instead-deeper</a></p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-07-24T13:47:20.653Z
Answers.LastActivityDate: 2023-07-25T19:30:32.170Z
Answers.LastEditDate: 2023-07-25T19:30:32.170Z
Answers.UserDisplayName: pi-tau
Answers.Body: <p>I am good at codes. How to shift between real temp to prefetch.
  <a href="https://viswaonlinetrainings.com/courses/sap-grc-online-training/" rel="nofollow noreferrer">Sap Grc Online Training</a>
  <a href="https://viswaonlinetrainings.com/courses/microsoft-intune-online-training/" rel="nofollow noreferrer">Microsoft Intune Online training From India</a></p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-07-24T13:04:20.487Z
Answers.LastActivityDate: 2023-07-24T13:04:20.487Z
Answers.UserDisplayName: Chaitu Viswa
Answers.Body: <p>If you use the same number of output channels for both approaches, they are basically the same. The first one uses <code>10*outputchannel</code> number of 2d filters. The second approach uses <code>outputchannels</code> number of 3d filters that have a depth of <code>10</code>.
  Both options are technically the same and use the same amount of weights. They can both capture dependencies between the different input layers to the same degree.</p>
  <p>I personally prefer working with 2d filters but you can choose whatever you like.</p>
  <p>I hope I was able to answer your question.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-04-08T00:48:09.480Z
Answers.LastActivityDate: 2023-04-08T00:48:09.480Z
Answers.UserDisplayName: lev1248
Body: <h1>Introduction</h1>
  <p>I am considering to use a convolutional neural network in implementing Monte Carlo control with function approximation. I am using a Monte Carlo estimate as it is unbiased and has nice convergence properties. Of course, it has high variance and slower convergence.</p>
  <p>The goal is to control a game such that episodic reinforcement learning is used. In fact, the game is <a href="https://mconsultingprep.com/mckinsey-problem-solving-game-digital-assessment" rel="nofollow noreferrer">Mckinsey Plant-defense game</a>. Below is an image of the game:</p>
  <p><a href="https://i.sstatic.net/Ofyk1.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/Ofyk1.png" alt="enter image description here" /></a></p>
  <h1>Problem specification</h1>
  <p>I have written a rough simulator of the game and then written a function that splits the board into various 14 x 14 layers. More specifically, the layers contain the following:</p>
  <ol>
  <li>Layer 1 is a <code>(14,14)</code> array of zeros with a 1 at the location of the plant.</li>
  <li>Layers 2-4 are <code>(14,14)</code> arrays of zeros with ones placed at coordinates containing terrain. For example, layer 2 contains only the location of cliffs.</li>
  <li>Layers 5-7 are like layers 2-4 but contain locations of defenders, i.e., snake.</li>
  <li>Layers 8 and 9 are also <code>(14,14)</code> arrays of zeros. However, if an attacker is present at <span class="math-container">$(i,j)$</span> in the grid, a value between zero and 1 is placed there. A such, both the remaining health and location are presented in a layer. Layer 8is for the foxes and layer 9 is for the groundhogs.</li>
  <li>The grid of the actual game varies. Sometimes it's a <code>(10,10)</code> grid, <code>(14,10)</code>, <code>(12,12)</code>, etc. As such, I cast these grids onto a <code>(14,14)</code> grid. The last layer is thus a <code>(14,14)</code> array of zeros with ones placed in locations that do not exist. In other words, the ones indicate where I have padded the original grid. For example, a <code>(12,12)</code> grid would have a border of ones.</li>
  </ol>
  <p>Naturally, there is spatial interaction in each layer but also interaction between layers. I am considering two approaches to a convolution architecture. Note, I am using PyTorch, so I apologise if I am using PyTorch-specific terminology.</p>
  <h1>Approaches: 2D (with channels) vs 3D (no channels) convolution</h1>
  <h2>Approach 1</h2>
  <p>Approach 1 uses 2D convolution with channels.</p>
  <p><span class="math-container">$$
  y_{h,i,j} = \sum_{k=1}^{C_{in}}\sum_{l=1}^{H_k}\sum_{m=1}^{W_k}w_{h,k,l,m}x_{k,i+l-1,j+m-1}
  $$</span>
  Where <span class="math-container">$h=1,2,\dots,C_{out}$</span> are output channels, <span class="math-container">$k=1,2,\dots,C_{in}$</span> is the input channels, <span class="math-container">$l=1,2,\dots,K_H$</span> is the height of the kernel, <span class="math-container">$m=1,2,\dots,K_W$</span> is the width of the kernel, <span class="math-container">$x$</span> is the layers of the map and <span class="math-container">$y$</span> is the output of the convolution. In <a href="https://pytorch.org/docs/stable/generated/torch.nn.Conv2d.html" rel="nofollow noreferrer">PyTorch's</a> <code>Conv2d</code> function, if I set <code>groups=1</code> then all input channels are convolved with all output channels as in the formula. <strong>Does this capture interaction between channels?</strong></p>
  <h2>Approach 2</h2>
  <p>The second approach would instead treat the 10 stacked layers as a 3D image of shape <code>(10,14,14)</code> with one channel. The formula for this would read as below:</p>
  <p><span class="math-container">$$
  y_{i,j,k} = \sum_{m=1}^{D_k}\sum_{n=1}^{H_k}\sum_{o=1}^{W_k}w_{m,n,o}x_{i+m-1,j+n-1,k+o-1}
  $$</span>
  It would seem that this would capture more of the dependencies between layers? I would imagine that this is the approach they would use in medical imaging where the image has layers that strongly interact.</p>
  <h1>Question</h1>
  <p><strong>Which approach is most suitable for modelling dependencies within layers and between layers?</strong> Can you please provide me with some intuition on why? I would also not mind reading recommended literature. Thank you for your time.</p>
  
Tags: <reinforcement-learning><convolutional-neural-networks><pytorch><convolution><convolution-arithmetic>
Title: 2D convolution with channels versus 3D convolution for layers of a map?
UserDisplayName: Dylan Solms
ThreadText: # 2D convolution with channels versus 3D convolution for layers of a map?
  
  (Asked by: username_0 on 2023-04-04)
  
  # Introduction
  
  I am considering to use a convolutional neural network in implementing Monte Carlo control with function approximation. I am using a Monte Carlo estimate as it is unbiased and has nice convergence properties. Of course, it has high variance and slower convergence.
  
  The goal is to control a game such that episodic reinforcement learning is used. In fact, the game is [Mckinsey Plant-defense game](https://mconsultingprep.com/mckinsey-problem-solving-game-digital-assessment). Below is an image of the game:
  
  [![enter image description here](https://i.sstatic.net/Ofyk1.png)](https://i.sstatic.net/Ofyk1.png)
  
  # Problem specification
  
  I have written a rough simulator of the game and then written a function that splits the board into various 14 x 14 layers. More specifically, the layers contain the following:
  
  1. Layer 1 is a `(14,14)` array of zeros with a 1 at the location of the plant.
  2. Layers 2-4 are `(14,14)` arrays of zeros with ones placed at coordinates containing terrain. For example, layer 2 contains only the location of cliffs.
  3. Layers 5-7 are like layers 2-4 but contain locations of defenders, i.e., snake.
  4. Layers 8 and 9 are also `(14,14)` arrays of zeros. However, if an attacker is present at $(i,j)$ in the grid, a value between zero and 1 is placed there. A such, both the remaining health and location are presented in a layer. Layer 8is for the foxes and layer 9 is for the groundhogs.
  5. The grid of the actual game varies. Sometimes it's a `(10,10)` grid, `(14,10)`, `(12,12)`, etc. As such, I cast these grids onto a `(14,14)` grid. The last layer is thus a `(14,14)` array of zeros with ones placed in locations that do not exist. In other words, the ones indicate where I have padded the original grid. For example, a `(12,12)` grid would have a border of ones.
  
  Naturally, there is spatial interaction in each layer but also interaction between layers. I am considering two approaches to a convolution architecture. Note, I am using PyTorch, so I apologise if I am using PyTorch-specific terminology.
  
  # Approaches: 2D (with channels) vs 3D (no channels) convolution
  
  ## Approach 1
  
  Approach 1 uses 2D convolution with channels.
  
  $$
  y\_{h,i,j} = \sum\_{k=1}^{C\_{in}}\sum\_{l=1}^{H\_k}\sum\_{m=1}^{W\_k}w\_{h,k,l,m}x\_{k,i+l-1,j+m-1}
  $$
  Where $h=1,2,\dots,C\_{out}$ are output channels, $k=1,2,\dots,C\_{in}$ is the input channels, $l=1,2,\dots,K\_H$ is the height of the kernel, $m=1,2,\dots,K\_W$ is the width of the kernel, $x$ is the layers of the map and $y$ is the output of the convolution. In [PyTorch's](https://pytorch.org/docs/stable/generated/torch.nn.Conv2d.html) `Conv2d` function, if I set `groups=1` then all input channels are convolved with all output channels as in the formula. **Does this capture interaction between channels?**
  
  ## Approach 2
  
  The second approach would instead treat the 10 stacked layers as a 3D image of shape `(10,14,14)` with one channel. The formula for this would read as below:
  
  $$
  y\_{i,j,k} = \sum\_{m=1}^{D\_k}\sum\_{n=1}^{H\_k}\sum\_{o=1}^{W\_k}w\_{m,n,o}x\_{i+m-1,j+n-1,k+o-1}
  $$
  It would seem that this would capture more of the dependencies between layers? I would imagine that this is the approach they would use in medical imaging where the image has layers that strongly interact.
  
  # Question
  
  **Which approach is most suitable for modelling dependencies within layers and between layers?** Can you please provide me with some intuition on why? I would also not mind reading recommended literature. Thank you for your time.
  
  **Tags:** reinforcement-learning, convolutional-neural-networks, pytorch, convolution, convolution-arithmetic
  
  
  ---
  
  ## 3 answers
  
  ### Answer by username_1 on 2023-07-24. Score: 1 | Accepted answer
  
  First of all, I don't think that the two approaches are the same as @username_3 claims. When using a 3D convolution the 3d filters have depth equal to `kernel_size` and you slide your filter along the $D$ dimension as well.
  
  I think that Approach 1 is better because you want to pay attention to **all** of the channels while sliding along the `14x14` plane. Note that the convolutional layer was designed to take advantage of translational invariance. So neurons are activated if they see something (plant, terrain, etc.) regardless of its location. The sliding kernel will slide along the `14x14` plane and whenever it "sees" something it will activate. Now regarding the channel dimension, you actually have a one-hot encoding of what exactly is present at a given location, as I understand 10 different options. You wouldn't want to slide a kernel along this dimension and output the same activation regardless of where it sees the $1$. You want to take the contents of the entire channel into consideration.
  
  If you set `groups=1` in the `Conv2d` layer initializer (which is the default option) then you get the standard normal convolution. This means that **each** of the output channels contains the information from **all** of the input channels. The kernel is convolved with the input, channels are not convolved. So, yes, this captures the interaction.
  
  The other extreme would be to set `groups=in_channels` (which is 10 in your case). Then you split your input tensor `(10, 14, 14)` into 10 input tensors of size `(1, 14, 14)`, you perform the convolution along each one separately and you concatenate the results. In this case you do not capture the interaction between the channels. Usually, when you do this, you add an additional `1x1` convolutional layer after that that will intermix the channels. This approach is called a depthwise-separable convolution and was introduced in the [MobileNet paper](https://arxiv.org/abs/1704.04861). This will harm the accuracy of the model, but greatly reduces the number of parameters and is the preferred approach for models that will be deployed on mobile phones and other edge devices.
  
  You can read more about splitting into groups here: <https://username_1.github.io/posts/res-nets/#the-resnext-block-going-wider-instead-deeper>
  
  
  ---
  
  ### Answer by username_2 on 2023-07-24. Score: -1
  
  I am good at codes. How to shift between real temp to prefetch.
  [Sap Grc Online Training](https://viswaonlinetrainings.com/courses/sap-grc-online-training/)
  [Microsoft Intune Online training From India](https://viswaonlinetrainings.com/courses/microsoft-intune-online-training/)
  
  
  ---
  
  ### Answer by username_3 on 2023-04-08. Score: -1
  
  If you use the same number of output channels for both approaches, they are basically the same. The first one uses `10*outputchannel` number of 2d filters. The second approach uses `outputchannels` number of 3d filters that have a depth of `10`.
  Both options are technically the same and use the same amount of weights. They can both capture dependencies between the different input layers to the same degree.
  
  I personally prefer working with 2d filters but you can choose whatever you like.
  
  I hope I was able to answer your question.
  
  
  ---
  
  