본문 바로가기
언어/ㄴTheano

[theano] The experiment for understanding about update of theano.function

by 공대우냉이 2016. 2. 2.

theano.function 에서 updates가 어떻게 돌아가는지 실험해보겠다.

일단 update를 할 때 기본적으로 theano.tensor.grad라는 함수를 알아야한다.

grad함수를 이용하여 weight들을 update하게된다.

다음의 예제 소스를 참고하여 weight들이 어떻게 변하는지 살펴보자.

Let me experiment how to update parameters in theano.function library.

When you use 'updates' in theano.function, basically, you know the theano.tensor.grad library.

Weight variable is updated by theano.tensor.grad lib.

we observe carefully How to update weight with referring to next example source code .

update_test.py    update.log

위의 소스에서 (w*x)^2의 식에서 x는 2, weight의 w는 3을 초기값으로 주었다.

우리는 object 2차 함수를 gradient descent방식을 이용하여 0에 수렴하게 weight를 조정할 것이다.

여기서 중요한 것은 예제에서 2가지 조건을 걸었다. 바로 learning rate이다.

learning rate가 중요한 이유는 learning rate가 크면 object함수 값이 발산해버린다는 것이다.

 learning rate is very important because object function value is divergenced if learning rate value is pretty big.

under picture is comparing to learning rates of gradient descent.

0.5 learning rate's object value is divergenced.

object=y축, weight=x축으로 생각할 수 있고 결국 y(object)가 0이 되게 하는 x(weight)를 찾는 것이라고 생각할 수 있다.

x축이 w에 해당하게 되고 learn_w는 해당 w값에서의 기울기 변화량값에 해당한다. 여기서 learning rate는 얼마만큼의 x축거리를 움직일 것인가를 결정해주는 변수이다.

너무 많은 거리를 움직이게 되면 y가 0에 수렴하는 부분을 지나쳐 반대편으로 넘어가는 경우가 생길 수가 있다.

그러면 weight의 부호가 바뀌게 되고 발산의 위험성도 높아진다.(물론 부호가 바뀐다고 발산한다는 것은 아니다.)

안전하게 y(object)가 0이 되게하는 x(weight)값을 찾기 위해서는 learning rate를 작게하여서 기울기의 변화량값이 0이 될때까지 조금씩 움직여야한다.

위의 두 learning rate를 가지고 이해하기 쉽게 그림으로 그려보았다.  손으로 iter 2,3까지는 직접 계산도 해보는 것이 이해하기에 도움이 될것이다.

 

learning rate는 작게 하는것이 좋다. 그러면 이제 update에 대해 살펴보자. update는 theano.function을 실행하면 output값을 반환하고

미리 지정해놓은 update function을 수행하게 된다. mechanism을 잘 이해하자.

직접 손으로 한번 풀어보고 log파일과 맞는지 비교해보고 update에 대한 부분은 넘어가도록 하자.