计算智能 作业二
题目:自选非线性分类或曲线拟合问题,用BP网络训练、学习。
自选题目:
下面列表中的数据是某地区20年公路运量数据,其中属性“人口数量”、“机动车数量”和“公路面积”作为输入,属性“公路客运量”和“公路货运量”作为输出。请用神经网络拟合此多输入多输出曲线。
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可以没有
# coding=utf-8
import os
os.environ["TF_CPP_MIN_LOG_LEVEL"]='2' # 只显示 warning 和 Error
###data (20,5),(20,5),(20,5):
import numpy as np
import tensorflow as tf
import pandas as pd
import matplotlib.pyplot as plt
#define constants
#输入3个特征,P人口数量、V车量、A路面积:
NUM_INPUT=3
#learning rate for adam
LEARNING_RATE=0.035
#输出一个特征,NP乘客量、NK货运量:
NUM_OUTPUT=2
#size of batch
BATCH_SIZE=20
TRAINING_EPOCHS=100000#
#从路谱中截取一系列TIME_STEPS=28个值作为训练的样本::
#X (:,2)
#y (:,1)
###:文件数据处理:
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)] #二维数组
#Original Data
#Input: the pupulation, number of vehicle, roadarea from 1990-2009
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]
#Output
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]
# normalize the original data and add the noise
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()
#数据归一化:
data=data/H_L_i_MAX
#trainData_in (:,3)
#trainData_out (:,2)
trainData_in=sampleinnorm.transpose()
trainData_out=sampleoutnorm.transpose()
#构建dnn网络,tensorflow计算图,1个隐藏层1个输出层,节点数分别为8,1:
x_input=tf.placeholder(tf.float32, [None,NUM_INPUT], name='x_input')
y_desired=tf.placeholder(tf.float32,[None,NUM_OUTPUT],name='y_desired')
#设置权重及偏置,注意不能全部设为0,否则有可能训练无法启动:
#1隐藏层:
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)
#2隐藏层:
#w2=tf.Variable(tf.truncated_normal([50,20],stddev=0.1),name='w2')
#b2=tf.Variable(tf.zeros([20]),name='b2')
#z2=tf.matmul(y1,w2)+b2
#y2=tf.nn.sigmoid(z2)
#输出层:
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_function:
loss=tf.losses.mean_squared_error(labels=y_desired,predictions=y_output)
#optimization
opt=tf.train.AdamOptimizer(learning_rate=LEARNING_RATE).minimize(loss)
#以下汇总一些参数用于TensorBoard:
for value in [loss]:
tf.summary.scalar(value.op.name,value) #汇总的标签及值
summary_op=tf.summary.merge_all() #汇总合并
#initialize variables:
init=tf.global_variables_initializer()
with tf.Session() as sess:
# 生成一个写日志的writer,并将当前的tensorflow计算图写入日志。
# tensorflow提供了多种写日志文件的API
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()
#For there was a normalization, cacalute the original output use the min and max value
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)
#show the results
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()