gradient_descent
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| gradient_descent [2026/05/12 21:48] – [ro01] hkimscil | gradient_descent [2026/05/12 22:02] (current) – [ro01] hkimscil | ||
|---|---|---|---|
| Line 696: | Line 696: | ||
| # 다시 learning rate값을 곱하여 이를 다음의 a, b | # 다시 learning rate값을 곱하여 이를 다음의 a, b | ||
| # 값으로 사용한다. . . . 이를 반복한다. | # 값으로 사용한다. . . . 이를 반복한다. | ||
| - | |||
| a <- rnorm(1) | a <- rnorm(1) | ||
| b <- rnorm(1) | b <- rnorm(1) | ||
| Line 719: | Line 718: | ||
| # Train the model with scaled features | # Train the model with scaled features | ||
| - | learning.rate = 1e-1 | + | learning.rate = 0.1 |
| # Record Loss for each epoch: | # Record Loss for each epoch: | ||
| as = c() | as = c() | ||
| bs = c() | bs = c() | ||
| + | das = c() | ||
| + | dbs = c() | ||
| + | ep <- c() | ||
| + | |||
| mses = c() | mses = c() | ||
| zx <- (x-mean(x))/ | zx <- (x-mean(x))/ | ||
| - | nlen <- 50 | + | |
| + | nlen <- 75 | ||
| for (epoch in 1:nlen) { | for (epoch in 1:nlen) { | ||
| predictions <- predict(zx, a, b) | predictions <- predict(zx, a, b) | ||
| - | residual <- residuals(predictions, | + | |
| loss <- mseloss(predictions, | loss <- mseloss(predictions, | ||
| mses <- append(mses, | mses <- append(mses, | ||
| | | ||
| grad <- gradient(zx, | grad <- gradient(zx, | ||
| + | db <- grad$b | ||
| + | da <- grad$a | ||
| + | | ||
| + | dbs <- append(dbs, db) | ||
| + | das <- append(das, da) | ||
| + | ep <- append(ep, epoch) | ||
| + | | ||
| step.b <- grad$b * learning.rate | step.b <- grad$b * learning.rate | ||
| step.a <- grad$a * learning.rate | step.a <- grad$a * learning.rate | ||
| Line 743: | Line 754: | ||
| bs <- append(bs, b) | bs <- append(bs, b) | ||
| } | } | ||
| - | mses | + | |
| - | as | + | tmp <- data.frame(ep, |
| - | bs | + | tmp |
| + | |||
| + | # 위의 루프를 이해했는지 체크하기 | ||
| + | a.init | ||
| + | b.init | ||
| + | tmp.p <- predict(zx, a.init, b.init) | ||
| + | # a.init b.init 일 때의 미분 결과 | ||
| + | # 즉, 기울기와 절편 값 | ||
| + | tmp.g <- gradient(zx, | ||
| + | tmp.g | ||
| + | |||
| + | # 이 값에 learning rate 값을 곱한 후에 | ||
| + | a.step <- tmp.g$a*learning.rate | ||
| + | b.step <- tmp.g$b*learning.rate | ||
| + | |||
| + | # 이를 원래 a값에서 빼준 값을 다음 순서의 | ||
| + | a.init <- a.init-a.step | ||
| + | b.init <- b.init-b.step | ||
| + | # a, b값으로 쓰고, 이 때의 미분 값을 구한 후에 | ||
| + | # (루프 안에서), 미분 결과를 다시 learning rate | ||
| + | # 로 곱해 준 값을 (두번째 단계의) a, b 값에서 | ||
| + | # 빼 준 값을 다음 단계의 a, b값으로 쓰고, 다시 | ||
| + | # . . . . . . 위의 loop문은 이것을 75번 반복한 것 | ||
| # scaled | # scaled | ||
| - | a | + | # 이렇게 해서 구한 제일 마지막 |
| - | b | + | # 표준화한 zx 변인을 사용해서 구한 값이므로 이 값을 |
| + | # 다시 x 를 사용했을 때의 값으로 환원을 해야 한다 (unscale). | ||
| + | data.frame(a, | ||
| # unscale coefficients to make them comprehensible | # unscale coefficients to make them comprehensible | ||
| Line 755: | Line 790: | ||
| # and | # and | ||
| # http:// | # http:// | ||
| - | # | ||
| a = a - (mean(x) / sd(x)) * b | a = a - (mean(x) / sd(x)) * b | ||
| b = b / sd(x) | b = b / sd(x) | ||
| - | a | + | cat(" unscaled |
| - | b | + | # 아래 lm 결과와 확인 |
| + | summary(mo) | ||
| - | # changes of estimators | + | # 아래는 a와 b를 구하는 과정에서 저장했던 temporary |
| + | # a, b 값들은 (as, bs 변인 내의) 모두 unscale한 것 | ||
| as <- as - (mean(x) /sd(x)) * bs | as <- as - (mean(x) /sd(x)) * bs | ||
| bs <- bs / sd(x) | bs <- bs / sd(x) | ||
| Line 768: | Line 804: | ||
| bs | bs | ||
| + | # 이것을 그래프로 나타내기 위해서 parameters 라는 | ||
| + | # 변인으로 저장 | ||
| + | # 이하 if 문들은 그래프에 처음 a, b값과 루프 문에서의 | ||
| + | # 그 값들이 변화하는 것을 모두 나타내기 위해서 사용한 | ||
| + | # 것으로 결과와 상관없다. | ||
| parameters <- data.frame(as, | parameters <- data.frame(as, | ||
| Line 828: | Line 869: | ||
| theme_classic() + | theme_classic() + | ||
| labs(title = ' | labs(title = ' | ||
| - | |||
| </ | </ | ||
| <tabbox ro01> | <tabbox ro01> | ||
| Line 842: | Line 882: | ||
| > # 다시 learning rate값을 곱하여 이를 다음의 a, b | > # 다시 learning rate값을 곱하여 이를 다음의 a, b | ||
| > # 값으로 사용한다. . . . 이를 반복한다. | > # 값으로 사용한다. . . . 이를 반복한다. | ||
| - | |||
| > a <- rnorm(1) | > a <- rnorm(1) | ||
| > b <- rnorm(1) | > b <- rnorm(1) | ||
| Line 865: | Line 904: | ||
| > | > | ||
| > # Train the model with scaled features | > # Train the model with scaled features | ||
| - | > learning.rate = 1e-1 | + | > learning.rate = 0.1 |
| > | > | ||
| > # Record Loss for each epoch: | > # Record Loss for each epoch: | ||
| > as = c() | > as = c() | ||
| > bs = c() | > bs = c() | ||
| + | > das = c() | ||
| + | > dbs = c() | ||
| + | > ep <- c() | ||
| + | > | ||
| > mses = c() | > mses = c() | ||
| > zx <- (x-mean(x))/ | > zx <- (x-mean(x))/ | ||
| > | > | ||
| - | > nlen <- 50 | + | > |
| + | > nlen <- 75 | ||
| > for (epoch in 1:nlen) { | > for (epoch in 1:nlen) { | ||
| + | + | ||
| - | + | + | + #residual <- residuals(predictions, |
| + loss <- mseloss(predictions, | + loss <- mseloss(predictions, | ||
| + mses <- append(mses, | + mses <- append(mses, | ||
| + | + | ||
| + grad <- gradient(zx, | + grad <- gradient(zx, | ||
| + | + db <- grad$b | ||
| + | + da <- grad$a | ||
| + | + | ||
| + | + dbs <- append(dbs, db) | ||
| + | + das <- append(das, da) | ||
| + | + ep <- append(ep, epoch) | ||
| + | + | ||
| + | + | ||
| + | + | ||
| Line 889: | Line 940: | ||
| + bs <- append(bs, b) | + bs <- append(bs, b) | ||
| + } | + } | ||
| - | > mses | + | > |
| - | [1] 13.5910678 | + | > tmp <- data.frame(ep, das, as, dbs, bs, mse) |
| - | [6] 1.5844012 | + | > tmp |
| - | [11] 0.2949490 | + | |
| - | [16] 0.1564681 | + | 1 1 -8.349964e+00 -0.1795060 6.595337e-01 0.4187294 18404982 |
| - | [21] 0.1415958 | + | 2 2 -6.679971e+00 |
| - | [26] 0.1399986 | + | 3 3 -5.343977e+00 |
| - | [31] 0.1398270 | + | 4 4 -4.275181e+00 |
| - | [36] 0.1398086 | + | 5 5 -3.420145e+00 |
| - | [41] 0.1398066 | + | 6 6 -2.736116e+00 |
| - | [46] 0.1398064 | + | 7 7 -2.188893e+00 |
| - | > as | + | 8 8 -1.751114e+00 |
| - | [1] 0.3157929 | + | 9 9 -1.400891e+00 |
| - | [7] 2.4725032 2.6257495 2.7483466 2.8464243 2.9248864 2.9876561 | + | 10 10 -1.120713e+00 |
| - | [13] 3.0378718 | + | 11 11 -8.965705e-01 |
| - | [19] 3.1860799 | + | 12 12 -7.172564e-01 |
| - | [25] 3.2249317 | + | 13 13 -5.738051e-01 |
| - | [31] 3.2351165 | + | 14 14 -4.590441e-01 |
| - | [37] 3.2377863 | + | 15 15 -3.672353e-01 |
| - | [43] 3.2384862 | + | 16 16 -2.937882e-01 |
| - | [49] 3.2386697 | + | 17 17 -2.350306e-01 |
| - | > bs | + | 18 18 -1.880245e-01 |
| - | [1] 0.32915377 | + | 19 19 -1.504196e-01 |
| - | [6] 0.15722177 0.14031795 0.12676108 | + | 20 20 -1.203357e-01 |
| - | [11] 0.10017533 0.09456670 0.09006858 | + | 21 21 -9.626853e-02 |
| - | [16] 0.08124752 0.07938660 0.07789414 | + | 22 22 -7.701482e-02 |
| - | [21] 0.07496734 0.07434989 0.07385470 | + | 23 23 -6.161186e-02 |
| - | [26] 0.07288360 0.07267873 | + | 24 24 -4.928949e-02 |
| - | [31] 0.07219222 0.07212425 0.07206973 0.07202601 0.07199095 | + | 25 25 -3.943159e-02 |
| - | [36] 0.07196282 0.07194027 0.07192218 0.07190768 0.07189604 | + | 26 26 -3.154527e-02 |
| - | [41] 0.07188671 0.07187923 0.07187323 0.07186841 0.07186455 | + | 27 27 -2.523622e-02 |
| - | [46] 0.07186146 0.07185897 0.07185698 0.07185539 0.07185410 | + | 28 28 -2.018897e-02 |
| + | 29 29 -1.615118e-02 | ||
| + | 30 30 -1.292094e-02 | ||
| + | 31 31 -1.033675e-02 | ||
| + | 32 32 -8.269403e-03 | ||
| + | 33 33 -6.615523e-03 | ||
| + | 34 34 -5.292418e-03 | ||
| + | 35 35 -4.233935e-03 | ||
| + | 36 36 -3.387148e-03 | ||
| + | 37 37 -2.709718e-03 | ||
| + | 38 38 -2.167774e-03 | ||
| + | 39 39 -1.734220e-03 | ||
| + | 40 40 -1.387376e-03 | ||
| + | 41 41 -1.109901e-03 | ||
| + | 42 42 -8.879204e-04 | ||
| + | 43 43 -7.103363e-04 | ||
| + | 44 44 -5.682691e-04 | ||
| + | 45 45 -4.546153e-04 | ||
| + | 46 46 -3.636922e-04 | ||
| + | 47 47 -2.909538e-04 | ||
| + | 48 48 -2.327630e-04 | ||
| + | 49 49 -1.862104e-04 | ||
| + | 50 50 -1.489683e-04 | ||
| + | 51 51 -1.191747e-04 | ||
| + | 52 52 -9.533973e-05 | ||
| + | 53 53 -7.627178e-05 | ||
| + | 54 54 -6.101743e-05 | ||
| + | 55 55 -4.881394e-05 | ||
| + | 56 56 -3.905115e-05 | ||
| + | 57 57 -3.124092e-05 | ||
| + | 58 58 -2.499274e-05 | ||
| + | 59 59 -1.999419e-05 | ||
| + | 60 60 -1.599535e-05 | ||
| + | 61 61 -1.279628e-05 | ||
| + | 62 62 -1.023703e-05 | ||
| + | 63 63 -8.189621e-06 | ||
| + | 64 64 -6.551696e-06 | ||
| + | 65 65 -5.241357e-06 | ||
| + | 66 66 -4.193086e-06 | ||
| + | 67 67 -3.354469e-06 | ||
| + | 68 68 -2.683575e-06 | ||
| + | 69 69 -2.146860e-06 | ||
| + | 70 70 -1.717488e-06 | ||
| + | 71 71 -1.373990e-06 | ||
| + | 72 72 -1.099192e-06 | ||
| + | 73 73 -8.793538e-07 | ||
| + | 74 74 -7.034830e-07 | ||
| + | 75 75 -5.627864e-07 | ||
| + | > | ||
| + | > # 위의 루프를 이해했는지 체크하기 | ||
| + | > a.init | ||
| + | [1] -1.014502 | ||
| + | > b.init | ||
| + | [1] 0.4846828 | ||
| + | > tmp.p <- predict(zx, a.init, b.init) | ||
| + | > # a.init b.init 일 때의 미분 결과 | ||
| + | > # 즉, 기울기와 절편 값 | ||
| + | > tmp.g <- gradient(zx, | ||
| + | > tmp.g | ||
| + | $b | ||
| + | [1] 0.6595337 | ||
| + | |||
| + | $a | ||
| + | [1] -8.349964 | ||
| + | |||
| + | > | ||
| + | > # 이 값에 learning rate 값을 곱한 후에 | ||
| + | > a.step <- tmp.g$a*learning.rate | ||
| + | > b.step <- tmp.g$b*learning.rate | ||
| + | > | ||
| + | > # 이를 원래 a값에서 빼준 값을 다음 순서의 | ||
| + | > a.init <- a.init-a.step | ||
| + | > b.init <- b.init-b.step | ||
| + | > # a, b값으로 쓰고, 이 때의 미분 값을 구한 후에 | ||
| + | > # (루프 안에서), 미분 결과를 다시 learning rate | ||
| + | > # 로 곱해 준 값을 (두번째 단계의) a, b 값에서 | ||
| + | > # 빼 준 값을 다음 단계의 a, b값으로 쓰고, 다시 | ||
| + | > # . . . . . . 위의 loop문은 이것을 75번 반복한 것 | ||
| > | > | ||
| > # scaled | > # scaled | ||
| - | > a | + | > # 이렇게 해서 구한 제일 마지막 |
| - | [1] 3.238683 | + | > # 표준화한 zx 변인을 사용해서 구한 값이므로 이 값을 |
| - | > b | + | > # 다시 x 를 사용했을 때의 값으로 환원을 해야 한다 (unscale). |
| - | [1] 0.0718541 | + | > data.frame(a, |
| + | | ||
| + | 1 3.160479 | ||
| > | > | ||
| > # unscale coefficients to make them comprehensible | > # unscale coefficients to make them comprehensible | ||
| Line 932: | Line 1062: | ||
| > # and | > # and | ||
| > # http:// | > # http:// | ||
| - | > # | ||
| > a = a - (mean(x) / sd(x)) * b | > a = a - (mean(x) / sd(x)) * b | ||
| > b = b / sd(x) | > b = b / sd(x) | ||
| - | > a | + | > cat(" unscaled |
| - | [1] 2.434234 | + | unscaled a: 1.614428 |
| - | > b | + | unscaled |
| - | [1] 0.00749967 | + | > # 아래 lm 결과와 확인 |
| + | > summary(mo) | ||
| + | |||
| + | Call: | ||
| + | lm(formula = y ~ x, data = sam) | ||
| + | |||
| + | Residuals: | ||
| + | | ||
| + | -0.92770 -0.20715 | ||
| + | |||
| + | Coefficients: | ||
| + | Estimate Std. Error t value Pr(> | ||
| + | (Intercept) | ||
| + | x | ||
| + | --- | ||
| + | Signif. codes: | ||
| + | |||
| + | Residual standard error: 0.3346 on 98 degrees of freedom | ||
| + | Multiple R-squared: | ||
| + | F-statistic: | ||
| > | > | ||
| - | > # changes of estimators | + | > # 아래는 a와 b를 구하는 과정에서 저장했던 temporary |
| + | > # a, b 값들은 (as, bs 변인 내의) 모두 unscale한 것 | ||
| > as <- as - (mean(x) /sd(x)) * bs | > as <- as - (mean(x) /sd(x)) * bs | ||
| > bs <- bs / sd(x) | > bs <- bs / sd(x) | ||
| > | > | ||
| > as | > as | ||
| - | [1] -3.36927396 | + | |
| - | [6] 0.52075655 | + | [9] 0.58772512 |
| - | [11] 1.80336556 | + | [17] 1.44059760 |
| - | [16] | + | [25] |
| - | [21] | + | [33] |
| - | [26] | + | [41] |
| - | [31] | + | [49] |
| - | [36] | + | [57] |
| - | [41] | + | [65] |
| - | [46] 2.43407700 | + | [73] 1.61442769 |
| > bs | > bs | ||
| - | [1] 0.034354957 | + | [1] 0.03998814 |
| - | [6] 0.016409799 | + | [10] 0.01797815 |
| - | [11] 0.010455658 | + | [19] 0.01495689 |
| - | [16] 0.008480095 | + | [28] 0.01454216 |
| - | [21] 0.007824610 | + | [37] 0.01448523 |
| - | [26] 0.007607122 | + | [46] 0.01447742 |
| - | [31] 0.007534961 | + | [55] 0.01447635 |
| - | [36] 0.007511018 | + | [64] 0.01447620 |
| - | [41] 0.007503073 | + | [73] 0.01447618 |
| - | [46] 0.007500438 | + | |
| > | > | ||
| + | > # 이것을 그래프로 나타내기 위해서 parameters 라는 | ||
| + | > # 변인으로 저장 | ||
| + | > # 이하 if 문들은 그래프에 처음 a, b값과 루프 문에서의 | ||
| + | > # 그 값들이 변화하는 것을 모두 나타내기 위해서 사용한 | ||
| + | > # 것으로 결과와 상관없다. | ||
| > parameters <- data.frame(as, | > parameters <- data.frame(as, | ||
| > | > | ||
| > cat(paste0(" | > cat(paste0(" | ||
| - | Intercept: | + | Intercept: |
| - | Slope: 0.00749967017274726 | + | Slope: 0.0144761780722265 |
| > summary(lm(y~x, | > summary(lm(y~x, | ||
| - | Estimate | + | |
| - | (Intercept) | + | (Intercept) |
| - | x 0.007499129 | + | x 0.01447618 |
| > | > | ||
| > if (as[nlen] > as[1]) { | > if (as[nlen] > as[1]) { | ||
| Line 995: | Line 1149: | ||
| + } | + } | ||
| > max(y) | > max(y) | ||
| - | [1] 4.090577 | + | [1] 3.878048 |
| > min(y) | > min(y) | ||
| - | [1] 2.185787 | + | [1] 2.092215 |
| > y.max | > y.max | ||
| - | [1] 4.090577 | + | [1] 3.878048 |
| > y.min | > y.min | ||
| - | [1] -3.369274 | + | [1] -4.450227 |
| > | > | ||
| > ggplot(sam, aes(x = x, y = y)) + | > ggplot(sam, aes(x = x, y = y)) + | ||
| Line 1027: | Line 1181: | ||
| Residuals: | Residuals: | ||
| | | ||
| - | -1.05708 -0.25025 -0.00149 0.17215 0.85818 | + | -0.92770 -0.20715 |
| Coefficients: | Coefficients: | ||
| Estimate Std. Error t value Pr(> | Estimate Std. Error t value Pr(> | ||
| - | (Intercept) | + | (Intercept) |
| - | x 0.007499 | + | x 0.014476 |
| --- | --- | ||
| Signif. codes: | Signif. codes: | ||
| - | Residual standard error: 0.3777 on 98 degrees of freedom | + | Residual standard error: 0.3346 on 98 degrees of freedom |
| - | Multiple R-squared: | + | Multiple R-squared: |
| - | F-statistic: | + | F-statistic: |
| > | > | ||
| > data.frame(head(as), | > data.frame(head(as), | ||
| - | head.as. | + | |
| - | 1 -3.36927396 | + | 1 -4.4502274 |
| - | 2 -2.21431158 | + | 2 -3.2427456 |
| - | 3 -1.28920093 | + | 3 -2.2756811 |
| - | 4 -0.54819753 | + | 4 -1.5011643 |
| - | 5 0.04533893 | + | 5 -0.8808568 |
| - | 6 0.52075655 | + | 6 -0.3840542 |
| > data.frame(tail(as), | > data.frame(tail(as), | ||
| - | tail.as. | + | tail.as. |
| - | 1 2.434011 | + | 1 1.614427 |
| - | 2 2.434077 | + | 2 1.614427 |
| - | 3 2.434130 | + | 3 1.614428 |
| - | 4 2.434173 | + | 4 1.614428 |
| - | 5 2.434207 | + | 5 1.614428 |
| - | 6 2.434234 | + | 6 1.614428 |
| > a | > a | ||
| - | [1] 2.434234 | + | [1] 1.614428 |
| > b | > b | ||
| - | [1] 0.00749967 | + | [1] 0.01447618 |
| > | > | ||
| > ggplot(sam, aes(x = x, y = y)) + | > ggplot(sam, aes(x = x, y = y)) + | ||
| Line 1071: | Line 1225: | ||
| + | + | ||
| + | + | ||
| - | > | + | > |
| </ | </ | ||
| - | {{pasted: | + | |
| - | {{pasted: | + | |
| </ | </ | ||
| - | {{pasted:20260506-041735.png?500}} | + | {{pasted:20260512-215110.png? |
| + | {{pasted: | ||
gradient_descent.1778622504.txt.gz · Last modified: by hkimscil
