<jupyter_start><jupyter_text>$f(x,y) = (x^2+y-11)^2+(x+y^2-7)^2$<jupyter_code>def himmelblau(x):
    return (x[0] ** 2 + x[1] - 11) ** 2 + (x[0] + x[1] ** 2 - 7) ** 2
x = np.arange(-6,6,0.1)
y = np.arange(-6,6,0.1)
print('x,y range:',x.shape,y.shape)
X,Y = np.meshgrid(x,y)
print('X,Y maps:',X.shape,Y.shape)
Z = himmelblau([X,Y])

fig = plt.figure('himmelblau')
ax = fig.gca(projection = '3d')
ax.plot_surface(X,Y,Z)
ax.view_init(60,-30)
ax.set_xlabel('x')
ax.set_ylabel('y')
plt.show()
x = torch.tensor([0.,0.],requires_grad = True)
optimizer = torch.optim.Adam([x],lr = 1e-3)
for step in range(20000):
    pred = himmelblau(x)
    optimizer.zero_grad()
    pred.backward()
    
    # x' = x - lr * delta
    # y' = y - lr * delta
    optimizer.step()
    
    if step % 2000 ==0:
        print(f"step {step}: x = {x.tolist()} f(x) = {pred.item()}")
x = torch.tensor([4.,0.],requires_grad = True)
optimizer = torch.optim.Adam([x],lr = 1e-3)
for step in range(20000):
    pred = himmelblau(x)
    optimizer.zero_grad()
    pred.backward()
    
    # x' = x - lr * delta
    # y' = y - lr * delta
    optimizer.step()
    
    if step % 2000 ==0:
        print(f"step {step}: x = {x.tolist()} f(x) = {pred.item()}")
x = torch.tensor([-4.,0.],requires_grad = True)
optimizer = torch.optim.Adam([x],lr = 1e-3)
for step in range(20000):
    pred = himmelblau(x)
    optimizer.zero_grad()
    pred.backward()
    
    # x' = x - lr * delta
    # y' = y - lr * delta
    optimizer.step()
    
    if step % 2000 ==0:
        print(f"step {step}: x = {x.tolist()} f(x) = {pred.item()}")<jupyter_output>step 0: x = [-3.999000072479248, -0.0009999999310821295] f(x) = 146.0
step 2000: x = [-3.526559829711914, -2.5002429485321045] f(x) = 19.4503231048584
step 4000: x = [-3.777446746826172, -3.2777843475341797] f(x) = 0.0012130826944485307
step 6000: x = [-3.7793045043945312, -3.283174753189087] f(x) = 5.636138666886836e-09
step 8000: x = [-3.779308319091797, -3.28318190574646] f(x) = 7.248672773130238e-10
step 10000: x = [-3.7793095111846924, -3.28318452835083] f(x) = 8.822098607197404e-11
step 12000: x = [-3.7793102264404297, -3.2831854820251465] f(x) = 8.185452315956354e-12
step 14000: x = [-3.7793102264404297, -3.2831859588623047] f(x) = 0.0
step 16000: x = [-3.7793102264404297, -3.2831859588623047] f(x) = 0.0
step 18000: x = [-3.7793102264404297, -3.2831859588623047] f(x) = 0.0
{'fraction_non_alphanumeric': 0.2041072925398156, 'fraction_numerical': 0.2372170997485331, 'mean_word_length': 3.347905282331512, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '378199', 'license': 'MIT', 'n_tokens_mistral': 1447, 'n_tokens_neox': 1122, 'n_words': 204, 'path': 'Pytorch/Pytorch_learn_by_dragen1860/lesson20-函数优化实例.ipynb', 'repo_name': 'tsuirak/skills', 'size': '2386'}