TensorFlow实现逻辑回归

目录

前言

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

TensorFlow实现逻辑回归

python 复制代码
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import make_blobs

# 生成二分类数据集
data, target = make_blobs(centers=2)
plt.scatter(data[:, 0], data[:, 1], c=target)
plt.show()

# 初始化参数 (修复维度)
w = tf.Variable(initial_value=np.random.randn(2, 1) * 0.01, dtype=tf.float32)  # 二分类只需 1 个输出单元
b = tf.Variable(initial_value=0.0, dtype=tf.float32)

# 输入数据(无需对 target 做 one-hot 编码)
x = tf.constant(data, dtype=tf.float32)
y = tf.constant(target.reshape(-1, 1), dtype=tf.float32)  # 目标形状为 (100, 1)

# 定义模型输出(logits)
def logits(x):
    return tf.matmul(x, w) + b  # 输出形状 (100, 1)

# 定义损失函数(二元交叉熵)
def loss(y_true, y_pred):
    y_pred = tf.clip_by_value(y_pred, 1e-9, 1.0)
    return -tf.reduce_mean(y_true * tf.math.log(y_pred) + (1 - y_true) * tf.math.log(1 - y_pred))

# 定义优化器
optimizer = tf.optimizers.SGD(0.001)

# 定义训练步骤
def run_optimization():
    with tf.GradientTape() as g:
        pred = tf.sigmoid(logits(x))  # 使用 sigmoid 激活函数
        cost = loss(y, pred)
        
    gradients = g.gradient(cost, [w, b])
    optimizer.apply_gradients(zip(gradients, [w, b]))

# 定义准确率计算
def accuracy(y_true, y_pred):
    y_pred_class = tf.cast(y_pred > 0.5, dtype=tf.float32)  # 概率转类别
    return tf.reduce_mean(tf.cast(tf.equal(y_true, y_pred_class), dtype=tf.float32))

# 开始训练
for i in range(1, 10001):
    run_optimization()
    
    if i % 100 == 0:
        y_pred_prob = tf.sigmoid(logits(x))
        acc = accuracy(y, y_pred_prob)
        loss_ = loss(y, y_pred_prob)
        print(f'Step {i}, 准确率: {acc.numpy()}, 损失: {loss_.numpy()}')

结果如下:

