红色是实际数据,绿色是预测的点
误差图
# -*- coding: utf-8 -*-
import neurolab as nl
import numpy as np
import matplotlib.pyplot as plt# 生成数据
min_value = -12
max_value = 12
num_datapoints = 90
x = np.linspace(min_value, max_value, num_datapoints)
y = 2 * np.square(x) + 7
y /= np.linalg.norm(y)
# 数组变形
data = x.reshape(num_datapoints, 1)
labels = y.reshape(num_datapoints, 1)# 画出原始数据图
plt.figure()
plt.scatter(data, labels)
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Input data')# 定义深度神经网络 每个隐藏层有10个神经元
multilayer_net = nl.net.newff([[min_value, max_value]], [10, 10, 1])# 设置训练算法为梯度下降
multilayer_net.trainf = nl.train.train_gd# 训练网络
error = multilayer_net.train(data, labels, epochs=800, show=100, goal=0.01)# 预测结果
predicted_output = multilayer_net.sim(data)# 画出误差
plt.figure()
plt.plot(error)
plt.xlabel('Number of epochs')
plt.ylabel('Error')
plt.title('Training error progress')# 画出预测结果
x2 = np.linspace(min_value, max_value, num_datapoints * 2)
y2 = multilayer_net.sim(x2.reshape(x2.size,1)).reshape(x2.size)
y3 = predicted_output.reshape(num_datapoints)plt.figure()
plt.plot(x2, y2, '-', x, y, '.', x, y3, 'p')
plt.title('Ground truth vs predicted output')plt.show()
标签:plt,multilayer,datapoints,value,神经网络,num,240727,深度,net
From: https://blog.51cto.com/u_15862653/11888519