Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
yulanznzn
pytorch_simple_teaching_example
提交
2a4ab4ca
P
pytorch_simple_teaching_example
项目概览
yulanznzn
/
pytorch_simple_teaching_example
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
pytorch_simple_teaching_example
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
未验证
提交
2a4ab4ca
编写于
7月 15, 2022
作者:
L
lizhiTech
提交者:
GitHub
7月 15, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add files via upload
上级
035020b7
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
216 addition
and
0 deletion
+216
-0
demo_infer.py
demo_infer.py
+62
-0
demo_train.py
demo_train.py
+154
-0
未找到文件。
demo_infer.py
0 → 100644
浏览文件 @
2a4ab4ca
import
torch
from
torch
import
nn
from
torchvision
import
datasets
from
torchvision.transforms
import
ToTensor
# ----------加载模型----------
# 定义模型
class
NeuralNetwork
(
nn
.
Module
):
def
__init__
(
self
):
# 初始化,实例化模型的时候就会调用
super
(
NeuralNetwork
,
self
).
__init__
()
self
.
flatten
=
nn
.
Flatten
()
# [64, 1, 28, 28] -> [64, 1*28*28]
self
.
linear_relu_stack
=
nn
.
Sequential
(
nn
.
Linear
(
28
*
28
,
512
),
# [64, 1*28*28] -> [64, 512]
nn
.
ReLU
(),
nn
.
Linear
(
512
,
512
),
# [64, 512] -> [64, 512]
nn
.
ReLU
(),
nn
.
Linear
(
512
,
10
)
# [64, 512] -> [64, 10]
)
def
forward
(
self
,
x
):
# 前向传播,输入数据进网络的时候才会调用
x
=
self
.
flatten
(
x
)
# [64, 1*28*28]
logits
=
self
.
linear_relu_stack
(
x
)
# [64, 10]
return
logits
if
__name__
==
'__main__'
:
# 加载MNIST数据集的测试集
test_data
=
datasets
.
FashionMNIST
(
root
=
"data"
,
train
=
False
,
download
=
True
,
transform
=
ToTensor
(),
)
# 加载模型
model
=
NeuralNetwork
()
model
.
load_state_dict
(
torch
.
load
(
"model.pth"
))
# 种类名称
classes
=
[
"T-shirt/top"
,
"Trouser"
,
"Pullover"
,
"Dress"
,
"Coat"
,
"Sandal"
,
"Shirt"
,
"Sneaker"
,
"Bag"
,
"Ankle boot"
,
]
# 切换到测试模式
model
.
eval
()
# 取第一个图像和对应的标签
image
,
label
=
test_data
[
0
][
0
],
test_data
[
0
][
1
]
with
torch
.
no_grad
():
pred
=
model
(
image
)
# 输入进网络得到输出
predicted
,
actual
=
classes
[
pred
[
0
].
argmax
(
0
)],
classes
[
label
]
# 得到预测值和标签值
print
(
f
'Predicted: "
{
predicted
}
", Actual: "
{
actual
}
"'
)
\ No newline at end of file
demo_train.py
0 → 100644
浏览文件 @
2a4ab4ca
import
torch
from
torch
import
nn
from
torch.utils.data
import
DataLoader
from
torchvision
import
datasets
from
torchvision.transforms
import
ToTensor
# 定义模型
class
NeuralNetwork
(
nn
.
Module
):
def
__init__
(
self
):
# 初始化,实例化模型的时候就会调用
super
(
NeuralNetwork
,
self
).
__init__
()
self
.
flatten
=
nn
.
Flatten
()
# [64, 1, 28, 28] -> [64, 1*28*28]
self
.
linear_relu_stack
=
nn
.
Sequential
(
nn
.
Linear
(
28
*
28
,
512
),
# [64, 1*28*28] -> [64, 512]
nn
.
ReLU
(),
nn
.
Linear
(
512
,
512
),
# [64, 512] -> [64, 512]
nn
.
ReLU
(),
nn
.
Linear
(
512
,
10
)
# [64, 512] -> [64, 10]
)
def
forward
(
self
,
x
):
# 前向传播,输入数据进网络的时候才会调用
x
=
self
.
flatten
(
x
)
# [64, 1*28*28]
logits
=
self
.
linear_relu_stack
(
x
)
# [64, 10]
return
logits
# 训练函数
def
train
(
train_dataloader
,
model
,
loss_fn
,
optimizer
):
"""
训练网络
输入:
train_dataloader: 训练集的dataloader
model: 网络模型
loss_fn: 损失函数
optimizer: 优化器
"""
# 切换到train模式
model
.
train
()
# 遍历dataloader
for
images
,
labels
in
train_dataloader
:
# 将数据和标签加载到device上
images
,
labels
=
images
.
to
(
device
),
labels
.
to
(
device
)
# 输入数据到模型里得到输出
pred
=
model
(
images
)
# 计算输出和标签的loss
loss
=
loss_fn
(
pred
,
labels
)
# 反向推导
optimizer
.
zero_grad
()
loss
.
backward
()
# 步进优化器
optimizer
.
step
()
# 测试函数
def
test
(
test_dataloader
,
model
,
loss_fn
):
"""
测试网络
输入:
test_dataloader: 测试集的dataloader
model: 网络模型
loss_fn: 损失函数
"""
# 测试集大小
size
=
len
(
test_dataloader
.
dataset
)
# 测试集的batch数量
num_batches
=
len
(
test_dataloader
)
# 切换到测试模型
model
.
eval
()
# 记录loss和准确率
test_loss
,
correct
=
0
,
0
# 梯度截断
with
torch
.
no_grad
():
for
images
,
labels
in
test_dataloader
:
# 遍历batch
# 加载到device
images
,
labels
=
images
.
to
(
device
),
labels
.
to
(
device
)
# 输入数据到模型里得到输出
pred
=
model
(
images
)
# 累加loss
test_loss
+=
loss_fn
(
pred
,
labels
).
item
()
# 累加正确率
correct
+=
(
pred
.
argmax
(
1
)
==
labels
).
sum
().
item
()
# 计算平均loss和准确率
test_loss
/=
num_batches
correct
/=
size
print
(
f
"Test Error:
\n
Accuracy:
{
(
100
*
correct
):
>
0.1
f
}
%, Avg loss:
{
test_loss
:
>
8
f
}
\n
"
)
if
__name__
==
'__main__'
:
# ----------数据集----------
# 加载MNIST数据集的训练集
training_data
=
datasets
.
FashionMNIST
(
root
=
"data"
,
train
=
True
,
download
=
True
,
transform
=
ToTensor
(),
)
# 加载MNIST数据集的测试集
test_data
=
datasets
.
FashionMNIST
(
root
=
"data"
,
train
=
False
,
download
=
True
,
transform
=
ToTensor
(),
)
# batch大小
batch_size
=
64
# 创建dataloader
train_dataloader
=
DataLoader
(
training_data
,
batch_size
=
batch_size
)
test_dataloader
=
DataLoader
(
test_data
,
batch_size
=
batch_size
)
# 遍历dataloader
for
images
,
labels
in
test_dataloader
:
print
(
"Shape of X [N, C, H, W]: "
,
images
.
shape
)
# 每个batch数据的形状
print
(
"Shape of y: "
,
labels
.
shape
)
# 每个batch标签的形状
break
# ----------模型----------
# 使用gpu或者cpu进行训练
device
=
"cuda"
if
torch
.
cuda
.
is_available
()
else
"cpu"
# 打印使用的是gpu/cpu
print
(
"Using {} device"
.
format
(
device
))
# 实例化模型
model
=
NeuralNetwork
().
to
(
device
)
# 打印模型结构
print
(
model
)
# ----------训练参数设置----------
loss_fn
=
nn
.
CrossEntropyLoss
()
# 损失函数设置
optimizer
=
torch
.
optim
.
SGD
(
model
.
parameters
(),
lr
=
1e-3
)
# 学习率设置
epochs
=
5
# 训练迭代次数设置
# ----------训练/测试----------
# 开始训练
for
t
in
range
(
epochs
):
# 迭代epochs次
print
(
f
"Epoch
{
t
+
1
}
\n
-------------------------------"
)
# 训练
train
(
train_dataloader
,
model
,
loss_fn
,
optimizer
)
# 当次训练完后测试目前模型在测试集上的表现
test
(
test_dataloader
,
model
,
loss_fn
)
print
(
"Done!"
)
# ----------保存模型----------
# 保存模型
torch
.
save
(
model
.
state_dict
(),
"model.pth"
)
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录