ValueError:를 사용하여 대상 크기(토치.크기([2,1]))다른 입력 크기(토치.크기([16,1]))가 사용되지 않습니다

0

질문

하려고 해요 모델을 구축에 대한 Quora 질문 쌍 데이터 집합이 출력이 이진 1 또는 0 인지만,이것을 얻을 오류가 있습니다. 내가 알고있는 출력의 모양을 내 모델은 다른 입력에서 모양만,없을 어떻게 해결해야 하는지 알고 있습니다. 일괄 처리 크기가 설정을 16

    class Bert_model (nn.Module):
      def __init__(self) :
        super(Bert_model,self).__init__()
        self.bert =  BertModel.from_pretrained('bert-base-uncased', return_dict=False)
        self.drop_layer = nn.Dropout(.25)
        self.output = nn.Linear(self.bert.config.hidden_size,1)
    
      def forward(self,input_ids,attention_mask):
        _,o2 = self.bert (input_ids =input_ids , attention_mask = attention_mask )
        o2 = self.drop_layer(o2)
        return self.output(o2)

    model = Bert_model()
    
    loss_fn = nn.BCELoss().to(device)

    def train_epoch(
      model, 
      data_loader, 
      loss_fn, 
      optimizer, 
      device, 
      n_examples
    ):
      model = model.train()
    
      losses = []
      correct_predictions = 0
      
      for d in data_loader:
        input_ids = d["input_ids"].to(device)
        attention_mask = d["attention_mask"].to(device)
        targets = d["target"].to(device)
    
        input_ids = input_ids.view(BATCH_SIZE,-1)
        attention_mask = attention_mask.view(BATCH_SIZE,-1)
    
        outputs = model(
          input_ids=input_ids,
          attention_mask=attention_mask
        )
    
        _, preds = torch.max(outputs, dim=1)
    
        targets = targets.unsqueeze(-1)
        loss = loss_fn(F.softmax(outputs,dim=1), targets)
    
        correct_predictions += torch.sum(preds == targets)
        losses.append(loss.item())
    
        loss.backward()
        nn.utils.clip_grad_norm_(model.parameters(), max_norm=1.0)
        optimizer.step()
        optimizer.zero_grad()
    
      return correct_predictions.double() / n_examples, np.mean(losses)

오류:

/usr/local/lib/python3.7/dist-packages/torch/nn/functional.py in
binary_cross_entropy(input, target, weight, size_average, reduce,
reduction)    2913         weight = weight.expand(new_size)    2914 
-> 2915     return torch._C._nn.binary_cross_entropy(input, target, weight, reduction_enum)    2916     2917  ValueError: Using a target
size (torch.Size([2, 1])) that is different to the input size
(torch.Size([16, 1])) is deprecated
deep-learning pytorch
2021-11-21 11:25:25
1

최고의 응답

0

에서 스택 추적이,오류에서 발생 BCELoss 계산이라는 사실로 인해 이 outputs.shape(16, 1)targets.shape(2, 1).

나는 주요 문제에서 당신의 코드: BCELoss 은 비교하는 데 사용되는 확률 분포(을 확인 docs)지만,모델 출력 있는 모양 (n, 1)n 은 배치 크기(의 경우 16). 사실에서 반품 문의 forward 당신이 통과 o2 선형층의 출력 형태는 1.

질문 쌍 데이터 집합을 위해 의미 된 바이너리 분류 작업,그래서 당신이 필요로 변환하여 출력으로는 확률 분포,예를 들어,사용 Sigmoid 또는 설정을 선형 레이어로 출력 크기 2,다음 사용하 softmax.

2021-11-21 15:50:29

또한 전환할 수 있습니다 BCELossCrossEntropyLoss을 의미하는 바이너리 분류 문제입니다.
aretor

나는 변경 기능을(BCEWithLogitsLoss)에 적용되는 s 상 출력,그때 제거 softmax. 문제는 여전히 존재하지만 이제 대상이기 때문에 크기(10,1)및 다른 입력에서(16,1)
BuzzedHub

이 말하는 것은 어렵 오류에 당신의 코드입니다. 는 16 은 정확한 배치 크기,주의깊게 확인할 때 당신의 대상 크기 변경 16~10. 을 피하십시오의 몸을 변경 질문에 그렇지 않으면 대답지 않을 것이 의미가 더 이상입니다.
aretor

다른 언어로

이 페이지는 다른 언어로되어 있습니다

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................