Python || Local Minima

Local minima: 指的是在做梯度下降時,最佳點落在了局部最低處,而非 Global Minima。可以用 Momentum 來降低困在 Local Minima 的機率。

Ref from sebastianraschka

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
def FindLocalMinima(numbers):

minima = []
length = len(numbers)
if length >= 2:
if numbers[0] < numbers[1]:
minima.append(numbers.index(numbers[0]))

if length > 3:
for i in range(1, length-1):
if numbers[i] < numbers[i-1] and numbers[i] < numbers[i+1]:
minima.append(numbers.index(numbers[i]))

if numbers[length-1] < numbers[length-2]:
minima.append(numbers.index(numbers[length-1]))

return minima


numbers = [2, 7, 45, 3, 6, 98, 12, 67, 8]
FindLocalMinima(numbers)

[0, 3, 6, 8]

如果我們假定非字串而是隨機整數去尋找local minima

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
def FindLocalMinima(numbers):

minima = []
length = len(numbers)
if length >= 2:
if numbers[0] < numbers[1]:
minima.append(numbers[0])

if length > 3:
for i in range(1, length-1):
if numbers[i] < numbers[i-1] and numbers[i] < numbers[i+1]:
minima.append(numbers[i])

if numbers[length-1] < numbers[length-2]:
minima.append(numbers[length-

return minima

rand_ary = rd.sample(range(1,2000000),1000000)
print("million local minima:")
print(FindLocalMinima(rand_ary))
© 2022 Tiffany's Blog All Rights Reserved. 本站访客数人次 本站总访问量
Theme by hiero