最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

deep learning - A GRU Neural Network Training Issue in PyTorch on Windows - Stack Overflow

programmeradmin0浏览0评论

I have been building a GRU neural network in Python using PyTorch. When I attempt to train it, I encounter the following error, and the process gets stuck without displaying any further information. I have PyTorch version 2.5.1+cpu installed on Windows. What could be the possible cause of this error, and how can I resolve it?

Error:

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)

Code:

dataloader_train = DataLoader(
  dataset_train, shuffle=True, batch_size=32
)

class Net(nn.Module):
    def __init__(self):
        super().__init__()
        # Define RNN layer
        self.gru = nn.GRU(
            input_size=1,
            hidden_size=32,
            num_layers=2,
            batch_first=True,
        )
        self.fc = nn.Linear(32, 1)

    def forward(self, x):
        h0 = torch.zeros(2, x.size(0), 32)
        out, _ = self.gru(x, h0)  
        out = self.fc(out[:, -1, :])
        return out


net = Net()
# Set up MSE loss
criterion = nn.MSELoss()
optimizer = optim.Adam(
  net.parameters(), lr=0.0001
)

for epoch in range(3):
    for seqs, labels in dataloader_train:
        # Reshape model inputs
        seqs = seqs.view(32, 96, 1)   #[batch_size, seq_length, input_size]
        # Get model outputs
        outputs = net(seqs)
        # Compute loss
        loss = criterion(outputs, labels)
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
    print(f"Epoch {epoch+1}, Loss: {loss.item()}")

Thank you very much for your assistance.

error neural network torch

发布评论

评论列表(0)

  1. 暂无评论