# How to use dataset with costume function?

**URL:** </static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-to-use-dataset-with-costume-function%2F21382%3E%5C
**Category:** Beginners\
**Created:** [August 8, 2022, 12:45pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-to-use-dataset-with-costume-function%2F21382 "2022-08-08T12:45:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![laro1](https://avatars.discourse-cdn.com/v4/letter/l/ad7895/32.png) [@laro1](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Flaro1)%5C
**Post date:** [August 8, 2022, 12:45pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-to-use-dataset-with-costume-function%2F21382%2F1 "2022-08-08T12:45:43Z")

</div>

I want to call `DatasetDict` `map` function with parameters, and I dont know how to do it.

I have function with the following API:

```auto

def tokenize_function(tokenizer, examples):
    s1 = examples["premise"]
    s2 = examples["hypothesis"]
    args = (s1, s2)
    return tokenizer(*args, padding="max_length", truncation=True)

```

And when I’m trying to use in this way:

```auto
   dataset = load_dataset("json", data_files=data_files)
   tokenizer = AutoTokenizer.from_pretrained(model_name)
   tokenized_datasets = dataset.map(tokenize_function, tokenizer, batched=True)

```

I’m getting error:  
`TypeError: list indices must be integers or slices, not str`

How can I call `map` function in my example ?

---

<div class="post-metadata">

**Author:** ![nielsr](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/nielsr/32/39617_2.png) [@nielsr](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fnielsr)%5C
**Post date:** [August 8, 2022, 5:03pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-to-use-dataset-with-costume-function%2F21382%2F2 "2022-08-08T17:03:16Z")

</div>

The function shouldn’t take the tokenizer as input, only the examples. You can add additional arguments to the function, which can then be passed as fn\_kwargs in the map method.

Option 1: define tokenizer outside the function

```auto
from transformers import AutoTokenizer

tokenizer = AutoTokenizer.from_pretrained(model_name)

dataset = load_dataset("json", data_files=data_files)

def tokenize_function(examples):
    s1 = examples["premise"]
    s2 = examples["hypothesis"]
    args = (s1, s2)
    return tokenizer(*args, padding="max_length", truncation=True)

tokenized_datasets = dataset.map(tokenize_function, tokenizer, batched=True)

```

Option 2: use `fn_kwargs` to pass the tokenizer to the function

```auto
from transformers import AutoTokenizer

tokenizer = AutoTokenizer.from_pretrained(model_name)

dataset = load_dataset("json", data_files=data_files)

def tokenize_function(examples, tokenizer):
    s1 = examples["premise"]
    s2 = examples["hypothesis"]
    args = (s1, s2)
    return tokenizer(*args, padding="max_length", truncation=True)

tokenized_datasets = dataset.map(tokenize_function, fn_kwargs={"tokenizer":tokenizer}, batched=True)

```

---

<div class="post-metadata">

**Author:** ![meti007](https://avatars.discourse-cdn.com/v4/letter/m/b5e925/32.png) [@meti007](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fmeti007)%5C
**Post date:** [June 19, 2023, 2:14am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-to-use-dataset-with-costume-function%2F21382%2F3 "2023-06-19T02:14:18Z")

</div>

how can i do a function on Dataset as batch here my code:  
def get\_sent(examples):  
sents =sentiment\_classifier(examples[‘text’])  
labels=[sent[0][‘label’] for sent in sents]  
examples[‘label’]=labels  
return examples

to\_label1 = to\_label1.map(get\_sent,batched=True)

when i run the code i got error and cant run function as batch.

---

<div class="post-metadata">

**Author:** ![mariosasko](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/mariosasko/32/31548_2.png) [@mariosasko](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fmariosasko)%5C
**Post date:** [June 19, 2023, 3:53pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-to-use-dataset-with-costume-function%2F21382%2F4 "2023-06-19T15:53:17Z")

</div>

Can you paste the error message?
