LitchiCheng 发表于 2024-10-20 20:54

一起读《动手学深度学习(PyTorch版)》- 线性神经网络 - 数据集处理

<div class='showpostmsg'><article data-content="[{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;3060-1621846615933&quot;,&quot;name&quot;:&quot;paragraph&quot;,&quot;data&quot;:{},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;p5PQ-1621846617594&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;协变量(covariate)、特征(feature)&quot;,&quot;marks&quot;:[]}]}],&quot;state&quot;:{}},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;ETn1-1728396638234&quot;,&quot;name&quot;:&quot;paragraph&quot;,&quot;data&quot;:{&quot;style&quot;:{&quot;textIndent&quot;:28}},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;M8JH-1728396638232&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;-&gt; 特征的加权和或这说由权重组成的这种关系决定了目标怎么到达&quot;,&quot;marks&quot;:[]}]}],&quot;state&quot;:{}},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;sR3b-1728396723109&quot;,&quot;name&quot;:&quot;paragraph&quot;,&quot;data&quot;:{&quot;style&quot;:{&quot;textIndent&quot;:28}},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;WfS6-1728396723102&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;-&gt; 权重决定了每个特征对于预测值的影响,更倾向于那种特征,像谁一点?&quot;,&quot;marks&quot;:[]}]}],&quot;state&quot;:{}},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;kHfu-1728396768758&quot;,&quot;name&quot;:&quot;paragraph&quot;,&quot;data&quot;:{&quot;style&quot;:{&quot;textIndent&quot;:28}},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;xV1s-1728396768751&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;-&gt; 权重,是一组模型参数,需要质量的度量方式和提高模型预测质量的方法来寻找最佳参数&quot;,&quot;marks&quot;:[]}]}],&quot;state&quot;:{}},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;b5WN-1728396926901&quot;,&quot;name&quot;:&quot;paragraph&quot;,&quot;data&quot;:{&quot;style&quot;:{&quot;textIndent&quot;:28}},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;qSZV-1728396926894&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;&quot;,&quot;marks&quot;:[]}]}],&quot;state&quot;:{}},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;bMbH-1728396928023&quot;,&quot;name&quot;:&quot;paragraph&quot;,&quot;data&quot;:{&quot;style&quot;:{&quot;textIndent&quot;:0}},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;BToo-1728396928022&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;度量方式:损失函数(loss function)&quot;,&quot;marks&quot;:[]}]}],&quot;state&quot;:{}},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;5p62-1728396952160&quot;,&quot;name&quot;:&quot;paragraph&quot;,&quot;data&quot;:{&quot;style&quot;:{&quot;textIndent&quot;:28}},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;D0Qp-1728396952159&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;-&gt; 量化目标实际和预测之间的差距,一般为非负数&quot;,&quot;marks&quot;:[]}]}],&quot;state&quot;:{}},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;Lpp5-1728397015564&quot;,&quot;name&quot;:&quot;paragraph&quot;,&quot;data&quot;:{&quot;style&quot;:{&quot;textIndent&quot;:28}},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;cX7y-1728397015557&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;-&gt; 回归问题损失函数一般用平方误差函数&quot;,&quot;marks&quot;:[]}]}],&quot;state&quot;:{}},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;54Io-1728397080521&quot;,&quot;name&quot;:&quot;paragraph&quot;,&quot;data&quot;:{&quot;style&quot;:{&quot;textIndent&quot;:0}},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;YT3G-1728397080514&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;&quot;,&quot;marks&quot;:[]}]}],&quot;state&quot;:{}},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;Dul3-1728397081243&quot;,&quot;name&quot;:&quot;paragraph&quot;,&quot;data&quot;:{&quot;style&quot;:{&quot;textIndent&quot;:0}},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;f8M7-1728397081242&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;提高方法:随机梯度下降&quot;,&quot;marks&quot;:[]}]}],&quot;state&quot;:{}},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;Haus-1728397096506&quot;,&quot;name&quot;:&quot;paragraph&quot;,&quot;data&quot;:{&quot;style&quot;:{&quot;textIndent&quot;:28}},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;YwTs-1728397096505&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;-&gt; 随机抽取小批量样本,不断地在损失函数递减地方向上更新参数减小误差&quot;,&quot;marks&quot;:[]}]}],&quot;state&quot;:{}},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;kdYc-1728397763099&quot;,&quot;name&quot;:&quot;paragraph&quot;,&quot;data&quot;:{&quot;style&quot;:{&quot;textIndent&quot;:0}},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;LfSK-1728397763092&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;&quot;,&quot;marks&quot;:[]}]}],&quot;state&quot;:{}},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;NWse-1728397764181&quot;,&quot;name&quot;:&quot;paragraph&quot;,&quot;data&quot;:{&quot;style&quot;:{&quot;textIndent&quot;:0}},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;aaWF-1728397764180&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;超参数(hyperparamter),都是训练时固定的参数&quot;,&quot;marks&quot;:[]}]}],&quot;state&quot;:{}},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;AHrK-1728397805748&quot;,&quot;name&quot;:&quot;paragraph&quot;,&quot;data&quot;:{&quot;style&quot;:{&quot;textIndent&quot;:28}},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;prNu-1728397805747&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;-&gt; 批量大小(batch size),每次小批量的样本数&quot;,&quot;marks&quot;:[]}]}],&quot;state&quot;:{}},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;8D5f-1728397849875&quot;,&quot;name&quot;:&quot;paragraph&quot;,&quot;data&quot;:{&quot;style&quot;:{&quot;textIndent&quot;:28}},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;Fy3p-1728397849868&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;-&gt; 学习率(learning rate),每次更新的系数&quot;,&quot;marks&quot;:[]}]}],&quot;state&quot;:{}},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;Yjw1-1728397926667&quot;,&quot;name&quot;:&quot;paragraph&quot;,&quot;data&quot;:{&quot;style&quot;:{&quot;textIndent&quot;:0}},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;Q840-1728397926665&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;&quot;,&quot;marks&quot;:[]}]}],&quot;state&quot;:{}},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;RHm5-1728397927391&quot;,&quot;name&quot;:&quot;paragraph&quot;,&quot;data&quot;:{&quot;style&quot;:{&quot;textIndent&quot;:0}},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;izIT-1728397927389&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;泛化(generalization)&quot;,&quot;marks&quot;:[]}]}],&quot;state&quot;:{}},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;84p5-1728397944029&quot;,&quot;name&quot;:&quot;paragraph&quot;,&quot;data&quot;:{&quot;style&quot;:{&quot;textIndent&quot;:28}},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;FzEk-1728397944028&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;-&gt; 一组能够在从未见过的数据上预测且误差较小的参数&quot;,&quot;marks&quot;:[]}]}],&quot;state&quot;:{}},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;CPmM-1728398015384&quot;,&quot;name&quot;:&quot;paragraph&quot;,&quot;data&quot;:{&quot;style&quot;:{&quot;textIndent&quot;:0}},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;B55l-1728398015382&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;&quot;,&quot;marks&quot;:[]}]}],&quot;state&quot;:{}},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;k5FR-1728398016246&quot;,&quot;name&quot;:&quot;paragraph&quot;,&quot;data&quot;:{&quot;style&quot;:{&quot;textIndent&quot;:0}},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;13iR-1728398016244&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;预测(prediction)和推断(inference)&quot;,&quot;marks&quot;:[]}]}],&quot;state&quot;:{}},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;IGBI-1728398198977&quot;,&quot;name&quot;:&quot;paragraph&quot;,&quot;data&quot;:{&quot;style&quot;:{&quot;textIndent&quot;:0}},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;JYQy-1728398198976&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;&quot;,&quot;marks&quot;:[]}]}],&quot;state&quot;:{}},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;GfyJ-1728398199679&quot;,&quot;name&quot;:&quot;paragraph&quot;,&quot;data&quot;:{&quot;style&quot;:{&quot;textIndent&quot;:0}},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;B9eV-1728398199678&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;神经网络图&quot;,&quot;marks&quot;:[]}]}],&quot;state&quot;:{}},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;B6xm-1728398205595&quot;,&quot;name&quot;:&quot;paragraph&quot;,&quot;data&quot;:{&quot;style&quot;:{&quot;textIndent&quot;:28}},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;MUSC-1728398205594&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;-&gt; 输入层的输入数可以称为特征维度(feature dimensionality)&quot;,&quot;marks&quot;:[]}]}],&quot;state&quot;:{}},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;5ILN-1728398310192&quot;,&quot;name&quot;:&quot;paragraph&quot;,&quot;data&quot;:{&quot;style&quot;:{&quot;textIndent&quot;:28}},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;Zgg6-1728398310191&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;-&gt; 当每个输入都和每个输出连接,称为全连接层(fully-connected layer)或者稠密层(dense layer)&quot;,&quot;marks&quot;:[]}]}],&quot;state&quot;:{}},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;LqZ0-1728918457174&quot;,&quot;name&quot;:&quot;paragraph&quot;,&quot;data&quot;:{&quot;style&quot;:{&quot;textIndent&quot;:28}},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;BX38-1728918457171&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;&quot;,&quot;marks&quot;:[]}]}],&quot;state&quot;:{}},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;6xlB-1728918457286&quot;,&quot;name&quot;:&quot;heading&quot;,&quot;data&quot;:{&quot;level&quot;:&quot;h3&quot;,&quot;style&quot;:{&quot;textIndent&quot;:0}},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;07uS-1728918457285&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;softmax实践&quot;,&quot;marks&quot;:[]}]}],&quot;state&quot;:{}},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;mJQP-1728918468538&quot;,&quot;name&quot;:&quot;paragraph&quot;,&quot;data&quot;:{&quot;style&quot;:{&quot;textIndent&quot;:0}},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;Fwnk-1728918468537&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;使用FasionMNIST数据集,显示样本&quot;,&quot;marks&quot;:[]}]}],&quot;state&quot;:{}}]">
<p>协变量(covariate)、特征(feature)</p>

