栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

人工智能-作业3:例题程序复现 PyTorch版

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

人工智能-作业3:例题程序复现 PyTorch版

PyTorch版代码,这里使用老师给的参考代码
# https://blog.csdn.net/qq_41033011/article/details/109325070
# https://github.com/Darwlr/Deep_learning/blob/master/06%20Pytorch%E5%AE%9E%E7%8E%B0%E5%8F%8D%E5%90%91%E4%BC%A0%E6%92%AD.ipynb
# torch.nn.Sigmoid(h_in)
 
import torch
 
x1, x2 = torch.Tensor([0.5]), torch.Tensor([0.3])
y1, y2 = torch.Tensor([0.23]), torch.Tensor([-0.07])
print("=====输入值:x1, x2;真实输出值:y1, y2=====")
print(x1, x2, y1, y2)
w1, w2, w3, w4, w5, w6, w7, w8 = torch.Tensor([0.2]), torch.Tensor([-0.4]), torch.Tensor([0.5]), torch.Tensor(
    [0.6]), torch.Tensor([0.1]), torch.Tensor([-0.5]), torch.Tensor([-0.3]), torch.Tensor([0.8])  # 权重初始值
w1.requires_grad = True
w2.requires_grad = True
w3.requires_grad = True
w4.requires_grad = True
w5.requires_grad = True
w6.requires_grad = True
w7.requires_grad = True
w8.requires_grad = True
 
 
def sigmoid(z):
    a = 1 / (1 + torch.exp(-z))
    return a
 
 
def forward_propagate(x1, x2):
    in_h1 = w1 * x1 + w3 * x2
    out_h1 = sigmoid(in_h1)  # out_h1 = torch.sigmoid(in_h1)
    in_h2 = w2 * x1 + w4 * x2
    out_h2 = sigmoid(in_h2)  # out_h2 = torch.sigmoid(in_h2)
 
    in_o1 = w5 * out_h1 + w7 * out_h2
    out_o1 = sigmoid(in_o1)  # out_o1 = torch.sigmoid(in_o1)
    in_o2 = w6 * out_h1 + w8 * out_h2
    out_o2 = sigmoid(in_o2)  # out_o2 = torch.sigmoid(in_o2)
 
    print("正向计算:o1 ,o2")
    print(out_o1.data, out_o2.data)
 
    return out_o1, out_o2
 
 
def loss_fuction(x1, x2, y1, y2):  # 损失函数
    y1_pred, y2_pred = forward_propagate(x1, x2)  # 前向传播
    loss = (1 / 2) * (y1_pred - y1) ** 2 + (1 / 2) * (y2_pred - y2) ** 2  # 考虑 : t.nn.MSELoss()
    print("损失函数(均方误差):", loss.item())
    return loss
 
 
def update_w(w1, w2, w3, w4, w5, w6, w7, w8):
    # 步长
    step = 1
    w1.data = w1.data - step * w1.grad.data
    w2.data = w2.data - step * w2.grad.data
    w3.data = w3.data - step * w3.grad.data
    w4.data = w4.data - step * w4.grad.data
    w5.data = w5.data - step * w5.grad.data
    w6.data = w6.data - step * w6.grad.data
    w7.data = w7.data - step * w7.grad.data
    w8.data = w8.data - step * w8.grad.data
    w1.grad.data.zero_()  # 注意:将w中所有梯度清零
    w2.grad.data.zero_()
    w3.grad.data.zero_()
    w4.grad.data.zero_()
    w5.grad.data.zero_()
    w6.grad.data.zero_()
    w7.grad.data.zero_()
    w8.grad.data.zero_()
    return w1, w2, w3, w4, w5, w6, w7, w8
 
 
