I want to create a CNN in Python, specifically, only with NumPy, if possible. For optimizing the time of convolution (actually correlation) in the network, I wanna try to use FFT-based convolution. The data that needs to be convoluted (correlated) is a 4D image tensor with shape [batch_size, width, height, channels] and 4D filter tensor [filter_width, filter_height, in_channel, out_channel]. I read a lot of articles about FFT-based convolution, but they aren't doing it in my way. Thus, I need your help.

How could I fft-convolve a 4D image and a 4D filter with stride?

  • $\begingroup$ Hello. It's not clear what your question is. Please, explicitly ask a specific question about your problem. $\endgroup$
    – nbro
    Sep 10, 2020 at 21:23
  • $\begingroup$ Hi, @nbro, I did it. $\endgroup$ Sep 10, 2020 at 21:26
  • $\begingroup$ Ok, thanks for clarifying that. If you're looking for code, this site is not appropriate to ask your question (i.e. programming questions are generally off-topic here), so your question would be off-topic. You can read more about what is on-topic here: ai.stackexchange.com/help/on-topic, and I suggest that you read it. Are you asking for code? If yes, then probably Stack Overflow is the best place to ask your question. If you want, I can migrate your question to Stack Overflow. $\endgroup$
    – nbro
    Sep 10, 2020 at 21:27
  • $\begingroup$ Actually, No. I want to understand how to FFT-Convolve two 4D tensors with clarifying. $\endgroup$ Sep 10, 2020 at 21:29
  • 1
    $\begingroup$ Thanks, @nbro again). You're really good moderator $\endgroup$ Sep 10, 2020 at 21:33

2 Answers 2


I think this should work for you: scipy.signal.correlate | SciPy

I used it myself while I was writing a CNN in numpy.

  • $\begingroup$ Thanks, but how to correlate with stride? $\endgroup$ Sep 10, 2020 at 21:24
  • $\begingroup$ Mm, it doesn't seem optimal, but I don't know a better way that just to correlate and take elements with the corresponding step. $\endgroup$ Sep 10, 2020 at 21:29
  • $\begingroup$ Anyway, thanks for good answer. I'll use it in my code. $\endgroup$ Sep 10, 2020 at 21:30
  • $\begingroup$ Given that the OP apparently wants to implement the correlation/convolution from scratch, then maybe you should spend some words to explain how that can be conceptually done. By the way, welcome to AI SE! Have a look at our on-topic page ai.stackexchange.com/help/on-topic to know more about the type of question we are looking for. $\endgroup$
    – nbro
    Sep 10, 2020 at 21:32

I think what you need to use is 3D convolution operation. Your data is 3D, width, height, and num_channels. Your data is similar to color images with RGB channels. However, since you are trying to consider the correlation amongst channels 2D convolution will not work for you. You can use 3D convolution which is available to use with deep learning tools such as Tensorflow.

  • $\begingroup$ P.S. My data is 4D: [ batch_size, width, height, and num_channels ]. $\endgroup$ Sep 16, 2020 at 11:13
  • $\begingroup$ Yes, my images are RGB $\endgroup$ Sep 16, 2020 at 11:13
  • $\begingroup$ What is the 4 dimensions in your data, i only see 3, batch_size is not a dimension of data, isnt it the batch size of the input of the neural network or is it somthing else. $\endgroup$
    – gihan
    Sep 16, 2020 at 14:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .