Tensorflow多元一次回归示例
程序员文章站
2022-05-28 12:10:41
...
Tensorflow多元一次回归示例
下面的示例对:y = w1.x1 + w2.x2 + … +wn.xk + b 多元一次方程进行线性回归
#源码
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
def normalize(X):
"""
归一化函数
:param X:
:return: x
"""
mean = np.mean(X)
std = np.std(X)
X = (X - mean)/std
return X
def appendBiasReshape(features_data, k, labels):
"""
对训练特征进行整形:np.reshape(np.c_[np.ones(m),features],[m,k+1]) 来源于对将多元一次方程式的表达:
w1.x1 + w2.x2 + ... +wn.xk + b = y => (1,x1,x2,x3,...,xk)(b,w1,w2,w3,...,wk)' = y
=> (x0,x1,x2,x3,...,xk)((w0,w1,w2,w3,...,wk)' = y 其中:x0=1,w0=b
:param features_data: 训练数据矩阵
:param k: 特征数(x向量维度)
:param labels: 标签向量(y向量)
:return:
"""
m = features_data.shape[0]
print("m=", m)
n = labels.shape[0]
print("n=",n)
x = np.reshape(np.c_[np.ones(m),features_data],[m,k+1])
y = np.reshape(labels,[n,1])
return x,y
#1. 特征训练数据
X_train = np.matrix([[1,3,5,2],[3,3,1,2],[1,3,5,2],[3,3,1,2],[3,3,1,2],[3,3,1,2]])
Y_train = np.array([1,2,2,4,3,6])
#特征数量
k = len(X_train[0])
#2. 对训练数据整形,并且X_train,Y_train 必须转换为numpy矩阵或者数组
X_train,Y_train = appendBiasReshape(X_train, Y_train)
m = len(X_train)
n = k+1
#3. X,Y 训练输入站位符
X = tf.placeholder(tf.float32, name="X", shape=[m, n])
Y = tf.placeholder(tf.float32, name="Y")
#4. 定义权重向量
w = tf.Variable(tf.random_normal([n, 1]))
#5. 定义目标函数
Y_hat = tf.matmul(X, w)
#6. 定义损失函数
loss = tf.reduce_mean(tf.square(Y - Y_hat), name="loss")
#7, 选择梯度下降法优化器
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.01, name="optimizer").minimize(loss)
#8. 定义变量初始化器
init_op = tf.global_variables_initializer()
total = []
with tf.Session() as sess:
""" 执行变量初始化 """
sess.run(init_op)
""" 保存会话执行图 """
writer = tf.summary.FileWriter("graphs", sess.graph)
""" 对训练数据执行100次训练 """
for i in range(100):
""" 执行训练:输出每一次的损失函数值,通过feed_dict指定训练参数值"""
_,l = sess.run([optimizer, loss], feed_dict={X: X_train, Y: Y_train})
total.append(l)
print('epoch{0}:loss ={1}'.format(i,l))
writer.close()
""" 从sess中获取训练后的权重向量 """
w_value = sess.run(w)
print('w_value ={0}, b_value:{1}'.format(w_value,X_train[:, 0]))
plt.plot(total)
plt.show()
TensorBoard 图示:
在控制台输入命令: tensorboard --logdir graphs
上一篇: Python多元线性回归