if __name__ == "__main__":
 
    print("=====更新前的权值=====")
    print(w1.data, w2.data, w3.data, w4.data, w5.data, w6.data, w7.data, w8.data)
 
    for i in range(11):
        print("=====第" + str(i) + "轮=====")
        L = loss_fuction(x1, x2, y1, y2) # 前向传播,求 Loss,构建计算图
        L.backward()  # 自动求梯度,不需要人工编程实现。反向传播,求出计算图中所有梯度存入w中
        print("tgrad W: ", round(w1.grad.item(), 2), round(w2.grad.item(), 2), round(w3.grad.item(), 2),
              round(w4.grad.item(), 2), round(w5.grad.item(), 2), round(w6.grad.item(), 2), round(w7.grad.item(), 2),
              round(w8.grad.item(), 2))
        w1, w2, w3, w4, w5, w6, w7, w8 = update_w(w1, w2, w3, w4, w5, w6, w7, w8)
 
    print("更新后的权值")
    print(w1.data, w2.data, w3.data, w4.data, w5.data, w6.data, w7.data, w8.data)

#运行结果

C:p_pycharmvtScriptspython.exe C:/p_pycharm/SVM.py
=====输入值:x1, x2;真实输出值:y1, y2=====
tensor([0.5000]) tensor([0.3000]) tensor([0.2300]) tensor([-0.0700])
=====更新前的权值=====
tensor([0.2000]) tensor([-0.4000]) tensor([0.5000]) tensor([0.6000]) tensor([0.1000]) tensor([-0.5000]) tensor([-0.3000]) tensor([0.8000])
=====第0轮=====
正向计算:o1 ,o2
tensor([0.4769]) tensor([0.5287])
损失函数(均方误差): 0.2097097933292389
	grad W:  -0.01 0.01 -0.01 0.01 0.03 0.08 0.03 0.07
=====第1轮=====
正向计算:o1 ,o2
tensor([0.4685]) tensor([0.5072])
损失函数(均方误差): 0.19503259658813477
	grad W:  -0.01 0.01 -0.01 0.01 0.03 0.08 0.03 0.07
=====第2轮=====
正向计算:o1 ,o2
tensor([0.4604]) tensor([0.4864])
损失函数(均方误差): 0.1813509315252304
	grad W:  -0.01 0.01 -0.01 0.01 0.03 0.08 0.03 0.07
=====第3轮=====
正向计算:o1 ,o2
tensor([0.4526]) tensor([0.4664])
损失函数(均方误差): 0.16865134239196777
	grad W:  -0.01 0.01 -0.01 0.0 0.03 0.08 0.03 0.07
=====第4轮=====
正向计算:o1 ,o2
tensor([0.4451]) tensor([0.4473])
损失函数(均方误差): 0.15690487623214722
	grad W:  -0.01 0.01 -0.01 0.0 0.03 0.07 0.03 0.06
=====第5轮=====
正向计算:o1 ,o2
tensor([0.4378]) tensor([0.4290])
损失函数(均方误差): 0.14607082307338715
	grad W:  -0.01 0.0 -0.01 0.0 0.03 0.07 0.02 0.06
=====第6轮=====
正向计算:o1 ,o2
tensor([0.4307]) tensor([0.4116])
损失函数(均方误差): 0.1361003816127777
	grad W:  -0.01 0.0 -0.01 0.0 0.03 0.07 0.02 0.06
=====第7轮=====
正向计算:o1 ,o2
tensor([0.4239]) tensor([0.3951])
损失函数(均方误差): 0.1269397884607315
	grad W:  -0.01 0.0 -0.01 0.0 0.03 0.06 0.02 0.05
=====第8轮=====
正向计算:o1 ,o2
tensor([0.4173]) tensor([0.3794])
损失函数(均方误差): 0.11853282898664474
	grad W:  -0.01 0.0 -0.01 0.0 0.03 0.06 0.02 0.05
=====第9轮=====
正向计算:o1 ,o2
tensor([0.4109]) tensor([0.3647])
损失函数(均方误差): 0.11082295328378677
	grad W:  -0.02 0.0 -0.01 0.0 0.03 0.06 0.02 0.05