powershell 复制代码
Step 100, 准确率: 1.0, 损失: 0.4105553925037384
Step 200, 准确率: 1.0, 损失: 0.28594353795051575
Step 300, 准确率: 1.0, 损失: 0.21672113239765167
Step 400, 准确率: 1.0, 损失: 0.17359928786754608
Step 500, 准确率: 1.0, 损失: 0.1445026844739914
Step 600, 准确率: 1.0, 损失: 0.12367361038923264
Step 700, 准确率: 1.0, 损失: 0.10807851701974869
Step 800, 准确率: 1.0, 损失: 0.09598751366138458
Step 900, 准确率: 1.0, 损失: 0.08634963631629944
Step 1000, 准确率: 1.0, 损失: 0.07849213480949402
Step 1100, 准确率: 1.0, 损失: 0.07196593284606934
Step 1200, 准确率: 1.0, 损失: 0.06646033376455307
Step 1300, 准确率: 1.0, 损失: 0.061753835529088974
Step 1400, 准确率: 1.0, 损失: 0.05768435448408127
Step 1500, 准确率: 1.0, 损失: 0.054130859673023224
Step 1600, 准确率: 1.0, 损失: 0.0510009303689003
Step 1700, 准确率: 1.0, 损失: 0.048222970217466354
Step 1800, 准确率: 1.0, 损失: 0.04574064165353775
Step 1900, 准确率: 1.0, 损失: 0.04350901022553444
Step 2000, 准确率: 1.0, 损失: 0.04149177670478821
Step 2100, 准确率: 1.0, 损失: 0.039659276604652405
Step 2200, 准确率: 1.0, 损失: 0.03798716142773628
Step 2300, 准确率: 1.0, 损失: 0.03645514324307442
Step 2400, 准确率: 1.0, 损失: 0.035046208649873734
Step 2500, 准确率: 1.0, 损失: 0.03374601528048515
Step 2600, 准确率: 1.0, 损失: 0.03254235163331032
Step 2700, 准确率: 1.0, 损失: 0.031424783170223236
Step 2800, 准确率: 1.0, 损失: 0.030384326353669167
Step 2900, 准确率: 1.0, 损失: 0.02941320464015007
Step 3000, 准确率: 1.0, 损失: 0.02850465290248394
Step 3100, 准确率: 1.0, 损失: 0.027652768418192863
Step 3200, 准确率: 1.0, 损失: 0.026852363720536232
Step 3300, 准确率: 1.0, 损失: 0.026098856702446938
Step 3400, 准确率: 1.0, 损失: 0.025388216599822044
Step 3500, 准确率: 1.0, 损失: 0.024716870859265327
Step 3600, 准确率: 1.0, 损失: 0.02408158965408802
Step 3700, 准确率: 1.0, 损失: 0.0234795231372118
Step 3800, 准确率: 1.0, 损失: 0.022908106446266174
Step 3900, 准确率: 1.0, 损失: 0.0223650261759758
Step 4000, 准确率: 1.0, 损失: 0.021848207339644432
Step 4100, 准确率: 1.0, 损失: 0.02135578542947769
Step 4200, 准确率: 1.0, 损失: 0.020886056125164032
Step 4300, 准确率: 1.0, 损失: 0.02043745294213295
Step 4400, 准确率: 1.0, 损失: 0.020008569583296776
Step 4500, 准确率: 1.0, 损失: 0.019598128274083138
Step 4600, 准确率: 1.0, 损失: 0.01920494996011257
Step 4700, 准确率: 1.0, 损失: 0.01882794313132763
Step 4800, 准确率: 1.0, 损失: 0.018466131761670113
Step 4900, 准确率: 1.0, 损失: 0.01811859756708145
Step 5000, 准确率: 1.0, 损失: 0.01778450421988964
Step 5100, 准确率: 1.0, 损失: 0.0174630805850029
Step 5200, 准确率: 1.0, 损失: 0.01715361326932907
Step 5300, 准确率: 1.0, 损失: 0.016855429857969284
Step 5400, 准确率: 1.0, 损失: 0.016567926853895187
Step 5500, 准确率: 1.0, 损失: 0.016290517523884773
Step 5600, 准确率: 1.0, 损失: 0.0160226970911026
Step 5700, 准确率: 1.0, 损失: 0.015763945877552032
Step 5800, 准确率: 1.0, 损失: 0.015513807535171509
Step 5900, 准确率: 1.0, 损失: 0.015271877869963646
Step 6000, 准确率: 1.0, 损失: 0.015037745237350464
Step 6100, 准确率: 1.0, 损失: 0.014811034314334393
Step 6200, 准确率: 1.0, 损失: 0.014591369777917862
Step 6300, 准确率: 1.0, 损失: 0.014378437772393227
Step 6400, 准确率: 1.0, 损失: 0.014171929098665714
Step 6500, 准确率: 1.0, 损失: 0.013971567153930664
Step 6600, 准确率: 1.0, 损失: 0.013777065090835094
Step 6700, 准确率: 1.0, 损失: 0.013588163070380688
Step 6800, 准确率: 1.0, 损失: 0.01340463850647211
Step 6900, 准确率: 1.0, 损失: 0.013226241804659367
Step 7000, 准确率: 1.0, 损失: 0.013052761554718018
Step 7100, 准确率: 1.0, 损失: 0.012884002178907394
Step 7200, 准确率: 1.0, 损失: 0.01271976437419653
Step 7300, 准确率: 1.0, 损失: 0.012559876777231693
Step 7400, 准确率: 1.0, 损失: 0.012404152192175388
Step 7500, 准确率: 1.0, 损失: 0.012252428568899632
Step 7600, 准确率: 1.0, 损失: 0.012104558758437634
Step 7700, 准确率: 1.0, 损失: 0.01196039654314518
Step 7800, 准确率: 1.0, 损失: 0.011819805018603802
Step 7900, 准确率: 1.0, 损失: 0.01168263703584671
Step 8000, 准确率: 1.0, 损失: 0.011548792943358421
Step 8100, 准确率: 1.0, 损失: 0.011418122798204422
Step 8200, 准确率: 1.0, 损失: 0.011290528811514378
Step 8300, 准确率: 1.0, 损失: 0.011165900155901909
Step 8400, 准确率: 1.0, 损失: 0.01104412879794836
Step 8500, 准确率: 1.0, 损失: 0.010925106704235077
Step 8600, 准确率: 1.0, 损失: 0.010808765888214111
Step 8700, 准确率: 1.0, 损失: 0.010695010423660278
Step 8800, 准确率: 1.0, 损失: 0.010583736933767796
Step 8900, 准确率: 1.0, 损失: 0.010474861599504948
Step 9000, 准确率: 1.0, 损失: 0.010368350893259048
Step 9100, 准确率: 1.0, 损失: 0.01026405580341816
Step 9200, 准确率: 1.0, 损失: 0.01016198005527258
Step 9300, 准确率: 1.0, 损失: 0.010061997920274734
Step 9400, 准确率: 1.0, 损失: 0.009964067488908768
Step 9500, 准确率: 1.0, 损失: 0.009868137538433075
Step 9600, 准确率: 1.0, 损失: 0.009774118661880493
Step 9700, 准确率: 1.0, 损失: 0.00968195591121912
Step 9800, 准确率: 1.0, 损失: 0.009591614827513695
Step 9900, 准确率: 1.0, 损失: 0.009503037668764591
Step 10000, 准确率: 1.0, 损失: 0.009416175074875355
相关推荐
NAGNIP2 小时前
一文搞懂深度学习中的通用逼近定理!
人工智能·算法·面试
冬奇Lab3 小时前
一天一个开源项目(第36篇):EverMemOS - 跨 LLM 与平台的长时记忆 OS,让 Agent 会记忆更会推理
人工智能·开源·资讯
冬奇Lab3 小时前
OpenClaw 源码深度解析(一):Gateway——为什么需要一个"中枢"
人工智能·开源·源码阅读
AngelPP7 小时前
OpenClaw 架构深度解析:如何把 AI 助手搬到你的个人设备上
人工智能
宅小年7 小时前
Claude Code 换成了Kimi K2.5后,我再也回不去了
人工智能·ai编程·claude
九狼7 小时前
Flutter URL Scheme 跨平台跳转
人工智能·flutter·github
ZFSS7 小时前
Kimi Chat Completion API 申请及使用
前端·人工智能
天翼云开发者社区8 小时前
春节复工福利就位!天翼云息壤2500万Tokens免费送,全品类大模型一键畅玩!
人工智能·算力服务·息壤
知识浅谈8 小时前
教你如何用 Gemini 将课本图片一键转为精美 PPT
人工智能
Ray Liang9 小时前
被低估的量化版模型,小身材也能干大事
人工智能·ai·ai助手·mindx