current position:Home>Pytorch loss = criterion (outputs, y), y has multiple values

Pytorch loss = criterion (outputs, y), y has multiple values

2022-02-03 01:38:03 CSDN Q & A

pytorch loss = criterion(outputs, y) ,y What if there are multiple values ?
Namely outputs Output 1 It's worth ,y There are several values , as long as outputs be equal to y in 1 Just a value ,loss = criterion(outputs, y) How to write ?

Part of the code is as follows :

for epoch in range(num_epochs):    train_loss = []    # The data in the loader is traversed     for batch, data in enumerate(train_loader):        #batch  Is the number   Indicates that several... Have been carried out batch        #data  Is a binary , The input and label of a data record are stored respectively         # The first dimension of each data is  batch_size = 30  Array of         lstm.train()   # sign LSTM In training ,Dropout  Start working         init_hidden = lstm.initHidden(len(data[0]))   # initialization LSTM The implicit unit variable         optimizer.zero_grad()        x,y = Variable(data[0]), Variable(data[1])  # Extract input and output pairs from data         if use_cuda:           x = x.cuda()           y = y.cuda()        outputs = lstm(x, init_hidden)   # Input LSTM, To produce output outputs        loss = criterion(outputs, y)    # Bring in the loss function and generate loss        loss.backward()  # Back propagation         optimizer.step()   # Update gradient         if use_cuda:            loss = loss.cpu()        train_loss.append(    # Record loss

Refer to the answer 1:

Great gods , How to solve this problem ?

Refer to the answer 2:

copyright notice
author[CSDN Q & A],Please bring the original link to reprint, thank you.

Random recommended