Closed rdisipio closed 4 years ago
Hi! You can simply use tf.constant
to build your input tensors, like this:
raw_text = "Here comes the sun"
tokens = tokenizer.encode(raw_text, add_special_tokens=False)
inputs = {'input_ids': tf.constant(tokens)}
outputs = model(inputs)
You can use datasets when using building a custom loop or using keras.fit for example, as these will generally feed the tensors directly to the model, instead of feeding the tf.data.Dataset
directly. Here's how I would go about starting a basic custom loop using a tf.data.Dataset
:
import tensorflow as tf
import numpy as np
from transformers import *
tokenizer = GPT2Tokenizer.from_pretrained('gpt2')
model = TFGPT2LMHeadModel.from_pretrained('gpt2')
raw_text = "Here comes the sun"
tokens = tokenizer.encode(raw_text, add_special_tokens=False)
inputs = tf.data.Dataset.from_tensor_slices( np.array([tokens]) )
for input_value in inputs:
outputs = model(input_value)
Please notice I converted to a numpy array by adding a dimension ([tokens]
) otherwise you would end up with individual IDs held by the dataset rather than sequences of ids.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
π Bug
Model I am using (Bert, XLNet....):
TFGPT2LMHeadModel
Language I am using the model on (English, Chinese....): English
The problem arise when using:
The tasks I am working on is:
run_generation.py
usingTFGPT2LMHeadModel
only.To Reproduce
Steps to reproduce the behavior: just run the code above, you should get the following error:
Expected behavior
Still not sure!
Environment
Additional context