当前位置: 首页 > news >正文

16- TensorFlow实现线性回归和逻辑回归 (TensorFlow系列) (深度学习)

知识要点

线性回归要点:

  • 生成线性数据: x = np.linspace(0, 10, 20) + np.random.rand(20)
  • 画点图: plt.scatter(x, y)
  • TensorFlow定义变量: w = tf.Variable(np.random.randn() * 0.02)
  • tensor 转换为 numpy数组: b.numpy()
  • 定义优化器: optimizer = tf.optimizers.SGD()
  • 定义损失:  tf.reduce_mean(tf.square(y_pred - y_true))   # 求均值
  • 自动微分: tf.GradientTape()
  • 计算梯度: gradients = g.gradient(loss, [w, b])
  • 更新w, b: optimizer.apply_gradients(zip(gradients, [w, b]))

逻辑回归要点:

  • 查看安装文件: pip list
  • 聚类数据生成器: make_blobs
  • 生成聚类数据: data, target = make_blobs(centers = 3)
  • 转换为tensor 数据: x = tf.constant(data, dtype = tf.float32)
  • 定义tensor变量: B = tf.Variable(0., dtype = tf.float32)
  • 矩阵运算: tf.matmul(x, W)
  • 返回值长度为batch_size的一维Tensor: tf.sigmoid(linear)
  • 调整形状: y_pred = tf.reshape(y_pred, shape = [100])
  • tf.clip_by_value(A, min, max):输入一个张量A,把A中的每一个元素的值都压缩在min和max之间
  • 均值: tf.reduce_mean()
  • 定义优化器: optimizer = tf.optimizers.SGD()
  • 计算梯度: gradients = g.gradient(loss, [W, B])    # with tf.GradientTape() as g
  • 迭代更新W, B: optimizer.apply_gradients(zip(gradients, [W, B]))
  • 准确率计算: (y_ == y_true).mean()


1 使用tensorflow实现 线性回归

实现一个算法主要从以下三步入手:

  1. 找到这个算法的预测函数, 比如线性回归的预测函数形式为:y = wx + b,

  2. 找到这个算法的损失函数 , 比如线性回归算法的损失函数为最小二乘法

  3. 找到让损失函数求得最小值的时候的系数, 这时一般使用梯度下降法.

使用TensorFlow实现算法的基本套路:

  1. 使用TensorFlow中的变量将算法的预测函数, 损失函数定义出来.

  2. 使用梯度下降法优化器求损失函数最小时的系数

  3. 分批将样本数据投喂给优化器,找到最佳系数

1.1 导包

import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression

1.2 生成线性数据

# 生成线性数据
x = np.linspace(0, 10, 20) + np.random.rand(20)
y = np.linspace(0, 10, 20) + np.random.rand(20)
plt.scatter(x, y)

1.3 初始化斜率变量

# 把w,b 定义为变量
w = tf.Variable(np.random.randn() * 0.02)
b = tf.Variable(0.)
print(w.numpy(), b.numpy())  # -0.031422824  0.0

1.4 定义线性模型和损失函数

# 定义线性模型
def linear_regression(x):return w * x +b# 定义损失函数
def mean_square_loss(y_pred, y_true):return tf.reduce_mean(tf.square(y_pred - y_true))

1.5 定义优化过程

# 定义优化器
optimizer = tf.optimizers.SGD()
# 定义优化过程
def run_optimization():# 把需要求导的计算过程放入gradient pape中执行,会自动实现求导with tf.GradientTape() as g:pred = linear_regression(x)loss = mean_square_loss(pred, y)# 计算梯度gradients = g.gradient(loss, [w, b])# 更新w, boptimizer.apply_gradients(zip(gradients, [w, b]))

1.6 执行迭代训练过程

# 训练
for step in range(5000):run_optimization()   # 持续迭代w, b# z展示结果if step % 100 == 0:pred = linear_regression(x)loss = mean_square_loss(pred, y)print(f'step:{step}, loss:{loss}, w:{w.numpy()}, b: {b.numpy()}')

 1.7 线性拟合

linear = LinearRegression()  # 线性回归
linear.fit(x.reshape(-1, 1), y)plt.scatter(x, y)
x_test = np.linspace(0, 10, 20).reshape(-1, 1)
plt.plot(x_test, linear.coef_ * x_test + linear.intercept_, c='r')  # 画线
plt.plot(x_test, w.numpy() * x_test + b.numpy(), c='g', lw=10, alpha=0.5)  # 画线

2. 使用TensorFlow实现 逻辑回归

实现逻辑回归的套路和实现线性回归差不多, 只不过逻辑回归的目标函数和损失函数不一样而已.

