User Tools

Site Tools


why_n-1_gradient_explanation

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
why_n-1_gradient_explanation [2025/09/05 19:22] – [리소스를 많이 사용하지 않고 msr값이 최소가 되는 v값을 찾는 방법] hkimscilwhy_n-1_gradient_explanation [2025/09/05 19:51] (current) – [리소스를 많이 사용하지 않고 msr값이 최소가 되는 v값을 찾는 방법] hkimscil
Line 122: Line 122:
   return(dx)   return(dx)
 } # function returns ds value } # function returns ds value
 +
 +
 +
 +
 +
  
  
Line 145: Line 150:
 </WRAP> </WRAP>
  
 +
 +<WRAP group>
 +<WRAP half column>
 <code> <code>
 residuals <- function(x, v) { residuals <- function(x, v) {
Line 160: Line 168:
 #  return(mean(residuals^2)) #  return(mean(residuals^2))
 } }
 +</code>
 +</WRAP>
 +<WRAP half column>
 +msr값을 구하기 위한 function 
  
 +</WRAP>
 +</WRAP>
 +
 +<WRAP group>
 +<WRAP half column>
 +<code>
 +zx <- (x-mean(x))/sd(x)
 # pick one random v in (x-v) # pick one random v in (x-v)
 v <- rnorm(1) v <- rnorm(1)
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +>
 +</code>
 +</WRAP>
 +<WRAP half column>
 +comment
 +  * 랜덤하게 v값을 찾음 ''v <- rnorm(1)'' 
 +  * 원래는 mean(x)값 근처의 값을 랜덤하게 골라야 하므로
 +  * ''v <- rnorm(1, mean(x), sd(x))'' 와 같이 써야 하지만 (현재의 경우, '' rnorm(1, 50, 5) ''가 된다) 
 +  * 그렇게 하질 않고 x 집합의 원소들을 표준점수화 한 후 '' zx <- (x-mean(x))/sd(x) ''
 +  * (이렇게 표준점수화 하면 x 변인의 평균이 0, 표준편차가 1 이 되는 집합으로 변한다)
 +  * '' v <- rnorm(1, 0, 1) ''로 구한다. 뒤의 인자인 0, 1 은 default이모로 생략.
 +  * 이렇게 하는 이유는 혹시 나중에 다른 x집합에 똑같은 작업을 하더라도 그 집합의 평균과 표준편차를 사용하지 않고
 +  * 단순히 '' rnorm(1)'' 을 이용해서 찾으려고 하는 것이다. 
 +
 +</WRAP>
 +</WRAP>
 +
 +
 +<WRAP group>
 +<WRAP half column>
 +<code>
 # Train the model with scaled features # Train the model with scaled features
 learning.rate = 1e-1 learning.rate = 1e-1
 +</code>
 +</WRAP>
 +<WRAP half column>
 +comment
 +  * 이 0.1은 gradient function으로 구한 해단 v값에 대한 y 기울기 값을 (미분값)
 +  * 구한 후, 여기에 곱하기 위해서 지정한다.
 +</WRAP>
 +</WRAP>
  
-grads <- c() +<WRAP group> 
-ssrs <- c()+<WRAP half column> 
 + 
 +<code>
 msrs <- c() msrs <- c()
-mres <- c() 
 vs <- c() vs <- c()
-steps <- c() 
-# Record Loss for each epoch: 
-zx <- (x-mean(x))/sd(x) 
  
 nlen <- 75 nlen <- 75
 for (epoch in 1:nlen) { for (epoch in 1:nlen) {
   residual <- residuals(zx, v)   residual <- residuals(zx, v)
-  ssr.x <- ssr(zx, v) 
   msr.x <- msr(zx, v)   msr.x <- msr(zx, v)
-  ssrs <- append(ssrs, ssr.x) 
-  msrs <- append(msrs, msr.x) 
      
   grad <- gradient(zx, v)   grad <- gradient(zx, v)
-  grads <- append(grads, grad) +  step.v <- grad * learning.rate  
-  step.v <- grad * learning.rate +  v <- v - step.v # 그 다음 v값 
-  steps <- append(steps, step.v) +  vs <- append(vs, v) # v값 저장
-  v <- v - step.v +
-  vs <- append(vs, v)+
 } }
  
-tail(grads) 
 tail(msrs) tail(msrs)
-tail(ssrs) 
 tail(vs) tail(vs)
  
 plot(msrs) plot(msrs)
-plot(ssrs) 
 plot(vs) plot(vs)
-plot(grads) +</code> 
-# scaled +</WRAP> 
-+<WRAP half column> 
-# zx <- (x-mean(x))/sd(x) +comment 
-# v.표준화 <- (v.원래 - mean(x))/sd(x) +</WRAP> 
-v.orig <- (v*sd(x))+mean(x)  +</WRAP>
-v.orig +
- +
-steps+
  
 +<WRAP group>
 +<WRAP half column>
 +<code>
 +# scaled
 +vs # 변화하는 v 값들의 집합 
 vs.orig <- (vs*sd(x))+mean(x)  vs.orig <- (vs*sd(x))+mean(x) 
 vs.orig vs.orig
-grads 
- 
  
 +# 마지막 v값이 최소값에 근접한 값
 +
 +v.orig <- (v*sd(x))+mean(x) 
 +v.orig
 </code> </code>
 +</WRAP>
 +<WRAP half column>
 +comment
 +</WRAP>
 +</WRAP>
why_n-1_gradient_explanation.1757067740.txt.gz · Last modified: 2025/09/05 19:22 by hkimscil

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki