资讯专栏INFORMATION COLUMN

【PyTorch基础教程1】线性模型(学不会来打我啊)

djfml / 3677人阅读

摘要:文章目录一线性模型二绘图工具三作业一线性模型不要小看简单线性模型哈哈,虽然这讲我们还没正式用到,但是用到的前向传播损失函数两种绘图等方法在后面是很常用的。

一、线性模型

不要小看简单线性模型哈哈,虽然这讲我们还没正式用到pytorch,但是用到的前向传播、损失函数、两种绘loss图等方法在后面是很常用的。
对下面的代码说明:

  • zip函数可以将x_datay_data组合元组列表,在for循环中每次遍历就是对于列表中的每个元组。
  • 函数forward()中,有一个变量w。这个变量最终的值是从for循环中传入的。
# -*- coding: utf-8 -*-"""Created on Tue Oct 12 14:30:13 2021@author: 86493"""import numpy as npimport matplotlib.pyplot as pltx_data = [1.0, 2.0, 3.0]y_data = [2.0, 4.0, 6.0]def forward(x):    return x * wdef loss(x, y):    y_pred = forward(x)    return (y_pred - y) * (y_pred - y)# 保存权重w_list = []# 保存权重的损失函数值mse_list = []# 穷举w值对应的损失函数MSEfor w in np.arange(0.0, 4.1, 0.1):    print("w = ", w)    loss_sum = 0    for x_val, y_val in zip(x_data, y_data):        # 为了打印y预测值,其实loss里也计算了        y_pred_val = forward(x_val)        loss_val = loss(x_val, y_val)        loss_sum += loss_val        print("/t", x_val, y_val,              y_pred_val, loss_val)    print("MSE = ", loss_sum / 3)    print("="*60)    w_list.append(w)    mse_list.append(loss_sum / 3)     # 绘loss变化图,横坐标是w,纵坐标是lossplt.plot(w_list, mse_list)plt.ylabel("Loss")plt.xlabel("w")plt.show()

刚才对应的打印结果为:

