为什么我非常简单的神经网络没有产生正确的结果?

发布于 2025-02-07 05:14:09 字数 2900 浏览 1 评论 0原文

我创建了一个非常简单的神经网络来求解以下ode:

“

我的代码似乎不正常,这很奇怪,因为颂歌很简单。以下是我的评论代码。我相信这个问题来自torch.gradient(preds),但我无法弄清楚。任何帮助将不胜感激!

inputs = np.linspace(0,2,50,dtype='float32')
inputs = torch.from_numpy(inputs)
inputs.requires_grad = True
inputs.float()

# Initial Condition
A = 0.0


# Known trial solution for general ODE 
trial_solution = lambda I: A + I * NN(I)


ODE = lambda x: x


# Numbers of neurons from layers 1 to 3 
neuron_1 = 9
neuron_2 = 12
neuron_3 = 8

# Initializing the weights and biases 
W1 = torch.randn(len(inputs), neuron_1, requires_grad=True).float()
B1 = torch.randn(neuron_1, requires_grad=True).float()

W2 = torch.randn(neuron_1, neuron_2, requires_grad=True)
B2 = torch.randn(neuron_2, requires_grad=True)

W3 = torch.randn(neuron_2, neuron_3, requires_grad=True)
B3 = torch.randn(neuron_3, requires_grad=True)

W4 = torch.randn(neuron_3, len(inputs), requires_grad=True)
B4 = torch.randn(len(inputs), requires_grad=True)

# General sigmoid function used for neurons inside hidden layers
def Sigmoid(z):
    return 1/(1+torch.exp(-z))


# Defining the structure of my Neural Network
def NN(x):
    z1 = W1.t() @ x  + B1
    z2 = Sigmoid(z1)
    z3 = W2.t() @ z2 + B2
    z4 = Sigmoid(z3)
    z5 = W3.t() @ z4 + B3
    z6 = Sigmoid(z5)
    return W4.t() @ z6 + B4

# Defining the mean squared error to be my loss function
def mse(t1,t2):
    diff = t1-t2
    return torch.sum(diff*diff)/diff.numel()

# Iterating over number of epochs for backprop. 
for i in range(10000):
    preds = trial_solution(inputs)
    preds_gradient = torch.gradient(preds) # Possible problem!! 
    preds_gradient =preds_gradient[0]
    target_gradient = ODE(inputs)
    loss = mse(preds_gradient,target_gradient)
    loss.backward()
    #print(loss)
    with torch.no_grad():
        W1 -= W1.grad*1e-2
        B1 -= B1.grad*1e-2
        W1.grad.zero_()
        B1.grad.zero_()
        
        W2 -= W2.grad*1e-2
        B2 -= B2.grad*1e-2
        W2.grad.zero_()
        B2.grad.zero_()
        
        W3 -= W3.grad*1e-2
        B3 -= B3.grad*1e-2
        W3.grad.zero_()
        B3.grad.zero_()
        
        W4 -= W4.grad*1e-2
        B4 -= B4.grad*1e-2
        W4.grad.zero_()
        B4.grad.zero_()
        
        



numpy_tensors = np.linspace(0,2,50,dtype='float32')
numpy_tensors = torch.from_numpy(numpy_tensors)


final_pred = trial_solution(inputs).detach().numpy()


xx = np.linspace(0,2,50)
yt = xx*xx * 0.5
plt.plot(numpy_tensors,final_pred, label='Prediction from NN')
plt.plot(xx,yt,label = 'True Solution')
plt.legend()

I have created a very simple neural network to solve the following ODE:

Text

My code doesn't seem to work well, which is pretty weird since the ODE is simple. Below is my code with comments. I believe the problem is coming from torch.gradient(preds) but I can't figure it out. Any help would be greatly appreciated!

inputs = np.linspace(0,2,50,dtype='float32')
inputs = torch.from_numpy(inputs)
inputs.requires_grad = True
inputs.float()

# Initial Condition
A = 0.0


# Known trial solution for general ODE 
trial_solution = lambda I: A + I * NN(I)


ODE = lambda x: x


# Numbers of neurons from layers 1 to 3 
neuron_1 = 9
neuron_2 = 12
neuron_3 = 8

# Initializing the weights and biases 
W1 = torch.randn(len(inputs), neuron_1, requires_grad=True).float()
B1 = torch.randn(neuron_1, requires_grad=True).float()

W2 = torch.randn(neuron_1, neuron_2, requires_grad=True)
B2 = torch.randn(neuron_2, requires_grad=True)

W3 = torch.randn(neuron_2, neuron_3, requires_grad=True)
B3 = torch.randn(neuron_3, requires_grad=True)

W4 = torch.randn(neuron_3, len(inputs), requires_grad=True)
B4 = torch.randn(len(inputs), requires_grad=True)

# General sigmoid function used for neurons inside hidden layers
def Sigmoid(z):
    return 1/(1+torch.exp(-z))


# Defining the structure of my Neural Network
def NN(x):
    z1 = W1.t() @ x  + B1
    z2 = Sigmoid(z1)
    z3 = W2.t() @ z2 + B2
    z4 = Sigmoid(z3)
    z5 = W3.t() @ z4 + B3
    z6 = Sigmoid(z5)
    return W4.t() @ z6 + B4

# Defining the mean squared error to be my loss function
def mse(t1,t2):
    diff = t1-t2
    return torch.sum(diff*diff)/diff.numel()

# Iterating over number of epochs for backprop. 
for i in range(10000):
    preds = trial_solution(inputs)
    preds_gradient = torch.gradient(preds) # Possible problem!! 
    preds_gradient =preds_gradient[0]
    target_gradient = ODE(inputs)
    loss = mse(preds_gradient,target_gradient)
    loss.backward()
    #print(loss)
    with torch.no_grad():
        W1 -= W1.grad*1e-2
        B1 -= B1.grad*1e-2
        W1.grad.zero_()
        B1.grad.zero_()
        
        W2 -= W2.grad*1e-2
        B2 -= B2.grad*1e-2
        W2.grad.zero_()
        B2.grad.zero_()
        
        W3 -= W3.grad*1e-2
        B3 -= B3.grad*1e-2
        W3.grad.zero_()
        B3.grad.zero_()
        
        W4 -= W4.grad*1e-2
        B4 -= B4.grad*1e-2
        W4.grad.zero_()
        B4.grad.zero_()
        
        



numpy_tensors = np.linspace(0,2,50,dtype='float32')
numpy_tensors = torch.from_numpy(numpy_tensors)


final_pred = trial_solution(inputs).detach().numpy()


xx = np.linspace(0,2,50)
yt = xx*xx * 0.5
plt.plot(numpy_tensors,final_pred, label='Prediction from NN')
plt.plot(xx,yt,label = 'True Solution')
plt.legend()

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文