How to create a model that can give an output with a range of 0 to 1 with a sigmoid activation function where the value closer to 0 means the lesser chance that the input number is not prime and the closer to 1 means the greter chance that the input number is prime.

This means that the output layer will contain one neuron with a sigmoid activation function.

But I don't know what the input looks like.

So, from this problem, I would like to ask, what does the dataset look like?

Is it a one-dimensional list of prime numbers? like: [2,3,5,...,9997]

Or is it actually an unsupervised learning problem?

What do the input, hidden, and output layers look like?

So in the end, I was expecting this kind of input-output:

Input: 77772
Output: 0.01

Input: 27777
Output: 0.69
  • $\begingroup$ To create a model with a sigmoid activation function for prime number prediction (0 to 1 scale), I need more details about your dataset, so that i can better help you. $\endgroup$
    – user76624
    Commented Sep 29, 2023 at 13:15
  • $\begingroup$ @RAG The dataset is the question too, whether it will be unsupervised learning or supervised learning. $\endgroup$ Commented Sep 30, 2023 at 19:18

1 Answer 1


One idea that comes to mind is having the features (inputs) be the digits of the number, rather than the number itself. You might also try converting to binary before hand and then using those digits as features, or use some kind of one hot encoding for the decimal digits. The rationale being that finding primes is a discrete task dealing with integers, you don't want your model interpreting the input as a continuous value.

You might get better results if you include the modulo of the input with some primes (or just random integers) as features as well.

As for the hidden layers pick your favorite architecture. Standard linear layers followed by ReLU activations are acceptable. As you said, the last layer would be a sigmoid activation (preceded by a linear layer, not a ReLU). You can experiment with things like batch norm if you want although I wouldn't imagine they would help much.

With this setup your training data would have features as the binary digits of a positive integer, and the target would be whether or not that integer is prime (0 or 1). Your loss would be binary cross entropy. Train with stochastic gradient descent or friend until convergence. Note that your training data is very imbalanced as most numbers are not prime.


You must log in to answer this question.

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