1 PyTorch教程3.1之线性回归-德赢Vwin官网 网
×

PyTorch教程3.1之线性回归

消耗积分:0 | 格式:pdf | 大小:0.37 MB | 2023-06-05

邹俩珍

分享资料个

每当我们想要预测一个数值时,就会出现回归问题。常见的例子包括预测价格(房屋、股票等)、预测住院时间(对于住院患者)、预测需求(对于零售销售)等等。并非每个预测问题都是经典的回归问题。稍后,我们将介绍分类问题,其目标是预测一组类别中的成员资格。

作为一个运行示例,假设我们希望根据房屋的面积(以平方英尺为单位)和年龄(以年为单位)来估算房屋的价格(以美元为单位)。要开发预测房价的模型,我们需要掌握由销售额组成的数据,包括每个房屋的销售价格、面积和年龄。在机器学习的术语中,数据集称为训练数据集训练集,每一行(包含与一次销售对应的数据)称为示例数据点实例样本)。我们试图预测的东西(价格)称为标签目标). 预测所依据的变量(年龄和面积)称为特征(或 协变量)。

%matplotlib inline
import math
import time
import numpy as np
import torch
from d2l import torch as d2l
%matplotlib inline
import math
import time
from mxnet import np
from d2l import mxnet as d2l
%matplotlib inline
import math
import time
from jax import numpy as jnp
from d2l import jax as d2l
No GPU/TPU found, falling back to CPU. (Set TF_CPP_MIN_LOG_LEVEL=0 and rerun for more info.)
%matplotlib inline
import math
import time
import numpy as np
import tensorflow as tf
from d2l import tensorflow as d2l

3.1.1. 基本

线性回归可能是解决回归问题的标准工具中最简单和最受欢迎的。追溯到 19 世纪初高斯,1809 年勒让德,1805 年,线性回归源于一些简单的假设。首先,我们假设特征之间的关系x和目标 y近似线性,即条件均值 E[Y∣X=x]可以表示为特征的加权和x. 由于观察噪声,此设置允许目标值仍可能偏离其预期值。接下来,我们可以假设任何此类噪声都表现良好,服从高斯分布。通常,我们会使用n来表示我们数据集中的示例数量。我们使用上标来枚举样本和目标,并使用下标来索引坐标。更具体地说,x(i)表示i-th 样品和 xj(i)表示其j-th 坐标。

3.1.1.1. 模型

每个解决方案的核心都是一个模型,该模型描述了如何将特征转换为对目标的估计。线性假设意味着目标(价格)的期望值可以表示为特征(面积和年龄)的加权和:

(3.1.1)price=warea⋅area+wage⋅age+b.

这里wareawage称为 权重,并且b称为偏差(或偏移量截距)。权重决定了每个特征对我们预测的影响。当所有特征都为零时,偏差决定了估计值。即使我们永远不会看到任何面积恰好为零的新建房屋,我们仍然需要偏差,因为它允许我们表达特征的所有线性函数(而不是将我们限制在通过原点的直线上)。严格来说, (3.1.1)是输入特征的仿射变换,其特点是通过加权求和对特征进行线性变换,结合平移通过增加偏差。给定一个数据集,我们的目标是选择权重 w和偏见b平均而言,使我们的模型预测尽可能接近数据中观察到的真实价格。

在通常关注仅具有少量特征的数据集的学科中,明确表达模型的长格式,如 (3.1.1)中,是常见的。在机器学习中,我们通常使用高维数据集,在这种情况下使用紧凑的线性代数符号会更方便。当我们的输入包括 d特征,我们可以为每个特征分配一个索引(介于1d) 并表达我们的预测y^(通常,“帽子”符号表示估计值)作为

(3.1.2)y^=w1x1+...+wdxd+b.

将所有特征收集到一个向量中 x∈Rd并将所有权重放入向量中 w∈Rd,我们可以通过点积来简洁地表达我们的模型wx:

(3.1.3)y^=w⊤x+b.

(3.1.3)中,向量x对应于单个示例的特征。

声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表德赢Vwin官网 网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

评论(0)
发评论

下载排行榜

全部0条评论

快来发表一下你的评论吧 !

'+ '

'+ '

