Skip to content

Commit

Permalink
修正:修改计算错误,增加详细说明介绍
Browse files Browse the repository at this point in the history
  • Loading branch information
zergtant committed Feb 21, 2019
1 parent 4a76df2 commit 6fff3a7
Showing 1 changed file with 107 additions and 46 deletions.
153 changes: 107 additions & 46 deletions chapter2/2.1.3-pytorch-basics-nerual-network.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -89,19 +89,24 @@
" # nn.Module子类的函数必须在构造函数中执行父类的构造函数\n",
" super(Net, self).__init__()\n",
" \n",
" # 卷积层 '1'表示输入图片为单通道, '6'表示输出通道数,'5'表示卷积核为3*3\n",
" # 卷积层 '1'表示输入图片为单通道, '6'表示输出通道数,'3'表示卷积核为3*3\n",
" self.conv1 = nn.Conv2d(1, 6, 3) \n",
" #线性层,输入84个特征,输出10个特征\n",
" self.fc1 = nn.Linear(1350, 10)\n",
" #正向传播\n",
" #线性层,输入1350个特征,输出10个特征\n",
" self.fc1 = nn.Linear(1350, 10) #这里的1350是如何计算的呢?这就要看后面的forward函数\n",
" #正向传播 \n",
" def forward(self, x): \n",
" print(x.size()) # 结果:[1, 1, 32, 32]\n",
" # 卷积 -> 激活 -> 池化 \n",
" x = self.conv1(x)\n",
" x = self.conv1(x) #根据卷积的尺寸计算公式,计算结果是30,具体计算公式后面第二张第四节 卷积神经网络 有详细介绍。\n",
" x = F.relu(x)\n",
" x = F.max_pool2d(x, (2, 2))\n",
" print(x.size()) # 结果:[1, 6, 30, 30]\n",
" x = F.max_pool2d(x, (2, 2)) #我们使用池化层,计算结果是15\n",
" x = F.relu(x)\n",
" print(x.size()) # 结果:[1, 6, 15, 15]\n",
" # reshape,‘-1’表示自适应\n",
" #这里做的就是压扁的操作 就是把后面的[1, 6, 15, 15]压扁,变为 [1, 1350]\n",
" x = x.view(x.size()[0], -1) \n",
" print(x.size()) # 这里就是fc1层的的输入1350 \n",
" x = self.fc1(x) \n",
" return x\n",
"\n",
Expand All @@ -128,48 +133,54 @@
"output_type": "stream",
"text": [
"Parameter containing:\n",
"tensor([[[[-0.2569, 0.3030, -0.2508],\n",
" [ 0.2527, 0.3013, -0.1692],\n",
" [ 0.0445, 0.0929, 0.2674]]],\n",
"tensor([[[[ 0.2745, 0.2594, 0.0171],\n",
" [ 0.0429, 0.3013, -0.0208],\n",
" [ 0.1459, -0.3223, 0.1797]]],\n",
"\n",
"\n",
" [[[-0.1670, -0.3166, 0.3315],\n",
" [ 0.1171, 0.3244, -0.2757],\n",
" [ 0.2158, -0.3002, -0.1742]]],\n",
" [[[ 0.1847, 0.0227, -0.1919],\n",
" [-0.0210, -0.1336, -0.2176],\n",
" [-0.2164, -0.1244, -0.2428]]],\n",
"\n",
"\n",
" [[[-0.1313, 0.2859, 0.1639],\n",
" [-0.0260, 0.2024, -0.1361],\n",
" [-0.1011, -0.1267, 0.2546]]],\n",
" [[[ 0.1042, -0.0055, -0.2171],\n",
" [ 0.3306, -0.2808, 0.2058],\n",
" [ 0.2492, 0.2971, 0.2277]]],\n",
"\n",
"\n",
" [[[-0.0267, 0.2839, -0.2581],\n",
" [-0.2461, -0.0073, 0.3096],\n",
" [ 0.2565, -0.2553, 0.3148]]],\n",
" [[[ 0.2134, -0.0644, -0.3044],\n",
" [ 0.0040, 0.0828, -0.2093],\n",
" [ 0.0204, 0.1065, 0.1168]]],\n",
"\n",
"\n",
" [[[ 0.0848, -0.3026, -0.2878],\n",
" [ 0.1124, 0.0947, -0.3182],\n",
" [-0.1941, -0.2659, 0.1424]]],\n",
" [[[ 0.1651, -0.2244, 0.3072],\n",
" [-0.2301, 0.2443, -0.2340],\n",
" [ 0.0685, 0.1026, 0.1754]]],\n",
"\n",
"\n",
" [[[-0.0537, 0.0605, -0.2139],\n",
" [ 0.2403, -0.1542, 0.0416],\n",
" [ 0.0483, 0.1220, -0.1305]]]], requires_grad=True)\n",
" [[[ 0.1691, -0.0790, 0.2617],\n",
" [ 0.1956, 0.1477, 0.0877],\n",
" [ 0.0538, -0.3091, 0.2030]]]], requires_grad=True)\n",
"Parameter containing:\n",
"tensor([ 0.3156, -0.1656, -0.0010, -0.1745, 0.0580, 0.0558], requires_grad=True)\n",
"tensor([ 0.2355, 0.2949, -0.1283, -0.0848, 0.2027, -0.3331],\n",
" requires_grad=True)\n",
"Parameter containing:\n",
"tensor([[-0.0126, 0.0179, 0.0082, ..., 0.0023, 0.0091, -0.0069],\n",
" [ 0.0142, 0.0079, 0.0038, ..., 0.0081, -0.0236, 0.0226],\n",
" [-0.0068, 0.0093, 0.0153, ..., -0.0085, -0.0012, -0.0122],\n",
"tensor([[ 2.0555e-02, -2.1445e-02, -1.7981e-02, ..., -2.3864e-02,\n",
" 8.5149e-03, -6.2071e-04],\n",
" [-1.1755e-02, 1.0010e-02, 2.1978e-02, ..., 1.8433e-02,\n",
" 7.1362e-03, -4.0951e-03],\n",
" [ 1.6187e-02, 2.1623e-02, 1.1840e-02, ..., 5.7059e-03,\n",
" -2.7165e-02, 1.3463e-03],\n",
" ...,\n",
" [-0.0071, 0.0234, -0.0262, ..., -0.0010, -0.0241, -0.0068],\n",
" [ 0.0203, -0.0004, 0.0267, ..., -0.0173, 0.0022, -0.0237],\n",
" [ 0.0247, 0.0200, -0.0201, ..., 0.0081, 0.0236, 0.0140]],\n",
" requires_grad=True)\n",
" [-3.2552e-03, 1.7277e-02, -1.4907e-02, ..., 7.4232e-03,\n",
" -2.7188e-02, -4.6431e-03],\n",
" [-1.9786e-02, -3.7382e-03, 1.2259e-02, ..., 3.2471e-03,\n",
" -1.2375e-02, -1.6372e-02],\n",
" [-8.2350e-03, 4.1301e-03, -1.9192e-03, ..., -2.3119e-05,\n",
" 2.0167e-03, 1.9528e-02]], requires_grad=True)\n",
"Parameter containing:\n",
"tensor([ 0.0198, -0.0144, -0.0156, -0.0073, 0.0185, -0.0177, -0.0012, -0.0125,\n",
" -0.0136, 0.0125], requires_grad=True)\n"
"tensor([ 0.0162, -0.0146, -0.0218, 0.0212, -0.0119, -0.0142, -0.0079, 0.0171,\n",
" 0.0205, 0.0164], requires_grad=True)\n"
]
}
],
Expand Down Expand Up @@ -218,6 +229,16 @@
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"torch.Size([1, 1, 32, 32])\n",
"torch.Size([1, 6, 30, 30])\n",
"torch.Size([1, 6, 15, 15])\n",
"torch.Size([1, 1350])\n"
]
},
{
"data": {
"text/plain": [
Expand All @@ -230,11 +251,31 @@
}
],
"source": [
"input = torch.randn(1, 1, 32, 32)\n",
"input = torch.randn(1, 1, 32, 32) # 这里的对应前面fforward的输入是32\n",
"out = net(input)\n",
"out.size()"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"torch.Size([1, 1, 32, 32])"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"input.size()"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand All @@ -244,7 +285,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -271,14 +312,14 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"29.04529571533203\n"
"28.92203712463379\n"
]
}
],
Expand All @@ -304,7 +345,7 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -313,11 +354,22 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 11,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"torch.Size([1, 1, 32, 32])\n",
"torch.Size([1, 6, 30, 30])\n",
"torch.Size([1, 6, 15, 15])\n",
"torch.Size([1, 1350])\n"
]
}
],
"source": [
"out = net(input)\n",
"out = net(input) # 这里调用的时候会打印出我们在forword函数中打印的x的大小\n",
"criterion = nn.MSELoss()\n",
"loss = criterion(out, y)\n",
"#新建一个优化器,SGD只需要要调整的参数和学习率\n",
Expand All @@ -334,15 +386,24 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"这样,神经网络的数据的一个完整的传播就已经通过PyTorch实现了,下面一章将介绍PyTorch提供的数据加载和处理工具,使用这些工具可以方便的处理所需要的数据"
"这样,神经网络的数据的一个完整的传播就已经通过PyTorch实现了,下面一章将介绍PyTorch提供的数据加载和处理工具,使用这些工具可以方便的处理所需要的数据。\n",
"\n",
"看完这节,大家可能对神经网络模型里面的一些参数的计算方式还有疑惑,这部分会在第二张第四节 卷积神经网络 有详细介绍,并且在第三章 第二节 MNIST数据集手写数字识别 的实践代码中有详细的注释说明。"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Pytorch for Deeplearning",
"display_name": "pytorch 1.0",
"language": "python",
"name": "pytorch"
"name": "pytorch1"
},
"language_info": {
"codemirror_mode": {
Expand All @@ -354,7 +415,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.7"
"version": "3.6.6"
}
},
"nbformat": 4,
Expand Down

0 comments on commit 6fff3a7

Please sign in to comment.