=====第10轮=====
正向计算:o1 ,o2
tensor([0.4047]) tensor([0.3507])
损失函数(均方误差): 0.10375461727380753
	grad W:  -0.02 -0.0 -0.01 -0.0 0.02 0.06 0.02 0.05
更新后的权值
tensor([0.3425]) tensor([-0.4540]) tensor([0.5855]) tensor([0.5676]) tensor([-0.2230]) tensor([-1.2686]) tensor([-0.5765]) tensor([0.1418])

Process finished with exit code 0


通过对比结果发现,两次运行的结果给不同。并且PyTorch版更加接近正确的结果

作业二是用链式求导法则手动计算反向传播过程中各参数梯度,作业三是用PyTorch的Tensor这种数据结构,在前向传播中动态构建计算图自动求导,效率比链式求导高很多。

激活函数Sigmoid用PyTorch自带函数torch.sigmoid() 原函数:
def sigmoid(z):
    a = 1 / (1 + torch.exp(-z))
    return a


def forward_propagate(x1, x2):
    in_h1 = w1 * x1 + w3 * x2
    out_h1 = sigmoid(in_h1)  # out_h1 = torch.sigmoid(in_h1)
    in_h2 = w2 * x1 + w4 * x2
    out_h2 = sigmoid(in_h2)  # out_h2 = torch.sigmoid(in_h2)

    in_o1 = w5 * out_h1 + w7 * out_h2
    out_o1 = sigmoid(in_o1)  # out_o1 = torch.sigmoid(in_o1)
    in_o2 = w6 * out_h1 + w8 * out_h2
    out_o2 = sigmoid(in_o2)  # out_o2 = torch.sigmoid(in_o2)

    print("正向计算:o1 ,o2")
    print(out_o1.data, out_o2.data)

    return out_o1, out_o2


替换后的函数

def forward_propagate(x1, x2):
    in_h1 = w1 * x1 + w3 * x2
    out_h1 = torch.sigmoid(in_h1)
    in_h2 = w2 * x1 + w4 * x2
    out_h2 = torch.sigmoid(in_h2)

    in_o1 = w5 * out_h1 + w7 * out_h2
    out_o1 = torch.sigmoid(in_o1)
    in_o2 = w6 * out_h1 + w8 * out_h2
    out_o2 = torch.sigmoid(in_o2)

    print("正向计算:o1 ,o2")
    print(out_o1.data, out_o2.data)

    return out_o1, out_o2


运行结果

C:p_pycharmvtScriptspython.exe C:/p_pycharm/SVM.py
=====输入值:x1, x2;真实输出值:y1, y2=====
tensor([0.5000]) tensor([0.3000]) tensor([0.2300]) tensor([-0.0700])
=====更新前的权值=====
tensor([0.2000]) tensor([-0.4000]) tensor([0.5000]) tensor([0.6000]) tensor([0.1000]) tensor([-0.5000]) tensor([-0.3000]) tensor([0.8000])
=====第0轮=====
正向计算:o1 ,o2
tensor([0.4769]) tensor([0.5287])
损失函数(均方误差): 0.2097097933292389
	grad W:  -0.01 0.01 -0.01 0.01 0.03 0.08 0.03 0.07
=====第1轮=====
正向计算:o1 ,o2
tensor([0.4685]) tensor([0.5072])
损失函数(均方误差): 0.19503259658813477
	grad W:  -0.01 0.01 -0.01 0.01 0.03 0.08 0.03 0.07
=====第2轮=====
正向计算:o1 ,o2
tensor([0.4604]) tensor([0.4864])
损失函数(均方误差): 0.1813509315252304
	grad W:  -0.01 0.01 -0.01 0.01 0.03 0.08 0.03 0.07
=====第3轮=====
正向计算:o1 ,o2
tensor([0.4526]) tensor([0.4664])
损失函数(均方误差): 0.16865134239196777
	grad W:  -0.01 0.01 -0.01 0.0 0.03 0.08 0.03 0.07
