神经网络拟合二次函数

调用Nndl实现的神经网络code,用ANN拟合二次方程。

ref: https://github.com/mnielsen/neural-networks-and-deep-learning

准备训练数据

#np.shape(x)
x=np.array(xrange(0,100))/100.0
f=x*x
# train=np.array([[xi],[fi]] for xi,fi in zip(x,f))

train=[(np.array([a[0]]).reshape(1,1),np.array([a[1]]).reshape(1,1)) for a in zip(x,f)]


x1=np.array(xrange(10,15))
f1=x1*x1
# train=np.array([[xi],[fi]] for xi,fi in zip(x,f))

test=[(np.array([a[0]]).reshape(1,1),np.array([a[1]]).reshape(1,1)) for a in zip(x1,f1)]


训练网络

net2 = network2.Network([1, 5, 1], cost=network2.QuadraticCost)
net2.large_weight_initializer()
err=net2.SGD(train, 30, 5, 1.5, evaluation_data=train, monitor_training_cost=True )
plt.plot(err[2])```

网络精度

比较拟合函数

a=[]
f=[]
for xi in np.array(xrange(0,100))/100.0: 
    a.append(net2.feedforward(np.array([xi]).reshape((1,1)))[0])
    f.append(xi*xi)
plt.plot(a)
plt.plot(f)

posted @ 2017-10-20 13:36  wlu  阅读(2646)  评论(0编辑  收藏  举报