<p>&nbsp;&nbsp;&nbsp;&nbsp;-&gt; 特征的加权和或这说由权重组成的这种关系决定了目标怎么到达</p>

<p>&nbsp;&nbsp;&nbsp;&nbsp;-&gt; 权重决定了每个特征对于预测值的影响,更倾向于那种特征,像谁一点?</p>

<p>&nbsp;&nbsp;&nbsp;&nbsp;-&gt; 权重,是一组模型参数,需要质量的度量方式和提高模型预测质量的方法来寻找最佳参数</p>

<p>度量方式:损失函数(loss function)</p>

<p>&nbsp;&nbsp;&nbsp;&nbsp;-&gt; 量化目标实际和预测之间的差距,一般为非负数</p>

<p>&nbsp;&nbsp;&nbsp;&nbsp;-&gt; 回归问题损失函数一般用平方误差函数</p>

<p>提高方法:随机梯度下降</p>

<p>&nbsp;&nbsp;&nbsp;&nbsp;-&gt; 随机抽取小批量样本,不断地在损失函数递减地方向上更新参数减小误差</p>

<p>超参数(hyperparamter),都是训练时固定的参数</p>

<p>&nbsp;&nbsp;&nbsp;&nbsp;-&gt; 批量大小(batch size),每次小批量的样本数</p>