=====第4轮=====
正向计算:o1 ,o2
tensor([0.4451]) tensor([0.4473])
损失函数(均方误差): 0.15690487623214722
	grad W:  -0.01 0.01 -0.01 0.0 0.03 0.07 0.03 0.06
=====第5轮=====
正向计算:o1 ,o2
tensor([0.4378]) tensor([0.4290])
损失函数(均方误差): 0.14607082307338715
	grad W:  -0.01 0.0 -0.01 0.0 0.03 0.07 0.02 0.06
=====第6轮=====
正向计算:o1 ,o2
tensor([0.4307]) tensor([0.4116])
损失函数(均方误差): 0.1361003816127777
	grad W:  -0.01 0.0 -0.01 0.0 0.03 0.07 0.02 0.06
=====第7轮=====
正向计算:o1 ,o2
tensor([0.4239]) tensor([0.3951])
损失函数(均方误差): 0.1269397884607315
	grad W:  -0.01 0.0 -0.01 0.0 0.03 0.06 0.02 0.05
=====第8轮=====
正向计算:o1 ,o2
tensor([0.4173]) tensor([0.3794])
损失函数(均方误差): 0.11853284388780594
	grad W:  -0.01 0.0 -0.01 0.0 0.03 0.06 0.02 0.05
=====第9轮=====
正向计算:o1 ,o2
tensor([0.4109]) tensor([0.3647])
损失函数(均方误差): 0.11082295328378677
	grad W:  -0.02 0.0 -0.01 0.0 0.03 0.06 0.02 0.05
=====第10轮=====
正向计算:o1 ,o2
tensor([0.4047]) tensor([0.3507])
损失函数(均方误差): 0.10375461727380753
	grad W:  -0.02 -0.0 -0.01 -0.0 0.02 0.06 0.02 0.05
更新后的权值
tensor([0.3425]) tensor([-0.4540]) tensor([0.5855]) tensor([0.5676]) tensor([-0.2230]) tensor([-1.2686]) tensor([-0.5765]) tensor([0.1418])

Process finished with exit code 0
激活函数Sigmoid改变为Relu

原函数

def sigmoid(z):
    a = 1 / (1 + torch.exp(-z))
    return a


def forward_propagate(x1, x2):
    in_h1 = w1 * x1 + w3 * x2
    out_h1 = sigmoid(in_h1)  # out_h1 = torch.sigmoid(in_h1)
    in_h2 = w2 * x1 + w4 * x2
    out_h2 = sigmoid(in_h2)  # out_h2 = torch.sigmoid(in_h2)

    in_o1 = w5 * out_h1 + w7 * out_h2
    out_o1 = sigmoid(in_o1)  # out_o1 = torch.sigmoid(in_o1)
    in_o2 = w6 * out_h1 + w8 * out_h2
    out_o2 = sigmoid(in_o2)  # out_o2 = torch.sigmoid(in_o2)

    print("正向计算:o1 ,o2")
    print(out_o1.data, out_o2.data)

    return out_o1, out_o2

替换后

def forward_propagate(x1, x2):
    in_h1 = w1 * x1 + w3 * x2
    out_h1 = torch.relu(in_h1)
    in_h2 = w2 * x1 + w4 * x2
    out_h2 = torch.relu(in_h2)


    in_o1 = w5 * out_h1 + w7 * out_h2
    out_o1 = torch.relu(in_o1)
    in_o2 = w6 * out_h1 + w8 * out_h2
    out_o2 = torch.relu(in_o2)

    print("正向计算:o1 ,o2")
    print(out_o1.data, out_o2.data)

    return out_o1, out_o2

运行结果

