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

2024-10-02 12:28:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用一个神经网络来预测一个点是否落在由X^2+Y^2+Z^2<;=25方程形成的球体上。我是用Pybrain和python来实现的,但是,无论我如何配置这个东西,我都不能获得超过10%的准确率。10次中有9次,网络会收敛到0.06、.07左右的错误,并拒绝继续训练。。。如果有人能透露一些情况,那就太好了!你知道吗

如果遇到这个问题,您会使用多少隐藏节点?Tanh还是sigmoid?学习率?动力?另外,我确实有一些神经网络的经验,我从头开始用java为tic-tac-toe编写了一个,我想学习如何更好地使用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)")

Tags: theanswerfromimporttargetinputnetif

热门问题