# 模型选择、欠拟合和过拟合

1 Like

1 Like

zszszszs

1 Like

RuntimeError: grad can be implicitly created only for scalar outputs

loss定义成nn.MSELoss()就行

1 Like
``````# 以20为最高幂直接给出一个不确切的解析解
w_=torch.linalg.inv(poly_features.t()@poly_features)@poly_features.t()@labels.reshape(200,1)
# 以3为最高幂直接给出解析解
w_=torch.linalg.inv(poly_features[:,:4].t()@poly_features[:,:4])@poly_features[:,:4].t()@labels.reshape(200,1)
``````
1 Like

``````import math
import torch

max_degree = 20
n_train, n_test = 100, 100
true_w = torch.zeros(max_degree)
true_w[:4] = torch.tensor([5, 1.2, -3.4, 5.6])

features = torch.randn((n_train+n_test, 1))
features = features[torch.randperm(len(features))]
poly_features = torch.pow(features, torch.arange(max_degree))
poly_features /= torch.tensor(
[math.gamma(i+1) for i in range(max_degree)]
)
labels = poly_features @ true_w
labels += torch.randn(labels.shape) * 0.1
labels = labels.reshape(-1, 1)
``````

`weight: [[nan nan nan nan nan ...]]`

train(poly_features[:n_train, :4], poly_features[n_train:, :4],

``````  labels[:n_train], labels[n_train:])
``````

ValueError : Failed to find font DejaVu Sans:style=normal:variant=normal:weight=normal:stretch=normal:size=10.0, and fallback to the default font was disabled

findfont: Font family [‘STIXGeneral’] not found. Falling back to DejaVu Sans

2 Likes

loss = nn.MSELoss(reduction=‘none’)改成 loss = nn.MSELoss()就好了。书上的代码都加了reduction=‘none’，沐神的课件里都没加，我之前是按课件走的，这回从书上粘贴了一次，两回的loss求法不一样就出错了