Ch7. Gradient Descent with R Code
Gradient Descent를 R code로 구현해보도록 하겠습니다.
1. 샘플 데이터 생성(난수)
x = runif(300,-10,10) # 균일분포 난수 생성Noise = rnorm(n = 300, mean = 0 , sd = 3)y = x + NoisexDF = data.frame(x = x, y = y)library(ggplot2)ggplot(DF,aes(x= x, y= y)) + geom_point(col = 'royalblue') + theme_bw()
2. Learning Rate 설정
xxxxxxxxxxalpha = 0.013. 초기 가중치 행렬 생성
xxxxxxxxxxWeights = matrix(c(0,0),nrow = 2)Weightsxxxxxxxxxx [,1][1,] 0[2,] 04. 회귀식 계산을 위한 행렬 생성
이러한 식을 R에서 만들어주어야 하는데, 코드는 다음과 같습니다.
xxxxxxxxxx# 행렬형태로 만들어 주기X = matrix(x)X = cbind(1,X)head(X)xxxxxxxxxx [,1] [,2][1,] 1 5.3357724[2,] 1 6.8523958[3,] 1 8.6630348[4,] 1 4.2440585[5,] 1 3.2915996[6,] 1 0.47893945. Error Loss function 만들기
xxxxxxxxxx# Error 계산# %*%는 행렬의 곱셈을 할 때 사용합니다.Error = function(x, y, Weight){ sum(( y - x %*% Weight )^2) / (2*length(y))} 6. 알고리즘 학습
학습을 돌리기 전에, Error(Cost)값과 가중치(회귀계수)가 저장될 빈공간을 만들어야 합니다.
xxxxxxxxxxError_Surface = c()Weight_Value = list()xxxxxxxxxxfor ( i in 1 : 300){ # X는 (300,2) 행렬 # Weights는 (2,1)행렬 # X * Weights => (300,1) 행렬[각 데이터에서의 Error 연산] error = (X %*% Weights - y) # Delta Funtion 계산 Delta_function = t(X) %*% error / length(y) # 가중치 수정 Weights = Weights - alpha * Delta_function Error_Surface[i] = Error(X, y, Weights) Weight_Value[[i]] = Weights }7. 시각화
xxxxxxxxxxp = ggplot(DF,aes(x = x, y = y)) + geom_point(col = 'royalblue', alpha = 0.4)+ theme_bw()for( i in 1:300){ p = p + geom_abline(slope = Weight_Value[[i]][2], intercept = Weight_Value[[i]][1], col = 'red', alpha = 0.4)}p
다음과 같이 가중치(회귀계수)가 조금씩 움직이면서 최적의 회귀선을 찾아가는 것을 알 수 있습니다.
xxxxxxxxxxDF$num = 1:300DF$Error_value = Error_Surfaceggplot(DF) + geom_line(aes(x = num , y = Error_value),group = 1) + geom_point(aes(x = num, y = Error_value )) + theme_bw() + ggtitle("Error Function") + xlab("Num of iterations")
Error 값 또한 감소하면서 최적의 가중치(기울기)에 수렴하는 것을 확인할 수 있습니다.
8. 최소제곱법을 이용한 선형회귀식과의 비교
- 일반 선형회귀(최소제곱법)
xxxxxxxxxxREG = lm(y ~ x )summary(REG)xxxxxxxxxxCall:lm(formula = y ~ x)Residuals: Min 1Q Median 3Q Max -8.7002 -1.9927 0.0004 1.8941 8.1390 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 0.4661 0.1707 2.73 0.0067 ** x 1.0014 0.0301 33.27 <2e-16 ***---Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1Residual standard error: 2.957 on 298 degrees of freedomMultiple R-squared: 0.7879, Adjusted R-squared: 0.7872 F-statistic: 1107 on 1 and 298 DF, p-value: < 2.2e-16일반 최소제곱법으로 회귀식을 추정했을 경우, 는 0.4661, 은 1.0014입니다.
- Gradient
xxxxxxxxxxWeight_Value[[300]] [,1][1,] -0.3328329[2,] 1.0313280경사하강법으로 회귀식을 추정했을 경우, 는 -0.3328, 은 1.031입니다. 를 구해보면 다음과 같습니다.
xxxxxxxxxxGR_MODEL = -0.3328329 + 1.0313280 * xactual = yrss <- sum((GR_MODEL - actual) ^ 2)tss <- sum((actual - mean(actual)) ^ 2)rsq <- 1 - rss/tssrsqxxxxxxxxxx[1] 0.7716216



댓글
댓글 쓰기