基于Tensorflow的线性回归

  • 用Tensorflow求逆矩阵
  • 用Tensorflow实现矩阵分解
  • 用Tensorflow实现线性回归
  • 理解线性回归中的损失函数
  • 用Tensorflow实现戴明回归(Deming Regression)
  • 用Tensorflow实现Lasson回归和岭回归(Ridge Regression)
  • 用Tensorflow实现弹性网络回归(Elastic Net Regression)
  • 用Tensorflow实现逻辑回归

    文章目录

    • 3.1用Tensorflow求逆矩阵
    • 3.2用Tensorflow实现矩阵的分解
    • 3.3用Tensorflow实现线性回归算法
    • 3.4理解线性回归中的损失函数
    • 3.5用Tensorflow实现戴明回归
    • 3.6用Tensorflow实现lasso回归和岭回归算法
    • 3.7用Tensorflow实现弹性网络回归算法

3.1用Tensorflow求逆矩阵

线性回归算法能表示Ax=b。这里主要解决的是用矩阵x来求解系数。

  1. 导入库,初始化计算图,生成数据

    import tensorflow as tf
    import numpy as np
    import matplotlib.pyplot as plt
    
    sess = tf.Session()
    x_vals = np.linspace(0, 10, 100)
    y_vals = x_vals + np.random.normal(0, 1, 100)
    
  2. 创建后续求逆方法所需要的矩阵,即创建A,和b
    A 是x_vals_column和ones_column的合并

    x_vals_column = np.transpose(np.matrix(x_vals))
    ones_column = np.transpose(np.matrix(np.repeat(1, 100)))
    A = np.column_stack((x_vals_column, ones_column))
    b = np.transpose(np.matrix(y_vals))
    
  3. 将矩阵A,b转换成张量

    A_tensor = tf.constant(A)
    b_tensor = tf.constant(b)
    
  4. 调用求逆矩阵函数matrix_inverse()

    tA_A = tf.matmul(tf.transpose(A_tensor), A_tensor)
    tA_A_inv = tf.matrix_inverse(tA_A)
    product = tf.matmul(tA_A_inv, tf.transpose(A_tensor))
    solution = tf.matmul(product, b_tensor)
    solution_eval = sess.run(solution)
    
  5. 从解中抽离系数,斜率和y截距

    slope = solution_eval[0][0]
    y_intercept = solution_eval[1][0]
    print('slope: ' + str(slope))
    print('y_intercept: ' + str(y_intercept))
    
  6. 画出对应的图像

    best_fit = []
    for i in x_vals:
        best_fit.append(slope*i + y_intercept)
    plt.plot(x_vals, y_vals, 'o', label='Data')
    plt.plot(x_vals, best_fit, 'r-', label="Best fit line", linewidth=3)
    plt.legend(loc='upper left')
    plt.show()
    

    基于Tensorflow的线性回归_第1张图片

3.2用Tensorflow实现矩阵的分解

当矩阵较大时,使用上面的方法来进行求解逆矩阵是十分耗时的,可以使用cholesky函数来进行分解,然后再进行求解矩阵

  1. 导入第三方库,创建计算图,生成数据集,获取矩阵A,b

    ops.reset_default_graph()
    sess = tf.Session()
    x_vals = np.linspace(0, 10, 100)
    y_vals = x_vals + np.random.normal(0, 1, 100)
    x_vals_column = np.transpose(np.matrix(x_vals))
    ones_column = np.transpose(np.matrix(np.repeat(1, 100)))
    A = np.column_stack((x_vals_column, ones_column))
    b = np.transpose(np.matrix(y_vals))
    A_tensor = tf.constant(A)
    b_tensor = tf.constant(b)
    
  2. 找到方阵的cholesky矩阵分解

    tA_A = tf.matmul(tf.transpose(A_tensor), A_tensor)
    L = tf.cholesky(tA_A)
    tA_b = tf.matmul(tf.transpose(A_tensor), b)
    sol1 = tf.matrix_solve(L, tA_b)
    sol2 = tf.matrix_solve(tf.transpose(L), sol1)
    
  3. 抽离系数,绘制图像

    solution_eval = sess.run(sol2)
    slope = solution_eval[0][0]
    y_intercept = solution_eval[1][0]
    print("slope: "+ str(slope))
    print("y_intercept: " + str(y_intercept))
    
    best_fit = []
    for i in x_vals:
        best_fit.append(slope*i+y_intercept)
    plt.plot(x_vals, y_vals, 'o', label='Data')
    plt.plot(x_vals, best_fit, 'r-', label="Best fit line", linewidth=3)
    plt.legend(loc='upper left')
    plt.show()
    
    

    基于Tensorflow的线性回归_第2张图片

3.3用Tensorflow实现线性回归算法

这里使用iris数据集,将使用(x值代表花瓣的宽度,y值代表花瓣的长度)找到最优的直线

  1. 导入第三方库,创建计算图,加载数据集

    from sklearn import datasets
    from tensorflow.python.framework import ops
    
    ops.reset_default_graph()
    sess = tf.Session()
    iris = datasets.load_iris()
    x_vals = np.array([x[3] for x in iris.data])
    y_vals = np.array([y[0] 

你可能感兴趣的:(基于Tensorflow的线性回归)