<p>&nbsp;&nbsp;&nbsp;&nbsp;-&gt; 学习率(learning rate),每次更新的系数</p>

<p>泛化(generalization)</p>

<p>&nbsp;&nbsp;&nbsp;&nbsp;-&gt; 一组能够在从未见过的数据上预测且误差较小的参数</p>

<p>预测(prediction)和推断(inference)</p>

<p>神经网络图</p>

<p>&nbsp;&nbsp;&nbsp;&nbsp;-&gt; 输入层的输入数可以称为特征维度(feature dimensionality)</p>

<p>&nbsp;&nbsp;&nbsp;&nbsp;-&gt; 当每个输入都和每个输出连接,称为全连接层(fully-connected layer)或者稠密层(dense layer)</p>

<p>softmax实践</p>

<p>使用FasionMNIST数据集,显示样本</p>

<pre>
<code>import torch
import torchvision
from torch.utils import data
from torchvision import transforms
import matplotlib.pyplot as plt

trans = transforms.ToTensor()
mnist_train = torchvision.datasets.FashionMNIST(root = "./data", train=True, transform=trans, download=True)
mnist_test = torchvision.datasets.FashionMNIST(root = "./data", train=True, transform=trans, download=True)

# print(len(mnist_train), len(mnist_test), mnist_train, mnist_train)
print(mnist_train.shape)

