3 입력 논리 AND 게이트에 대한 진리표는 신경망 아래에 만들었지 만 [1,1,0]의 예상 출력은 정확하지 않습니다. 출력은 다음과 같아야합니다. 그러나 0.9로 예측하면 약 1을 의미합니다. 따라서 출력이 올바르지 않습니다. 그래서 내가 알아야 할 것은 출력 예측을 더 정확하게 만드는 방법입니다. 저를 안내해주십시오.신경망에서 정확한 예측을 얻는 방법
import numpy as np
class NeuralNetwork():
def __init__(self):
self.X = np.array([[0, 0, 0],
[0, 0, 1],
[0, 1, 0],
[0, 1, 1],
[1, 0, 0],
[1, 0, 1],
[1, 1, 1]])
self.y = np.array([[0],
[0],
[0],
[0],
[0],
[0],
[1]])
np.random.seed(1)
# randomly initialize our weights with mean 0
self.syn0 = 2 * np.random.random((3, 4)) - 1
self.syn1 = 2 * np.random.random((4, 1)) - 1
def nonlin(self,x, deriv=False):
if (deriv == True):
return x * (1 - x)
return 1/(1 + np.exp(-x))
def train(self,steps):
for j in xrange(steps):
# Feed forward through layers 0, 1, and 2
l0 = self.X
l1 = self.nonlin(np.dot(l0, self.syn0))
l2 = self.nonlin(np.dot(l1, self.syn1))
# how much did we miss the target value?
l2_error = self.y - l2
if (j % 10000) == 0:
print "Error:" + str(np.mean(np.abs(l2_error)))
# in what direction is the target value?
# were we really sure? if so, don't change too much.
l2_delta = l2_error * self.nonlin(l2, deriv=True)
# how much did each l1 value contribute to the l2 error (according to the weights)?
l1_error = l2_delta.dot(self.syn1.T)
# in what direction is the target l1?
# were we really sure? if so, don't change too much.
l1_delta = l1_error * self.nonlin(l1, deriv=True)
self.syn1 += l1.T.dot(l2_delta)
self.syn0 += l0.T.dot(l1_delta)
print("Output after training:")
print(l2)
def predict(self,newInput):
# Multiply the input with weights and find its sigmoid activation for all layers
layer0 = newInput
print("predict -> layer 0 : "+str(layer0))
layer1 = self.nonlin(np.dot(layer0, self.syn0))
print("predict -> layer 1 : "+str(layer1))
layer2 = self.nonlin(np.dot(layer1, self.syn1))
print("predicted output is : "+str(layer2))
if __name__ == '__main__':
ann=NeuralNetwork()
ann.train(100000)
ann.predict([1,1,0])
는 출력 :
Error:0.48402933124
Error:0.00603525276229
Error:0.00407346660344
Error:0.00325224335386
Error:0.00277628698655
Error:0.00245737222701
Error:0.00222508289674
Error:0.00204641406194
Error:0.00190360175536
Error:0.00178613765229
Output after training:
[[ 1.36893057e-04]
[ 5.80758383e-05]
[ 1.19857670e-03]
[ 1.85443483e-03]
[ 2.13949603e-03]
[ 2.19360982e-03]
[ 9.95769492e-01]]
predict -> layer 0 : [1, 1, 0]
predict -> layer 1 : [ 0.00998162 0.91479567 0.00690524 0.05241988]
predicted output is : [ 0.99515547]
교육 후에 얻은 가중치로 입력 내용을 올바르게 예측 했습니까? –
@ NanduKalidindi 그건 내가 이해할 필요가있는 한 가지 사항이지만, 가중치는 출력을 더 정확하게 추측하기 위해 자동으로 생성됩니다. 그래서 당신이 여기에서 묻고있는 것이 나에게 분명하지 않습니다. 내가 틀렸다면 나를 바로 잡아주세요. –
예. 기존 입력을 사용하여 입력 내용 이외의 값을 예측할 수있는 가중치를 계산할 수 있습니다. 가중치가 올바른지 검증 할 수있는 한 가지 방법은 주어진 모든 입력에 대해'ann.predict()'메소드를 실행하고 계산 된 값을 해당 출력으로 교차 점검하는 것입니다. –