Python 样式转换中的Adam优化器

Python 样式转换中的Adam优化器,python,image,optimization,tensorflow,deep-learning,Python,Image,Optimization,Tensorflow,Deep Learning,我正在尝试练习这篇文章中的练习题,是否有人知道如何用Adam Optimizer替换基本梯度下降。 我认为这些代码可能是改变的地方。非常感谢你的帮助 # Reduce the dimensionality of the gradient. grad = np.squeeze(grad) # Scale the step-size according to the gradient-values. step_size_scaled =

我正在尝试练习这篇文章中的练习题,是否有人知道如何用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

请参阅中的幻灯片36和37

必须在范围(num\u迭代次数)中i的
上方声明:
行存在于该GitHub文件中。另外,根据您的要求从下面初始化
beta1
beta2
变量
然后,您可以用以下代码块替换代码块:

# 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没有属性'sqrt'。 详细错误如下所示。

非常感谢,让我试试。我像这样初始化beta1和beta2:beta1=tf.Variable(0,name='beta1')beta2=tf.Variable(0,name='beta2')session.run([beta1.initializer,beta2.initializer]),但出现了一些问题:Tensor'对象没有属性'sqrt'oh,尝试将上述代码中的最后一行替换为
mixed_image-=step_size*first_unbias/(tf.sqrt(second_unbias)+1e-8)
,然后告诉我。如果这不能解决那个特定的错误,我需要看看您的实现。
  beta1=tf.Variable(0,name='beta1') 
  beta2=tf.Variable(0,name='beta2') 
  session.run([beta1.initializer,beta2.initializer])