Pregunta

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?

¿Fue útil?

Solución

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 :)

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top