Label casting during `map` process is canceled after the `map` process
MEMBER
Hi ! By default `map()` tries to keep the types of each column of the dataset, so here it reuses the int type since all your float values can be converted to integers. But I agree it would be nice to store float values as float values and don't try to reuse the same type in this case.

In the meantime, you can either store the float values in a new column, or pass the output `features=` manually to `map()`
2025-03-25T10:14:53Z
https://github.com/huggingface/datasets/issues/7472#issuecomment-2750758708
https://api.github.com/repos/huggingface/datasets/issues/7472
IC_kwDODunzps6j9T80
https://api.github.com/repos/huggingface/datasets/issues/comments/2750758708/reactions
2025-03-25T10:14:53Z
https://api.github.com/repos/huggingface/datasets/issues/comments/2750758708
https://avatars.githubusercontent.com/u/42851186?v=4
https://api.github.com/users/lhoestq/events{/privacy}
https://api.github.com/users/lhoestq/followers
https://api.github.com/users/lhoestq/following{/other_user}
https://api.github.com/users/lhoestq/gists{/gist_id}
https://github.com/lhoestq
lhoestq
MDQ6VXNlcjQyODUxMTg2
https://api.github.com/users/lhoestq/orgs
https://api.github.com/users/lhoestq/received_events
https://api.github.com/users/lhoestq/repos
https://api.github.com/users/lhoestq/starred{/owner}{/repo}
https://api.github.com/users/lhoestq/subscriptions
User
https://api.github.com/users/lhoestq
public
CONTRIBUTOR
Hi @lhoestq 

Thank you for the answer & suggestion!

Can we add some flag to `map()` function like `reuses_original_type=True` and skip reusing the original type when it's False?

Let me know if it sounds like a reasonable solution. I am happy to submit a PR for this.
2025-03-26T06:05:01Z
https://github.com/huggingface/datasets/issues/7472#issuecomment-2753333216
https://api.github.com/repos/huggingface/datasets/issues/7472
IC_kwDODunzps6kHIfg
https://api.github.com/repos/huggingface/datasets/issues/comments/2753333216/reactions
2025-03-26T06:05:01Z
https://api.github.com/repos/huggingface/datasets/issues/comments/2753333216
https://avatars.githubusercontent.com/u/11156001?v=4
https://api.github.com/users/yoshitomo-matsubara/events{/privacy}
https://api.github.com/users/yoshitomo-matsubara/followers
https://api.github.com/users/yoshitomo-matsubara/following{/other_user}
https://api.github.com/users/yoshitomo-matsubara/gists{/gist_id}
https://github.com/yoshitomo-matsubara
yoshitomo-matsubara
MDQ6VXNlcjExMTU2MDAx
https://api.github.com/users/yoshitomo-matsubara/orgs
https://api.github.com/users/yoshitomo-matsubara/received_events
https://api.github.com/users/yoshitomo-matsubara/repos
https://api.github.com/users/yoshitomo-matsubara/starred{/owner}{/repo}
https://api.github.com/users/yoshitomo-matsubara/subscriptions
User
https://api.github.com/users/yoshitomo-matsubara
public
MEMBER
In general we try to avoid adding new parameters when it's already possible to achieve the same results with existing parameters (here `features=`). But since it's not always convenient to know in advance the `features=` I'm open to contributions to adding this parameter yes
2025-03-26T09:14:01Z
https://github.com/huggingface/datasets/issues/7472#issuecomment-2753691926
https://api.github.com/repos/huggingface/datasets/issues/7472
IC_kwDODunzps6kIgEW
https://api.github.com/repos/huggingface/datasets/issues/comments/2753691926/reactions
2025-03-26T09:14:17Z
https://api.github.com/repos/huggingface/datasets/issues/comments/2753691926
https://avatars.githubusercontent.com/u/42851186?v=4
https://api.github.com/users/lhoestq/events{/privacy}
https://api.github.com/users/lhoestq/followers
https://api.github.com/users/lhoestq/following{/other_user}
https://api.github.com/users/lhoestq/gists{/gist_id}
https://github.com/lhoestq
lhoestq
MDQ6VXNlcjQyODUxMTg2
https://api.github.com/users/lhoestq/orgs
https://api.github.com/users/lhoestq/received_events
https://api.github.com/users/lhoestq/repos
https://api.github.com/users/lhoestq/starred{/owner}{/repo}
https://api.github.com/users/lhoestq/subscriptions
User
https://api.github.com/users/lhoestq
public
CONTRIBUTOR
Thank you for sharing the context. Good to know that. 

