--------------- TypeError Traceback (most recent call last) <ipython-input-41-8666c766a777> in <module> 15 # pycwt = CWT(widths, "cmorlet", 3, 2048) 16 ---> 17 out = py(x_ten) 18 19 print(out.shape) /

111555: <pre><code class="lang-auto">class CWT(nn.Module):
    def __init__(
        self,
        widths,
        wavelet="ricker",
        channels=1,
        filter_len=2000,
    ):
        """PyTorch implementation of a continuous wavelet transform.

        Args:
            widths (iterable): The wavelet scales to use, e.g. np.arange(1, 33)
            wavelet (str, optional): Name of wavelet. Either "ricker" or "morlet".
            Defaults to "ricker".
            channels (int, optional): Number of audio channels in the input. Defaults to 3.
            filter_len (int, optional): Size of the wavelet filter bank. Set to
            the number of samples but can be smaller to save memory. Defaults to 2000.
        """
        super().__init__()
        self.widths = widths
        self.wavelet = getattr(self, wavelet)
        self.filter_len = filter_len
        self.channels = channels
        self.wavelet_bank = self._build_wavelet_bank()

    def ricker(self, points, a):
        # https://github.com/scipy/scipy/blob/v1.7.1/scipy/signal/wavelets.py#L262-L306
        A = 2 / (np.sqrt(3 * a) * (np.pi ** 0.25))
        wsq = a ** 2
        vec = torch.arange(0, points) - (points - 1.0) / 2
        xsq = vec ** 2
        mod = 1 - xsq / wsq
        gauss = torch.exp(-xsq / (2 * wsq))
        total = A * mod * gauss
        return total

    def morlet(self, points, s):
        x = torch.arange(0, points) - (points - 1.0) / 2
        x = x / s
        # https://pywavelets.readthedocs.io/en/latest/ref/cwt.html#morlet-wavelet
        wavelet = torch.exp(-(x ** 2.0) / 2.0) * torch.cos(5.0 * x)
        output = np.sqrt(1 / s) * wavelet
        return output

    def cmorlet(self, points, s, wavelet_width=1, center_freq=1):
        # https://pywavelets.readthedocs.io/en/latest/ref/cwt.html#complex-morlet-wavelets
        x = torch.arange(0, points) - (points - 1.0) / 2
        x = x / s
        norm_constant = np.sqrt(np.pi * wavelet_width)
        exp_term = torch.exp(-(x ** 2) / wavelet_width)
        kernel_base = exp_term / norm_constant
        kernel = kernel_base * torch.exp(1j * 2 * np.pi * center_freq * x)
        return kernel

    def _build_wavelet_bank(self):
        """This function builds a 2D wavelet filter using wavelets at different scales

        Returns:
            tensor: Tensor of shape (num_widths, 1, channels, filter_len)
        """
        wavelet_bank = [
            torch.conj(torch.flip(self.wavelet(self.filter_len, w), [-1]))
            for w in self.widths
        ]
        wavelet_bank = torch.stack(wavelet_bank)
        wavelet_bank = wavelet_bank.view(
            wavelet_bank.shape[0], 1, 1, wavelet_bank.shape[1]
        )
        wavelet_bank = torch.cat([wavelet_bank] * self.channels, 2)
        return wavelet_bank

    def forward(self, x):
        """Compute CWT arrays from a batch of multi-channel inputs

        Args:
            x (torch.tensor): Tensor of shape (batch_size, channels, time)

        Returns:
            torch.tensor: Tensor of shape (batch_size, channels, widths, time)
        """
        x = x.unsqueeze(1)
        if self.wavelet_bank.is_complex():
            wavelet_real = self.wavelet_bank.real.to(device=x.device, dtype=x.dtype)
            wavelet_imag = self.wavelet_bank.imag.to(device=x.device, dtype=x.dtype)

            output_real = nn.functional.conv2d(x, wavelet_real, padding="same")
            output_imag = nn.functional.conv2d(x, wavelet_imag, padding="same")
#This
            output_real = torch.transpose(output_real, 1, 2)
            output_imag = torch.transpose(output_imag, 1, 2)
            return torch.complex(output_real, output_imag)
#             return output_real, output_imag
        else:
            self.wavelet_bank = self.wavelet_bank.to(device=x.device, dtype=x.dtype)
            output = nn.functional.conv2d(x, self.wavelet_bank, padding="same")
            return torch.transpose(output, 1, 2)
