# Getting import error

**URL:** <https://discuss.huggingface.co/t/getting-import-error/956>\
**Category:** 🤗Transformers\
**Created:** [September 1, 2020, 11:45pm UTC](https://discuss.huggingface.co/t/getting-import-error/956 "2020-09-01T23:45:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tuhinnlp](https://avatars.discourse-cdn.com/v4/letter/t/e8c25b/32.png) [@tuhinnlp](https://discuss.huggingface.co/u/tuhinnlp)\
**Post date:** [September 1, 2020, 11:45pm UTC](https://discuss.huggingface.co/t/getting-import-error/956/1 "2020-09-01T23:45:35Z")

</div>

```
from transformers import BertPreTrainedModel, RobertaModel
import torch

class RobertaForMD(BertPreTrainedModel): # Metaphor Detection, modified from BertForTokenClassification
    def __init__ (self, config):
        super(). __init__ (config)
        self.num_labels = config.num_labels

        self.bert = RobertaModel(config)
        self.dropout = torch.nn.Dropout(config.hidden_dropout_prob)
        self.classifier = torch.nn.Linear(config.hidden_size, self.config.num_labels)
        # self.loss = torch.nn.BCEWithLogitsLoss(pos_weight=torch.tensor([3], dtype=torch.float32))
        self.loss = torch.nn.BCEWithLogitsLoss()

        self.init_weights()

    def forward(
        self,
        input_ids=None,
        attention_mask=None,
        token_type_ids=None,
        position_ids=None,
        head_mask=None,
        inputs_embeds=None,
        labels=None,
        word_posi=None
    ):
        outputs = self.bert(
            input_ids,
            attention_mask=attention_mask,
            token_type_ids=token_type_ids,
            position_ids=position_ids,
            head_mask=head_mask,
            inputs_embeds=inputs_embeds,
        )

        last_hidden_state = outputs[0]
        last_hidden_state = self.dropout(last_hidden_state)
        batch_size = input_ids.shape[0]

        word_state = torch.empty((0, last_hidden_state.shape[2]), dtype=torch.float32).cuda()
        for i in range(batch_size):
            word_state = torch.cat((word_state, last_hidden_state[i][word_posi[i]].unsqueeze(0)))

        logits = self.classifier(word_state)

        outputs = (logits,) + outputs[2:] # add hidden states and attention if they are here

        if labels is not None:
            loss = self.loss(logits.view(-1), labels.to(torch.float32))
            outputs = (loss,) + outputs

        return outputs # (loss), logits, (hidden_states), (attentions)

```

I am calling this using  
model = RobertaForMD.from\_pretrained(model\_name, num\_labels=1)

Error

File “main.py”, line 276, in main  
model = RobertaForMD.from\_pretrained(model\_name, num\_labels=1)  
File “/nas/home/tuhinc/miniconda3/lib/python3.7/site-packages/transformers/modeling\_utils.py”, line 438, in from\_pretrained  
\*\*kwargs,  
File “/nas/home/tuhinc/miniconda3/lib/python3.7/site-packages/transformers/configuration\_utils.py”, line 199, in from\_pretrained  
config\_dict, kwargs = cls.get\_config\_dict(pretrained\_model\_name\_or\_path, \*\*kwargs)  
File “/nas/home/tuhinc/miniconda3/lib/python3.7/site-packages/transformers/configuration\_utils.py”, line 269, in get\_config\_dict  
raise EnvironmentError(msg)  
OSError: Can’t load ‘roberta-large’. Make sure that:

- ‘roberta-large’ is a correct model identifier listed on ‘[https://huggingface.co/models](https://huggingface.co/models)’

- or ‘roberta-large’ is the correct path to a directory containing a ‘config.json’ file

---

<div class="post-metadata">

**Author:** ![tuhinnlp](https://avatars.discourse-cdn.com/v4/letter/t/e8c25b/32.png) [@tuhinnlp](https://discuss.huggingface.co/u/tuhinnlp)\
**Post date:** [September 2, 2020, 3:06am UTC](https://discuss.huggingface.co/t/getting-import-error/956/3 "2020-09-02T03:06:04Z")

</div>

> [@tuhinnlp](#):
>
> `config`

Not sure how you mean , any code or link which does that ?

---

<div class="post-metadata">

**Author:** ![prajjwal1](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/prajjwal1/32/2246_2.png) [@prajjwal1](https://discuss.huggingface.co/u/prajjwal1)\
**Post date:** [September 2, 2020, 8:13am UTC](https://discuss.huggingface.co/t/getting-import-error/956/4 "2020-09-02T08:13:10Z")

</div>

Oh, sorry I mistook for `AutoModel`. Can you just try doing this instead ?  
`AutoModel.from_pretrained('roberta-large', config)`. This should work.

---

<div class="post-metadata">

**Author:** ![valhalla](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/valhalla/32/4179_2.png) [@valhalla](https://discuss.huggingface.co/u/valhalla)\
**Post date:** [September 2, 2020, 8:25am UTC](https://discuss.huggingface.co/t/getting-import-error/956/5 "2020-09-02T08:25:13Z")

</div>

This worked for me

```auto
model = RobertaForMD.from_pretrained("roberta-large")

```