I submitted a PR #7483. Could you review the PR?
2025-03-27T07:09:00Z
https://github.com/huggingface/datasets/issues/7472#issuecomment-2756957318
https://api.github.com/repos/huggingface/datasets/issues/7472
IC_kwDODunzps6kU9SG
https://api.github.com/repos/huggingface/datasets/issues/comments/2756957318/reactions
2025-03-27T07:09:00Z
https://api.github.com/repos/huggingface/datasets/issues/comments/2756957318
https://avatars.githubusercontent.com/u/11156001?v=4
https://api.github.com/users/yoshitomo-matsubara/events{/privacy}
https://api.github.com/users/yoshitomo-matsubara/followers
https://api.github.com/users/yoshitomo-matsubara/following{/other_user}
https://api.github.com/users/yoshitomo-matsubara/gists{/gist_id}
https://github.com/yoshitomo-matsubara
yoshitomo-matsubara
MDQ6VXNlcjExMTU2MDAx
https://api.github.com/users/yoshitomo-matsubara/orgs
https://api.github.com/users/yoshitomo-matsubara/received_events
https://api.github.com/users/yoshitomo-matsubara/repos
https://api.github.com/users/yoshitomo-matsubara/starred{/owner}{/repo}
https://api.github.com/users/yoshitomo-matsubara/subscriptions
User
https://api.github.com/users/yoshitomo-matsubara
public
CONTRIBUTOR
Hi @lhoestq 

