2

我写了这个 Pybrain 神经网络的简单测试,但它并没有像我期望的那样运行。我们的想法是在最多 4095 个数字的数据集上对其进行训练,其中包含素数和非素数类。

#!/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()

忽略(请)这是否可能的问题,我的问题是测试 7、6 和 100 是否为素数的最后三个打印语句都返回相同的结果:

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

(或类似的东西)我解释这些结果的方式是神经网络以 65% 的把握预测这些数字的每一个都是质数。我的神经网络是否学会了对所有输入都一视同仁,还是我做错了什么?

4

1 回答 1

0

实际上,您似乎只使用了一个输入。

d.binaryString(7).split()

相当于

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

评估为

['111'].

我认为你的意图是

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

结果是

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

PS 检查你输入统计模型的确切内容总是一个好主意:)

于 2012-10-11T09:46:59.577 回答