C:p_pycharmvtScriptspython.exe C:/p_pycharm/SVM.py
=====输入值:x1, x2;真实输出值:y1, y2=====
tensor([0.5000]) tensor([0.3000]) tensor([0.2300]) tensor([-0.0700])
=====更新前的权值=====
tensor([0.2000]) tensor([-0.4000]) tensor([0.5000]) tensor([0.6000]) tensor([0.1000]) tensor([-0.5000]) tensor([-0.3000]) tensor([0.8000])
=====第0轮=====
正向计算:o1 ,o2
tensor([0.0250]) tensor([0.])
损失函数(均方误差): 0.023462500423192978
	grad W:  -0.01 0.0 -0.01 0.0 -0.05 0.0 -0.0 0.0
=====第1轮=====
正向计算:o1 ,o2
tensor([0.0389]) tensor([0.])
损失函数(均方误差): 0.020715968683362007
	grad W:  -0.01 0.0 -0.01 0.0 -0.05 0.0 0.0 0.0
=====第2轮=====
正向计算:o1 ,o2
tensor([0.0535]) tensor([0.])
损失函数(均方误差): 0.01803365722298622
	grad W:  -0.02 0.0 -0.01 0.0 -0.05 0.0 0.0 0.0
=====第3轮=====
正向计算:o1 ,o2
tensor([0.0690]) tensor([0.])
损失函数(均方误差): 0.015410471707582474
	grad W:  -0.02 0.0 -0.01 0.0 -0.04 0.0 0.0 0.0
=====第4轮=====
正向计算:o1 ,o2
tensor([0.0855]) tensor([0.])
损失函数(均方误差): 0.012893404811620712
	grad W:  -0.02 0.0 -0.01 0.0 -0.04 0.0 0.0 0.0
=====第5轮=====
正向计算:o1 ,o2
tensor([0.1026]) tensor([0.])
损失函数(均方误差): 0.010560503229498863
	grad W:  -0.02 0.0 -0.01 0.0 -0.04 0.0 0.0 0.0
=====第6轮=====
正向计算:o1 ,o2
tensor([0.1200]) tensor([0.])
损失函数(均方误差): 0.008496038615703583
	grad W:  -0.02 0.0 -0.01 0.0 -0.04 0.0 0.0 0.0
=====第7轮=====
正向计算:o1 ,o2
tensor([0.1371]) tensor([0.])
损失函数(均方误差): 0.006765476893633604
	grad W:  -0.02 0.0 -0.01 0.0 -0.03 0.0 0.0 0.0
=====第8轮=====
正向计算:o1 ,o2
tensor([0.1532]) tensor([0.])
损失函数(均方误差): 0.005397447384893894
	grad W:  -0.02 0.0 -0.01 0.0 -0.03 0.0 0.0 0.0
=====第9轮=====
正向计算:o1 ,o2
tensor([0.1679]) tensor([0.])
损失函数(均方误差): 0.004378797020763159
	grad W:  -0.01 0.0 -0.01 0.0 -0.02 0.0 0.0 0.0
=====第10轮=====
正向计算:o1 ,o2
tensor([0.1807]) tensor([0.])
损失函数(均方误差): 0.0036630069371312857
	grad W:  -0.01 0.0 -0.01 0.0 -0.02 0.0 0.0 0.0
更新后的权值
tensor([0.3877]) tensor([-0.4000]) tensor([0.6126]) tensor([0.6000]) tensor([0.5074]) tensor([-0.5000]) tensor([-0.3000]) tensor([0.8000])

Process finished with exit code 0


损失函数MSE用PyTorch自带函数 t.nn.MSELoss()替代

将原函数总结为

def loss_fuction(x1, x2, y1, y2):  # 损失函数
    y1_pred, y2_pred = forward_propagate(x1, x2)  # 前向传播
    loss_f = torch.nn.MSELoss()
    y_pred = torch.cat((y1_pred, y2_pred), dim=0)
    y = torch.cat((y1, y2), dim=0)
    loss = loss_f(y_pred,y)
    print("损失函数(均方误差):", loss.item())
    return loss
损失函数MSE改变为交叉熵

原函数替换为

