会员可以在此提问,百战未来老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 135040个问题

epochs = 100    #训练的次数

loss_epoch = []  # 记录损失

for epoch in range(epochs):

    total_loss = 0.0   # 损失初始化为0

    train_samples = 0  # 训练样本数初始化为0

    for train_x, train_y in dataloader:

        # 将1个batch的数据送给模型

        y_pred = mode1(train_x.type(torch.float32))

        # 计算损失

        loss = loss_fn(y_pred, train_y.type(torch.float32))

        # 梯度清零

        optimizer.zero_grad()

        # 自动微分

        loss.backward()

        # 优化更新参数

        optimizer.step()

        # 累计损失和样本数

        total_loss += loss.item()

        train_samples += len(train_y)


    # 计算平均损失

    epoch_loss = total_loss / train_samples

    # 记录平均损失

    loss_epoch.append(epoch_loss)


报错:


d:\miniconda3\envs\DeepLearn\Lib\site-packages\torch\nn\modules\loss.py:626: UserWarning: Using a target size (torch.Size([32])) that is different to the input size (torch.Size([32, 1])). This will likely lead to incorrect results due to broadcasting. Please ensure they have the same size.

  return F.mse_loss(input, target, reduction=self.reduction)

d:\miniconda3\envs\DeepLearn\Lib\site-packages\torch\nn\modules\loss.py:626: UserWarning: Using a target size (torch.Size([4])) that is different to the input size (torch.Size([4, 1])). This will likely lead to incorrect results due to broadcasting. Please ensure they have the same size.

  return F.mse_loss(input, target, reduction=self.reduction)


AI Agent智能体开发大厂孵化器/第二十阶段:【进阶】深度学习框架与神经网络架构/PyTorch框架 1楼
AI Agent智能体开发大厂孵化器/第二十阶段:【进阶】深度学习框架与神经网络架构/PyTorch框架 2楼
AI Agent智能体开发大厂孵化器/第二十阶段:【进阶】深度学习框架与神经网络架构/PyTorch框架 3楼

课程分类

百战未来微信公众号

百战未来微信小程序

©2014-2026百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637