Suppose I have a known function, for example:

def func(x):
    m = np.mean(x)
    if m > 1: 
       return 1
       return 0

Are there any algorithms to convert this into a neural network?

A naive way would be:

  • use func to generate training set
  • train the NN using the training set

But this will be slow and the trained NN will probably be inaccurate. Are there better ways to "copy" a known function?

As the questions suggests, I am not asking about the about trivial example function, I am asking about a general known function!

  • $\begingroup$ Why is it necessary to do something like this? If you know the function, why not use it directly? $\endgroup$
    – mikkola
    Jul 6, 2022 at 12:29
  • $\begingroup$ it can be really useful, but I am not gonna talk about it here to raise unnecessary debates. of course you can think of it as "unnecessary", but that doesn't change the fact that the above is a valid academic question $\endgroup$ Jul 6, 2022 at 12:35

1 Answer 1


In this case it is possible to translate the function into a neural network without training because you know the function and don't need to learn the weights.

If you know the number of elements in x, then the mean can be a single layer network with the number of elements in x as an input and no output. The weights of the network are just 1/len(x), and you can set the bias to 0. Then you can build your own activation function which returns 1 if the output is superior to 1, 0 else.

  • $\begingroup$ Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center. $\endgroup$
    – Community Bot
    Jul 6, 2022 at 15:08
  • $\begingroup$ note that I highlighted example, the function I gave is just an example! and it is so easy to just manually set the paramerter, what i am asking is how to do it generally! $\endgroup$ Jul 7, 2022 at 3:14
  • $\begingroup$ in that case I don't think it's possible to help you further. But your suggested method is probably best: you can generate samples using the deterministic function and use those to train a NN. The most difficult part will be to find the appropriate architecture and compexity $\endgroup$
    – theophile
    Jul 7, 2022 at 7:52

You must log in to answer this question.

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