2017-05-04 52 views
-1

有些人可以制作一个简单的神经网络,将输入变量的总和作为输出。 如果输入变量是X1,X2,X3,则输出为Y = X1 + X2 + X3。简单的神经网络,将输入变量的总和作为Python中的输出?

简单的Python程序,使用矩阵乘法会有帮助。

谢谢。

这里是我尝试应用它的代码只是“iamtrask”代码的修改版本,但它并没有给我正确的答案,并且在增加测试用例时趋于饱和[1.](set_size )。 “

import numpy as np 

outputs=[] 

#initializinf hyper parameters 
set_size=20 
iterations=10000 
input_variables=3 

# sigmoid function 
def nonlin(x, deriv=False): 
    if (deriv == True): 
     return 1 * (1 - x) 
    return 1/(1 + np.exp(-x)) 

#inverse of sigmoid, Logit function 
def logit(x): 
    return np.log(x/(1-x)) 

#initializing inputs with random values 
inputs = 2 * np.random.random((set_size, input_variables)) - 1 
X=np.array(inputs) 

#Getting desired output using mathematical operations 
for h in range(set_size): 
    outputs.append(nonlin((X[h][0]) + (X[h][1]) + (X[h][2]))) 



# output dataset 
y = np.array([outputs]).T # converting list into array and taking transpose 

# seed random numbers to make calculation 
# deterministic (just a good practice) 
np.random.seed(1) 

# initialize weights randomly with mean 0 
syn0 = 2 * np.random.random((input_variables, set_size)) - 1 
syn1 = 2 * np.random.random((set_size, 1)) - 1 
print(y) 

for iter in range(0,10000): 
    # forward propagation 
    l0 = X 
    l1 = nonlin(np.dot(l0, syn0)) 
    l2 = nonlin(np.dot(l1, syn1)) 
    # how much did we miss? 
    #l1_error = y - l1 
    l2_error = y - l2 
    #print(l1_error) 

    l2_delta = l2_error * nonlin(l2, deriv=True) 

    l1_error = l2_delta.dot(syn1.T) 

    l1_delta = l1_error * nonlin(l1, deriv=True) 
    # multiply how much we missed by the 
    # slope of the sigmoid at the values in l1 
    #l1_delta = l1_error * nonlin(l1, True) 

    # update weights 
    syn1 += l1.T.dot(l2_delta) 
    syn0 += l0.T.dot(l1_delta) 
print("Output After Training:") 
#out=logit(l2) 
print(l2) 

#testing the trained network with new values 
X1=input("Enter the new inputs:") 
mynums = [float(i) for i in X1.split()] 
#mynums = map(float, X1.split()) 
print(mynums) 
l0 = mynums 
l1 = nonlin(np.dot(l0, syn0)) 
l2 = nonlin(np.dot(l1, syn1)) 
print(l2) 
+2

你有什么到现在做了什么? –

+1

Stack Overflow不是我们为您编写代码的网站。当您遇到困难时,我们可以帮助您调试自己的代码。你可以这样做! – Dbz

+0

好吧,所以我添加了我正在尝试修改和运行的代码。 –

回答

0

”用于描述反向传播的内部工作原理的裸骨骼神经网络实现。 11行代码!

X = np.array([ [0,0,1],[0,1,1],[1,0,1],[1,1,1] ]) 
y = np.array([[0,1,1,0]]).T 
syn0 = 2*np.random.random((3,4)) - 1 
syn1 = 2*np.random.random((4,1)) - 1 
for j in xrange(60000): 
    l1 = 1/(1+np.exp(-(np.dot(X,syn0)))) 
    l2 = 1/(1+np.exp(-(np.dot(l1,syn1)))) 
    l2_delta = (y - l2)*(l2*(1-l2)) 
    l1_delta = l2_delta.dot(syn1.T) * (l1 * (1-l1)) 
    syn1 += l1.T.dot(l2_delta) 
    syn0 += X.T.dot(l1_delta) 

http://iamtrask.github.io/2015/07/12/basic-python-network/