def loss_fuction(x1, x2, y1, y2):  # 损失函数
    y1_pred, y2_pred = forward_propagate(x1, x2)  # 前向传播
    loss_f = torch.nn.CrossEntropyLoss
    y_pred = torch.cat((y1_pred, y2_pred), dim=0)
    y = torch.cat((y1, y2), dim=0)
    loss = loss_f(y_pred,y)
    print("损失函数(均方误差):", loss.item())
    return loss

改变步长,训练次数,观察、总结并陈述

1.步长为1,迭代1次

C:p_pycharmvtScriptspython.exe C:/p_pycharm/SVM.py
=====输入值:x1, x2;真实输出值:y1, y2=====
tensor([0.5000]) tensor([0.3000]) tensor([0.2300]) tensor([-0.0700])
=====更新前的权值=====
tensor([0.2000]) tensor([-0.4000]) tensor([0.5000]) tensor([0.6000]) tensor([0.1000]) tensor([-0.5000]) tensor([-0.3000]) tensor([0.8000])
=====第0轮=====
正向计算:o1 ,o2
tensor([0.4769]) tensor([0.5287])
损失函数(均方误差): 0.2097097933292389
	grad W:  -0.01 0.01 -0.01 0.01 0.03 0.08 0.03 0.07
更新后的权值
tensor([0.2084]) tensor([-0.4126]) tensor([0.5051]) tensor([0.5924]) tensor([0.0654]) tensor([-0.5839]) tensor([-0.3305]) tensor([0.7262])

Process finished with exit code 0


步长为1,迭代10次

C:p_pycharmvtScriptspython.exe C:/p_pycharm/SVM.py
=====输入值:x1, x2;真实输出值:y1, y2=====
tensor([0.5000]) tensor([0.3000]) tensor([0.2300]) tensor([-0.0700])
=====更新前的权值=====
tensor([0.2000]) tensor([-0.4000]) tensor([0.5000]) tensor([0.6000]) tensor([0.1000]) tensor([-0.5000]) tensor([-0.3000]) tensor([0.8000])
=====第0轮=====
正向计算:o1 ,o2
tensor([0.4769]) tensor([0.5287])
损失函数(均方误差): 0.2097097933292389
	grad W:  -0.01 0.01 -0.01 0.01 0.03 0.08 0.03 0.07
=====第1轮=====
正向计算:o1 ,o2
tensor([0.4685]) tensor([0.5072])
损失函数(均方误差): 0.19503259658813477
	grad W:  -0.01 0.01 -0.01 0.01 0.03 0.08 0.03 0.07
=====第2轮=====
正向计算:o1 ,o2
tensor([0.4604]) tensor([0.4864])
损失函数(均方误差): 0.1813509315252304
	grad W:  -0.01 0.01 -0.01 0.01 0.03 0.08 0.03 0.07
=====第3轮=====
正向计算:o1 ,o2
tensor([0.4526]) tensor([0.4664])
损失函数(均方误差): 0.16865134239196777
	grad W:  -0.01 0.01 -0.01 0.0 0.03 0.08 0.03 0.07
=====第4轮=====
正向计算:o1 ,o2
tensor([0.4451]) tensor([0.4473])
损失函数(均方误差): 0.15690487623214722
	grad W:  -0.01 0.01 -0.01 0.0 0.03 0.07 0.03 0.06
=====第5轮=====
正向计算:o1 ,o2
tensor([0.4378]) tensor([0.4290])
损失函数(均方误差): 0.14607082307338715
	grad W:  -0.01 0.0 -0.01 0.0 0.03 0.07 0.02 0.06
=====第6轮=====
正向计算:o1 ,o2
tensor([0.4307]) tensor([0.4116])
损失函数(均方误差): 0.1361003816127777
	grad W:  -0.01 0.0 -0.01 0.0 0.03 0.07 0.02 0.06
=====第7轮=====
正向计算:o1 ,o2
tensor([0.4239]) tensor([0.3951])
损失函数(均方误差): 0.1269397884607315
	grad W:  -0.01 0.0 -0.01 0.0 0.03 0.06 0.02 0.05
