# Error occuring during usig .to\_tf\_dataset()

**URL:** </static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Ferror-occuring-during-usig-to-tf-dataset%2F65174%3E%5C
**Category:** 🤗Transformers\
**Created:** [December 9, 2023, 2:57pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Ferror-occuring-during-usig-to-tf-dataset%2F65174 "2023-12-09T14:57:12Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![OleksiiVNTU](https://avatars.discourse-cdn.com/v4/letter/o/df705f/32.png) [@OleksiiVNTU](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FOleksiiVNTU)%5C
**Post date:** [December 9, 2023, 2:57pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Ferror-occuring-during-usig-to-tf-dataset%2F65174%2F1 "2023-12-09T14:57:12Z")

</div>

![image](https://us1.discourse-cdn.com/hellohellohello/original/3X/9/7/97322e21091e9aa078faaf6078419132cdcf0840.png)  
What should I do in this case? The continuation of the error:“AttributeError: module ‘numpy’ has no attribute ‘object’.  
`np.object` was a deprecated alias for the builtin `object`. To avoid this error in existing code, use `object` by itself. Doing this will not modify any behavior and is safe.  
The aliases was originally deprecated in NumPy 1.20; for more details and guidance see the original release note at:  
[NumPy 1.20.0 Release Notes — NumPy v2.0.dev0 Manual](https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations)”

---

<div class="post-metadata">

**Author:** ![raygx](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/raygx/32/17328_2.png) [@raygx](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fraygx)%5C
**Post date:** [December 16, 2023, 3:02am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Ferror-occuring-during-usig-to-tf-dataset%2F65174%2F2 "2023-12-16T03:02:07Z")

</div>

## I am also getting this error. This is the log from Kaggle notebook

AttributeError Traceback (most recent call last)  
Cell In[98], line 2  
1 print(“Preparing Training and Testing sets to TRAIN the MODEL”)  
----\> 2 tf\_train\_set = model.prepare\_tf\_dataset(  
3 tokenized\_inputs[“train”],  
4 shuffle=True,  
5 batch\_size=16,  
6 collate\_fn=data\_collator,  
7 )  
9 tf\_test\_set = model.prepare\_tf\_dataset(  
10 tokenized\_inputs[“test”],  
11 shuffle=False,  
12 batch\_size=16,  
13 collate\_fn=data\_collator,  
14 )

File /opt/conda/lib/python3.10/site-packages/transformers/modeling\_tf\_utils.py:1488, in TFPreTrainedModel.prepare\_tf\_dataset(self, dataset, batch\_size, shuffle, tokenizer, collate\_fn, collate\_fn\_args, drop\_remainder, prefetch)  
1486 if drop\_remainder is None:  
1487 drop\_remainder = shuffle  
 → 1488 tf\_dataset = dataset.to\_tf\_dataset(  
1489 columns=feature\_cols,  
1490 label\_cols=label\_cols,  
1491 batch\_size=batch\_size,  
1492 shuffle=shuffle,  
1493 drop\_remainder=drop\_remainder,  
1494 collate\_fn=collate\_fn,  
1495 collate\_fn\_args=collate\_fn\_args,  
1496 prefetch=prefetch,  
1497 )  
1498 return tf\_dataset

File /opt/conda/lib/python3.10/site-packages/datasets/arrow\_dataset.py:381, in TensorflowDatasetMixin.to\_tf\_dataset(self, columns, batch\_size, shuffle, collate\_fn, drop\_remainder, collate\_fn\_args, label\_cols, dummy\_labels, prefetch)  
378 retained\_columns = [key for key in self.features.keys() if key in cols\_to\_retain]  
379 dataset = self.with\_format(“numpy”, columns=retained\_columns)  
 → 381 columns\_to\_dtypes, output\_signature = self.\_get\_output\_signature(  
382 dataset, collate\_fn, collate\_fn\_args, batch\_size=batch\_size if drop\_remainder else None  
383 )  
384 all\_columns = list(columns\_to\_dtypes.keys())  
385 all\_dtypes = list(columns\_to\_dtypes.values())

File /opt/conda/lib/python3.10/site-packages/datasets/arrow\_dataset.py:244, in TensorflowDatasetMixin.\_get\_output\_signature(dataset, collate\_fn, collate\_fn\_args, batch\_size)  
242 raise ValueError(“Unable to get the output signature because the dataset is empty.”)  
243 test\_batch\_size = min(len(dataset), 4)  
 → 244 test\_batch = dataset[:test\_batch\_size]  
245 test\_batch = [{key: value[i] for key, value in test\_batch.items()} for i in range(test\_batch\_size)]  
246 test\_batch = collate\_fn(test\_batch, \*\*collate\_fn\_args)

File /opt/conda/lib/python3.10/site-packages/datasets/arrow\_dataset.py:1764, in Dataset. **getitem** (self, key)  
1762 def **getitem** (self, key): # noqa: F811  
1763 “”“Can be used to index columns (by string names) or rows (by integer index or iterable of indices or bools).”“”  
 → 1764 return self.\_getitem(  
1765 key,  
1766 )

File /opt/conda/lib/python3.10/site-packages/datasets/arrow\_dataset.py:1749, in Dataset.\_getitem(self, key, decoded, \*\*kwargs)  
1747 formatter = get\_formatter(format\_type, features=self.features, decoded=decoded, \*\*format\_kwargs)  
1748 pa\_subtable = query\_table(self.\_data, key, indices=self.\_indices if self.\_indices is not None else None)  
 → 1749 formatted\_output = format\_table(  
1750 pa\_subtable, key, formatter=formatter, format\_columns=format\_columns, output\_all\_columns=output\_all\_columns  
1751 )  
1752 return formatted\_output

File /opt/conda/lib/python3.10/site-packages/datasets/formatting/formatting.py:540, in format\_table(table, key, formatter, format\_columns, output\_all\_columns)  
538 else:  
539 pa\_table\_to\_format = pa\_table.drop(col for col in pa\_table.column\_names if col not in format\_columns)  
 → 540 formatted\_output = formatter(pa\_table\_to\_format, query\_type=query\_type)  
541 if output\_all\_columns:  
542 if isinstance(formatted\_output, MutableMapping):

File /opt/conda/lib/python3.10/site-packages/datasets/formatting/formatting.py:285, in Formatter. **call** (self, pa\_table, query\_type)  
283 return self.format\_column(pa\_table)  
284 elif query\_type == “batch”:  
 → 285 return self.format\_batch(pa\_table)

File /opt/conda/lib/python3.10/site-packages/datasets/formatting/formatting.py:346, in NumpyFormatter.format\_batch(self, pa\_table)  
345 def format\_batch(self, pa\_table: pa.Table) → dict:  
 → 346 batch = self.numpy\_arrow\_extractor(\*\*self.np\_array\_kwargs).extract\_batch(pa\_table)  
347 if self.decoded:  
348 batch = self.python\_features\_decoder.decode\_batch(batch)

File /opt/conda/lib/python3.10/site-packages/datasets/formatting/formatting.py:160, in NumpyArrowExtractor.extract\_batch(self, pa\_table)  
159 def extract\_batch(self, pa\_table: pa.Table) → dict:  
 → 160 return {col: self.\_arrow\_array\_to\_numpy(pa\_table[col]) for col in pa\_table.column\_names}

File /opt/conda/lib/python3.10/site-packages/datasets/formatting/formatting.py:160, in (.0)  
159 def extract\_batch(self, pa\_table: pa.Table) → dict:  
 → 160 return {col: self.\_arrow\_array\_to\_numpy(pa\_table[col]) for col in pa\_table.column\_names}

File /opt/conda/lib/python3.10/site-packages/datasets/formatting/formatting.py:196, in NumpyArrowExtractor.\_arrow\_array\_to\_numpy(self, pa\_array)  
194 array: List = pa\_array.to\_numpy(zero\_copy\_only=zero\_copy\_only).tolist()  
195 if len(array) \> 0:  
 → 196 if any(  
197 (isinstance(x, np.ndarray) and (x.dtype == np.object or x.shape != array[0].shape))  
198 or (isinstance(x, float) and np.isnan(x))  
199 for x in array  
200 ):  
201 return np.array(array, copy=False, \*\*{\*\*self.np\_array\_kwargs, “dtype”: np.object})  
202 return np.array(array, copy=False, \*\*self.np\_array\_kwargs)

File /opt/conda/lib/python3.10/site-packages/datasets/formatting/formatting.py:197, in (.0)  
194 array: List = pa\_array.to\_numpy(zero\_copy\_only=zero\_copy\_only).tolist()  
195 if len(array) \> 0:  
196 if any(  
 → 197 (isinstance(x, np.ndarray) and (x.dtype == np.object or x.shape != array[0].shape))  
198 or (isinstance(x, float) and np.isnan(x))  
199 for x in array  
200 ):  
201 return np.array(array, copy=False, \*\*{\*\*self.np\_array\_kwargs, “dtype”: np.object})  
202 return np.array(array, copy=False, \*\*self.np\_array\_kwargs)

File /opt/conda/lib/python3.10/site-packages/numpy/ **init**.py:305, in **getattr** (attr)  
300 warnings.warn(  
301 f"In the future `np.{attr}` will be defined as the "  
302 “corresponding NumPy scalar.”, FutureWarning, stacklevel=2)  
304 if attr in **former\_attrs** :  
 → 305 raise AttributeError( **former\_attrs** [attr])  
307 # Importing Tester requires importing all of UnitTest which is not a  
308 # cheap import Since it is mainly used in test suits, we lazy import it  
309 # here to save on the order of 10 ms of import time for most users  
310 #  
311 # The previous way Tester was imported also had a side effect of adding  
312 # the full `numpy.testing` namespace  
313 if attr == ‘testing’:

AttributeError: module ‘numpy’ has no attribute ‘object’.  
`np.object` was a deprecated alias for the builtin `object`. To avoid this error in existing code, use `object` by itself. Doing this will not modify any behavior and is safe.  
The aliases was originally deprecated in NumPy 1.20; for more details and guidance see the original release note at:  
[NumPy 1.20.0 Release Notes — NumPy v2.5.dev0 Manual](https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations)

@sgugger@amyeroberts

---

<div class="post-metadata">

**Author:** ![OleksiiVNTU](https://avatars.discourse-cdn.com/v4/letter/o/df705f/32.png) [@OleksiiVNTU](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FOleksiiVNTU)%5C
**Post date:** [December 16, 2023, 9:02am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Ferror-occuring-during-usig-to-tf-dataset%2F65174%2F3 "2023-12-16T09:02:20Z")

</div>

Thank you very much for your response. Could you provide the example of code snippet about how should I change

> np.object

on

> object

---

<div class="post-metadata">

**Author:** ![raygx](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/raygx/32/17328_2.png) [@raygx](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fraygx)%5C
**Post date:** [December 16, 2023, 9:53am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Ferror-occuring-during-usig-to-tf-dataset%2F65174%2F4 "2023-12-16T09:53:17Z")

</div>

I ma also getting same error, so i am asking experts for an answer.

But from what i understand, the error is from numpy as it has deprecated np.object but the transformer library is not updated. You can try downgrading numpy to version 1.20.0 or below. That should fix your problem temporarily.

---

<div class="post-metadata">

**Author:** ![OleksiiVNTU](https://avatars.discourse-cdn.com/v4/letter/o/df705f/32.png) [@OleksiiVNTU](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FOleksiiVNTU)%5C
**Post date:** [December 16, 2023, 10:59am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Ferror-occuring-during-usig-to-tf-dataset%2F65174%2F5 "2023-12-16T10:59:29Z")

</div>

I tried downgrading but it did not provide any solution for me. In my opinion, the problem also could be with Kaggle environment because this problem I encountered working with Kaggle environment

---

<div class="post-metadata">

**Author:** ![raygx](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/raygx/32/17328_2.png) [@raygx](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fraygx)%5C
**Post date:** [December 16, 2023, 11:15am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Ferror-occuring-during-usig-to-tf-dataset%2F65174%2F6 "2023-12-16T11:15:05Z")

</div>

😄😄 i was working on kaggle as well.  
You could try running your notebook on colab.

---

<div class="post-metadata">

**Author:** ![raygx](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/raygx/32/17328_2.png) [@raygx](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fraygx)%5C
**Post date:** [January 3, 2024, 12:43am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Ferror-occuring-during-usig-to-tf-dataset%2F65174%2F7 "2024-01-03T00:43:41Z")

</div>

@OleksiiVNTU In kaggle what worked for me is using the Kaggle Notebook that I have created several months back, as it had the old environment code ran perfectly.

> **[IMDB Classification Using Transformers](https://www.kaggle.com/code/reganmaharjan/imdb-classification-using-transformers)**
>
> Explore and run machine learning code with Kaggle Notebooks | Using data from IMDB Dataset of 50K Movie Reviews

This is my Notebook. You can copy edit and remove my code and add yours to Run.  
In this notebook I am having problem with GPT2. If you know the fix that’d be help too.
