Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenDocCN
d2l-zh
提交
752a2b75
D
d2l-zh
项目概览
OpenDocCN
/
d2l-zh
通知
2
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,发现更多精彩内容 >>
提交
752a2b75
编写于
5月 18, 2018
作者:
A
Aston Zhang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fit code
上级
0588339e
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
17 addition
and
14 deletion
+17
-14
chapter_supervised-learning/underfit-overfit.md
chapter_supervised-learning/underfit-overfit.md
+17
-14
未找到文件。
chapter_supervised-learning/underfit-overfit.md
浏览文件 @
752a2b75
...
...
@@ -76,10 +76,19 @@ $$y = 1.2x - 3.4x^2 + 5.6x^3 + 5.0 + \text{noise}$$
需要注意的是,我们用以上相同的数据生成函数来生成训练数据集和测试数据集。两个数据集的样本数都是100。
```
{.python .input}
from mxnet import ndarray as nd
from mxnet import autograd
from mxnet import gluon
%matplotlib inline
import sys
sys.path.append('..')
import gluonbook as gb
from mxnet import autograd, gluon, nd
from mxnet.gluon import loss as gloss, nn
import matplotlib as mpl
import matplotlib.pyplot as plt
gb.set_fig_size(mpl)
```
```
{.python .input}
num_train = 100
num_test = 100
true_w = [1.2, -3.4, 5.6]
...
...
@@ -104,16 +113,10 @@ y += .1 * nd.random.normal(shape=y.shape)
以下的训练步骤在
[
使用Gluon的线性回归
](
linear-regression-gluon.md
)
有过详细描述。这里不再赘述。
```
{.python .input}
%matplotlib inline
import matplotlib as mpl
mpl.rcParams['figure.dpi']= 120
import matplotlib.pyplot as plt
def train(X_train, X_test, y_train, y_test):
# 线性回归模型
net = gluon.nn.Sequential()
with net.name_scope():
net.add(gluon.nn.Dense(1))
net = nn.Sequential()
net.add(nn.Dense(1))
net.initialize()
# 设一些默认参数
learning_rate = 0.01
...
...
@@ -125,8 +128,8 @@ def train(X_train, X_test, y_train, y_test):
# 默认SGD和均方误差
trainer = gluon.Trainer(net.collect_params(), 'sgd', {
'learning_rate': learning_rate})
square_loss = gl
uon.l
oss.L2Loss()
# 保存训练和测试损失
square_loss = gloss.L2Loss()
# 保存训练和测试损失
。
train_loss = []
test_loss = []
for e in range(epochs):
...
...
@@ -140,7 +143,7 @@ def train(X_train, X_test, y_train, y_test):
net(X_train), y_train).mean().asscalar())
test_loss.append(square_loss(
net(X_test), y_test).mean().asscalar())
# 打印结果
# 打印结果
。
plt.plot(train_loss)
plt.plot(test_loss)
plt.legend(['train','test'])
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录