tensorflow Elastic Net回归,拟合 iris 数据

引言:

之前写过一篇 tensorflow线性回归–拟合iris花瓣数据。今天的Elastic Net Regression和它差不多,只不过是损失函数变了一下。我对Elastic Net 回归的理解就是: 线性回归 + Losso回归 + Ridge回归。看起来比较吓人,其实也没什么,就是把最优化目标由一项变成了三项,而已。
网上找了一下,觉得这里介绍的几个回归很不错。下面就直接讲代码了!

代码

# 导入库
import tensorflow as tf
import numpy as np 
import matplotlib.pyplot as plt 
from sklearn import datasets

sess = tf.Session()
iris = datasets.load_iris()
# x[1], x[2], x[3] 分布是 iris 的花萼宽度、花瓣长度、花瓣宽度
# y[0] 是花萼长度,所以就是要拟合 y = a1*x1 + a2*x2 + a3*x3 + b
# 最终求出 A(3×1)和 b(1×1)
x_vals = np.array([[x[1], x[2], x[3]] for x in iris.data])
y_vals = np.array([y[0] for y in iris.data])

# 每次取 batch_size 个数据训练
batch_size = 50
learning_rate = 0.01
train_times = 50

x_data = tf.placeholder(tf.float32, shape=[None, 3])
y_data = tf.placeholder(tf.float32, shape=[None, 1])

A = tf.Variable(tf.zeros(shape=[3, 1]))
b = tf.Variable(tf.zeros(shape=[1, 1]))

y_output = tf.add(tf.matmul(x_data, A), b)
loss_least_squares = tf.reduce_mean(tf.square(y_output - y_data))
loss_losso = tf.reduce_mean(tf.abs(A))
loss_ridge = tf.reduce_mean(tf.square(A))

# 试了一下,损失函数用 tf.add() 或者直接 + 都可以。可以根据需要设定加权系数
# loss = tf.add(tf.add(loss_least_squares, loss_losso), loss_ridge)
loss = loss_least_squares + loss_losso + loss_ridge
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss)

init = tf.global_variables_initializer()
sess.run(init)

loss_vals = [] #记录 loss 值
for i in range(train_times):
	indexs = np.random.choice(len(x_vals), batch_size)
	x_rand = x_vals[indexs]
	y_rand = np.transpose([y_vals[indexs]])
	sess.run(optimizer, feed_dict={x_data:x_rand, y_data:y_rand})
	loss_vals.append(sess.run(loss, feed_dict={x_data:x_rand, y_data:y_rand}))

# 显示结果
[AA] = sess.run(A).T
[[bb]] = sess.run(b)
print('A = ', AA, '\tb = ', bb)

plt.plot(range(train_times), loss_vals, 'r-', 2)
plt.title('Loss per Generation')
plt.xlabel('Generation')
plt.ylabel('Loss')
plt.show()

结果

A = [0.932318 0.6139728 0.02031609] b = 0.43629923
tensorflow Elastic Net回归,拟合 iris 数据_第1张图片

参考文献

Nick McClure. TensorFlow机器学习攻略(影印版)[M]. 东南大学出版社(南京).2017.10

你可能感兴趣的:(tensorflow)