批量大小越大,则梯度下降越快?
(ai) ref: https://www.perplexity.ai/search/zai-pytorch-zhong-ruo-wo-diao-XuETZW8eSoSQFqW3H_kRqw
如果使用 SGD,是的:
1lr = 0.032num_epochs = 33net = linreg4loss = squared_loss5
6for epoch in range(num_epochs):7 for X, y in data_iter(batch_size, features, labels):8 l = loss(net(X, w, b), y) # X和y的小批量损失9 # 因为l形状是(batch_size,1),而不是一个标量。l中的所有元素被加到一起,10 # 并以此计算关于[w,b]的梯度11 l.sum().backward()12 sgd([w, b], lr, batch_size) # 使用参数的梯度更新参数13 with torch.no_grad():14 train_l = loss(net(features, w, b), labels)15 print(f'epoch {epoch + 1}, loss {float(train_l.mean()):f}')