=====第8轮=====
正向计算:o1 ,o2
tensor([0.4173]) tensor([0.3794])
损失函数(均方误差): 0.11853282898664474
	grad W:  -0.01 0.0 -0.01 0.0 0.03 0.06 0.02 0.05
=====第9轮=====
正向计算:o1 ,o2
tensor([0.4109]) tensor([0.3647])
损失函数(均方误差): 0.11082295328378677
	grad W:  -0.02 0.0 -0.01 0.0 0.03 0.06 0.02 0.05
更新后的权值
tensor([0.3273]) tensor([-0.4547]) tensor([0.5764]) tensor([0.5672]) tensor([-0.1985]) tensor([-1.2127]) tensor([-0.5561]) tensor([0.1883])

Process finished with exit code 0


步长未10,迭代1次

C:p_pycharmvtScriptspython.exe C:/p_pycharm/SVM.py
=====输入值:x1, x2;真实输出值:y1, y2=====
tensor([0.5000]) tensor([0.3000]) tensor([0.2300]) tensor([-0.0700])
=====更新前的权值=====
tensor([0.2000]) tensor([-0.4000]) tensor([0.5000]) tensor([0.6000]) tensor([0.1000]) tensor([-0.5000]) tensor([-0.3000]) tensor([0.8000])
=====第0轮=====
正向计算:o1 ,o2
tensor([0.4769]) tensor([0.5287])
损失函数(均方误差): 0.2097097933292389
	grad W:  -0.01 0.01 -0.01 0.01 0.03 0.08 0.03 0.07
=====第1轮=====
正向计算:o1 ,o2
tensor([0.3945]) tensor([0.3225])
损失函数(均方误差): 0.0905664712190628
	grad W:  -0.02 -0.0 -0.01 -0.0 0.02 0.05 0.02 0.04
=====第2轮=====
正向计算:o1 ,o2
tensor([0.3403]) tensor([0.2198])
损失函数(均方误差): 0.04808921366930008
	grad W:  -0.01 -0.0 -0.01 -0.0 0.01 0.03 0.01 0.02
=====第3轮=====
正向计算:o1 ,o2
tensor([0.3042]) tensor([0.1671])
损失函数(均方误差): 0.03085227683186531
	grad W:  -0.01 -0.0 -0.01 -0.0 0.01 0.02 0.01 0.02
=====第4轮=====
正向计算:o1 ,o2
tensor([0.2804]) tensor([0.1357])
损失函数(均方误差): 0.022416697815060616
	grad W:  -0.01 -0.0 -0.0 -0.0 0.01 0.02 0.01 0.01
=====第5轮=====
正向计算:o1 ,o2
tensor([0.2643]) tensor([0.1149])
损失函数(均方误差): 0.01768375188112259
	grad W:  -0.01 -0.0 -0.0 -0.0 0.0 0.01 0.0 0.01
=====第6轮=====
正向计算:o1 ,o2
tensor([0.2533]) tensor([0.1001])
损失函数(均方误差): 0.014743898995220661
	grad W:  -0.0 -0.0 -0.0 -0.0 0.0 0.01 0.0 0.01
=====第7轮=====
正向计算:o1 ,o2
tensor([0.2456]) tensor([0.0890])
损失函数(均方误差): 0.012769920751452446
	grad W:  -0.0 -0.0 -0.0 -0.0 0.0 0.01 0.0 0.01
=====第8轮=====
正向计算:o1 ,o2
tensor([0.2402]) tensor([0.0804])
损失函数(均方误差): 0.011361523531377316
	grad W:  -0.0 -0.0 -0.0 -0.0 0.0 0.01 0.0 0.01
=====第9轮=====
正向计算:o1 ,o2
tensor([0.2363]) tensor([0.0734])
损失函数(均方误差): 0.010307400487363338
	grad W:  -0.0 -0.0 -0.0 -0.0 0.0 0.01 0.0 0.01
