No need to assign parameters individually; no need to define a loss function; no need to manually implement mini-batch stochastic gradient descent
Complex model, with all basic components
every iteration
Completely traverse the data set once
(Get a mini-batch input and corresponding labels from it)
Call net(x) to generate predictions and calculate loss 1 (forward propagation)
Compute gradients via backpropagation
Update model parameters by calling the optimizer
Calculate the loss after each iteration and print it to monitor the training process
num_epochs =3for epoch inrange(num_epochs):for X, y in data_iter:
l = loss(net(X),y)
trainer.zero_grad()
l.backward()
trainer.step()
l = loss(net(features), labels)print(f'epoch {
epoch +1}, loss {
l:f}')
Compare the true parameters of the generated data set with the model parameters obtained by training on the limited data set
Access the desired layer from net and read the weights and biases of that layer
w = net[0].weight.data
print('w的估计误差:', true_w - w.reshape(true_w.shape))
b = net[0].bias.data
print('b的估计误差:', true_b - b)