训练神经网络用来预测的简单实例

研一学生,老师布置的一个tensorflow任务。搭建简单的全链接神经网络,再根据几个影响洪水流量的因素,来预测洪水流量。这个是实际数据
下面是代码。

import tensorflow as tf
import numpy as np



"""定义数据形式"""


x_data = np.asarray([[84,21.6,4,110,2,1410],
[29.5,34,2.27,110,3,1770],
[152.5,24.9,1.95,90.3,3,1360],
[40.1,22.7,2.67,89.7,3,1133],
[110.6,23.2,1.11,110,1,449],
[74,9.5,1.42,99.5,2,780],
[14,9.9,1.75,45.3,1,1100],
[339.2,38,14.14,110,2,5870],
[187.1,31.9,4.16,23,1,2310],
[18.7,19.8,0.52,88.9,1,391],
[39.1,25.6,1.63,28.2,3,72],
[110.3,58.9,4.2,70.4,1,604],
[89.1,26.4,3.71,966.8,2,280],
[40.3,18.5,0.9,110,3,649],
[93.6,16.3,1.36,96.6,2,394],
[93.6,24.8,3.9,87.2,3,368],
[23.2,7.1,0.45,78.6,2,93.5],
[35.5,6,0.22,98.7,1,199],
[58.4,19.8,1.15,47.6,1,200],
[61.8,18.3,1.37,61.5,1,68.6]])
x_data1 = np.asarray([[183.5,46.2,3.6,82.8,2,333],
[146.5,23.1,1.63,59.8,2,350],
[142.4,28.8,2.79,39.5,2,1580],
[49.8,21.3,1.04,71.3,3,106],
[75.5,31.9,3.6,53.8,1,42.6]])
X= np.asarray([[84,21.6,4,110,2,1410],
[29.5,34,2.27,110,3,1770],
[152.5,24.9,1.95,90.3,3,1360],
[40.1,22.7,2.67,89.7,3,1133],
[110.6,23.2,1.11,110,1,449],
[74,9.5,1.42,99.5,2,780],
[14,9.9,1.75,45.3,1,1100],
[339.2,38,14.14,110,2,5870],
[187.1,31.9,4.16,23,1,2310],
[18.7,19.8,0.52,88.9,1,391],
[39.1,25.6,1.63,28.2,3,72],
[110.3,58.9,4.2,70.4,1,604],
[89.1,26.4,3.71,966.8,2,280],
[40.3,18.5,0.9,110,3,649],
[93.6,16.3,1.36,96.6,2,394],
[93.6,24.8,3.9,87.2,3,368],
[23.2,7.1,0.45,78.6,2,93.5],
[35.5,6,0.22,98.7,1,199],
[58.4,19.8,1.15,47.6,1,200],
[61.8,18.3,1.37,61.5,1,68.6],
[183.5,46.2,3.6,82.8,2,333],
[146.5,23.1,1.63,59.8,2,350],
[142.4,28.8,2.79,39.5,2,1580],
[49.8,21.3,1.04,71.3,3,106],
[75.5,31.9,3.6,53.8,1,42.6]])
y_data = np.asarray([7.630,2.590,3.560,1.318,2.790,2.288,1.700,1.0700,6.040,0.470,0.251,6.222,3.030,1.140,2.099,2.480,0.111,0.226,0.233,0.610])  
y_data1 = np.asarray([8.434,2.540,2.490,0.392,1.230])
# 引入数据
Max= np.max(X,axis = 0)
Min= np.min(X,axis = 0)
# 求各列最大值、最小值


