這是我關于這個問題的第二個問題。最初,我遇到了AttributeError錯誤:'numpy.ndarray'物件沒有屬性'log'。然后U12-Forward幫助我解決了這個問題。但是又出現了一個新的問題。
import torch
import numpy as np
import matplotlib.pyplot as plt
x = torch.tensor([[5., 10.],
[1., 2.]], requires_grad=True)
var_history = []
fn_history = []
alpha =0.001
優化器 = torch.optimation.SGD([x], lr=alpha)
def function_parabola(variable)。
return np.prod(np.log(np.log(variable 7) )
def make_gradient_step(function, variable) 。
function_result = function(variable)
function_result.backward()
優化器.步驟()
優化器.zero_grad()
for i in range(500)。
var_history.append(x.data.numpy().copy())
fn_history.append(function_parabola(x).data.cpu().detach().numpy().copy()
make_gradient_step(function_parabola, x)
print(x)
def show_contours(objective,
x_lims=[-10.0, 10.0] 。
y_lims=[-10.0, 10.0]。
x_ticks=100,
y_ticks=100)。)
x_step = (x_lims[1] - x_lims[0] ) / x_ticks
y_step = (y_lims[1] - y_lims[0] ) / y_ticks
X, Y = np.mgrid[x_lims[0]:x_lims[1] :x_step, y_lims[0]:y_lims[1] :y_step]
res = []
for x_index in range(X.shape[0)。
res.append([])
for y_index in range(X.shape[1]) 。
x_val = X[x_index, y_index] 。
y_val = Y[x_index, y_index].
res[-1].append(objective(np.array([x_val, y_val]).T))
res = np.array(res)
plt.figure(figsize=(7,7)
plt.contour(X, Y, res, 100)
plt.xlabel('$x_1$')
plt.ylabel('$x_2$')
show_contours(function_parabola)
plt.scatter(np.array(var_history)[:,0], np.array(var_history)[:, 1], s=10, c='r') 。
plt.show()
uj5u.com熱心網友回復:
修改function_parabola()以操作PyTorch張量,并利用PyTorch等同于原來的numpy操作,像這樣:
import Torch
import numpy as np
import matplotlib.pyplot as plt
x = torch.tensor([[5., 10.],
[1., 2.]], requires_grad=True)
var_history = []
fn_history = []
alpha =0.001
優化器 = torch.optimation.SGD([x], lr=alpha)
def function_parabola(variable)。
return (torch.prod(torch.log(torch.as_tensor(variable 7))))))
def make_gradient_step(function, variable) 。
function_result = function(variable)
function_result.backward()
優化器.步驟()
優化器.zero_grad()
for i in range(500)。
var_history.append(x.data.numpy().copy())
fn_history.append(function_parabola(x).data.cpu().detach().numpy())
make_gradient_step(function_parabola, x)
print(x)
def show_contours(objective,
x_lims=[-10.0, 10.0] 。
y_lims=[-10.0, 10.0]。
x_ticks=100,
y_ticks=100)。)
x_step = (x_lims[1] - x_lims[0] ) / x_ticks
y_step = (y_lims[1] - y_lims[0] ) / y_ticks
X, Y = np.mgrid[x_lims[0]:x_lims[1] :x_step, y_lims[0]:y_lims[1] :y_step]
res = []
for x_index in range(X.shape[0)。
res.append([])
for y_index in range(X.shape[1]) 。
x_val = X[x_index, y_index] 。
y_val = Y[x_index, y_index].
res[-1].append(objective(np.array([x_val, y_val]).T))
res = np.array(res)
plt.figure(figsize=(7,7)
plt.contour(X, Y, res, 100)
plt.xlabel('$x_1$')
plt.ylabel('$x_2$')
show_contours(function_parabola)
plt.scatter(np.array(var_history)[:,0], np.array(var_history)[:, 1], s=10, c='r') 。
plt.show()
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/309445.html
標籤:
下一篇:如何用python數值解差分方程
