我编写了这个简单的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%的确定性预测这些数字中的每一个都是质数。我的神经网络是否学会了将所有输入视为相同,还是做错了?
最佳答案
看起来您实际上只使用了一个输入。
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]
附言检查输入到统计模型中的确切信息总是一个好主意:)
关于python - 神经网络报告针对不同激活的相同响应,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12829102/