使用tensorflow实现逻辑斯蒂回归

  1. 找到预测函数 : 1/(1 + e^{-z})
  2. 找到损失函数 : -(y_true * log(y_pred) + (1 - y_true)log(1 - y_pred))
  3. 梯度下降法求损失最小的时候的系数

2.1 导包

import  tensorflow as tf
from sklearn.datasets import make_blobs
import numpy as np
import matplotlib.pyplot as plt
  • 聚类数据生成器: make_blobs

2.2 描聚类数据点

data, target = make_blobs(centers = 3)
plt.scatter(data[:, 0] , data[:, 1], c = target)
x = data.copy()
y = target.copy()
print(x.shape, y.shape)  # (100, 2) (100,)

 2.3 数据转换为张量 (tensor)

x = tf.constant(data, dtype = tf.float32)
y = tf.constant(target, dtype = tf.float32)

2.4 定义预测函数

# 定义预测变量
W = tf.Variable(np.random.randn(2, 1) * 0.2, dtype = tf.float32)
B = tf.Variable(0., dtype = tf.float32)

2.5 定义目标函数

def sigmoid(x):linear = tf.matmul(x, W) + Breturn tf.nn.sigmoid(linear)

2.6 定义损失

# 定义损失
def cross_entropy_loss(y_true, y_pred):# y_pred 是概率,存在可能性是0, 需要进行截断y_pred = tf.reshape(y_pred, shape = [100])y_pred = tf.clip_by_value(y_pred, 1e-9, 1)return tf.reduce_mean(-(tf.multiply(y_true, tf.math.log(y_pred)) + tf.multiply((1 - y_pred),tf.math.log(1 - y_pred))))

2.7 定义优化器

# 定义优化器
optimizer = tf.optimizers.SGD()def run_optimization():with tf.GradientTape() as g:# 计算预测值pred = sigmoid(x)  # 结果为概率loss = cross_entropy_loss(y, pred)#计算梯度gradients = g.gradient(loss, [W, B])# 更新W, Boptimizer.apply_gradients(zip(gradients, [W, B]))

2.8 定义准确率

# 计算准确率
def accuracy(y_true, y_pred):# 需要把概率转换为类别# 概率大于0.5 可以判断为正例y_pred = tf.reshape(y_pred, shape = [100])y_ = y_pred.numpy() > 0.5y_true = y_true.numpy()return (y_ == y_true).mean()

2.9 开始训练

# 定义训练过程
for i in range(5000):run_optimization()if i % 100 == 0:pred = sigmoid(x)acc = accuracy(y, pred)loss = cross_entropy_loss(y, pred)print(f'训练次数:{i}, 准确率: {acc}, 损失: {loss}')

http://www.lryc.cn/news/21788.html

相关文章:

  • 无自动化测试系统设计方法论
  • 架构初探-学习笔记
  • 在成都想转行IT,选择什么专业比较好?
  • 【Spark分布式内存计算框架——Spark Streaming】4.入门案例(下)Streaming 工作原理
  • 2、算法先导---思维能力与工具
  • WordPress 函数:add_theme_support() 开启主题自定义功能(全面)
  • Winform控件开发(16)——Timer(史上最全)
  • 游戏高度可配置化:通用数据引擎(data-e)及其在模块化游戏开发中的应用构想图解
  • CountDownLatch与CyclicBarrier原理剖析
  • NLP中的对话机器人——预训练基准模型
  • C语言学习及复习笔记-【14】C文件读写
  • 模拟退火算法优化灰色
  • Pandas怎么添加数据列删除列
  • C++类和对象:构造函数和析构函数
  • 【Stata】从入门到精通.零基础小白必学的教程,一学就fei
  • 【RuoYi优化】调整JVM启动内存
  • [架构模型]MVC模型详细介绍,并应用到unity中
  • ?? JavaScript 双问号(空值合并运算符)
  • 作业2.25----通过操作Cortex-A7核,串口输入相应的命令,控制LED灯进行工作
  • 0101基础概念-图-数据结构和算法(Java)
  • Linux基础命令和工具使用详解
  • 一个好的python文件可以有几种用途?
  • HDFS优化
  • 行测-判断推理-图形推理-样式规律-黑白运算
  • java+springboot+vue高校学生医疗保险管理系统
  • [已解决] AHK 映射 ESC 延迟 500 ms 的严重问题
  • QML state详解
  • 一起Talk Android吧(第五百零六回:如何调整组件在约束布局中的角度)
  • 微信投票-课后程序(JAVA基础案例教程-黑马程序员编著-第七章-课后作业)
  • duboo+zookeeper分布式架构入门