train = df2[:25]
test = df2[25:]
def convert_data_to_examples(train, test, text, Airline_Cat):
train_InputExamples = train.apply(lambda x: InputExample(guid=None,
text_a = x[text],
label = x[Airline_Cat]), axis = 1)
validation_InputExamples = test.apply(lambda x: InputExample(guid=None,
text_a = x[text],
label = x[Airline_Cat]), axis = 1)
return train_InputExamples, validation_InputExamples
train_InputExamples, validation_InputExamples = convert_data_to_examples(train, test, 'text', 'Airline_Cat')
from tqdm import tqdm
def convert_examples_to_tf_dataset(examples, tokenizer, max_length=128):
features = []
for e in tqdm(examples):
input_dict = tokenizer.encode_plus(
e.text_a,
add_special_tokens=True,
max_length=max_length,
return_token_type_ids=True,
return_attention_mask=True,
pad_to_max_length=True,
truncation=True
)
input_ids, token_type_ids, attention_mask = (input_dict["input_ids"],input_dict["token_type_ids"], input_dict['attention_mask'])
features.append(InputFeatures( input_ids=input_ids, attention_mask=attention_mask, token_type_ids=token_type_ids, label=e.label))
def gen():
for f in features:
yield (
{
"input_ids": f.input_ids,
"attention_mask": f.attention_mask,
"token_type_ids": f.token_type_ids,
},
f.label,
)
return tf.data.Dataset.from_generator(
gen,
({"input_ids": tf.int32, "attention_mask": tf.int32, "token_type_ids": tf.int32}, tf.int64),
(
{
"input_ids": tf.TensorShape([None]),
"attention_mask": tf.TensorShape([None]),
"token_type_ids": tf.TensorShape([None]),
},
tf.TensorShape([]),
),
)
DATA_COLUMN = 'text'
LABEL_COLUMN = 'Airline_Cat'
train_data = convert_examples_to_tf_dataset(list(train_InputExamples), tokenizer)
train_data = train_data.shuffle(25).batch(32).repeat(2)
validation_data = convert_examples_to_tf_dataset(list (validation_InputExamples), tokenizer)
validation_data = validation_data.batch(32)
from tensorflow import keras
model_auto.compile(
optimizer = tf.keras.optimizers.Adam(learning_rate=5e-5),
loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=tf.metrics.SparseCategoricalAccuracy())
model_auto.fit(train_data.shuffle(25).batch(32),
validation_data=validation_data.shuffle(25).batch(32),
epochs=2,
batch_size=32)
This is my code. Im trying to train and validate a dataset using a bert model in google colab.
Epoch 1/2
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-28-5086da520d6f> in <module>
7 validation_data=validation_data.shuffle(25).batch(32),
8 epochs=2,
----> 9 batch_size=32)
1 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/func_graph.py in autograph_handler(*args, **kwargs)
1145 except Exception as e: # pylint:disable=broad-except
1146 if hasattr(e, "ag_error_metadata"):
-> 1147 raise e.ag_error_metadata.to_exception(e)
1148 else:
1149 raise
ValueError: in user code:
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1021, in train_function *
return step_function(self, iterator)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1010, in step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1000, in run_step **
outputs = model.train_step(data)
File "/usr/local/lib/python3.7/dist-packages/transformers/modeling_tf_utils.py", line 1403, in train_step
y_pred = self(x, training=True)
File "/usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py", line 67, in error_handler
raise e.with_traceback(filtered_tb) from None
ValueError: Exception encountered when calling layer "tf_bert_for_sequence_classification" (type TFBertForSequenceClassification).
in user code:
File "/usr/local/lib/python3.7/dist-packages/transformers/modeling_tf_utils.py", line 1642, in run_call_with_unpacked_inputs *
return func(self, **unpacked_inputs)
File "/usr/local/lib/python3.7/dist-packages/transformers/models/bert/modeling_tf_bert.py", line 1655, in call *
outputs = self.bert(
File "/usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py", line 67, in error_handler **
raise e.with_traceback(filtered_tb) from None
ValueError: Exception encountered when calling layer "bert" (type TFBertMainLayer).
in user code:
File "/usr/local/lib/python3.7/dist-packages/transformers/modeling_tf_utils.py", line 1642, in run_call_with_unpacked_inputs *
return func(self, **unpacked_inputs)
File "/usr/local/lib/python3.7/dist-packages/transformers/models/bert/modeling_tf_bert.py", line 767, in call *
batch_size, seq_length = input_shape
ValueError: too many values to unpack (expected 2)
Call arguments received:
• self=tf.Tensor(shape=(None, None, None), dtype=int32)
• input_ids=None
• attention_mask=tf.Tensor(shape=(None, None, None), dtype=int32)
• token_type_ids=tf.Tensor(shape=(None, None, None), dtype=int32)
• position_ids=None
• head_mask=None
• inputs_embeds=None
• encoder_hidden_states=None
• encoder_attention_mask=None
• past_key_values=None
• use_cache=None
• output_attentions=False
• output_hidden_states=False
• return_dict=True
• training=True
Call arguments received:
• self={'input_ids': 'tf.Tensor(shape=(None, None, None), dtype=int32)', 'attention_mask': 'tf.Tensor(shape=(None, None, None), dtype=int32)', 'token_type_ids': 'tf.Tensor(shape=(None, None, None), dtype=int32)'}
• input_ids=None
• attention_mask=None
• token_type_ids=None
• position_ids=None
• head_mask=None
• inputs_embeds=None
• output_attentions=None
• output_hidden_states=None
• return_dict=None
• labels=None
• training=True
It has been throwing this error for quite a time. And also if it runs by luck sometimes, session is being crashed by itself saying that ram is utilised completely when not even half of the ram has been used. Can anyone please help me with this? Thanks in advance.