w1 = tf.Variable(tf.random_normal([6,3],stddev=1.0,seed=1))
b1 = tf.Variable(tf.random_normal([3],stddev=1.0,seed=1))
w2 = tf.Variable(tf.random_normal([3,1],stddev=1.0,seed=1))
b2 = tf.Variable(tf.random_normal([1],stddev=1.0,seed=1))
xs=tf.placeholder(tf.float32,[None,6])
ys=tf.placeholder(tf.float32,[None,1])
keep_prob=tf.placeholder(tf.float32)
# 定义变量
"""建立网络"""
#定义隐藏层,输入6个节点,输出3个节点
l1= tf.nn.sigmoid(tf.matmul(xs,w1) + b1)   
# dropout
l1_drop = tf.nn.dropout(l1,keep_prob=keep_prob)
# 定义输出层,输入3个节点,输出1个节点
prediction= tf.matmul(l1_drop,w2) + b2

"""反向反馈"""
#损失函数,算出的是每个例子的平方,要求和(reduction_indices=[1],按行求和),再求均值
loss=tf.reduce_mean(tf.reduce_sum(tf.square(ys-prediction),reduction_indices=[1]))

"""训练"""
#优化算法,minimize(loss)以0.2的学习率对loss进行减小
train_step= tf.train.GradientDescentOptimizer(0.05).minimize(loss)


with tf.Session() as sess:
    init=tf.global_variables_initializer()
    sess.run(init)
    for i in range(501):
         for(x,y) in zip(x_data,y_data):
                x= x.reshape([-1,6])
                y = y.reshape([-1,1])
                a = (x- Min) / (Max - Min)   # 归一化数据
                sess.run(train_step,feed_dict={xs:a,ys:y,keep_prob:0.6})   
                if i%500==0:
                    print("Flood type impact factor")
                    print(x)
                    print("flood type")
                    print(y)
                    print("flood type forecast")
                    a = (x- Min) / (Max - Min)  # 归一化数据
                    print(sess.run(prediction,feed_dict={xs:a,keep_prob:0.6}))     
                    sess.run(train_step,feed_dict={xs:a,ys:y,keep_prob:0.6})
                    print("the loss")
                    print(sess.run(loss,feed_dict={xs:a,ys:y,keep_prob:0.6}))
                  
    print('training is done!')   # 训练结束
    

    for(x1,y1) in zip(x_data1,y_data1):      #预测数据
            x1 = x1.reshape([-1,6])
            y1 = y1.reshape([-1,1])
            print("Flood type impact factor")
            print(x1)
            print("flood type")
            print(y1)
            print("flood type forecast")
            b = (x1- Min) / (Max - Min)   # 归一化数据
            print(sess.run(prediction,feed_dict={xs:b,keep_prob:1.0}))  # 输出预测值             
            print("the loss")
            print(sess.run(loss,feed_dict={xs:b,ys:y1,keep_prob:1.0}))
           

结果:

