-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGradient Descent.go
More file actions
68 lines (56 loc) · 1.49 KB
/
Copy pathGradient Descent.go
File metadata and controls
68 lines (56 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package main
import (
"fmt"
"math"
)
type Gradient struct {
f func(float64) float64 // 目標函數
x float64 // 初始值
h float64 // 一個很小的步長
learningRate float64 // 學習率
toleranceError float64 // 容忍誤差
iterate int // 最大迭代次數
}
// 定義f(x)
func f(x float64) float64 {
return x * x - 2
}
// 計算目標函數的導數,使用中心差商法
func centralDifference(f func(float64) float64, x float64, h float64) float64 {
return (f(x + h) - f(x - h)) / (2 * h)
}
// 計算最佳解
func updateGradient(gradient *Gradient) {
// 使用梯度下降求解
for i := 0; i < gradient.iterate; i++ {
// 計算f'(x)
dfx := centralDifference(gradient.f, gradient.x, gradient.h)
// 計算新的x
gradient.x = gradient.x - gradient.learningRate * dfx
// 檢查收斂條件
if math.Abs(dfx) < gradient.toleranceError {
break
}
}
}
// 初始化Gradient會使用到的參數
func initializeGradient(f func(float64) float64, x float64, h float64, learningRate float64, toleranceError float64, iterate int) *Gradient {
gradient := &Gradient {
f: f,
x: x,
h: h,
learningRate: learningRate,
toleranceError: toleranceError,
iterate: iterate,
}
return gradient
}
func main() {
// 初始化梯度下降的參數
gradient := initializeGradient(f, 1.0, 1e-6, 0.03, 1e-6, 2000)
// 使用梯度下降求解
updateGradient(gradient)
// 輸出結果
fmt.Printf("梯度下降求得的解: %v\n", gradient.x)
fmt.Printf("理論解: %v\n", 0)
}