Gradient Checkingãå®è£ ãã
çããããã«ã¡ã¯
ãå
æ°ã§ããï¼ç§ã¯åãã¦ãã¾ãã
ä»æ¥ã¯Gradient Checkingãç´¹ä»ãã¾ãã
Gradient Checking
æ©æ¢°å¦ç¿ã®å®è£
ãè¡ã£ã¦ããã¨ã
å¾é
ã®è¨ç®ãããç®æãåºããã¨ãå¤ãã§ããï¼Back Propagationãªã©ï¼
ç°¡åãªè¨ç®ãããã«ã¯é©å½ã«æã§è¨ç®ãããã®ãå®è£
ããã°ããã§ãã
ã ãã ãæ¬å½ã«åã£ã¦ããã®ãããããªãã¨ããå®è£
æ¹éå¤ããå ´åã§ãç価ã«ãªãã®ããªã©
ä¸å®ã«ãªã£ã¦ããç®æãåºã¦ãã¾ããï¼å¤åï¼
ãããªã¨ãã«Gradient Checkingã使ãã¾ãããã
æ°å¼
ããã©ã¡ã¼ã¿ã«åºã¥ãé¢æ°Jï¼é¢æ°Jããã©ã¡ã¼ã¿ã®å
容ã¯èªåã§å®ç¾©ãæ§ç¯ãã¦ãã ããï¼
ã¨å®ç¾©ããã¨ä»¥ä¸ãGradient Checkingãå®è£
ããå¼ã¨ãªãã¾ãã
EPSILONã¯å°ããå®æ°ã§ãã大ä½0.0001ç¨åº¦ãè¯ãããã§ãã
ããã°ã©ã ã¨ãã¦ä¸è¨å¼ãæ§ç¯ããã«ã¯ãå³ãè¨ç®ããé¢æ°ã¨å·¦ãè¨ç®ããé¢æ°ãç¨æãã
ããããæ¯è¼ããã°è¯ããã¨ã«ãªãã¾ãã
å®è£
ä»åã¯ç°¡åãªæ°å¼ã§å®é¨ãããã¨æãã¾ãã
å¼ï¼
å¾®åå¼ï¼
左辺ï¼gradient_calcï¼ã¨å³è¾ºï¼functionï¼ã
Source Code
# coding:utf-8 import numpy as np def function(x): return x ** 2 def gradient_calc(x): return 2 * x def gradient_miss_calc(x): return 3 * x def gradient_checking(forward, gradient, x): """ :params forward: forward function :params backward: gradient function :params x: data :return: correct(True) or fall(False) """ epsilon = 0.0001 gradient_checker = (forward(x + epsilon) - forward(x - epsilon)) / (2 * epsilon) diff = np.abs(gradient(x) - gradient_checker) if diff < 0.0001: print "correct" return True else: print "incorrect" return False if __name__ == '__main__': x = 3 print "correct pattern" gradient_checking(function, gradient_calc, x) print "incorrect pattern" gradient_checking(function, gradient_miss_calc, x)
æ¨æºåºå
correct pattern correct incorrect pattern incorrect
æåã¯æ£ããå¾®åã®å¼ãä¸ãã¦ãã¾ããã次ã®å¼ã«ã¯ç°ãªãå¾®åå¼ãä¸ãã¦ãã¾ãã
ã¡ããã¨å¾®åå¼ãééã£ã¦ãããã¨ãæ¤åºãã¦ãããã¨ããããã¾ãã
ä»åã¯ã左辺ã¨å³è¾ºã¯ã ãããçããã¨ã®ãã¨ãªã®ã§ãä¸å®ä»¥ä¸ã®å·®ã§ããã°
æ£ããã¨ãããã¨ã«ãã¦ãã¾ãã