• tensorflow神经网络多维曲线拟合


    1、参考:神经网络(二) 曲线拟合纯python代码参考(BP算法)

    计算智能 作业二
    题目:自选非线性分类或曲线拟合问题,用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()
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    注意正则化时,公式分母不能为0
    上面公式中的2* 和-1可以没有

    3、python+Tensorflow代码

    # 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()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
  • 相关阅读:
    两条命令解决移动硬盘无法弹出的问题
    react native引用原生组件时无法显示的问题处理
    Monoxide relay机制和连弩挖矿
    程序员保密协议
    计算机毕业设计Java靓车汽车销售网站(源码+mysql数据库+系统+lw文档)
    nn.PairwiseDistance 和 torch.cdist 和 MSELoss 计算距离
    python flaskweb与sqlite连接
    合宙Air724UG LuatOS-Air LVGL API控件-滑动条 (Slider)
    JavaSE——数组
    ZooKeeper-API基础
  • 原文地址:https://blog.csdn.net/lijil168/article/details/125468791