'+ ''+ '
'+ ''+ ''+ '
'+ ''+ '' ); $.get('/article/vipdownload/aid/'+webid,function(data){ if(data.code ==5){ $(pop_this).attr('href',"//www.hzfubeitong.com/m/login/index.html"); return false } if(data.code == 2){ //跳转到VIP升级页面 window.location.href="https://m.elecfans.com/vip/index?aid=" + webid return false } //是会员 if (data.code > 0) { $('body').append(htmlSetNormalDownload); var getWidth=$("#poplayer").width(); $("#poplayer").css("margin-left","-"+getWidth/2+"px"); $('#tips').html(data.msg) $('.download_confirm').click(function(){ $('#dialog').remove(); }) } else { var down_url = $('#vipdownload').attr('data-url'); isBindAnalysisForm(pop_this, down_url, 1) } }); }); //是否开通VIP $.get('/article/vipdownload/aid/'+webid,function(data){ if(data.code == 2 || data.code ==5){ //跳转到VIP升级页面 $('#vipdownload>span').text("开通VIP 免费下载") return false }else{ // 待续费 if(data.code == 3) { vipExpiredInfo.ifVipExpired = true vipExpiredInfo.vipExpiredDate = data.data.endoftime } $('#vipdownload .icon-vip-tips').remove() $('#vipdownload>span').text("VIP免积分下载") } }); }).on("click",".download_cancel",function(){ $('#dialog').remove(); }) var setWeixinShare={};//定义默认的微信分享信息,页面如果要自定义分享,直接更改此变量即可 if(window.navigator.userAgent.toLowerCase().match(/MicroMessenger/i) == 'micromessenger'){ var d={ title:'PyTorch教程3.1之线性回归',//标题 desc:$('[name=description]').attr("content"), //描述 imgUrl:'https://'+location.host+'/static/images/ele-logo.png',// 分享图标,默认是logo link:'',//链接 type:'',// 分享类型,music、video或link,不填默认为link dataUrl:'',//如果type是music或video,则要提供数据链接,默认为空 success:'', // 用户确认分享后执行的回调函数 cancel:''// 用户取消分享后执行的回调函数 } setWeixinShare=$.extend(d,setWeixinShare); $.ajax({ url:"//www.hzfubeitong.com/app/wechat/index.php?s=Home/ShareConfig/index", data:"share_url="+encodeURIComponent(location.href)+"&format=jsonp&domain=m", type:'get', dataType:'jsonp', success:function(res){ if(res.status!="successed"){ return false; } $.getScript('https://res.wx.qq.com/open/js/jweixin-1.0.0.js',function(result,status){ if(status!="success"){ return false; } var getWxCfg=res.data; wx.config({ //debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。 appId:getWxCfg.appId, // 必填,公众号的唯一标识 timestamp:getWxCfg.timestamp, // 必填,生成签名的时间戳 nonceStr:getWxCfg.nonceStr, // 必填,生成签名的随机串 signature:getWxCfg.signature,// 必填,签名,见附录1 jsApiList:['onMenuShareTimeline','onMenuShareAppMessage','onMenuShareQQ','onMenuShareWeibo','onMenuShareQZone'] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2 }); wx.ready(function(){ //获取“分享到朋友圈”按钮点击状态及自定义分享内容接口 wx.onMenuShareTimeline({ title: setWeixinShare.title, // 分享标题 link: setWeixinShare.link, // 分享链接 imgUrl: setWeixinShare.imgUrl, // 分享图标 success: function () { setWeixinShare.success; // 用户确认分享后执行的回调函数 }, cancel: function () { setWeixinShare.cancel; // 用户取消分享后执行的回调函数 } }); //获取“分享给朋友”按钮点击状态及自定义分享内容接口 wx.onMenuShareAppMessage({ title: setWeixinShare.title, // 分享标题 desc: setWeixinShare.desc, // 分享描述 link: setWeixinShare.link, // 分享链接 imgUrl: setWeixinShare.imgUrl, // 分享图标 type: setWeixinShare.type, // 分享类型,music、video或link,不填默认为link dataUrl: setWeixinShare.dataUrl, // 如果type是music或video,则要提供数据链接,默认为空 success: function () { setWeixinShare.success; // 用户确认分享后执行的回调函数 }, cancel: function () { setWeixinShare.cancel; // 用户取消分享后执行的回调函数 } }); //获取“分享到QQ”按钮点击状态及自定义分享内容接口 wx.onMenuShareQQ({ title: setWeixinShare.title, // 分享标题 desc: setWeixinShare.desc, // 分享描述 link: setWeixinShare.link, // 分享链接 imgUrl: setWeixinShare.imgUrl, // 分享图标 success: function () { setWeixinShare.success; // 用户确认分享后执行的回调函数 }, cancel: function () { setWeixinShare.cancel; // 用户取消分享后执行的回调函数 } }); //获取“分享到腾讯微博”按钮点击状态及自定义分享内容接口 wx.onMenuShareWeibo({ title: setWeixinShare.title, // 分享标题 desc: setWeixinShare.desc, // 分享描述 link: setWeixinShare.link, // 分享链接 imgUrl: setWeixinShare.imgUrl, // 分享图标 success: function () { setWeixinShare.success; // 用户确认分享后执行的回调函数 }, cancel: function () { setWeixinShare.cancel; // 用户取消分享后执行的回调函数 } }); //获取“分享到QQ空间”按钮点击状态及自定义分享内容接口 wx.onMenuShareQZone({ title: setWeixinShare.title, // 分享标题 desc: setWeixinShare.desc, // 分享描述 link: setWeixinShare.link, // 分享链接 imgUrl: setWeixinShare.imgUrl, // 分享图标 success: function () { setWeixinShare.success; // 用户确认分享后执行的回调函数 }, cancel: function () { setWeixinShare.cancel; // 用户取消分享后执行的回调函数 } }); }); }); } }); } function openX_ad(posterid, htmlid, width, height) { if ($(htmlid).length > 0) { var randomnumber = Math.random(); var now_url = encodeURIComponent(window.location.href); var ga = document.createElement('iframe'); ga.src = 'https://www1.elecfans.com/www/delivery/myafr.php?target=_blank&cb=' + randomnumber + '&zoneid=' + posterid+'&prefer='+now_url; ga.width = width; ga.height = height; ga.frameBorder = 0; ga.scrolling = 'no'; var s = $(htmlid).append(ga); } } openX_ad(828, '#berry-300', 300, 250);