# ValueError: could not broadcast input array from shape (30,512,32128) into shape (30,512)

**URL:** <https://discuss.huggingface.co/t/valueerror-could-not-broadcast-input-array-from-shape-30-512-32128-into-shape-30-512/73040>\
**Category:** 🤗Transformers\
**Created:** [February 13, 2024, 6:25pm UTC](https://discuss.huggingface.co/t/valueerror-could-not-broadcast-input-array-from-shape-30-512-32128-into-shape-30-512/73040 "2024-02-13T18:25:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![llmtrails](https://avatars.discourse-cdn.com/v4/letter/l/e9bcb4/32.png) [@llmtrails](https://discuss.huggingface.co/u/llmtrails)\
**Post date:** [February 13, 2024, 6:25pm UTC](https://discuss.huggingface.co/t/valueerror-could-not-broadcast-input-array-from-shape-30-512-32128-into-shape-30-512/73040/1 "2024-02-13T18:25:48Z")

</div>

I am getting this error when I run the training on flan-t5-base llm. Using Hugging face transformers. I tried multiple options to fix this compute\_matrics but I am hitting a brick wall. I am a high school user of transformer for a science project. I am using google colab pro version of my parent.

Error I am getting is

ValueError Traceback (most recent call last)  
 in \<cell line: 2\>()  
1 # Start training  
----\> 2 trainer.train()

9 frames

/usr/local/lib/python3.10/dist-packages/transformers/trainer.py in train(self, resume\_from\_checkpoint, trial, ignore\_keys\_for\_eval, \*\*kwargs)  
1537 hf\_hub\_utils.enable\_progress\_bars()  
1538 else:  
 → 1539 return inner\_training\_loop(  
1540 args=args,  
1541 resume\_from\_checkpoint=resume\_from\_checkpoint,

/usr/local/lib/python3.10/dist-packages/transformers/trainer.py in \_inner\_training\_loop(self, batch\_size, args, resume\_from\_checkpoint, trial, ignore\_keys\_for\_eval)  
1942  
1943 self.control = self.callback\_handler.on\_epoch\_end(args, self.state, self.control)  
 → 1944 self.\_maybe\_log\_save\_evaluate(tr\_loss, model, trial, epoch, ignore\_keys\_for\_eval)  
1945  
1946 if DebugOption.TPU\_METRICS\_DEBUG in self.args.debug:

/usr/local/lib/python3.10/dist-packages/transformers/trainer.py in \_maybe\_log\_save\_evaluate(self, tr\_loss, model, trial, epoch, ignore\_keys\_for\_eval)  
2289 metrics = None  
2290 if self.control.should\_evaluate:  
 → 2291 metrics = self.evaluate(ignore\_keys=ignore\_keys\_for\_eval)  
2292 self.\_report\_to\_hp\_search(trial, self.state.global\_step, metrics)  
2293

/usr/local/lib/python3.10/dist-packages/transformers/trainer.py in evaluate(self, eval\_dataset, ignore\_keys, metric\_key\_prefix)  
3093  
3094 eval\_loop = self.prediction\_loop if self.args.use\_legacy\_prediction\_loop else self.evaluation\_loop  
 → 3095 output = eval\_loop(  
3096 eval\_dataloader,  
3097 description=“Evaluation”,

/usr/local/lib/python3.10/dist-packages/transformers/trainer.py in evaluation\_loop(self, dataloader, description, prediction\_loss\_only, ignore\_keys, metric\_key\_prefix)  
3384 )  
3385 else:  
 → 3386 metrics = self.compute\_metrics(EvalPrediction(predictions=all\_preds, label\_ids=all\_labels))  
3387 else:  
3388 metrics = {}

in compute\_metrics(eval\_pred)  
24 logits, labels = eval\_pred  
25 # Convert logits to predicted token IDs  
—\> 26 predictions = np.argmax(logits, axis=-1)  
27 # Flatten the predictions and labels for comparison  
28 true\_labels = labels.flatten()

/usr/local/lib/python3.10/dist-packages/numpy/core/overrides.py in argmax(\*args, \*\*kwargs)

/usr/local/lib/python3.10/dist-packages/numpy/core/fromnumeric.py in argmax(a, axis, out, keepdims)  
1214 “”"  
1215 kwds = {‘keepdims’: keepdims} if keepdims is not np.\_NoValue else {}  
 → 1216 return \_wrapfunc(a, ‘argmax’, axis=axis, out=out, \*\*kwds)  
1217  
1218

/usr/local/lib/python3.10/dist-packages/numpy/core/fromnumeric.py in \_wrapfunc(obj, method, \*args, \*\*kwds)  
52 bound = getattr(obj, method, None)  
53 if bound is None:  
—\> 54 return \_wrapit(obj, method, \*args, \*\*kwds)  
55  
56 try:

/usr/local/lib/python3.10/dist-packages/numpy/core/fromnumeric.py in \_wrapit(obj, method, \*args, \*\*kwds)  
41 except AttributeError:  
42 wrap = None  
—\> 43 result = getattr(asarray(obj), method)(\*args, \*\*kwds)  
44 if wrap:  
45 if not isinstance(result, mu.ndarray):

## ValueError: could not broadcast input array from shape (30,512,32128) into shape (30,512)

My code is this.

# Import necessary libraries

import pandas as pd  
from sklearn.model\_selection import train\_test\_split  
from datasets import Dataset, DatasetDict, load\_metric  
from transformers import T5Tokenizer, T5ForConditionalGeneration, TrainingArguments, Trainer, AutoTokenizer

from sklearn.metrics import accuracy\_score, recall\_score, confusion\_matrix  
import numpy as np  
import matplotlib.pyplot as plt

“”"# Load and prepare datasets

**Load Datasets**

load the CSV files and you will need to combine the seperate files into a single file and then split it into Train, Validate and Test  
“”"

# Load datasets

def load\_dataset(file\_path):  
df = pd.read\_csv(file\_path, sep = ‘|’)  
df.columns = [‘text’, ‘labels’] # Assuming two columns: ‘input’ and ‘output’  
return df

“”“Split the data set into Train, validate and test”“”

# Split dataset

def split\_dataset(df):  
train\_df, temp\_df = train\_test\_split(df, test\_size=0.2, random\_state=42)  
val\_df, test\_df = train\_test\_split(temp\_df, test\_size=0.5, random\_state=42)  
return map(Dataset.from\_pandas, (train\_df, val\_df, test\_df))

“”“Now we load the data and complete the split”“”

# Load and split each dataset

sThread\_train, sThread\_val, sThread\_test = split\_dataset(load\_dataset(‘tags.csv’))  
dThread\_train, dThread\_val, dThread\_test = split\_dataset(load\_dataset(‘tagd.csv’))  
tThread\_train, tThread\_val, tThread\_test = split\_dataset(load\_dataset(‘tagn.csv’))

display(sThread\_train)

“”“Combine the split data sets”“”

combined\_dataset = DatasetDict({  
‘train’: Dataset.from\_dict(pd.concat([sThread\_train.to\_pandas(), dThread\_train.to\_pandas(), tThread\_train.to\_pandas()])),  
‘validation’: Dataset.from\_dict(pd.concat([sThread\_val.to\_pandas(), dThread\_val.to\_pandas(), tThread\_val.to\_pandas()])),  
‘test’: Dataset.from\_dict(pd.concat([sThread\_test.to\_pandas(), dThread\_test.to\_pandas(), tThread\_test.to\_pandas()]))  
})

display(combined\_dataset)

“”"  
def filter\_non\_strings(dataset):  
return dataset.filter(lambda example: isinstance(example[‘text’], str) and isinstance(example[‘labels’], str))  
“”"

# Apply the filtering function to each split in the dataset

#filtered\_dataset = DatasetDict({split: filter\_non\_strings(combined\_dataset[split]) for split in combined\_dataset.keys()})

#display(filtered\_dataset)

“”"# Setup Tokenizer and Initiatlization

Initialize the tokenizer and model  
“”"

# Initialize the tokenizer and model

tokenizer = AutoTokenizer.from\_pretrained(‘google/flan-t5-base’)  
model = T5ForConditionalGeneration.from\_pretrained(‘google/flan-t5-base’)

“”“Tokenization function”“”

# Tokenization function

def tokenize\_function(examples):  
model\_inputs = tokenizer(examples[‘text’], padding=‘max\_length’, truncation=True, max\_length=512)  
with tokenizer.as\_target\_tokenizer():  
labels = tokenizer(examples[‘labels’], padding=‘max\_length’, truncation=True, max\_length=512)  
model\_inputs[‘labels’] = labels[‘input\_ids’]  
return model\_inputs  
“”"

def tokenize\_function(examples):  
model\_inputs = tokenizer(examples[‘text’], padding=‘max\_length’, truncation=True, max\_length=512)  
labels = tokenizer(examples[‘labels’], padding=‘max\_length’, truncation=True, max\_length=512)  
return model\_inputs, labels

def tokenize\_function(examples):  
assert isinstance(examples[‘text’], list), “Text data is not in list format”  
assert all(isinstance(text, str) for text in examples[‘text’]), “Not all text entries are strings”  
# Tokenize the text and labels together in the model\_inputs dictionary  
model\_inputs = tokenizer(examples[‘text’], padding=‘max\_length’, truncation=True, max\_length=512)  
# Tokenize labels and add them to model\_inputs under the key ‘labels’  
labels = tokenizer(examples[‘labels’], padding=‘max\_length’, truncation=True, max\_length=512)  
model\_inputs[‘labels’] = labels[‘input\_ids’]  
return model\_inputs  
“”"

“”“Tokenize data sets”“”

# Tokenize datasets - combined does not work

tokenized\_datasets = combined\_dataset.map(tokenize\_function, batched=True)

# Then, apply the tokenize\_function to the filtered dataset

#tokenized\_datasets = filtered\_dataset.map(tokenize\_function, batched=True)

“”“Training arguments”“”

# Training arguments

training\_args = TrainingArguments(  
output\_dir=“./results”,  
evaluation\_strategy=“epoch”,  
learning\_rate=2e-5,  
per\_device\_train\_batch\_size=4,  
per\_device\_eval\_batch\_size=4,  
num\_train\_epochs=3,  
weight\_decay=0.01,  
save\_strategy=“epoch”,  
load\_best\_model\_at\_end=True,  
)

“”“Define a function to compute accuracy”“”

# prompt: how to find the shape of the shape of the input array of the training set

display(tokenized\_datasets)

# Define a function to compute accuracy

def compute\_metrics(eval\_pred):  
logits, labels = eval\_pred  
predictions = np.argmax(logits, axis=2) # Fix the axis  
accuracy = accuracy\_score(labels, predictions)  
recall = recall\_score(labels, predictions, average=‘weighted’) # Use ‘binary’ for binary classification  
return {“accuracy”: accuracy, “recall”: recall}

“”“Initialize Trainer”“”

# Initialize Trainer

trainer = Trainer(  
model=model,  
args=training\_args,  
train\_dataset=tokenized\_datasets[‘train’],  
eval\_dataset=tokenized\_datasets[‘validation’],  
compute\_metrics=compute\_metrics  
)

“”“Start Training”“”

# Start training

trainer.train()

---

<div class="post-metadata">

**Author:** ![llmtrails](https://avatars.discourse-cdn.com/v4/letter/l/e9bcb4/32.png) [@llmtrails](https://discuss.huggingface.co/u/llmtrails)\
**Post date:** [February 13, 2024, 10:11pm UTC](https://discuss.huggingface.co/t/valueerror-could-not-broadcast-input-array-from-shape-30-512-32128-into-shape-30-512/73040/2 "2024-02-13T22:11:33Z")

</div>

This got solved by changing the compute function to this one. I found that the logits was a tuple. And the first element was the logits, Once that was identified then I could handle it. This fixed my problem and the code completed successfully…  
def compute\_metrics(eval\_pred):  
logits, labels = eval\_pred  
# Assuming logits are the first element of the tuple  
logits\_shape = logits[0].shape if isinstance(logits, tuple) else logits.shape  
#print(f"logits shape: {logits\_shape}“) # This will print the shape of the logits  
#print(f"labels shape: {labels.shape}”) # Continue to print labels shape

```
# Proceed with your metrics calculation
predictions = np.argmax(logits[0], axis=-1) if isinstance(logits, tuple) else np.argmax(logits, axis=-1)
labels = labels.flatten()
predictions = predictions.flatten()
accuracy = accuracy_score(labels, predictions)
recall = recall_score(labels, predictions, average='weighted', zero_division=0)

return {"accuracy": accuracy, "recall": recall}

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/hellohellohello/original/2X/d/de4155eb4aa4108ecb32a1389d7cc37ae69f88b7.png) [@system](https://discuss.huggingface.co/u/system)\
**Post date:** [February 14, 2024, 10:12am UTC](https://discuss.huggingface.co/t/valueerror-could-not-broadcast-input-array-from-shape-30-512-32128-into-shape-30-512/73040/3 "2024-02-14T10:12:06Z")

</div>

Closed on February 14, 2024, 10:12am UTC

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.
