简体   繁体   English

使用Python的numpy的随机梯度下降实现

[英]Stochastic gradient descent implementation with Python's numpy

I have to implement stochastic gradient descent using python numpy library. 我必须使用python numpy库实现随机梯度下降。 For that purpose I'm given the following function definitions: 为此,我给出了以下函数定义:

def compute_stoch_gradient(y, tx, w):
    """Compute a stochastic gradient for batch data."""

def stochastic_gradient_descent(
        y, tx, initial_w, batch_size, max_epochs, gamma):
    """Stochastic gradient descent algorithm."""

I'm also given the following help function: 我还获得了以下帮助功能:

def batch_iter(y, tx, batch_size, num_batches=1, shuffle=True):
    """
    Generate a minibatch iterator for a dataset.
    Takes as input two iterables (here the output desired values 'y' and the input data 'tx')
    Outputs an iterator which gives mini-batches of `batch_size` matching elements from `y` and `tx`.
    Data can be randomly shuffled to avoid ordering in the original data messing with the randomness of the minibatches.
    Example of use :
    for minibatch_y, minibatch_tx in batch_iter(y, tx, 32):
        <DO-SOMETHING>
    """
    data_size = len(y)

    if shuffle:
        shuffle_indices = np.random.permutation(np.arange(data_size))
        shuffled_y = y[shuffle_indices]
        shuffled_tx = tx[shuffle_indices]
    else:
        shuffled_y = y
        shuffled_tx = tx
    for batch_num in range(num_batches):
        start_index = batch_num * batch_size
        end_index = min((batch_num + 1) * batch_size, data_size)
        if start_index != end_index:
            yield shuffled_y[start_index:end_index], shuffled_tx[start_index:end_index]

I implemented the following two functions: 我实现了以下两个功能:

def compute_stoch_gradient(y, tx, w):
    """Compute a stochastic gradient for batch data."""
    e = y - tx.dot(w)
    return (-1/y.shape[0])*tx.transpose().dot(e)


def stochastic_gradient_descent(y, tx, initial_w, batch_size, max_epochs, gamma):
    """Stochastic gradient descent algorithm."""
    ws = [initial_w]
    losses = []
    w = initial_w
    for n_iter in range(max_epochs):
        for minibatch_y,minibatch_x in batch_iter(y,tx,batch_size):
            w = ws[n_iter] - gamma * compute_stoch_gradient(minibatch_y,minibatch_x,ws[n_iter])
            ws.append(np.copy(w))
            loss = y - tx.dot(w)
            losses.append(loss)

    return losses, ws

I'm not sure the iteration should be done in range(max_epochs) or in a larger range. 我不确定迭代应在range(max_epochs)或更大范围内完成。 I say this because I read that an epoch is "each time we run through the entire data set". 我之所以这样说,是因为我读到一个时代是“每次我们遍历整个数据集”。 So I think an epoch consists on more of one iteration... 所以我认为一个时代包含了更多的迭代...

In a typical implementation, a mini-batch gradient descent with batch size B should pick B data points from the dataset randomly and update the weights based on the computed gradients on this subset. 在典型的实现中,批次大小为B的小批量梯度下降应从数据集中随机选择B个数据点,并基于此子集上计算出的梯度来更新权重。 This process itself will continue many number of times until convergence or some threshold maximum iteration. 该过程本身将持续许多次,直到收敛或某个阈值最大迭代为止。 Mini-batch with B=1 is SGD which can be noisy sometimes. B = 1的小批量是SGD,有时可能很吵。

Along with the above comments, you may want to play with the batch size and the learning rate (step size) since they have significance impact on the convergence rate of stochastic and mini-batch gradient descent. 除上述注释外,您可能还希望使用批处理大小和学习率(步长),因为它们对随机和小批量梯度下降的收敛速度有重要影响。

The following plots show the impacts of these two parameters on the convergence rate of SGD with logistic regression while doing sentiment analysis on amazon product review dataset, an assignment that appeared in a coursera course on Machine Learning - Classification by the University of Washington: 下图显示了这两个参数对logistic regressionSGD收敛速度的影响,同时对亚马逊产品评论数据集进行情感分析,该任务出现在华盛顿大学的机器学习-分类课程中:

在此处输入图片说明 在此处输入图片说明

For more detailed information on this you may refer to https://sandipanweb.wordpress.com/2017/03/31/online-learning-sentiment-analysis-with-logistic-regression-via-stochastic-gradient-ascent/?frame-nonce=987e584e16 有关此的更多详细信息,您可以参考https://sandipanweb.wordpress.com/2017/03/31/online-learning-sentiment-analysis-with-logistic-regression-via-stochastic-gradient-ascent/?frame -nonce = 987e584e16

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM