{
"cells": [
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"%matplotlib inline\n",
"%reload_ext autoreload\n",
"%autoreload 1\n",
"%aimport d2lzh_pytorch\n",
"import torch\n",
"import torchvision\n",
"import torchvision.transforms as transforms\n",
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"import sys\n",
"import d2lzh_pytorch as d2l\n",
"from torch import nn\n",
"from torch.nn import init"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"tags": [
"获取和读取数据"
]
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"60000 10000\n"
]
}
],
"source": [
"batch_size = 256\n",
"train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"tags": [
"定义和初始化模型"
]
},
"outputs": [],
"source": [
"from collections import OrderedDict\n",
"\n",
"num_inputs = 784\n",
"num_outputs = 10\n",
"net = nn.Sequential(\n",
" # FlattenLayer(),\n",
" # nn.Linear(num_inputs, num_outputs)\n",
" OrderedDict([\n",
" ('flatten', d2l.FlattenLayer()),\n",
" ('linear', nn.Linear(num_inputs, num_outputs))\n",
" ])\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Parameter containing:\n",
"tensor([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], requires_grad=True)"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"init.normal_(net.linear.weight, mean=0, std=0.01)\n",
"init.constant_(net.linear.bias, val=0) "
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"tags": [
"softmax和交叉熵损失函数"
]
},
"outputs": [],
"source": [
"loss = nn.CrossEntropyLoss()"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"tags": [
"定义优化算法"
]
},
"outputs": [],
"source": [
"optimizer = torch.optim.SGD(net.parameters(), lr=0.1)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"tags": [
"训练模型"
]
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"epoch 1, loss 0.0031, train acc 0.749, test acc 0.777\n",
"epoch 2, loss 0.0022, train acc 0.815, test acc 0.784\n",
"epoch 3, loss 0.0021, train acc 0.826, test acc 0.817\n",
"epoch 4, loss 0.0020, train acc 0.832, test acc 0.819\n",
"epoch 5, loss 0.0019, train acc 0.837, test acc 0.825\n"
]
}
],
"source": [
"num_epochs = 5\n",
"d2l.train_softmax(net, train_iter, test_iter, loss, num_epochs, batch_size, None, None, optimizer)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"celltoolbar": "Tags",
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.10"
}
},
"nbformat": 4,
"nbformat_minor": 4
}