TFEncoderDecoderModel can not be trained with TF Keras fit() method
Author: kmkarakaya
Pull request: no
Labels: name: bug

### System Info

- `transformers` version: 4.21.0
- Platform: Linux-4.15.0-188-generic-x86_64-with-glibc2.31
- Python version: 3.9.12
- Huggingface_hub version: 0.8.1
- PyTorch version (GPU?): not installed (NA)
- Tensorflow version (GPU?): 2.6.2 (True)
- Flax version (CPU?/GPU?/TPU?): not installed (NA)
- Jax version: not installed
- JaxLib version: not installed
- Using GPU in script?: <fill in>
- Using distributed or parallel set-up in script?: <fill in>

### Who can help?

_No response_

### Information

- [x] The official example scripts
- [ ] My own modified scripts

### Tasks

- [x] An officially supported task in the `examples` folder (such as GLUE/SQuAD, ...)
- [ ] My own task or dataset (give details below)

### Reproduction

Use the example here: https://huggingface.co/docs/transformers/v4.20.1/en/model_doc/encoder-decoder#transformers.TFEncoderDecoderModel.call.example 

1. try to fit the model: **model.fit**(input_ids=input_ids, decoder_input_ids=input_ids)
2. You will receive errors "TypeError: fit() got an unexpected keyword argument 'input_ids'"
3.
![image](https://user-images.githubusercontent.com/41159849/182850011-08f8bb4f-d40f-42f5-9d56-9d916ff4efe9.png)

4. you can try this : **model.fit**(input_ids, input_ids)
5. but you receive many errors: 
![image](https://user-images.githubusercontent.com/41159849/182850264-b4e8f827-e53c-434d-ad96-85eb70af1217.png)


### Expected behavior

I should be able to train a TFEncoderDecoderModel with TF Keras fit() method

Comments:
Hi @kmkarakaya 👋  Having a popular project like `transformers` means we get many support and feature requests — if we want to maximize how much we help the community, the community has to help us stay productive 🙏

To that end, please share a *short* script where the issue is clearly reproducible on *any* computer. Thank you 🤗

Hi @gante,  
Here is the script ( https://huggingface.co/docs/transformers/v4.20.1/en/model_doc/encoder-decoder#transformers.TFEncoderDecoderModel.call.example ) which **_I modified it to train the model as below_**:

import tensorflow as tf
from transformers import TFEncoderDecoderModel, BertTokenizer
model = TFEncoderDecoderModel.from_encoder_decoder_pretrained("bert-base-cased", "gpt2")
tokenizer = BertTokenizer.from_pretrained("bert-base-cased")
model.compile(loss=None)
**model.fit**(input_ids=input_ids, decoder_input_ids=input_ids, labels=input_ids)

**The error message:**
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()


Hi @kmkarakaya -- technically I can't reproduce the script, since I don't have access to your `input_ids`.

However, looking at the code, I can tell that `model.fit` is not being called correctly. Please check its [documentation](https://www.tensorflow.org/api_docs/python/tf/keras/Model#fit), especially its `x` and `y` arguments :)

@gante As I wrote in every message this code belongs to the HF repo  https://huggingface.co/docs/transformers/v4.20.1/en/model_doc/encoder-decoder#transformers.TFEncoderDecoderModel.call.example 

here is the complete & full code from the HF link:
I hope this time you can help to fix the problem:

from transformers import TFEncoderDecoderModel, BertTokenizer
model = TFEncoderDecoderModel.from_encoder_decoder_pretrained("bert-base-cased", "gpt2")
tokenizer = BertTokenizer.from_pretrained("bert-base-cased")
input_ids = tokenizer.encode(
    "Hello, my dog is cute", add_special_tokens=True, return_tensors="tf"
) 
model.compile(loss=None)
model.fit(input_ids=input_ids, decoder_input_ids=input_ids, labels=input_ids)


@gante Please note that my question is related to **TFEncoderDecoderModel** therefore, model.fit(x,y) is not enough! We need to provide **encoder input, decoder input and decoder output** as **the HF suggests in its official documentation**: https://huggingface.co/docs/transformers/v4.20.1/en/model_doc/encoder-decoder#transformers.TFEncoderDecoderModel.call.example


Thus, this bug's title is "**_TFEncoderDecoderModel can not be trained with TF Keras fit() method_**". If you know how to train **TFEncoderDecoderModel** with TF or Keras please share with me.

Because in the current **model.fit()** I am not able to do it.
Thank you for your attention.

Hi @kmkarakaya -- the [example you linked](https://huggingface.co/docs/transformers/v4.20.1/en/model_doc/encoder-decoder#transformers.TFEncoderDecoderModel.call.example) runs fine and, as I've written above, the issue with your example is in the arguments to `model.fit`.

Please see our [examples](https://github.com/huggingface/transformers/tree/main/examples/tensorflow) to learn how to prepare the data for training. For instance, see [here](https://github.com/huggingface/transformers/blob/main/examples/tensorflow/language-modeling/run_mlm.py#L563) -- you need to prepare your data into a dataset in advance.

Finally, as per our [issues guidelines](https://github.com/huggingface/transformers/blob/main/ISSUES.md), we reserve GitHub issues for bugs in the repository and/or feature requests. For any other matters, we'd like to invite you to use our [forum](https://discuss.huggingface.co/) 🤗

This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread.

Please note that issues that do not follow the [contributing guidelines](https://github.com/huggingface/transformers/blob/main/CONTRIBUTING.md) are likely to be ignored.