# Question NLP course from Huggingface

**URL:** <https://discuss.huggingface.co/t/question-nlp-course-from-huggingface/50498>\
**Category:** Course\
**Created:** [August 12, 2023, 6:53am UTC](https://discuss.huggingface.co/t/question-nlp-course-from-huggingface/50498 "2023-08-12T06:53:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![DannyAI](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/dannyai/32/18432_2.png) [@DannyAI](https://discuss.huggingface.co/u/DannyAI)\
**Post date:** [August 12, 2023, 6:53am UTC](https://discuss.huggingface.co/t/question-nlp-course-from-huggingface/50498/1 "2023-08-12T06:53:22Z")

</div>

So I am following the Huggingface course for Question answering and I am getting this error

> **AttributeError** : ‘TFBertForQuestionAnswering’ object has no attribute ‘prepare\_tf\_dataset’

from the code given in the course

> tf\_train\_dataset = model.prepare\_tf\_dataset(  
> train\_dataset,  
> collate\_fn=data\_collator,  
> shuffle=True,  
> batch\_size=16,  
> )  
> tf\_eval\_dataset = model.prepare\_tf\_dataset(  
> validation\_dataset,  
> collate\_fn=data\_collator,  
> shuffle=False,  
> batch\_size=16,  
> )

Please what can be done to solve this issue?  
package versions:  
transformers → 4.18.0  
datasets- → 2.14.4  
tensorflow --\>2.10.0  
Here is the course link: [Question answering - Hugging Face NLP Course](https://huggingface.co/learn/nlp-course/chapter7/7?fw=tf#post-processing)

---

<div class="post-metadata">

**Author:** ![Apoorva7](https://avatars.discourse-cdn.com/v4/letter/a/f1d935/32.png) [@Apoorva7](https://discuss.huggingface.co/u/Apoorva7)\
**Post date:** [August 21, 2023, 7:04am UTC](https://discuss.huggingface.co/t/question-nlp-course-from-huggingface/50498/2 "2023-08-21T07:04:17Z")

</div>

I’m also getting similar error for Machine translation task using MBART.

`AttributeError: 'MBartForConditionalGeneration' object has no attribute 'prepare_tf_dataset'`

code:

```auto
train_dataset = model.prepare_tf_dataset(
    tokenized_datasets["train"],
    batch_size=batch_size,
    shuffle=True,
    collate_fn=data_collator,
)

```

Kindly share any solution.

---

<div class="post-metadata">

**Author:** ![DannyAI](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/dannyai/32/18432_2.png) [@DannyAI](https://discuss.huggingface.co/u/DannyAI)\
**Post date:** [August 21, 2023, 7:47am UTC](https://discuss.huggingface.co/t/question-nlp-course-from-huggingface/50498/3 "2023-08-21T07:47:22Z")

</div>

**Here’s what I did for this similar error, more of a hack around it.**

> tokenized\_datasets = raw\_datasets.map(  
> prepare\_train\_features,  
> batched=True,  
> batch\_size=10,  
> remove\_columns=raw\_datasets[“train”].column\_names,  
> # num\_proc=3,  
> )

Having tokenized my dataset, then I used the tensorflow to convert the data to tensorflow format.

**Convert numpy format dataset to TensorFlow format**

> train\_set = tf.data.Dataset.from\_tensor\_slices((  
> {  
> “input\_ids”: tokenized\_datasets[“train”][“input\_ids”],  
> “attention\_mask”: tokenized\_datasets[“train”][“attention\_mask”]  
> },  
> {  
> “start\_positions”: tokenized\_datasets[“train”][“start\_positions”],  
> “end\_positions”: tokenized\_datasets[“train”][“end\_positions”]  
> }  
> ))

> validation\_set = tf.data.Dataset.from\_tensor\_slices((  
> {  
> “input\_ids”: tokenized\_datasets[“validation”][“input\_ids”],  
> “attention\_mask”: tokenized\_datasets[“validation”][“attention\_mask”]  
> },  
> {  
> “start\_positions”: tokenized\_datasets[“validation”][“start\_positions”],  
> “end\_positions”: tokenized\_datasets[“validation”][“end\_positions”]  
> }  
> ))

> print(train\_set) # checking the shape of the data  
> print(validation\_set)

**Define the batch size**

> batch\_size = 5 # Adjust this value as needed  
> optimizer = tf.keras.optimizers.Adam(learning\_rate=5e-5)

**Optionally uncomment the next line for float16 training**

> tf.keras.mixed\_precision.set\_global\_policy(“mixed\_float16”)  
> model.compile(optimizer=optimizer)  
> train\_set = train\_set.batch(batch\_size)  
> validation\_set = validation\_set.batch(batch\_size)  
> model.fit(train\_set, validation\_data=validation\_set, epochs=1)

Hope this helps.

---

<div class="post-metadata">

**Author:** ![Apoorva7](https://avatars.discourse-cdn.com/v4/letter/a/f1d935/32.png) [@Apoorva7](https://discuss.huggingface.co/u/Apoorva7)\
**Post date:** [August 21, 2023, 8:16am UTC](https://discuss.huggingface.co/t/question-nlp-course-from-huggingface/50498/4 "2023-08-21T08:16:35Z")

</div>

Thanks for sharing, I’ll try it.