Flood type impact factor
[[  84.    21.6    4.   110.     2.  1410. ]]
flood type
[[7.63]]
flood type forecast
[[3.011912]]
the loss
36.307384
Flood type impact factor
[[  29.5    34.      2.27  110.      3.   1770.  ]]
flood type
[[2.59]]
flood type forecast
[[3.5220785]]
the loss
0.57745796
Flood type impact factor
[[ 152.5    24.9     1.95   90.3     3.   1360.  ]]
flood type
[[3.56]]
flood type forecast
[[0.09772214]]
the loss
2.138551
Flood type impact factor
[[  40.1    22.7     2.67   89.7     3.   1133.  ]]
flood type
[[1.318]]
flood type forecast
[[3.6203427]]
the loss
10.3016815
Flood type impact factor
[[110.6   23.2    1.11 110.     1.   449.  ]]
flood type
[[2.79]]
flood type forecast
[[3.8501773]]
the loss
2.1896467
Flood type impact factor
[[ 74.     9.5    1.42  99.5    2.   780.  ]]
flood type
[[2.288]]
flood type forecast
[[2.930126]]
the loss
0.73138344
Flood type impact factor
[[1.40e+01 9.90e+00 1.75e+00 4.53e+01 1.00e+00 1.10e+03]]
flood type
[[1.7]]
flood type forecast
[[0.3001214]]
the loss
1.770085
Flood type impact factor
[[3.392e+02 3.800e+01 1.414e+01 1.100e+02 2.000e+00 5.870e+03]]
flood type
[[1.07]]
flood type forecast
[[0.14820538]]
the loss
1.1144357
Flood type impact factor
[[1.871e+02 3.190e+01 4.160e+00 2.300e+01 1.000e+00 2.310e+03]]
flood type
[[6.04]]
flood type forecast
[[1.8404315]]
the loss
12.595966
Flood type impact factor
[[ 18.7   19.8    0.52  88.9    1.   391.  ]]
flood type
[[0.47]]
flood type forecast
[[3.7093942]]
the loss
9.051708
Flood type impact factor
[[39.1  25.6   1.63 28.2   3.   72.  ]]
flood type
[[0.251]]
flood type forecast
[[0.34850144]]
the loss
2.9426494
Flood type impact factor
[[110.3  58.9   4.2  70.4   1.  604. ]]
flood type
[[6.222]]
flood type forecast
[[0.8305911]]
the loss
13.582411
Flood type impact factor
[[ 89.1   26.4    3.71 966.8    2.   280.  ]]
flood type
[[3.03]]
flood type forecast
[[2.4930983]]
the loss
0.31448096
Flood type impact factor
[[ 40.3  18.5   0.9 110.    3.  649. ]]
flood type
[[1.14]]
flood type forecast
[[3.951956]]
the loss
0.107887365
Flood type impact factor
[[ 93.6   16.3    1.36  96.6    2.   394.  ]]
flood type
[[2.099]]
flood type forecast
[[3.4683976]]
the loss
0.5340204
Flood type impact factor
[[ 93.6  24.8   3.9  87.2   3.  368. ]]
flood type
[[2.48]]
flood type forecast
[[0.8310331]]
the loss
0.09079781
Flood type impact factor
[[23.2   7.1   0.45 78.6   2.   93.5 ]]
flood type
[[0.111]]
flood type forecast
[[1.5435278]]
the loss
0.00022639649
Flood type impact factor
[[ 35.5    6.     0.22  98.7    1.   199.  ]]
flood type
[[0.226]]
flood type forecast
[[0.3350114]]
the loss
0.25371858
Flood type impact factor
[[ 58.4   19.8    1.15  47.6    1.   200.  ]]
flood type
[[0.233]]
flood type forecast
[[1.0244242]]
the loss
0.010964109
Flood type impact factor
[[61.8  18.3   1.37 61.5   1.   68.6 ]]
flood type
[[0.61]]
flood type forecast
[[0.3420692]]
the loss
0.09547221
Flood type impact factor
[[  84.    21.6    4.   110.     2.  1410. ]]
flood type
[[7.63]]
flood type forecast
[[3.5187538]]
the loss
14.240079
Flood type impact factor
[[  29.5    34.      2.27  110.      3.   1770.  ]]
flood type
[[2.59]]
flood type forecast
[[3.8832018]]
the loss
1.4452486
Flood type impact factor
[[ 152.5    24.9     1.95   90.3     3.   1360.  ]]
flood type
[[3.56]]
flood type forecast
[[3.5620987]]
the loss
3.0804124e-05
Flood type impact factor
[[  40.1    22.7     2.67   89.7     3.   1133.  ]]
flood type
[[1.318]]
flood type forecast
[[3.6538432]]
the loss
2.3808177
Flood type impact factor
[[110.6   23.2    1.11 110.     1.   449.  ]]
flood type
[[2.79]]
flood type forecast
[[2.2412157]]
the loss
0.33445433
Flood type impact factor
[[ 74.     9.5    1.42  99.5    2.   780.  ]]
flood type
[[2.288]]
flood type forecast
[[3.3375213]]
the loss
0.0015552249
Flood type impact factor
[[1.40e+01 9.90e+00 1.75e+00 4.53e+01 1.00e+00 1.10e+03]]
flood type
[[1.7]]
flood type forecast
[[0.86183906]]
the loss
0.09792301
Flood type impact factor
[[3.392e+02 3.800e+01 1.414e+01 1.100e+02 2.000e+00 5.870e+03]]
flood type
[[1.07]]
flood type forecast
[[2.9461348]]
the loss
2.5178275
Flood type impact factor
[[1.871e+02 3.190e+01 4.160e+00 2.300e+01 1.000e+00 2.310e+03]]
flood type
[[6.04]]
flood type forecast
[[5.010922]]
the loss
8.682045
Flood type impact factor
[[ 18.7   19.8    0.52  88.9    1.   391.  ]]
flood type
[[0.47]]
flood type forecast
[[0.75438094]]
the loss
0.6282564
Flood type impact factor
[[39.1  25.6   1.63 28.2   3.   72.  ]]
flood type
[[0.251]]
flood type forecast
[[-0.9405174]]
the loss
0.5903699
Flood type impact factor
[[110.3  58.9   4.2  70.4   1.  604. ]]
flood type
[[6.222]]
flood type forecast
[[6.7587657]]
the loss
9.357428
Flood type impact factor
[[ 89.1   26.4    3.71 966.8    2.   280.  ]]
flood type
[[3.03]]
flood type forecast
[[3.2960494]]
the loss
0.029496038
Flood type impact factor
[[ 40.3  18.5   0.9 110.    3.  649. ]]
flood type
[[1.14]]
flood type forecast
[[-0.22610092]]
the loss
0.01735407
Flood type impact factor
[[ 93.6   16.3    1.36  96.6    2.   394.  ]]
flood type
[[2.099]]
flood type forecast
[[3.1050422]]
the loss
0.13359529
Flood type impact factor
[[ 93.6  24.8   3.9  87.2   3.  368. ]]
flood type
[[2.48]]
flood type forecast
[[2.6392806]]
the loss
0.058958903
Flood type impact factor
[[23.2   7.1   0.45 78.6   2.   93.5 ]]
flood type
[[0.111]]
flood type forecast
[[-0.76252294]]
the loss
0.67580014
Flood type impact factor
[[ 35.5    6.     0.22  98.7    1.   199.  ]]
flood type
[[0.226]]
flood type forecast
[[1.1140964]]
the loss
1.1160929
Flood type impact factor
[[ 58.4   19.8    1.15  47.6    1.   200.  ]]
flood type
[[0.233]]
flood type forecast
[[1.5106726]]
the loss
6.885285
Flood type impact factor
[[61.8  18.3   1.37 61.5   1.   68.6 ]]
flood type
[[0.61]]
flood type forecast
[[1.7346666]]
the loss
1.4590164
training is done!
Flood type impact factor
[[183.5  46.2   3.6  82.8   2.  333. ]]
flood type
[[8.434]]
flood type forecast
[[2.9582427]]
the loss
29.983921
Flood type impact factor
[[146.5   23.1    1.63  59.8    2.   350.  ]]
flood type
[[2.54]]
flood type forecast
[[2.3579404]]
the loss
0.03314567
Flood type impact factor
[[ 142.4    28.8     2.79   39.5     2.   1580.  ]]
flood type
[[2.49]]
flood type forecast
[[2.7192621]]
the loss
0.052561115
Flood type impact factor
[[ 49.8   21.3    1.04  71.3    3.   106.  ]]
flood type
[[0.392]]
flood type forecast
[[0.5346551]]
the loss
0.020350479
Flood type impact factor
[[75.5 31.9  3.6 53.8  1.  42.6]]
flood type
[[1.23]]
flood type forecast
[[2.4229717]]
the loss
1.4231815
In [ ]:
#显示第一次训练和最后一次训练的各项数据以及最后预测时的数据
​

我在完成这个小任务的时候遇到了几个问题。一个是placeholder变量与输入的数据shape不符,后来用reshape函数解决这个问题。一个是训练数据拟合很好,测试数据结果不好,也就是过拟合,我用了dropout。另一个是预测时,输入不同数据,输出相同结果,所以我对输入数据进行了归一化处理。输入数据的格式一定要弄对,没有np.array就无法运行。

评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值