w =  0.0	 1.0 2.0 0.0 4.0	 2.0 4.0 0.0 16.0	 3.0 6.0 0.0 36.0MSE =  18.666666666666668============================================================w =  0.1	 1.0 2.0 0.1 3.61	 2.0 4.0 0.2 14.44	 3.0 6.0 0.30000000000000004 32.49MSE =  16.846666666666668============================================================w =  0.2	 1.0 2.0 0.2 3.24	 2.0 4.0 0.4 12.96	 3.0 6.0 0.6000000000000001 29.160000000000004MSE =  15.120000000000003============================================================w =  0.30000000000000004	 1.0 2.0 0.30000000000000004 2.8899999999999997	 2.0 4.0 0.6000000000000001 11.559999999999999	 3.0 6.0 0.9000000000000001 26.009999999999998MSE =  13.486666666666665============================================================w =  0.4	 1.0 2.0 0.4 2.5600000000000005	 2.0 4.0 0.8 10.240000000000002	 3.0 6.0 1.2000000000000002 23.04MSE =  11.946666666666667============================================================w =  0.5	 1.0 2.0 0.5 2.25	 2.0 4.0 1.0 9.0	 3.0 6.0 1.5 20.25MSE =  10.5============================================================w =  0.6000000000000001	 1.0 2.0 0.6000000000000001 1.9599999999999997	 2.0 4.0 1.2000000000000002 7.839999999999999	 3.0 6.0 1.8000000000000003 17.639999999999993MSE =  9.146666666666663============================================================w =  0.7000000000000001	 1.0 2.0 0.7000000000000001 1.6899999999999995	 2.0 4.0 1.4000000000000001 6.759999999999998	 3.0 6.0 2.1 15.209999999999999MSE =  7.886666666666666============================================================w =  0.8	 1.0 2.0 0.8 1.44	 2.0 4.0 1.6 5.76	 3.0 6.0 2.4000000000000004 12.959999999999997MSE =  6.719999999999999============================================================w =  0.9	 1.0 2.0 0.9 1.2100000000000002	 2.0 4.0 1.8 4.840000000000001	 3.0 6.0 2.7 10.889999999999999MSE =  5.646666666666666============================================================w =  1.0	 1.0 2.0 1.0 1.0	 2.0 4.0 2.0 4.0	 3.0 6.0 3.0 9.0MSE =  4.666666666666667============================================================w =  1.1	 1.0 2.0 1.1 0.8099999999999998	 2.0 4.0 2.2 3.2399999999999993	 3.0 6.0 3.3000000000000003 7.289999999999998MSE =  3.779999999999999============================================================w =  1.2000000000000002	 1.0 2.0 1.2000000000000002 0.6399999999999997	 2.0 4.0 2.4000000000000004 2.5599999999999987	 3.0 6.0 3.6000000000000005 5.759999999999997MSE =  2.986666666666665============================================================w =  1.3	 1.0 2.0 1.3 0.48999999999999994	 2.0 4.0 2.6 1.9599999999999997	 3.0 6.0 3.9000000000000004 4.409999999999998MSE =  2.2866666666666657============================================================w =  1.4000000000000001	 1.0 2.0 1.4000000000000001 0.3599999999999998	 2.0 4.0 2.8000000000000003 1.4399999999999993	 3.0 6.0 4.2 3.2399999999999993MSE =  1.6799999999999995============================================================w =  1.5	 1.0 2.0 1.5 0.25	 2.0 4.0 3.0 1.0	 3.0 6.0 4.5 2.25MSE =  1.1666666666666667============================================================w =  1.6	 1.0 2.0 1.6 0.15999999999999992	 2.0 4.0 3.2 0.6399999999999997	 3.0 6.0 4.800000000000001 1.4399999999999984MSE =  0.746666666666666============================================================w =  1.7000000000000002	 1.0 2.0 1.7000000000000002 0.0899999999999999	 2.0 4.0 3.4000000000000004 0.3599999999999996	 3.0 6.0 5.1000000000000005 0.809999999999999MSE =  0.4199999999999995============================================================w =  1.8	 1.0 2.0 1.8 0.03999999999999998	 2.0 4.0 3.6 0.15999999999999992	 3.0 6.0 5.4 0.3599999999999996MSE =  0.1866666666666665============================================================w =  1.9000000000000001	 1.0 2.0 1.9000000000000001 0.009999999999999974	 2.0 4.0 3.8000000000000003 0.0399999999999999	 3.0 6.0 5.7 0.0899999999999999MSE =  0.046666666666666586============================================================w =  2.0	 1.0 2.0 2.0 0.0	 2.0 4.0 4.0 0.0	 3.0 6.0 6.0 0.0MSE =  0.0============================================================w =  2.1	 1.0 2.0 2.1 0.010000000000000018	 2.0 4.0 4.2 0.04000000000000007	 3.0 6.0 6.300000000000001 0.09000000000000043MSE =  0.046666666666666835============================================================w =  2.2	 1.0 2.0 2.2 0.04000000000000007	 2.0 4.0 4.4 0.16000000000000028	 3.0 6.0 6.6000000000000005 0.36000000000000065MSE =  0.18666666666666698============================================================w =  2.3000000000000003	 1.0 2.0 2.3000000000000003 0.09000000000000016	 2.0 4.0 4.6000000000000005 0.36000000000000065	 3.0 6.0 6.9 0.8100000000000006MSE =  0.42000000000000054============================================================w =  2.4000000000000004	 1.0 2.0 2.4000000000000004 0.16000000000000028	 2.0 4.0 4.800000000000001 0.6400000000000011	 3.0 6.0 7.200000000000001 1.4400000000000026MSE =  0.7466666666666679============================================================w =  2.5	 1.0 2.0 2.5 0.25	 2.0 4.0 5.0 1.0	 3.0 6.0 7.5 2.25MSE =  1.1666666666666667============================================================w =  2.6	 1.0 2.0 2.6 0.3600000000000001	 2.0 4.0 5.2 1.4400000000000004	 3.0 6.0 7.800000000000001 3.2400000000000024MSE =  1.6800000000000008============================================================w =  2.7	 1.0 2.0 2.7 0.49000000000000027	 2.0 4.0 5.4 1.960000000000001	 3.0 6.0 8.100000000000001 4.410000000000006MSE =  2.2866666666666693==========================================           
               
                                           
                       
                 

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/122570.html

相关文章

  • 转行测试的看过来!0基础~自动化测试,我整理的超全习指南...

    摘要:在这个阶段,学习工具什么的,重点在于接口测试的学习,所有的工具的学习,都是在为了接口测试的学习做铺垫。接口测试工具的使用。 很多朋友想要入行软件测试,但是都不知道该怎么学。 抽个时间简单的给大家说下,对于0基础的朋友,应该怎么去学习软件测试。 学习软件测试有2条路可以选。 最省事的当然是找个...

    Hanks10100 评论0 收藏0
  • 第7期 Datawhale 组队习计划

    马上就要开始啦这次共组织15个组队学习 涵盖了AI领域从理论知识到动手实践的内容 按照下面给出的最完备学习路线分类 难度系数分为低、中、高三档 可以按照需要参加 - 学习路线 - showImg(https://segmentfault.com/img/remote/1460000019082128); showImg(https://segmentfault.com/img/remote/...

    dinfer 评论0 收藏0
  • ApacheCN 编程/大数据/数据科/人工智能习资源 2019.6

    摘要:请回复这个帖子并注明组织个人信息来申请加入。权限分配灵活,能者居之。数量超过个,在所有组织中排名前。网站日超过,排名的峰值为。导航归档社区自媒体平台微博知乎专栏公众号博客园简书合作侵权,请联系请抄送一份到赞助我们 Special Sponsors showImg(https://segmentfault.com/img/remote/1460000018907426?w=1760&h=...

    Bmob 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<