Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Laurence001
d2l-zh
提交
e320bf8a
D
d2l-zh
项目概览
Laurence001
/
d2l-zh
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
D
d2l-zh
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
e320bf8a
编写于
12月 08, 2021
作者:
A
Aston Zhang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
sync lib
上级
463ed646
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
10 addition
and
14 deletion
+10
-14
d2l/tensorflow.py
d2l/tensorflow.py
+3
-5
d2l/torch.py
d2l/torch.py
+7
-9
未找到文件。
d2l/tensorflow.py
浏览文件 @
e320bf8a
...
...
@@ -1352,10 +1352,8 @@ def train_concise_ch11(trainer_fn, hyperparams, data_iter, num_epochs=2):
net
.
add
(
tf
.
keras
.
layers
.
Dense
(
1
,
kernel_initializer
=
tf
.
random_normal_initializer
(
stddev
=
0.01
)))
optimizer
=
trainer_fn
(
**
hyperparams
)
# 注意:MeanSquaredError计算平方误差时不带系数1/2
loss
=
tf
.
keras
.
losses
.
MeanSquaredError
()
# 注意:L2Loss=1/2*MSELoss。
# TensorFlow的MSE损失与MXNet的L2损失大概相差2倍。
# 因此,我们将TensorFlow中的损失减半
animator
=
d2l
.
Animator
(
xlabel
=
'epoch'
,
ylabel
=
'loss'
,
xlim
=
[
0
,
num_epochs
],
ylim
=
[
0.22
,
0.35
])
n
,
timer
=
0
,
d2l
.
Timer
()
...
...
@@ -1363,7 +1361,7 @@ def train_concise_ch11(trainer_fn, hyperparams, data_iter, num_epochs=2):
for
X
,
y
in
data_iter
:
with
tf
.
GradientTape
()
as
g
:
out
=
net
(
X
)
l
=
loss
(
y
,
out
)
/
2
l
=
loss
(
y
,
out
)
params
=
net
.
trainable_variables
grads
=
g
.
gradient
(
l
,
params
)
optimizer
.
apply_gradients
(
zip
(
grads
,
params
))
...
...
@@ -1372,7 +1370,7 @@ def train_concise_ch11(trainer_fn, hyperparams, data_iter, num_epochs=2):
timer
.
stop
()
p
=
n
/
X
.
shape
[
0
]
q
=
p
/
tf
.
data
.
experimental
.
cardinality
(
data_iter
).
numpy
()
r
=
(
d2l
.
evaluate_loss
(
net
,
data_iter
,
loss
)
/
2
,)
r
=
(
d2l
.
evaluate_loss
(
net
,
data_iter
,
loss
),)
animator
.
add
(
q
,
r
)
timer
.
start
()
print
(
f
'loss:
{
animator
.
Y
[
0
][
-
1
]:.
3
f
}
,
{
timer
.
avg
():.
3
f
}
sec/epoch'
)
...
...
d2l/torch.py
浏览文件 @
e320bf8a
...
...
@@ -259,15 +259,13 @@ def train_epoch_ch3(net, train_iter, loss, updater):
if
isinstance
(
updater
,
torch
.
optim
.
Optimizer
):
# 使用PyTorch内置的优化器和损失函数
updater
.
zero_grad
()
l
.
backward
()
l
.
sum
().
backward
()
updater
.
step
()
metric
.
add
(
float
(
l
)
*
len
(
y
),
accuracy
(
y_hat
,
y
),
y
.
size
().
numel
())
else
:
# 使用定制的优化器和损失函数
l
.
sum
().
backward
()
updater
(
X
.
shape
[
0
])
metric
.
add
(
float
(
l
.
sum
()),
accuracy
(
y_hat
,
y
),
y
.
numel
())
metric
.
add
(
float
(
l
.
sum
()),
accuracy
(
y_hat
,
y
),
y
.
numel
())
# 返回训练损失和训练精度
return
metric
[
0
]
/
metric
[
2
],
metric
[
1
]
/
metric
[
2
]
...
...
@@ -1415,8 +1413,8 @@ def train_concise_ch11(trainer_fn, hyperparams, data_iter, num_epochs=4):
optimizer
=
trainer_fn
(
net
.
parameters
(),
**
hyperparams
)
loss
=
nn
.
MSELoss
()
# L2Loss=1/2*MSELoss
# 注意:MSELoss计算平方误差时不带系数1/2
loss
=
nn
.
MSELoss
(
reduction
=
'none'
)
animator
=
d2l
.
Animator
(
xlabel
=
'epoch'
,
ylabel
=
'loss'
,
xlim
=
[
0
,
num_epochs
],
ylim
=
[
0.22
,
0.35
])
n
,
timer
=
0
,
d2l
.
Timer
()
...
...
@@ -1425,14 +1423,14 @@ def train_concise_ch11(trainer_fn, hyperparams, data_iter, num_epochs=4):
optimizer
.
zero_grad
()
out
=
net
(
X
)
y
=
y
.
reshape
(
out
.
shape
)
l
=
loss
(
out
,
y
)
/
2
l
.
backward
()
l
=
loss
(
out
,
y
)
l
.
mean
().
backward
()
optimizer
.
step
()
n
+=
X
.
shape
[
0
]
if
n
%
200
==
0
:
timer
.
stop
()
animator
.
add
(
n
/
X
.
shape
[
0
]
/
len
(
data_iter
),
(
d2l
.
evaluate_loss
(
net
,
data_iter
,
loss
)
/
2
,))
(
d2l
.
evaluate_loss
(
net
,
data_iter
,
loss
),))
timer
.
start
()
print
(
f
'loss:
{
animator
.
Y
[
0
][
-
1
]:.
3
f
}
,
{
timer
.
avg
():.
3
f
}
sec/epoch'
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录