From 40bc5e2ef144e9954e6d48b4b8f356395a4b5c9c Mon Sep 17 00:00:00 2001 From: zhangruiyuan Date: Thu, 10 Jun 2021 00:36:12 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=80=E4=BA=9B=E8=A1=A8=E8=BF=B0=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/1.7/16.md | 48 ++++++++++++++++++++++++------------------------ docs/1.7/a.html | 14 ++++++++++++++ 2 files changed, 38 insertions(+), 24 deletions(-) create mode 100644 docs/1.7/a.html diff --git a/docs/1.7/16.md b/docs/1.7/16.md index e6867d85..4df1ad6e 100644 --- a/docs/1.7/16.md +++ b/docs/1.7/16.md @@ -58,7 +58,7 @@ print(x_train.shape) ![../_img/sphx_glr_nn_tutorial_001.png](img/7c783def0bbe536f41ed172041b7e89e.png) -出: +输出如下: ```py (50000, 784) @@ -81,7 +81,7 @@ print(y_train.min(), y_train.max()) ``` -出: +输出如下: ```py tensor([[0., 0., 0., ..., 0., 0., 0.], @@ -140,7 +140,7 @@ print(preds[0], preds.shape) ``` -出: +输出如下: ```py tensor([-2.5964, -2.3153, -2.1321, -2.4480, -2.2930, -1.9507, -2.1289, -2.4175, @@ -168,7 +168,7 @@ print(loss_func(preds, yb)) ``` -出: +输出如下: ```py tensor(2.3735, grad_fn=) @@ -191,7 +191,7 @@ print(accuracy(preds, yb)) ``` -出: +输出如下: ```py tensor(0.0938) @@ -238,7 +238,7 @@ for epoch in range(epochs): ``` -就是这样:我们完全从头开始创建并训练了一个最小的神经网络(在这种情况下,是逻辑回归,因为我们没有隐藏的层)! +就是这样:我们完全从头开始创建并训练了一个最小的神经网络(在这种情况下,是逻辑回归,因为我们没有隐藏层)! 让我们检查损失和准确率,并将其与我们之前获得的进行比较。 我们希望损失会减少,准确率会增加,而且确实如此。 @@ -247,7 +247,7 @@ print(loss_func(model(xb), yb), accuracy(model(xb), yb)) ``` -出: +输出如下: ```py tensor(0.0811, grad_fn=) tensor(1.) @@ -256,9 +256,9 @@ tensor(0.0811, grad_fn=) tensor(1.) ## 使用`torch.nn.functional` -现在,我们将重构代码,使其执行与以前相同的操作,只是我们将开始利用 PyTorch 的`nn`类使其更加简洁和灵活。 从这里开始的每一步,我们都应该使代码中的一个或多个:更短,更易理解和/或更灵活。 +现在,我们将重构代码,使其执行与以前相同的操作,只是我们将开始利用 PyTorch 的`nn`类使其更加简洁和灵活。 从这里开始的每一步,应该使我们的代码占一个或多个特点:更简洁、更易理解、和/或更灵活。 -第一步也是最简单的步骤,就是用`torch.nn.functional`(通常按照惯例将其导入到名称空间`F`中)替换我们的手写激活和损失函数,从而缩短代码长度。 该模块包含`torch.nn`库中的所有函数(而该库的其他部分包含类)。 除了广泛的损失和激活函数外,您还会在这里找到一些方便的函数来创建神经网络,例如合并函数。 (还有一些用于进行卷积,线性层等的函数,但是正如我们将看到的那样,通常可以使用库的其他部分来更好地处理这些函数。) +第一步也是最简单的步骤,就是用`torch.nn.functional`(通常按照惯例将其导入到名称空间`F`中)替换我们的手写激活和损失函数,从而缩短代码长度。 该模块包含`torch.nn`库中的所有函数(而该库的其他部分包含类)。 除了广泛的损失和激活函数外,您还会在这里找到一些方便的函数来创建神经网络,例如池化函数。 (还有一些用于进行卷积,线性层等的函数,但是正如我们将看到的那样,通常可以使用库的其他部分来更好地处理这些函数。) 如果您使用的是负对数似然损失和对数 softmax 激活,那么 Pytorch 会提供结合了两者的单一函数`F.cross_entropy`。 因此,我们甚至可以从模型中删除激活函数。 @@ -279,7 +279,7 @@ print(loss_func(model(xb), yb), accuracy(model(xb), yb)) ``` -出: +输出如下: ```py tensor(0.0811, grad_fn=) tensor(1.) @@ -322,7 +322,7 @@ print(loss_func(model(xb), yb)) ``` -出: +输出如下: ```py tensor(2.3903, grad_fn=) @@ -379,7 +379,7 @@ print(loss_func(model(xb), yb)) ``` -出: +输出如下: ```py tensor(0.0808, grad_fn=) @@ -409,7 +409,7 @@ print(loss_func(model(xb), yb)) ``` -出: +输出如下: ```py tensor(2.4215, grad_fn=) @@ -425,7 +425,7 @@ print(loss_func(model(xb), yb)) ``` -出: +输出如下: ```py tensor(0.0824, grad_fn=) @@ -487,7 +487,7 @@ print(loss_func(model(xb), yb)) ``` -出: +输出如下: ```py tensor(2.2999, grad_fn=) @@ -545,7 +545,7 @@ print(loss_func(model(xb), yb)) ``` -出: +输出如下: ```py tensor(0.0819, grad_fn=) @@ -597,7 +597,7 @@ print(loss_func(model(xb), yb)) ``` -出: +输出如下: ```py tensor(0.0821, grad_fn=) @@ -648,7 +648,7 @@ for epoch in range(epochs): ``` -出: +输出如下: ```py 0 tensor(0.3743) @@ -717,7 +717,7 @@ fit(epochs, model, loss_func, opt, train_dl, valid_dl) ``` -出: +输出如下: ```py 0 0.3120644524335861 @@ -763,7 +763,7 @@ fit(epochs, model, loss_func, opt, train_dl, valid_dl) ``` -出: +输出如下: ```py 0 0.32337012240886687 @@ -812,7 +812,7 @@ fit(epochs, model, loss_func, opt, train_dl, valid_dl) ``` -出: +输出如下: ```py 0 0.30119081069231035 @@ -879,7 +879,7 @@ fit(epochs, model, loss_func, opt, train_dl, valid_dl) ``` -出: +输出如下: ```py 0 0.327303307390213 @@ -896,7 +896,7 @@ print(torch.cuda.is_available()) ``` -出: +输出如下: ```py True @@ -938,7 +938,7 @@ fit(epochs, model, loss_func, opt, train_dl, valid_dl) ``` -出: +输出如下: ```py 0 0.1833980613708496 diff --git a/docs/1.7/a.html b/docs/1.7/a.html new file mode 100644 index 00000000..7ff97344 --- /dev/null +++ b/docs/1.7/a.html @@ -0,0 +1,14 @@ + + + + + + Document + + +
+
  • TIP
  • +

    您可以使用标准的 python 调试器逐步浏览 PyTorch 代码,从而可以在每一步检查各种变量值。 取消注释以下`set_trace()`即可尝试

    +
    + + \ No newline at end of file -- GitLab