计算智能 作业二 题目:自选非线性分类或曲线拟合问题,用BP网络训练、学习。 自选题目: 下面列表中的数据是某地区20年公路运量数据,其中属性“人口数量”、“机动车数量”和“公路面积”作为输入,属性“公路客运量”和“公路货运量”作为输出。请用神经网络拟合此多输入多输出曲线。
2、结果对比
纯python代码BP算法的结果:
100000步训练后,结果为:
python+Tensorflow Adam优化算法的结果:
100000步训练后,结果为: 优化的更快!可能过拟合
Tensorflow计算图(神经网络模型=拟合函数数学模型)
cmd命令:tensorboard --logdir=C:\temp\log_simple_stats
训练迭代过程中,损失函数值:
注意训练的数据需要正则化
samplein = np.mat([population,vehicle,roadarea]) #3*20
sampleinminmax = np.array([samplein.min(axis=1).T.tolist()[0],samplein.max(axis=1).T.tolist()[0]]).transpose()#3*2
sampleout = np.mat([passengertraffic,freighttraffic])#2*20
sampleoutminmax = np.array([sampleout.min(axis=1).T.tolist()[0],sampleout.max(axis=1).T.tolist()[0]]).transpose()#2*2
sampleinnorm = (2*(np.array(samplein.T)-sampleinminmax.transpose()[0])/(sampleinminmax.transpose()[1]-sampleinminmax.transpose()[0])-1).transpose()
sampleoutnorm = (2*(np.array(sampleout.T).astype(float)-sampleoutminmax.transpose()[0])/(sampleoutminmax.transpose()[1]-sampleoutminmax.transpose()[0])-1).transpose()
注意正则化时,公式分母不能为0 上面公式中的2* 和-1可以没有
3、python+Tensorflow代码
import os
os.environ["TF_CPP_MIN_LOG_LEVEL"]='2'
import numpy as np
import tensorflow as tf
import pandas as pd
import matplotlib.pyplot as plt
NUM_INPUT=3
LEARNING_RATE=0.035
NUM_OUTPUT=2
BATCH_SIZE=20
TRAINING_EPOCHS=100000
H_L_i_MAX=[1.,1.,1.]
file_path = r'C:\Users\li\AppData\Local\ttt\data.xls'
dataFrame = pd.read_excel(file_path)
data=dataFrame.values[:5000,(0,2,1)]
population=[20.55,22.44,25.37,27.13,29.45,30.10,30.96,34.06,36.42,38.09,39.13,39.99,41.93,44.59,47.30,52.89,55.73,56.76,59.17,60.63]
vehicle=[0.6,0.75,0.85,0.9,1.05,1.35,1.45,1.6,1.7,1.85,2.15,2.2,2.25,2.35,2.5,2.6,2.7,2.85,2.95,3.1]
roadarea=[0.09,0.11,0.11,0.14,0.20,0.23,0.23,0.32,0.32,0.34,0.36,0.36,0.38,0.49,0.56,0.59,0.59,0.67,0.69,0.79]
passengertraffic=[5126,6217,7730,9145,10460,11387,12353,15750,18304,19836,21024,19490,20433,22598,25107,33442,36836,40548,42927,43462]
freighttraffic=[1237,1379,1385,1399,1663,1714,1834,4322,8132,8936,11099,11203,10524,11115,13320,16762,18673,20724,20803,21804]
samplein = np.mat([population,vehicle,roadarea])
sampleinminmax = np.array([samplein.min(axis=1).T.tolist()[0],samplein.max(axis=1).T.tolist()[0]]).transpose()
sampleout = np.mat([passengertraffic,freighttraffic])
sampleoutminmax = np.array([sampleout.min(axis=1).T.tolist()[0],sampleout.max(axis=1).T.tolist()[0]]).transpose()
sampleinnorm = (2*(np.array(samplein.T)-sampleinminmax.transpose()[0])/(sampleinminmax.transpose()[1]-sampleinminmax.transpose()[0])-1).transpose()
sampleoutnorm = (2*(np.array(sampleout.T).astype(float)-sampleoutminmax.transpose()[0])/(sampleoutminmax.transpose()[1]-sampleoutminmax.transpose()[0])-1).transpose()
data=data/H_L_i_MAX
trainData_in=sampleinnorm.transpose()
trainData_out=sampleoutnorm.transpose()
x_input=tf.placeholder(tf.float32, [None,NUM_INPUT], name='x_input')
y_desired=tf.placeholder(tf.float32,[None,NUM_OUTPUT],name='y_desired')
w1=tf.Variable(tf.truncated_normal([NUM_INPUT,8],stddev=0.1),name='w1')
b1=tf.Variable(tf.zeros([8]),name='b1')
z1=tf.matmul(x_input,w1)+b1
y1=tf.nn.sigmoid(z1)
w=tf.Variable(tf.truncated_normal([8,NUM_OUTPUT],stddev=0.1),name='w')
b=tf.Variable(tf.zeros([NUM_OUTPUT]),name='b')
y_output=tf.add(tf.matmul(y1,w),b,name='y_output')
loss=tf.losses.mean_squared_error(labels=y_desired,predictions=y_output)
opt=tf.train.AdamOptimizer(learning_rate=LEARNING_RATE).minimize(loss)
for value in [loss]:
tf.summary.scalar(value.op.name,value)
summary_op=tf.summary.merge_all()
init=tf.global_variables_initializer()
with tf.Session() as sess:
summary_writer=tf.summary.FileWriter(r'C:\temp\log_simple_stats',sess.graph)
sess.run(init)
num_batches=int(len(trainData_in)/BATCH_SIZE)
for epoch in range(TRAINING_EPOCHS):
for i in range(num_batches):
batch_x=trainData_in[i*BATCH_SIZE:(i+1)*BATCH_SIZE]
batch_y=trainData_out[i*BATCH_SIZE:(i+1)*BATCH_SIZE]
if NUM_OUTPUT==1:
batch_y.resize((BATCH_SIZE,NUM_OUTPUT))
_,summary=sess.run([opt,summary_op], feed_dict={x_input: batch_x, y_desired: batch_y})
summary_writer.add_summary(summary,global_step=epoch*num_batches+i)
if i %10==0:
los=sess.run(loss,feed_dict={x_input:batch_x,y_desired:batch_y})
print('epoch:%4d,'%epoch,'%4d'%i)
print("Loss ",los)
print("__________________")
print("Finished!")
saver=tf.train.Saver()
save_path=saver.save(sess,'../data')
print('Model saved to %s' % save_path)
summary_writer.close()
networkout = sess.run(y_output,feed_dict={x_input:sampleinnorm.transpose(),y_desired:sampleoutnorm.transpose()}).transpose()
diff = sampleoutminmax[:,1]-sampleoutminmax[:,0]
networkout2 = (networkout+1)/2
networkout2[0] = networkout2[0]*diff[0]+sampleoutminmax[0][0]
networkout2[1] = networkout2[1]*diff[1]+sampleoutminmax[1][0]
sampleout = np.array(sampleout)
plt.figure(2)
plt.subplot(2,1,1)
plt.plot(sampleout[0],color="blue", linewidth=1.5, linestyle="-", label="real curve of passengertraffic")
plt.plot(networkout2[0],color="red", linewidth=1.5, linestyle="--", label="fitting curve")
plt.legend(loc='upper left')
plt.subplot(2,1,2)
plt.plot(sampleout[1],color="blue", linewidth=1.5, linestyle="-", label="real curve of freighttraffic")
plt.plot(networkout2[1],color="red", linewidth=1.5, linestyle="--", label="fitting curve")
plt.legend(loc='upper left')
plt.show()
|