1. 程式人生 > >『cs231n』作業2選講_通過代碼理解優化器

『cs231n』作業2選講_通過代碼理解優化器

cit err red numpy 優化器 包含 iteration mom 效果

1)、Adagrad
一種自適應學習率算法,實現代碼如下:

cache += dx**2
x += - learning_rate * dx / (np.sqrt(cache) + eps)

這種方法的好處是,對於高梯度的權重,它們的有效學習率被降低了;而小梯度的權重叠代過程中學習率提升了。要註意的是,這裏開根號很重要。平滑參數eps是為了避免除以0的情況,eps一般取值1e-4 到1e-8。

2)、RMSprop
RMSProp方法對Adagrad算法做了一個簡單的優化,以減緩它的叠代強度:

cache = decay_rate * cache + (1 - decay_rate) * dx**2
x += - learning_rate * dx / (np.sqrt(cache) + eps)

其中,decay_rate是一個超參數,其值可以在 [0.9, 0.99, 0.999]中選擇。

3)、Adam
Adam有點像RMSProp+momentum,效果比RMSProp稍好,其簡化版的代碼如下:

m = beta1*m + (1-beta1)*dx
v = beta2*v + (1-beta2)*(dx**2)
x += - learning_rate * m / (np.sqrt(v) + eps)

論文中推薦eps = 1e-8,beta1 = 0.9,beta2 = 0.999。

import numpy as np

"""

輸入:
  - w: 
  - dw: 
  - config: 包含各種超參數
返回:
  - next_w: 
  - config: 

"""


def sgd(w, dw, config=None):

  if config is None: config = {}
  config.setdefault(‘learning_rate‘, 1e-2)

  w -= config[‘learning_rate‘] * dw
  return w, config


def sgd_momentum(w, dw, config=None):
  """
 結合動量的SGD(最常用)
 
  - learning_rate: 
  - momentum: 動量值
  - velocity: A numpy array of the same shape as w and dw used to store a moving
    average of the gradients.
  """
  if config is None: config = {}
  config.setdefault(‘learning_rate‘, 1e-2)
  config.setdefault(‘momentum‘, 0.9)
  v = config.get(‘velocity‘, np.zeros_like(w))
  
  next_w = None
 
  next_w = w
  v = config[‘momentum‘]* v - config[‘learning_rate‘]*dw
  next_w +=v
 
  config[‘velocity‘] = v

  return next_w, config



def rmsprop(x, dx, config=None):
  """
 
  - learning_rate: 
  - decay_rate: 
  - epsilon: 小數值 避免分母為零
  - cache: 
  """
  if config is None: config = {}
  config.setdefault(‘learning_rate‘, 1e-2)
  config.setdefault(‘decay_rate‘, 0.99)
  config.setdefault(‘epsilon‘, 1e-8)
  config.setdefault(‘cache‘, np.zeros_like(x))

  next_x = None
 
  next_x = x
  config[‘cache‘] = config[‘decay_rate‘]*config[‘cache‘]+(1-config[‘decay_rate‘])*(dx*dx)
  x += -config[‘learning_rate‘]* dx / (np.sqrt(config[‘cache‘])+config[‘epsilon‘])
 

  return next_x, config


def adam(x, dx, config=None):
  """
 
  - learning_rate
  - beta1: m的衰減率
  - beta2: v的衰減率
  - epsilon
  - m: Moving average of gradient.
  - v: Moving average of squared gradient.
  - t: Iteration number.
  """
  if config is None: config = {}
  config.setdefault(‘learning_rate‘, 1e-3)
  config.setdefault(‘beta1‘, 0.9)
  config.setdefault(‘beta2‘, 0.999)
  config.setdefault(‘epsilon‘, 1e-8)
  config.setdefault(‘m‘, np.zeros_like(x))
  config.setdefault(‘v‘, np.zeros_like(x))
  config.setdefault(‘t‘, 0)
  
  next_x = None
 
 
  config[‘t‘]+=1 
  config[‘m‘] = config[‘beta1‘]*config[‘m‘] + (1- config[‘beta1‘])*dx
  config[‘v‘] = config[‘beta2‘]*config[‘v‘] + (1- config[‘beta2‘])*(dx**2)   
  mb = config[‘m‘]/(1-config[‘beta1‘]**config[‘t‘])
  vb = config[‘v‘]/(1-config[‘beta2‘]**config[‘t‘])
  next_x = x -config[‘learning_rate‘]* mb / (np.sqrt(vb) + config[‘epsilon‘])
      

  return next_x, config

『cs231n』作業2選講_通過代碼理解優化器