Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8665797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:34:27+00:00 2026-06-12T17:34:27+00:00

I wrote this simple test of Pybrain neural networks, but it doesn’t act like

  • 0

I wrote this simple test of Pybrain neural networks, but it doesn’t act like I expect it to. The idea is to train it on a dataset of numbers up to 4095, with a class for prime and non-prime.

#!/usr/bin/env python
# A simple feedforward neural network that attempts to learn Primes

from pybrain.datasets import ClassificationDataSet
from pybrain.tools.shortcuts import buildNetwork
from pybrain.supervised import BackpropTrainer

class PrimesDataSet(ClassificationDataSet):
    """ A dataset for primes """

    def generatePrimes(self, n):
        if n == 2:
            return [2]
        elif n < 2:
            return []
        s = range(3, n + 1, 2)
        mroot = n ** 0.5
        half = (n + 1) / 2 - 1
        i = 0
        m = 3
        while m <= mroot:
            if s[i]:
                j = (m * m - 3) / 2
                s[j] = 0
                while j < half:
                    s[j] = 0
                    j += m
            i = i + 1
            m = 2 * i + 3
        return [2] + [x for x in s if x]

    def binaryString(self, n):
        return "{0:12b}".format(n)

    def __init__(self):
        ClassificationDataSet.__init__(self, 12, 1)
        primes = self.generatePrimes(4095)
        for prime in primes:
            b = self.binaryString(prime).split()
            self.addSample(b, [1])
        for n in range(4095):
            if n not in primes:
                b = self.binaryString(n).split()
                self.addSample(b, [0])

def testTraining():
    d = PrimesDataSet()
    d._convertToOneOfMany()
    n = buildNetwork(d.indim, 12, d.outdim, recurrent=True)
    t = BackpropTrainer(n, learningrate = 0.01, momentum = 0.99, verbose = True)
    t.trainOnDataset(d, 100)
    t.testOnData(verbose=True)
    print "Is 7 prime? ",   n.activate(d.binaryString(7).split())
    print "Is 6 prime? ",   n.activate(d.binaryString(6).split())
    print "Is 100 prime? ", n.activate(d.binaryString(100).split())


if __name__ == '__main__':
    testTraining()

Disregarding (please) the question of whether this is even possible, my problem is that the last three print statements testing whether 7, 6, and 100 are prime all return the same:

Is 7 prime?  [ 0.34435841  0.65564159]
Is 6 prime?  [ 0.34435841  0.65564159]
Is 100 prime?  [ 0.34435841  0.65564159]

(or something similar)
The way that I am interpreting these results is that the neural network predicts with 65% certainty that each of those numbers is a prime number. Has my neural network learnt to treat all inputs the same, or am I doing something wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T17:34:29+00:00Added an answer on June 12, 2026 at 5:34 pm

    It looks like you are using only a single input, actually.

    d.binaryString(7).split()
    

    is equivalent to

    "{0:12b}".format(7).split()
    

    which evaluates to

    ['111'].
    

    I think what you intended was something like

    [int(c) for c in "{0:012b}".format(7)]
    

    the result of which is

    [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1]
    

    P.S. It’s always a good idea to check what exactly it is you feed into your statistical model 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote a simple test program in c++ but why does this crash on:
I wrote this simple code (in python) in test.py. I try to run timeit
I am trying to understand the fundamentals of blocks. I wrote this simple test:
I wrote this simple test code, by adapting a piece from a book, to
I have wrote this simple console app to test when we change the timezone
still fairly new to ruby, and wrote this very simple recursive function. def test(input)
i wrote this simple example: //file TestController.java public interface TestController { public List<Test> findAll();
i wrote this code for showing some simple picture on the applet screen, but
This is for Play! Framework 2.0. I'm trying to write a simple test case
I wrote this simple C code and compiled it using Visual Studio 2010, with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.