【问题标题】:Adam Optimizer in Style Transfer风格迁移中的 Adam Optimizer
【发布时间】:2019-02-15 00:47:24
【问题描述】:

我正在尝试练习style transfer tutorial 中的练习题,有没有人知道如何用 Adam Optimizer 替换基本梯度下降。 我认为这些代码可能是改变的地方。非常感谢您的帮助。

       # Reduce the dimensionality of the gradient.
        grad = np.squeeze(grad)

        # Scale the step-size according to the gradient-values.
        step_size_scaled = step_size / (np.std(grad) + 1e-8)

        # Update the image by following the gradient.
        mixed_image -= grad * step_size_scaled

【问题讨论】:

    标签: python image optimization tensorflow deep-learning


    【解决方案1】:

    参考Stanford CS231n slides的幻灯片36和37,

    first_moment = 0
    second_moment = 0
    

    必须在该 GitHub 文件中的 for i in range(num_iterations): 行上方声明。此外,根据您的要求从下面初始化 beta1beta2 变量。
    然后,您可以将代码块替换为以下内容:

    # Reduce the dimensionality of the gradient.
    grad = np.squeeze(grad)
    
    # Calculate moments
    first_moment = beta1 * first_moment + (1 - beta1) * grad
    second_moment = beta2 * second_moment + (1 - beta2) * grad * grad
    
    # Bias correction steps
    first_unbias = first_moment / (1 - beta1 ** i)
    second_unbias = second_moment / (1 - beta2 ** i)
    
    # Update the image by following the gradient (AdaGrad/RMSProp step)
    mixed_image -= step_size * first_unbias / (tf.sqrt(second_unbias) + 1e-8)
    

    【讨论】:

    • 非常感谢,让我试试。
    • 我像这样初始化 beta1 和 beta2: beta1=tf.Variable(0,name='beta1') beta2=tf.Variable(0,name='beta2') session.run([beta1 .initializer,beta2.initializer]),但是出了点问题:Tensor' object has no attribute 'sqrt'
    【解决方案2】:

    我像这样初始化 beta1 和 beta2:

      beta1=tf.Variable(0,name='beta1') 
      beta2=tf.Variable(0,name='beta2') 
      session.run([beta1.initializer,beta2.initializer])
    

    但是,出了点问题:Tensor' 对象没有属性 'sqrt'。 详细的错误如下所示。

    【讨论】:

    • 哦,尝试将上面代码中的最后一行替换为mixed_image -= step_size * first_unbias / (tf.sqrt(second_unbias) + 1e-8),然后告诉我。如果这不能解决那个特定的错误,我需要看看你的实现。
    猜你喜欢
    • 2021-10-04
    • 1970-01-01
    • 1970-01-01
    • 2019-11-24
    • 1970-01-01
    • 2017-07-02
    • 2019-10-18
    • 2018-05-06
    • 2017-11-11
    相关资源
    最近更新 更多