Let me know if there is something that I should add to [the PR](https://github.com/huggingface/datasets/pull/7483)!
2025-04-07T04:06:51Z
https://github.com/huggingface/datasets/issues/7472#issuecomment-2781966045
https://api.github.com/repos/huggingface/datasets/issues/7472
IC_kwDODunzps6l0W7d
https://api.github.com/repos/huggingface/datasets/issues/comments/2781966045/reactions
2025-04-07T04:06:51Z
https://api.github.com/repos/huggingface/datasets/issues/comments/2781966045
https://avatars.githubusercontent.com/u/11156001?v=4
https://api.github.com/users/yoshitomo-matsubara/events{/privacy}
https://api.github.com/users/yoshitomo-matsubara/followers
https://api.github.com/users/yoshitomo-matsubara/following{/other_user}
https://api.github.com/users/yoshitomo-matsubara/gists{/gist_id}
https://github.com/yoshitomo-matsubara
yoshitomo-matsubara
MDQ6VXNlcjExMTU2MDAx
https://api.github.com/users/yoshitomo-matsubara/orgs
https://api.github.com/users/yoshitomo-matsubara/received_events
https://api.github.com/users/yoshitomo-matsubara/repos
https://api.github.com/users/yoshitomo-matsubara/starred{/owner}{/repo}
https://api.github.com/users/yoshitomo-matsubara/subscriptions
User
https://api.github.com/users/yoshitomo-matsubara
public
CONTRIBUTOR
Closing this issue as the PR #7483 was merged
2025-04-10T05:11:14Z
https://github.com/huggingface/datasets/issues/7472#issuecomment-2791577829
https://api.github.com/repos/huggingface/datasets/issues/7472
IC_kwDODunzps6mZBjl
https://api.github.com/repos/huggingface/datasets/issues/comments/2791577829/reactions
2025-04-10T05:11:14Z
https://api.github.com/repos/huggingface/datasets/issues/comments/2791577829
https://avatars.githubusercontent.com/u/11156001?v=4
https://api.github.com/users/yoshitomo-matsubara/events{/privacy}
https://api.github.com/users/yoshitomo-matsubara/followers
https://api.github.com/users/yoshitomo-matsubara/following{/other_user}
https://api.github.com/users/yoshitomo-matsubara/gists{/gist_id}
https://github.com/yoshitomo-matsubara
yoshitomo-matsubara
MDQ6VXNlcjExMTU2MDAx
https://api.github.com/users/yoshitomo-matsubara/orgs
https://api.github.com/users/yoshitomo-matsubara/received_events
https://api.github.com/users/yoshitomo-matsubara/repos
https://api.github.com/users/yoshitomo-matsubara/starred{/owner}{/repo}
https://api.github.com/users/yoshitomo-matsubara/subscriptions
User
https://api.github.com/users/yoshitomo-matsubara
public
### Describe the bug

When preprocessing a multi-label dataset, I introduced a step to convert int labels to float labels as [BCEWithLogitsLoss](https://pytorch.org/docs/stable/generated/torch.nn.BCEWithLogitsLoss.html) expects float labels and forward function of models in transformers package internally use `BCEWithLogitsLoss`

However, the casting was canceled after `.map` process and the label values still use int values, which leads to an error
```
  File "/home/yoshitomo/anaconda3/envs/torchdistill/lib/python3.10/site-packages/transformers/models/bert/modeling_bert.py", line 1711, in forward
    loss = loss_fct(logits, labels)
  File "/home/yoshitomo/anaconda3/envs/torchdistill/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1736, in _wrapped_call_impl
    return self._call_impl(*args, **kwargs)
  File "/home/yoshitomo/anaconda3/envs/torchdistill/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1747, in _call_impl
    return forward_call(*args, **kwargs)
  File "/home/yoshitomo/anaconda3/envs/torchdistill/lib/python3.10/site-packages/torch/nn/modules/loss.py", line 819, in forward
    return F.binary_cross_entropy_with_logits(
  File "/home/yoshitomo/anaconda3/envs/torchdistill/lib/python3.10/site-packages/torch/nn/functional.py", line 3628, in binary_cross_entropy_with_logits
    return torch.binary_cross_entropy_with_logits(
RuntimeError: result type Float can't be cast to the desired output type Long
```

This seems like happening only when the original labels are int values (see examples below)

### Steps to reproduce the bug

If the original dataset uses a list of int labels, it will cancel the int->float casting

```python
from datasets import Dataset

data = {
    'text': ['text1', 'text2', 'text3', 'text4'],
    'labels': [[0, 1, 2], [3], [3, 4], [3]]
}

dataset = Dataset.from_dict(data)

label_set = set([label for labels in data['labels'] for label in labels])
label2idx = {label: idx for idx, label in enumerate(sorted(label_set))}


def multi_labels_to_ids(labels):
    ids = [0.0] * len(label2idx)
    for label in labels:
        ids[label2idx[label]] = 1.0
    return ids

def preprocess(examples):
    result = {'sentence': [[0, 3, 4] for _ in range(len(examples['labels']))]}
    print('"labels" are int', examples['labels'])
    result['labels'] = [multi_labels_to_ids(l) for l in examples['labels']]
    print('"labels" were converted to multi-label format with float values', result['labels'])
    return result


preprocessed_dataset = dataset.map(preprocess, batched=True, remove_columns=['labels', 'text'])
print(preprocessed_dataset[0]['labels'])
# Output: "[1, 1, 1, 0, 0]"
# Expected: "[1.0, 1.0, 1.0, 0.0, 0.0]"
```

If the original dataset uses non-int labels, it works as expected.

```python
from datasets import Dataset

data = {
    'text': ['text1', 'text2', 'text3', 'text4'],
    'labels': [['label1', 'label2', 'label3'], ['label4'], ['label4', 'label5'], ['label4']]
}

dataset = Dataset.from_dict(data)

label_set = set([label for labels in data['labels'] for label in labels])
label2idx = {label: idx for idx, label in enumerate(sorted(label_set))}


def multi_labels_to_ids(labels):
    ids = [0.0] * len(label2idx)
    for label in labels:
        ids[label2idx[label]] = 1.0
    return ids

def preprocess(examples):
    result = {'sentence': [[0, 3, 4] for _ in range(len(examples['labels']))]}
    print('"labels" are int', examples['labels'])
    result['labels'] = [multi_labels_to_ids(l) for l in examples['labels']]
    print('"labels" were converted to multi-label format with float values', result['labels'])
    return result


preprocessed_dataset = dataset.map(preprocess, batched=True, remove_columns=['labels', 'text'])
print(preprocessed_dataset[0]['labels'])
# Output: "[1.0, 1.0, 1.0, 0.0, 0.0]"
# Expected: "[1.0, 1.0, 1.0, 0.0, 0.0]"
```

Note that the only difference between these two examples is
>    'labels': [[0, 1, 2], [3], [3, 4], [3]]

v.s

>    'labels': [['label1', 'label2', 'label3'], ['label4'], ['label4', 'label5'], ['label4']]



### Expected behavior

Even if the original dataset uses a list of int labels, the int->float casting during `.map` process should not be canceled as shown in the above example

### Environment info

OS Ubuntu 22.04 LTS
Python 3.10.11
datasets v3.4.1