Commit 1304c450 by Leo

upload code

parent 3d320be3
++ "b/\345\275\225\346\222\255/3-\346\225\260\346\215\256\345\244\204\347\220\206/.gitkeep"
import math
import math
# 定义问题
def question(ipt):
return ipt ** 2 + math.sin(5 * ipt)
# 找导数/梯度
def grad_func(ipt):
return 2 * ipt + 5 * math.cos(5 * ipt)
# 移步函数
def dealer(ipt_x, gradient, learning_rate):
# 打印用
print(f'current input: {ipt_x}')
print(f'current gradient: {gradient}')
print(f'current learning rate: {learning_rate}')
# 功能
# 走一步
ipt_x -= learning_rate * gradient
# 这一步好不好
y = question(ipt_x)
return ipt_x, y
# 超参 hyperparameter
x = float(input('start point: \n'))
lr = float(input('learning rate: \n'))
iteration_number = int(input('number of iterations: \n'))
extra_move = int(input('extra move after finding the local minimum: \n'))
start_lr = lr
# 局部最优
local_x = x
local_y = question(x)
# 保存找到的x和y
x_record = []
y_record = []
gradient_record = []
i = 0
while i < iteration_number:
# 你的extra move 走太多了,有点停不下来
# if i > iteration_number * 1.1:
# break
# 记录xy
x_record.append(x)
y_record.append(question(x))
# 算梯度
gradient = grad_func(x)
gradient_record.append(gradient)
# 对于梯度为0,局部最优的强行调整
if math.fabs(gradient) < 0.005:
local_x = x
local_y = question(x)
iteration_number += extra_move
if gradient > 0:
gradient += 1
x -= 1
else:
gradient -= 1
x += 1
lr = start_lr
# 走一步
x, current_y = dealer(x, gradient, lr)
if current_y < local_y:
local_x = x
local_y = current_y
# 如果当前找到了更好的,就减小步长,走小一点
lr *= 0.97
else:
# 如果,当前没有更好,则周围可能也没有特别好
lr *= 1.03
# if i % 10 == 0:
# lr *= 0.99
i += 1
print(f' The minimum found is: {local_y}')
import matplotlib.pyplot as plt
plt.plot(x_record, y_record)
plt.show()
plt.plot(range(0, iteration_number), x_record)
plt.show()
plt.plot(range(0, iteration_number), y_record)
plt.show()
plt.plot(range(0, iteration_number), gradient_record)
plt.show()
++ "b/\347\233\264\346\222\255/1-\347\254\254\344\270\200\351\230\266\346\256\265/1.1-AI\346\246\202\350\277\260/.gitkeep"
++ "b/\347\233\264\346\222\255/1-\347\254\254\344\270\200\351\230\266\346\256\265/1.2-AI+\347\224\237\345\214\226\347\216\257\346\235\220\350\277\233\345\261\225/.gitkeep"
++ "b/\347\233\264\346\222\255/1-\347\254\254\344\270\200\351\230\266\346\256\265/1.3-AI+\347\247\221\347\240\224\345\210\233\346\226\260-AI+\347\224\237\345\214\226\347\216\257\346\235\220\351\200\211\351\242\230/.gitkeep"
++ "b/\347\233\264\346\222\255/1-\347\254\254\344\270\200\351\230\266\346\256\265/1.4-AI+\347\247\221\347\240\224\345\210\233\346\226\260-\347\224\237\345\214\226\347\216\257\346\235\220\345\255\246\347\247\221\347\232\204\346\225\260\346\215\256\351\233\206\345\244\204\347\220\206/.gitkeep"
++ "b/\347\233\264\346\222\255/1-\347\254\254\344\270\200\351\230\266\346\256\265/1.5-\347\273\217\345\205\270\345\272\224\347\224\250-\347\273\217\345\205\270\346\234\272\345\231\250\345\255\246\344\271\240\346\250\241\345\236\213+\345\210\206\347\261\273\351\227\256\351\242\230/.gitkeep"
++ "b/\347\233\264\346\222\255/1-\347\254\254\344\270\200\351\230\266\346\256\265/1.6-\347\273\217\345\205\270\345\272\224\347\224\250-\347\273\217\345\205\270\346\234\272\345\231\250\345\255\246\344\271\240\346\250\241\345\236\213+\345\233\236\345\275\222\351\227\256\351\242\230/.gitkeep"
++ "b/\347\233\264\346\222\255/1-\347\254\254\344\270\200\351\230\266\346\256\265/1.7-\347\273\217\345\205\270\345\272\224\347\224\250-\350\277\233\351\230\266\346\234\272\345\231\250\345\255\246\344\271\240\346\250\241\345\236\213+\345\210\206\347\261\273\351\227\256\351\242\230/.gitkeep"
++ "b/\347\233\264\346\222\255/2-\347\254\254\344\272\214\351\230\266\346\256\265/2.1-AI+\347\247\221\347\240\224\345\210\233\346\226\260-\347\224\237\345\214\226\347\216\257\346\235\220\346\225\260\346\215\256\347\232\204\347\211\271\345\276\201\346\217\220\345\217\226/.gitkeep"
++ "b/\347\233\264\346\222\255/2-\347\254\254\344\272\214\351\230\266\346\256\265/2.2-\347\273\217\345\205\270\345\272\224\347\224\250-\347\245\236\347\273\217\347\275\221\347\273\234\346\250\241\345\236\213+\347\224\237\345\214\226\347\216\257\346\235\220/.gitkeep"
++ "b/\347\233\264\346\222\255/2-\347\254\254\344\272\214\351\230\266\346\256\265/2.3-AI+\347\247\221\347\240\224\345\210\233\346\226\260-AI+\347\224\237\345\214\226\347\216\257\346\235\220\345\256\236\351\252\214\350\256\276\350\256\241/.gitkeep"
++ "b/\347\233\264\346\222\255/2-\347\254\254\344\272\214\351\230\266\346\256\265/2.4-\347\273\217\345\205\270\345\272\224\347\224\250AI+\347\247\221\347\240\224\345\210\233\346\226\260-AI+\347\224\237\347\211\251\345\255\246\347\247\221/.gitkeep"
++ "b/\347\233\264\346\222\255/2-\347\254\254\344\272\214\351\230\266\346\256\265/2.5-\347\273\217\345\205\270\345\272\224\347\224\250AI+\347\247\221\347\240\224\345\210\233\346\226\260-AI+\345\214\226\345\255\246\345\255\246\347\247\221/.gitkeep"
++ "b/\347\233\264\346\222\255/2-\347\254\254\344\272\214\351\230\266\346\256\265/2.6-\347\273\217\345\205\270\345\272\224\347\224\250AI+\347\247\221\347\240\224\345\210\233\346\226\260-AI+\347\216\257\345\242\203\345\255\246\347\247\221/.gitkeep"
++ "b/\347\233\264\346\222\255/2-\347\254\254\344\272\214\351\230\266\346\256\265/2.7-\347\273\217\345\205\270\345\272\224\347\224\250AI+\347\247\221\347\240\224\345\210\233\346\226\260-AI+\346\235\220\346\226\231\345\255\246\347\247\221/.gitkeep"
++ "b/\347\233\264\346\222\255/2-\347\254\254\344\272\214\351\230\266\346\256\265/2.8-AI+\347\247\221\347\240\224\345\210\233\346\226\260-AI+\347\273\223\346\236\234\345\210\206\346\236\220/.gitkeep"
++ "b/\347\233\264\346\222\255/3-\347\254\254\344\270\211\351\230\266\346\256\265/3.1-\347\247\221\347\240\224\350\256\272\346\226\207\346\241\206\346\236\266\343\200\201\345\267\245\345\205\267/.gitkeep"
++ "b/\347\233\264\346\222\255/3-\347\254\254\344\270\211\351\230\266\346\256\265/3.2-\347\247\221\347\240\224\350\256\272\346\226\207\345\206\231\344\275\234-\345\274\225\350\250\200/.gitkeep"
++ "b/\347\233\264\346\222\255/3-\347\254\254\344\270\211\351\230\266\346\256\265/3.3-\347\247\221\347\240\224\350\256\272\346\226\207\345\206\231\344\275\234-\346\226\271\346\263\225\343\200\201\347\273\223\346\236\234/.gitkeep"
++ "b/\347\233\264\346\222\255/3-\347\254\254\344\270\211\351\230\266\346\256\265/3.4-\347\247\221\347\240\224\350\256\272\346\226\207\345\206\231\344\275\234-\351\242\230\347\233\256\343\200\201\346\221\230\350\246\201\343\200\201\350\256\250\350\256\272/.gitkeep"
++ "b/\347\233\264\346\222\255/3-\347\254\254\344\270\211\351\230\266\346\256\265/3.5-\347\247\221\347\240\224\350\256\272\346\226\207\345\206\231\344\275\234-\346\227\266\346\200\201\343\200\201\350\277\236\346\216\245\350\257\215/.gitkeep"
++ "b/\347\233\264\346\222\255/3-\347\254\254\344\270\211\351\230\266\346\256\265/3.6-\347\247\221\347\240\224\350\256\272\346\226\207\351\200\211\345\210\212\343\200\201\346\212\225\347\250\277/.gitkeep"
++ "b/\347\233\264\346\222\255/3-\347\254\254\344\270\211\351\230\266\346\256\265/3.7-\347\247\221\347\240\224\350\256\272\346\226\207\350\277\224\344\277\256\343\200\201\346\240\241\347\250\277/.gitkeep"
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment