首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在随机梯度下降中增加费用的目的是什么?

在随机梯度下降中增加费用的目的是什么?
EN

Stack Overflow用户
提问于 2017-08-29 21:02:28
回答 1查看 74关注 0票数 1

我正试图实现一个基于斯坦福在他们第一次分配给cs224n的脚手架基础上的SGD。实现是用python实现的。该脚手架如下:

代码语言:javascript
复制
def load_saved_params():
'''A helper function that loads previously saved parameters and resets
iteration start.'''
return st, params, state #st = starting iteration

def save_params(iter, params):
'''saves the parameters'''

现在,主要功能(我使用多个哈希符号跟踪感兴趣的语句)

代码语言:javascript
复制
def sgd(f, x0, step, iterations, postprocessing=None, useSaved=False,
    PRINT_EVERY=10):
""" Stochastic Gradient Descent

Implement the stochastic gradient descent method in this function.

Arguments:
f -- the function to optimize, it should take a single
     argument and yield two outputs, a cost and the gradient
     with respect to the arguments
x0 -- the initial point to start SGD from
step -- the step size for SGD
iterations -- total iterations to run SGD for
postprocessing -- postprocessing function for the parameters
                  if necessary. In the case of word2vec we will need to
                  normalize the word vectors to have unit length.
PRINT_EVERY -- specifies how many iterations to output loss

Return:
x -- the parameter value after SGD finishes
"""

# Anneal learning rate every several iterations
ANNEAL_EVERY = 20000

if useSaved:
    start_iter, oldx, state = load_saved_params()
    if start_iter > 0:
        x0 = oldx
        step *= 0.5 ** (start_iter / ANNEAL_EVERY)

    if state:
        random.setstate(state)
else:
    start_iter = 0

x = x0

if not postprocessing:
    postprocessing = lambda x: x

expcost = None ######################################################

for iter in xrange(start_iter + 1, iterations + 1):
    # Don't forget to apply the postprocessing after every iteration!
    # You might want to print the progress every few iterations.

    cost = None

    ### END YOUR CODE

    if iter % PRINT_EVERY == 0:
        if not expcost:
            expcost = cost
        else:
            expcost = .95 * expcost + .05 * cost ########################
        print "iter %d: %f" % (iter, expcost)

    if iter % SAVE_PARAMS_EVERY == 0 and useSaved:
        save_params(iter, x)

    if iter % ANNEAL_EVERY == 0:
        step *= 0.5

return x

为了我的目的,我不使用费用。但是代码中的费用的目的是什么。在什么情况下可以使用呢?为什么它被用于修改成本函数计算的成本?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-08-29 21:14:53

如果您注意到,expcost只用于打印成本。这只是一种平滑成本函数的方法,因为它可以明显地从一批一批跳到另一批,尽管模型有所改进

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45948279

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档