import tensorflow.compat.v1 as tf
import pandas as pd
import numpy as np
import time
import matplotlib.pyplot as plt
from sklearn.utils import shuffle
tf.disable_v2_behavior()
### 程序执行开始时间
begintime=time.time()
### 读取数据文件
datafile=pd.read_csv("data/boston.csv",header=0) # header,第几行为列名称
# print(datafile.describe())
# print(datafile)
### 转换为numpy数组
datafile=datafile.values
datafile=np.array(datafile)
# print(datafile)
### 特征数据归一化处理
for i in range(13):
datafile[:,i]=(datafile[:,i]-datafile[:,i].min())/(datafile[:,i].max()-datafile[:,i].min())
print(datafile)
### 取特征数据
x_data=datafile[:,:13]
### 取标签数据
y_data=datafile[:,13]
# print(x_data,x_data.shape)
# print(y_data,y_data.shape)
### 定义特征数据和标签数据的占位符
x=tf.placeholder(tf.float32,[None,13],name="X")
y=tf.placeholder(tf.float32,[None,1],name="Y")
### 定义命名空间,将下面的语句打个包,查看计算图时,将语句包并为一个子图
with tf.name_scope("Model"):
### 初始化W
### [[w1],
### [w2],
### ....,
### [w13]]
w=tf.Variable(tf.random_normal([13,1],stddev=0.01),name="W")
### 初始化b
b=tf.Variable(1.0,name="b")
### x与w进行矩阵叉乘,结果矩阵[1,1]
### [x1,x2,....,x13]*[[w1], =[x1*w1+x2*w2+....+x13*w13]
### [w2],
### ....,
### [w13]]
def model(x,w,b):
return tf.matmul(x,w)+b
### [x1 x2 xn]*[w1 +b=[x1*w1+x2*w2+xn*wn]+b
### w2
### wn]
### 预测计算操作,前向计算节点
pred=model(x,w,b)
### 迭代轮次
train_epochs=100
### 学习率
learning_rate=0.01
### 定义损失函数
with tf.name_scope("LossFunction"):
loss_function=tf.reduce_mean(tf.pow(y-pred,2)) # 均方误差
### 创建优化器
optimizer=tf.train.GradientDescentOptimizer(learning_rate).minimize(loss_function)
### 常用优化器
### tf.train.GradientDescentOptimizer
### tf.train.AdadeltaOptimizer
### tf.train.AdagradOptimizer
### tf.train.AdagradDAOptimizer
### tf.train.MomentumOptimizer
### tf.train.AdamOptimizer
### tf.train.FtrlOptimizer
### tf.train.ProximalGradientDescentOptimizer
### tf.train.ProximalAdagradOptimizer
### tf.train.RMSPropOptimizer
### 申明会话
sess=tf.Session()
init=tf.global_variables_initializer()
### 启动会话
sess.run(init)
### 迭代训练
for epoch in range(train_epochs):
loss_sum=0.0
for xs,ys in zip(x_data,y_data):
xs=xs.reshape(1,13)
ys=ys.reshape(1,1)
_,loss=sess.run([optimizer,loss_function],feed_dict={x:xs,y:ys})
loss_sum=loss_sum+loss
xvalues,yvalues=shuffle(x_data,y_data)
b0temp=b.eval(session=sess)
w0temp=w.eval(session=sess)
loss_average=loss_sum/len(y_data)
print("epoch=",epoch+1,"loss=",loss_average,"b=",b0temp,"w=",w0temp)
print("程序运行耗时:{:.10f}秒".format(time.time()-begintime))
100轮训练:
epoch= 100 loss= 18.785152495525058 b= 15.169162 w= [[ -9.793129 ]
[ 1.8619859 ]
[ -0.77917975]
[ -0.02257809]
[ -4.7770057 ]
[ 22.329779 ]
[ -1.1400712 ]
[ -9.092865 ]
[ 6.7502885 ]
[ -6.813758 ]
[ -3.8187435 ]
[ 2.829926 ]
[-19.119265 ]]
程序运行耗时:94.3756029606秒
1000轮训练:
epoch= 1000 loss= 18.456547232155945 b= 25.442104 w= [[-10.0094385 ]
[ 1.7411479 ]
[ -0.81524694]
[ 0.07088397]
[ -8.433186 ]
[ 20.897005 ]
[ -1.0631353 ]
[-11.03569 ]
[ 7.3070827 ]
[ -7.8692083 ]
[ -5.0638556 ]
[ 2.339065 ]
[-19.25514 ]]
程序运行耗时:1542.9099886417秒