</code></pre>
<p>==&gt;</p>
<pre><code class="lang-auto">&gt; *# pycwt = CWT(widths, "cmorlet", 3, 4096)*
&gt; 
&gt; *# pycwt = CWT(widths, "ricker", 3)*
&gt; *x = np.load(train.loc[i, 'file_path'])*
&gt; *x *= signal.tukey(4096, 0.2)*
&gt; 
&gt; 
&gt; *x = apply_bandpass(x, 30, 480)*
&gt; *x_ten = torch.tensor(x, dtype=torch.float32).view(1, 3, 4096)*
&gt; 
&gt; 
&gt; *widths = np.arange(start=10, stop=90)*
&gt; 
&gt; *pycwt = CWT(widths, "cmorlet", 3, 4096)*
&gt; *# pycwt = CWT(widths, "cmorlet", 3, 2048)*
&gt; *#this==&gt;&gt;&gt;&gt;*
&gt; *out = pycwt(x_ten)*
&gt; 
&gt; *print(out.shape)*
&gt; *mag = torch.absolute(out)*
&gt; 
&gt; *plt.imshow(*
&gt; *    mag[0, 0].numpy(),*
&gt; *    aspect="auto",*
&gt; *    vmax=mag[0, 0].max(),*
&gt; *    vmin=mag[0, 0].min(),*
&gt; *);*
```This is executing code.

Blockquote

Explanation:
It works fine with google colab, but
But it doesn't work with kaggle kernel.
Why is this? I'm having a hard time finding a good answer.
I don't understand why it doesn't work when I use tuples of numbers.</code></pre>

ptrblck: <p>Could you update your code and post the entire error message as it’s a bit hard to see what is exactly failing with which error, please?</p>

111555: <blockquote>
<hr>
<p>TypeError                                 Traceback (most recent call last)<br>
 in <br>
27 # pycwt = CWT(widths, “cmorlet”, 3, 2048)*<br>
28 # &gt; <em>#this==&gt;&gt;&gt;&gt;</em><br>
—&gt; 29 out = pycwt(x_ten)<br>
30 # &gt;<br>
31 print(out.shape)</p>
<p>/opt/conda/lib/python3.7/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)<br>
725             result = self._slow_forward(*input, **kwargs)<br>
726         else:<br>
 → 727             result = self.forward(*input, **kwargs)<br>
728         for hook in itertools.chain(<br>
729                 _global_forward_hooks.values(),</p>
<p> in forward(self, x)<br>
84             wavelet_imag = self.wavelet_bank.imag.to(device=x.device, dtype=x.dtype)<br>
85<br>
—&gt; 86             output_real = nn.functional.conv2d(x, wavelet_real, padding=“same”)<br>
87             output_imag = nn.functional.conv2d(x, wavelet_imag, padding=“same”)<br>
88 <span class="hashtag-raw">#This</span></p>
<p>TypeError: conv2d(): argument ‘padding’ must be tuple of ints, not str</p>
<blockquote>
<p>Blockquote</p>
</blockquote>
</blockquote>
<p>I’m really sorry, ptrblck. I had a brain fart about my first question. I think I was drunk.</p>
<aside class="onebox allowlistedgeneric" data-onebox-src="https://www.kaggle.com/anjum48/continuous-wavelet-transform-cwt-in-pytorch">
  <header class="source">
      <img src="https://www.kaggle.com/static/images/favicon.ico" class="site-icon" width="32" height="32">

      <a href="https://www.kaggle.com/anjum48/continuous-wavelet-transform-cwt-in-pytorch" target="_blank" rel="noopener nofollow ugc">kaggle.com</a>
  </header>

  <article class="onebox-body">
    <img width="100" height="100" src="https://storage.googleapis.com/kaggle-avatars/thumbnails/421965-kg.jpg" class="thumbnail onebox-avatar">

<h3><a href="https://www.kaggle.com/anjum48/continuous-wavelet-transform-cwt-in-pytorch" target="_blank" rel="noopener nofollow ugc">Continuous wavelet transform (CWT) in PyTorch</a></h3>

  <p>Explore and run machine learning code with Kaggle Notebooks | Using data from G2Net Gravitational Wave Detection</p>


  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>
<p>
The code is from this person.<br>
In case you are wondering, let me organize the code in a way that is easy to understand.</p>
<p>I’m really sorry.</p>

111555: <p>This post was flagged by the community and is temporarily hidden.</p>

ptrblck: <p>The error message indicates that the <code>str</code> argument is not recognized for <code>padding</code> in <code>conv2d</code>, so you might need to update to PyTorch <code>1.9.0</code> to be able to use this argument type.</p>

111555: <p>It’s amazing. I’m impressed.</p>
<h1>
<a name="pip3-install-upgrade-torch-torchvision-1" class="anchor" href="#pip3-install-upgrade-torch-torchvision-1"></a>!pip3 install --upgrade torch torchvision</h1>
<p>Now it’s done.<br>
However, please note that it is not possible to reproduce the problem unless you first remove the kernel and then try again.</p>