# RuntimeError: Given groups=1, weight of size [1, 1, 512, 0], expected input[2, 10, 1, 500] to have 1 channels, but got 10 channels instead. Trying to compute the cross-correlation

username: vin
cooked: <p>Hello，I’m facing an issue raising this message: <code> corr_idex = F.conv2d(signal, reference_signal_tensor) RuntimeError: Given groups=1, weight of size [1, 1, 512, 0], expected input[2, 10, 1, 500] to have 1 channels, but got 10 channels instead</code>.<br>
For the reminder, I’m trying to compute the <code>cross-correlation</code> using one layer neural network <code>conv2d</code>, aiming to align the signal between the original signal and reference signal. Unfortunately, I can’t run my code because the tensors size of my original signal and reference signal don’t match.<br>
Here is tensors’ sizes:<br>
original signal : torch.Size([2, 10, 1, 500<br>
reference signal : torch.Size([1, 1, 512, 0])</p>
<p>Therefore, my question is: How can I reshape the tensors and make them match? I don’t know if my problem is because I didn’t correctly unsqueeze the signal.<br>
Need your help. Thanks.</p>
<p>Here is my cross-corralation code:</p>
<pre><code class="lang-auto">def _signal_align(self, signal, reference_signal):
        
        signal = torch.tensor(signal)

        reference_signal_tensor = torch.tensor(reference_signal)

        signal = signal.unsqueeze(2)

        reference_signal_tensor = reference_signal_tensor.unsqueeze(0).unsqueeze(0)

        reference_signal_tensor = torch.flip(reference_signal_tensor, dims=[2])

        # cross-correlation
        corr_idex = F.conv2d(signal, reference_signal_tensor)
        # shift btw signal and reference
        shift_signal = max_corr_idx - (len(signal) - 1)
        # roll function used to shift the signal by calculated shift
        align_signal = torch.roll(signal, shifts=int(shift_signal), dims=0)
        return align_signal
</code></pre>

username: ptrblck
cooked: <aside class="quote no-group" data-username="vin" data-post="1" data-topic="173903">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/v/a87d85/48.png" class="avatar"> vin:</div>
<blockquote>
<p>reference signal : torch.Size([1, 1, 512, 0])</p>
</blockquote>
</aside>
<p>Your reference is empty:</p>
<pre data-code-wrap="python"><code class="lang-python">x = torch.randn([1, 1, 512, 0])
print(x)
# tensor([], size=(1, 1, 512, 0))
</code></pre>
<p>so the output will also be empty.</p>

username: vin
cooked: <p>After some changes, I got this message <code>RuntimeError: Given groups=1, weight of size [1, 1, 512, 511], expected input[2, 10, 1, 500] to have 1 channels, but got 10 channels instead</code>. So now, my reference signal is not empty again right? How can I match the channels and the dimensions, just by reshaping with: <code>signal.reshape(-1)</code>  or <code>signal.flatten(-1)</code> or any suggestion. Thanks</p>

username: ptrblck
cooked: <p>No, <code>.reshape</code> or <code>.view</code> operations could interleave your tensor and create an invalid input.<br>
Use <code>.permute</code> instead to permute the dimensions of a tensor.<br>
Assuming <code>dim2</code> in <code>[2, 10, 1, 500]</code> represents the channel dimension, you could use:</p>
<pre><code class="lang-python">x = x.permute(0, 2, 1, 3)
</code></pre>
<p>but make sure it is indeed the channel dimension.</p>

username: vin
cooked: <p>Sir, this is after using the .<code>.permute()</code>. Unfortunately, I got this: <code> corr_idex = F.conv2d(signal, reference_signal_tensor) RuntimeError: Calculated padded input size per channel: (10 x 500). Kernel size: (512 x 511). Kernel size can't be greater than actual input size</code>. What should be the difference if I use the <code>F.conv1d()</code> rather than <code>F.conv2d()</code>? If I switch my network to  <code>F.conv1d()</code> I suddenly got this message: <code>RuntimeError: Expected 2D (unbatched) or 3D (batched) input to conv1d, but got input of size: [2, 1, 10, 500]</code> where I consider the dimension of the reference_signal after flipping at <code>dim=1</code>. How can I make it run well?</p>
<pre><code class="lang-auto">    def _signal_align(self, signal, reference_signal):
       
        signal = torch.tensor(signal)
        reference_signal_tensor = torch.tensor(reference_signal)
        signal = signal.unsqueeze(2)
        signal = torch.mean(signal, dim=2, keepdim=True)
        signal = signal.permute(0, 2, 1, 3)
        print(signal.size())
        reference_signal_tensor = reference_signal_tensor.unsqueeze(0).unsqueeze(0)
        print(reference_signal_tensor.size())
        reference_signal_tensor = torch.flip(reference_signal_tensor, dims=[2])
        print(reference_signal_tensor.size())
        # cross-correlation
        corr_idex = F.conv2d(signal, reference_signal_tensor)
        # # find index
        max_corr_idx = torch.argmax(corr_idex)
        # shift btw signal and reference
        shift_signal = max_corr_idx - (len(signal) - 1)
        # roll function used to shift the signal by calculated shift
        align_signal = torch.roll(signal, shifts=int(shift_signal), dims=0)
        return align_signal
</code></pre>

username: ptrblck
cooked: <aside class="quote no-group" data-username="vin" data-post="5" data-topic="173903">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/v/a87d85/48.png" class="avatar"> vin:</div>
<blockquote>
<p><code>RuntimeError: Calculated padded input size per channel: (10 x 500). Kernel size: (512 x 511). Kernel size can't be greater than actual input size</code></p>
</blockquote>
</aside>
<p>Try to change the order of input arguments as your current weight is larger than the input:</p>
<pre data-code-wrap="python"><code class="lang-python">a = torch.randn([1, 1, 512, 511])
b = torch.randn([2, 1, 10, 500])

# works since kernel is smaller than input
F.conv2d(a, b)

# fails
F.conv2d(b, a)
# RuntimeError: Calculated padded input size per channel: (10 x 500). Kernel size: (512 x 511). Kernel size can't be greater than actual input size
</code></pre>
<aside class="quote no-group" data-username="vin" data-post="5" data-topic="173903">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/v/a87d85/48.png" class="avatar"> vin:</div>
<blockquote>
<p>What should be the difference if I use the <code>F.conv1d()</code> rather than <code>F.conv2d()</code>?</p>
</blockquote>
</aside>
<p><code>F.conv2d</code> expects a 4-dimensional input in the shape <code>[batch_size, channels, height, width]</code> and uses a 4-dimensional filter in the shape <code>[out_channels, in_channels, height, width]</code> while <code>F.conv1d</code> uses a 3-dimensional inputs and filters by replacing <code>height</code> and <code>width</code> with the <code>sequence_length</code>.</p>

username: vin
cooked: <p>I got this after changing the order : <code>corr_idex = F.conv2d(reference_signal_tensor, signal) RuntimeError: Input type (torch.FloatTensor) and weight type (CPUComplexDoubleType) should be the same or input should be a MKLDNN tensor and weight is a dense tensor</code></p>

username: vin
cooked: <p>Please Sir, can you check this update:</p>
<pre><code class="lang-auto">  def _signal_align(self, signal, reference_signal):
        # Convert input signals to complex-valued tensors
        signal = torch.tensor(signal).unsqueeze(-1)
        reference_signal_tensor = torch.tensor(reference_signal)
        reference_signal_tensor = reference_signal_tensor.flip(0).unsqueeze(0).unsqueeze(-1).float()
        signal = signal.permute(0, 2, 1, 3)
        corr_idex = F.conv2d(reference_signal_tensor, signal, padding=1, stride=1)
        # Find the index of the maximum correlation
        max_corr_idx = torch.argmax(torch.abs(corr_idex))
        # Compute the signal shift and align the signal
        shift_signal = max_corr_idx - (len(signal) - 1)
        align_signal = torch.roll(signal, shifts=int(shift_signal), dims=0)
        # Transpose the aligned signal tensor back to the original shape
        align_signal = align_signal.transpose(2, 3).transpose(1, 2).squeeze(-1)

        return align_signal
</code></pre>
<p>But got this error: <code>corr_idex = F.conv2d(reference_signal_tensor, signal, padding=1, stride=1) RuntimeError: Given groups=1, weight of size [2, 500, 10, 1], expected input[1, 512, 511, 1] to have 500 channels, but got 512 channels instead</code></p>

username: ptrblck
cooked: <p>The previous error is raised by a <code>dtype</code> and <code>device</code> mismatch, but it seems you have already solved it.<br>
The new one is raised by an invalid shape as <code>F.conv2d</code> expects an input in the shape <code>[batch_size, channels, height, width]</code> and a filter weight in the shape <code>[out_channels, in_channels, height, width]</code> in the default setup.<br>
Could you explain what each dimension of your input and filter represents?</p>

username: vin
cooked: <p>Hi Sir. As I said before, this script will help to align the signal to compute the cross correlation. However, the input signal is none than the collected signal from a device which represent the 2-dimension signal enable to perform the convolution network (conv2d). From the script, I directly call the convolution network without designing a proper or complet model as we usually do. This code <code>corr_idex = F.conv2d(reference_signal_tensor, signal, padding=1, stride=1)</code> is directily call and assuming both the reference and original signal. Unfortunately, after calculation, the reference signal is in <code>dim=1</code> which may mismatch with the original_signal.</p>
<p>So, If I can get the point, isn’t possible by directly calling the convolution itself,  right? Should I design a small network which consider only one layer and call it first into the signal, and start aligning-to-correlation step? Is this request a huge data, or by considering one-by-one signal the one layer network can perform good result? The goal here is to align each single one-by-one before to reach the last step that use a network to classify all data for prediction and analyse.</p>
<p>Request for your suggestion. Or should I consider traditional methods to align<br>
and correlate the signal? Need your help</p>