训练网络以识别X ^ 2 + Y ^ 2 + Z ^ 2 <= 25

时间:2016-08-01 15:28:42

标签: python machine-learning 3d neural-network pybrain

我正在尝试使用神经网络来预测某个点是否落在由等式X ^ 2 + Y ^ 2 + Z ^ 2 <= 25所形成的球体内或之内。我正在这样做然而,使用Pybrain和python,无论我如何配置,我都无法获得高于10%的准确度。 10次​​中有9次,网络将收集关于.06,.07的错误,并拒绝继续训练......如果有人能够了解情况那么好!

如果遇到此问题,您会使用多少个隐藏节点? Tanh还是S形?学习率?动量?附:我确实有一些神经网络的经验,我从头开始在java编写一个tic-tac-toe,并想学习如何更好地使用Pybrain,我想这将是一个很好的项目! (3维,无限数据,明确切割)

这可能只是一个pybrain问题吗?我应该尝试其他设置吗?如果有,任何建议?

下面的代码,如果你想给它一个镜头 - 每个变量都可以在执行时确定,所以你可以乱七八糟(除了tanh / sigmoid - 如果你想尝试sigmoid,只需删除net的声明中的hiddenclass = Tanhlayer参数,它将默认)

from pybrain.tools.shortcuts import buildNetwork
from pybrain.datasets import SupervisedDataSet
from pybrain.supervised.trainers import BackpropTrainer
from pybrain.structure import TanhLayer
from random import randint
import random

def getAnswer(decimal):
    answer = -1
    if decimal >= 0:
        answer = 1

    return answer

nodes = int(input("node number: "))
bias = bool(input("Bias: "))
net = buildNetwork(3, nodes, 1, hiddenclass=TanhLayer, bias=bias)
net.randomize()

#Generate Dataset
ds = SupervisedDataSet(3, 1)
dataSize = int(input("Sample size: "))

#Randomly generate points for the data set
randRate = int(input("Cluter probability: "))
for i in range(0, dataSize):
    randX = randint(-6, 6)
    randY = randint(-6, 6)
    randZ = randint(-6, 6)
    target = -1

    if(randX**2 + randY**2 + randZ**2) <= 25:
        target = 1

    if (random.random() < randRate) and target == 1:  #Clutter the data
        target = -1

    #print("Adding ", randX, ", ", randY, ", ", randZ, " - target: ", target)
    ds.addSample([randX, randY, randZ], [target, ])

#Train the model using the user specified input
ls = float(input('Enter learning speed:'))
mom = float(input('Enter momentum: '))
epochs = input('Enter training amount (Enter "con" to go until Convergence"): ')
dispRate = int(input('Enter display rate: '))

trainer = BackpropTrainer(net, ds, ls, mom)
if epochs != 'con':
    epochs = int(epochs)
    for i in range(0, epochs):
        error = trainer.train()
        if(i % dispRate == 0):
            print("Training is ", (i/epochs) * 100, "% Complete. Error = ", error)
else:
    trainer.trainUntilConvergence(ds, verbose=True)

print("Testing accuracy...") #Test the accuracy with 1000 random test points
correct = 0
for n in range(0, 1000):
    randX = randint(-6, 6)
    randY = randint(-6, 6)
    randZ = randint(-6, 6)
    answer = getAnswer(net.activate([randX, randY, randZ]))
    inSphere = randX**2 + randY**2 + randZ**2 <= 25
    if(answer == 1) and inSphere == True:
        correct += 1
    elif(answer == -1) and inSphere == False:
        correct += 1
print("Accuracy: ", 100 * (correct/1000), "%")

inp = ''
#Let the user test the model
while inp != 'quit':
    tX = input('sample X: ')
    tY = input('sample Y: ')
    tZ = input('sample Z: ')
    print(net.activate([tX, tY, tZ]))
    inp = input("keep going? (quit to stop)")

0 个答案:

没有答案