Deep Learning [2] -- Logistic regression model from scratch

本文介绍了逻辑回归的基本概念,然后从理论和实践两个方面阐述了如何从零开始实现逻辑回归模型,包括模型定义、损失函数、优化算法以及小批量随机梯度下降的详细过程。通过实例展示了训练过程和结果,强调了损失函数和优化算法在模型训练中的重要性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

I. What is logistic regression?

 简单来说,线性回归解决的是:由已知的线性个特征输入值作为参数,通过已知数据集训练(training dataset)出一个线性回归模型(logistic regression model),从而来预测一个输出结果,这个输出结果是一个连续值。一个简单的例子:有个房主想卖掉自己的房子?,向中介联系预估其房屋的现有市值,房主给了x1, x2 = square, age。中介便通过以往卖出去的房屋数据,建立了一个线性回归模型,帮助该房主做出了较为合理的房屋估值。

II. [Theoretically] Implement a simple logistic regression model from scratch.

  • define model
    logistic regression model
  • define loss function
    loss function
    loss function
  • define optimizer
     当模型和损失函数形式较为简单时,上面的误差最小化问题的解可以直接用公式表达出来。这类 解叫作解析解(analytical solution)。本节使用的线性回归和平方误差刚好属于这个范畴。然而, 大多数深度学习模型并没有解析解,只能通过优化算法有限次迭代模型参数来尽可能降低损失函 数的值。这类解叫作数值解(numerical solution)。
     在求数值解的优化算法中,小批量随机梯度下降(mini-batch stochastic gradient descent)在深 度学习中被广泛使用。它的算法很简单:先选取一组模型参数的初始值,如随机选取;接下来对 参数进行多次迭代,使每次迭代都可能降低损失函数的值。在每次迭代中,先随机均匀采样一个由固定数目训练数据样本所组成的小批量(mini-batch)B,然后求小批量中数据样本的平均损 失有关模型参数的导数(梯度),最后用此结果与预先设定的一个正数的乘积作为模型参数在本 次迭代的减小量。
     在训练本节讨论的线性回归模型的过程中,模型的每个参数将作如下迭代:

    optimizer
  • Neural network graph
    neural network graph of logistic regression model
    note: 在图3.1所示的神经网络中,输入分别为x1 和x2 ,因此输入层的输入个数为2。输入个数也叫特征 数或特征向量维度。图3.1中网络的输出为o,输出层的输出个数为1。需要注意的是,我们直接将 图3.1中神经网络的输出o作为线性回归的输出,即yˆ = o。由于输入层并不涉及计算,按照惯例, 图3.1所示的神经网络的层数为1。所以,线性回归是一个单层神经网络。输出层中负责计算o的 单元又叫神经元。在线性回归中,o的计算依赖于x1 和x2 。也就是说,输出层中的神经元和输入 层中各个输入完全连接。因此,这里的输出层又叫全连接层(fully-connected layer)或稠密层(dense layer)。

III. [Pratically] Implement a simple logistic regression model from scratch.

  • prepare training dataset
# global variables
num_inputs = 2  # feature nums
num_examples = 1000  # sample nums
batch_size = 10


def 
Sure, here is an example Python code for Uplift model with low-rank regularization logistic regression for multiple correlated binary responses: ```python import numpy as np from sklearn.linear_model import LogisticRegression # define the Uplift model with low-rank regularization logistic regression class UpliftLowRankLogisticRegression: def __init__(self, rank=5, alpha=1.0): self.rank = rank self.alpha = alpha self.models = [] self.w = None def fit(self, X, y, t): # calculate the treatment and control groups X_treatment = X[t == 1] y_treatment = y[t == 1] X_control = X[t == 0] y_control = y[t == 0] # fit the logistic regression model for each response for i in range(y.shape[1]): model = LogisticRegression(penalty='l2', C=self.alpha) model.fit(np.hstack((X_treatment, y_treatment[:, i].reshape(-1, 1))), y_treatment[:, i]) self.models.append(model) # use SVD to learn the low-rank representation w U, S, Vt = np.linalg.svd(y_control - self.predict(X_control)) self.w = Vt[:self.rank].T def predict(self, X): # calculate the uplift score for each response uplift_scores = np.zeros((X.shape[0], len(self.models))) for i, model in enumerate(self.models): uplift_scores[:, i] = model.predict_proba(X)[:, 1] # calculate the predicted response for the control group y_control_pred = np.dot(X, self.w) # calculate the predicted response for the treatment group y_treatment_pred = y_control_pred + uplift_scores # return the predicted response matrix return np.vstack((y_control_pred, y_treatment_pred)) ``` The `UpliftLowRankLogisticRegression` class takes two hyperparameters: `rank` for the rank of the low-rank representation w and `alpha` for the regularization strength of logistic regression. In the `fit` method, the treatment and control groups are separated, and logistic regression models are fitted for each response using the treatment group. Then, SVD is used to learn the low-rank representation w from the predicted responses of the control group. In the `predict` method, the uplift scores for each response are calculated using the logistic regression models and added to the predicted responses of the control group to obtain the predicted responses of the treatment group. The predicted response matrix is returned by stacking the predicted responses of the control and treatment groups vertically.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值