Assume that you are doing convolution inside a CNN network, by using FFT because FFT is much way faster than using 4-5 for-loops etc.

But how should I train the weights if I know the output of my CNN network and how well it predict the class labels?

Can I use Reinforcement Learning?


1 Answer 1


If you're doing this strictly for learning the inner machinery of how a CNN works, then whipping up something in C++ or python or your language of choice is fine, and can be a good learning exercise. In order to train the weights, you'll want to define a loss function that takes the expected output (i.e. labels), and the predicted output, and do the back-propagation of the error signal through your network. There is a python tutorial that does this here: https://towardsdatascience.com/convolutional-neural-networks-from-the-ground-up-c67bb41454e1

If you want something with performance and stability, I'd strongly recommend starting with either Pytorch or Tensorflow, as they have built-in loss functions and autograd built in, and they are optimized for Gpu execution, so not running 4-5 for loops. Pytorch has a CNN tutorial here: https://pytorch.org/tutorials/beginner/blitz/cifar10_tutorial.html

and Tensorflow here: https://www.tensorflow.org/tutorials/images/cnn

  • $\begingroup$ Thank you. I'm doing everything in C. So loss function you say. So if I have the weight matrix $W$ and the output vector $J$. If the L2-norm of $J$ is small, then the $W$ matrix is optimal. In this case, I'm using Support Vector Machine as classificer, not Neural netork. But I will use a CNN network for extracting the features into a featuremap, but I don't know how to tune in the $W$ matrix with respct on $J$ vector (output). $\endgroup$
    – euraad
    Commented Aug 29, 2023 at 14:10
  • $\begingroup$ In the python tutorial linked above, you can find the loss function for categorical cross-entropy defined in python, 2 lines of code so pretty straightforward to translate into C. But without knowing more about the problem you're trying to solve, it may or may not be applicable. $\endgroup$ Commented Aug 30, 2023 at 10:19
  • $\begingroup$ Perhaps you can describe what you're trying to do end-to-end, i.e. what problem you're trying to solve overall, and we can help advise on best practices. For example, if you're doing a classification problem, you probably want to keep everything within the CNN instead of extracting feature maps and feeding into a separate SVM. I'd also repeat if this is anything other than a learning exercise it's highly recommended to use an existing framework. $\endgroup$ Commented Aug 30, 2023 at 10:20
  • $\begingroup$ Thank you. Well, my idea is to use FFT with padding over an image, before classification with SVM. I know how to train the weights for SVM, but not the filter/kernel/weight matrix for CNN. $\endgroup$
    – euraad
    Commented Aug 30, 2023 at 17:31
  • $\begingroup$ Both of the tutorials I linked do image classification, so hopefully that helps. In general, a single neural network is used with convolutional layer followed by max pooling layer, repeated, then followed by one or more dense fully connected layers. The output of the last fully connected layer is the image class. In pytorch the entire model for the CNN is about 15-20 lines of code, in C, you're looking at hundreds or thousands of lines of code if you're doing this all from scratch. good luck $\endgroup$ Commented Aug 31, 2023 at 9:49

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.