def get_fasion_mnist_labels(labels):
    # labels is num dict
    text_lables = ['t-shirt', 'trouser', 'pullover', "dress", "coat", "sandal", "shirt", "sneaker", "bag", "ankle boot"]
    return for i in labels]

print(get_fasion_mnist_labels())

def show_images(imgs, num_rows, num_cols, titles=None, scale=1.5):
    figsize = (num_cols * scale, num_rows * scale)
    _, axes = plt.subplots(num_rows, num_cols, figsize=figsize)
    # axes is n*m to 1*nm   
    axes = axes.flatten()
    for i, (ax, img) in enumerate(zip(axes, imgs)):
      if torch.is_tensor(img):
            ax.imshow(img.numpy())
      else:
            ax.imshow(img)
      ax.axes.get_xaxis().set_visible(False)
      ax.axes.get_yaxis().set_visible(False)
      if titles:
            ax.set_title(titles)
    plt.show()
    return axes

X, y = next(iter(data.DataLoader(mnist_train, batch_size=18)))
show_images(X.reshape(18,28,28), 2, 9 ,titles=get_fasion_mnist_labels(y))
</code></pre>

<p> &nbsp;</p>

<article data-content="[{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;tice-1728919427709&quot;,&quot;name&quot;:&quot;paragraph&quot;,&quot;data&quot;:{},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;kAm5-1728919427710&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;使用内置的数据迭代器,进行随机读取小批量&quot;,&quot;marks&quot;:[]}]}],&quot;state&quot;:{}}]">
<p>使用内置的数据迭代器,进行随机读取小批量</p>

<pre>
<code>train_iter = data.DataLoader(mnist_train, batch_size, shuffle=True, num_workers=get_dataloader_workers())
X1, y1 = next(iter(train_iter))
show_images(X1.reshape(batch_size, 28, 28), 16, 16, titles=get_fasion_mnist_labels(y1))</code></pre>

<p>运行1次</p>

<article _blank="" data-content="[{&quot;type&quot;:&quot;block&quot;,&quot;name&quot;:&quot;image&quot;,&quot;data&quot;:{&quot;version&quot;:1,&quot;url&quot;:&quot;&lt;a target=">
<p> &nbsp;</p>

<article data-content="[{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;UzjH-1728919412224&quot;,&quot;name&quot;:&quot;paragraph&quot;,&quot;data&quot;:{&quot;style&quot;:{&quot;textIndent&quot;:0}},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;I4kt-1728919412223&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;再运行1次,和上一次的结果时不一样的&quot;,&quot;marks&quot;:[]}]}],&quot;state&quot;:{}}]">
<p>再运行1次,和上一次的结果时不一样的</p>

<p> &nbsp;</p>
</article>
</article>
</article>
</article>
</div><script>                                        var loginstr = '<div class="locked">查看本帖全部内容,请<a href="javascript:;"   style="color:#e60000" class="loginf">登录</a>或者<a href="https://bbs.eeworld.com.cn/member.php?mod=register_eeworld.php&action=wechat" style="color:#e60000" target="_blank">注册</a></div>';
                                       
                                        if(parseInt(discuz_uid)==0){
                                                                                                (function($){
                                                        var postHeight = getTextHeight(400);
                                                        $(".showpostmsg").html($(".showpostmsg").html());
                                                        $(".showpostmsg").after(loginstr);
                                                        $(".showpostmsg").css({height:postHeight,overflow:"hidden"});
                                                })(jQuery);
                                        }                </script><script type="text/javascript">(function(d,c){var a=d.createElement("script"),m=d.getElementsByTagName("script"),eewurl="//counter.eeworld.com.cn/pv/count/";a.src=eewurl+c;m.parentNode.insertBefore(a,m)})(document,523)</script>
页: [1]
查看完整版本: 一起读《动手学深度学习(PyTorch版)》- 线性神经网络 - 数据集处理