更新后的权值
tensor([0.9458]) tensor([-0.2734]) tensor([0.9475]) tensor([0.6759]) tensor([-0.8954]) tensor([-2.9388]) tensor([-1.1210]) tensor([-1.2041])

Process finished with exit code 0


步长10次,迭代10次

C:p_pycharmvtScriptspython.exe C:/p_pycharm/SVM.py
=====输入值:x1, x2;真实输出值:y1, y2=====
tensor([0.5000]) tensor([0.3000]) tensor([0.2300]) tensor([-0.0700])
=====更新前的权值=====
tensor([0.2000]) tensor([-0.4000]) tensor([0.5000]) tensor([0.6000]) tensor([0.1000]) tensor([-0.5000]) tensor([-0.3000]) tensor([0.8000])
=====第0轮=====
正向计算:o1 ,o2
tensor([0.4769]) tensor([0.5287])
损失函数(均方误差): 0.2097097933292389
	grad W:  -0.01 0.01 -0.01 0.01 0.03 0.08 0.03 0.07
=====第1轮=====
正向计算:o1 ,o2
tensor([0.3945]) tensor([0.3225])
损失函数(均方误差): 0.0905664712190628
	grad W:  -0.02 -0.0 -0.01 -0.0 0.02 0.05 0.02 0.04
=====第2轮=====
正向计算:o1 ,o2
tensor([0.3403]) tensor([0.2198])
损失函数(均方误差): 0.04808921366930008
	grad W:  -0.01 -0.0 -0.01 -0.0 0.01 0.03 0.01 0.02
=====第3轮=====
正向计算:o1 ,o2
tensor([0.3042]) tensor([0.1671])
损失函数(均方误差): 0.03085227683186531
	grad W:  -0.01 -0.0 -0.01 -0.0 0.01 0.02 0.01 0.02
=====第4轮=====
正向计算:o1 ,o2
tensor([0.2804]) tensor([0.1357])
损失函数(均方误差): 0.022416697815060616
	grad W:  -0.01 -0.0 -0.0 -0.0 0.01 0.02 0.01 0.01
=====第5轮=====
正向计算:o1 ,o2
tensor([0.2643]) tensor([0.1149])
损失函数(均方误差): 0.01768375188112259
	grad W:  -0.01 -0.0 -0.0 -0.0 0.0 0.01 0.0 0.01
=====第6轮=====
正向计算:o1 ,o2
tensor([0.2533]) tensor([0.1001])
损失函数(均方误差): 0.014743898995220661
	grad W:  -0.0 -0.0 -0.0 -0.0 0.0 0.01 0.0 0.01
=====第7轮=====
正向计算:o1 ,o2
tensor([0.2456]) tensor([0.0890])
损失函数(均方误差): 0.012769920751452446
	grad W:  -0.0 -0.0 -0.0 -0.0 0.0 0.01 0.0 0.01
=====第8轮=====
正向计算:o1 ,o2
tensor([0.2402]) tensor([0.0804])
损失函数(均方误差): 0.011361523531377316
	grad W:  -0.0 -0.0 -0.0 -0.0 0.0 0.01 0.0 0.01
=====第9轮=====
正向计算:o1 ,o2
tensor([0.2363]) tensor([0.0734])
损失函数(均方误差): 0.010307400487363338
	grad W:  -0.0 -0.0 -0.0 -0.0 0.0 0.01 0.0 0.01
更新后的权值
tensor([0.9458]) tensor([-0.2734]) tensor([0.9475]) tensor([0.6759]) tensor([-0.8954]) tensor([-2.9388]) tensor([-1.1210]) tensor([-1.2041])

Process finished with exit code 0


权值无论如何初始化,不会影响最终结果。 反向传播算法的主要思想:将训练集数据输入到算法的输入层,经过隐藏层,最后达到输出层并输出结果
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/870091.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号