2

I used a pretrained Roberta model,and this is my roberta model.the pretrained model is https://huggingface.co/rinna/japanese-roberta-base And I install the sentencepiece.

!pip install sentencepiece
class RoBERTaClass(torch.nn.Module):
  def __init__(self, pretrained, drop_rate, otuput_size):
    super().__init__()
    self.roberta = RobertaForMaskedLM.from_pretrained(pretrained)
    self.drop = torch.nn.Dropout(drop_rate)
    self.fc = torch.nn.Linear(768, otuput_size)
  
  def forward(self, ids, mask):
    _, out = self.roberta(ids, attention_mask=mask,return_dict=False)
    out = self.fc(self.drop(out))
    return out
/usr/local/lib/python3.7/dist-packages/transformers/tokenization_utils_base.py:2269: FutureWarning: The `pad_to_max_length` argument is deprecated and will be removed in a future version, use `padding=True` or `padding='longest'` to pad to the longest sequence in the batch, or use `padding='max_length'` to pad to a max length. In this case, you can give a specific length with `max_length` (e.g. `max_length=45`) or leave max_length to None to pad to the maximal input size of the model (e.g. 512 for Bert).
  FutureWarning,
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-20-49ec9d027d5c> in <module>()
     12 
     13 # モデルの学習
---> 14 log = train_model(dataset_train, dataset_valid, BATCH_SIZE, model, criterion, optimizer, NUM_EPOCHS, device=device)
     15 
     16 # 正解率の算出

2 frames
<ipython-input-16-7edee8da91ec> in forward(self, ids, mask)
      7 
      8   def forward(self, ids, mask):
----> 9     _, out = self.roberta(ids, attention_mask=mask,return_dict=False)
     10     out = self.fc(self.drop(out))
     11     return out

ValueError: not enough values to unpack (expected 2, got 1)

what should i do? i dont know where should i fix.

and i fix the model another problem i have.

class RoBERTaClass(torch.nn.Module):
  def __init__(self, pretrained, drop_rate, otuput_size):
    super().__init__()
    self.roberta = RobertaForMaskedLM.from_pretrained(pretrained)
    self.drop = torch.nn.Dropout(drop_rate)
    self.fc = torch.nn.Linear(768, otuput_size)  # ROBERTAの出力に合わせて768次元を指定
  
  def forward(self, ids, mask):
    out = self.roberta(ids, attention_mask=mask,return_dict=False)
    out = self.fc(self.drop(out))
    return out
TypeError                                 Traceback (most recent call last)
<ipython-input-24-49ec9d027d5c> in <module>()
     12 
     13 # モデルの学習
---> 14 log = train_model(dataset_train, dataset_valid, BATCH_SIZE, model, criterion, optimizer, NUM_EPOCHS, device=device)
     15 
     16 # 正解率の算出

5 frames
/usr/local/lib/python3.7/dist-packages/torch/nn/functional.py in dropout(input, p, training, inplace)
   1277     if p < 0.0 or p > 1.0:
   1278         raise ValueError("dropout probability has to be between 0 and 1, " "but got {}".format(p))
-> 1279     return _VF.dropout_(input, p, training) if inplace else _VF.dropout(input, p, training)
   1280 
   1281 

TypeError: dropout(): argument 'input' (position 1) must be Tensor, not tuple

and this is my creatdataset class.Where do i need to fix.thank you for answer.

class CreateDataset(Dataset):
  def __init__(self, X, y, tokenizer, max_len):
    self.X = X
    self.y = y
    self.tokenizer = tokenizer
    self.max_len = max_len

  def __len__(self):  # len(Dataset)で返す値を指定
    return len(self.y)

  def __getitem__(self, index):  # Dataset[index]で返す値を指定
    text = self.X[index]
    inputs = self.tokenizer.encode_plus(
      text,
      add_special_tokens=True,
      max_length=self.max_len,
      pad_to_max_length=True
    )
    ids = inputs['input_ids']
    mask = inputs['attention_mask']

    return {
      'ids': torch.LongTensor(ids),
      'mask': torch.LongTensor(mask),
      'labels': torch.Tensor(self.y[index])
    }
Chloe
  • 21
  • 2
  • assign to single variable - `out = ...` instead of `_, out = ...` - and use `print()` to see what you really get from this function. – furas May 10 '22 at 15:38
  • your links shows example with single variable - `outputs = model(...)` - why do you use two variable `_, out` ? – furas May 10 '22 at 15:40
  • Thank you sir. And i used `out=.....`,i got another problem is TypeError: dropout(): argument 'input' (position 1) must be Tensor, not tuple.What should I do?@furas – Chloe May 10 '22 at 15:47
  • you should better read example from your link - in next line it uses `outputs[0]` instead of `outputs` - and you may also need `dropout( out[0] )` – furas May 10 '22 at 15:59
  • sry,sir and where need to fix.Thank you for your answer.@furas – Chloe May 10 '22 at 16:18
  • you have `drop(out)` only in one place - in line after `outputs = model(...)` - so you need `out = self.fc( self.drop(out[0]) )` – furas May 10 '22 at 19:42
  • Sry,sir.It doesn't work.@furas – Chloe May 10 '22 at 19:49
  • sentence `it doesn't works` is useless for us. If you get new error message then you should show it. We can't run it, and we can't read in your mind. You have to put all details in question. Frankly, your main problem was resolved and now you have different problem and you should rather create new question on new page. – furas May 10 '22 at 20:02
  • BTW: first you could use `print()` (`print(type(..))`, `print(len(...))`, etc.) to see what you really have in variables - it is called `"print debuging"` and it helps to see what code is doing. – furas May 10 '22 at 20:04

0 Answers0