ValueError: operands could not be broadcast together with shapes (5,) (14,)

i have an error and no body can solve it… is there any body to help?
it is the erro
" evaluate_parameters_dict[“precision”] = precision + evaluate_parameters_dict[“precision”]
ValueError: operands could not be broadcast together with shapes (5,) (14,)

in this code:

import torch
from sklearn.metrics import precision_recall_fscore_support
import numpy as np
from torch_chatbot.config import DEVICE
from torch_chatbot.utils import Dataset, binary_accuracy
from torch_chatbot.models import LSTM_Model
from torch import nn
import time
from torch_chatbot.make_model import MakeModel
from torch_chatbot.config import LOG_PATH, NEG_CSV, MODEL_PATH, GLOVE_MODEL_PATH, \

log_file = open(LOG_PATH, 'w')


make_model = MakeModel(dataset)
# qa_similar_model, model_embedding = make_model.modeler()
model = LSTM_Model(vocab_size=len(dataset.vocab), embedding_dim=EMBEDDING_SIZE, pad_idx=dataset.PAD_IDX,
                   lstm_units=LSTM_UNITS, lstm_layers=N_LAYERS, dropout=DROPOUT, model=MODEL_NAME)[dataset.PAD_IDX] = torch.zeros(EMBEDDING_SIZE)
# model.embedding.weight.requires_grad = True
criterion = nn.MSELoss()
optimizer = torch.optim.Adam(model.parameters(), lr=0.001)
model =
criterion =

def count_parameters(input_model):
    method for calculate number of model's parameter
    :param input_model: model
    :return: number of parameters
    return sum(p.numel() for p in input_model.parameters() if p.requires_grad)

def epoch_time(s_time, e_time):
    method for calculate time
    :param s_time: start time
    :param e_time: end time
    :return: Minutes and Seconds
    elapsed_time = e_time - s_time
    elapsed_mins = int(elapsed_time / 60)
    elapsed_secs = int(elapsed_time - (elapsed_mins * 60))
    return elapsed_mins, elapsed_secs

def train(model, iterator, optimizer, criterion):
    epoch_loss = 0
    epoch_acc = 0
    iter_len = len(iterator)
    n_batch = 0
    for batch in iterator:
        n_batch += 1
        qa_similary, qhidden = model(batch.question, batch.answer)
        # print("qa", qa_similary)
        loss = criterion(qa_similary, batch.label.float())
        acc = binary_accuracy(qa_similary, batch.label)
        epoch_loss += loss.item()
        epoch_acc += acc.item()
        if (n_batch % (iter_len // 3)) == 0:
            print(f'\t train on: {(n_batch / iter_len) * 100:.2f}% of samples')
            print(f'\t accuracy: {(epoch_acc / n_batch)}')
            print(f'\t loss: {(epoch_loss / n_batch)}')

            log_file.write(f'\t train on: {(n_batch / iter_len) * 100:.2f}% of samples\n')
            log_file.write(f'\t accuracy: {(epoch_acc / n_batch)}\n')
            log_file.write(f'\t loss: {(epoch_loss / n_batch)}\n')
    print("*************MODEL IS TRAINING!****************")
    return epoch_loss / len(iterator), epoch_acc / len(iterator)

def evaluate(model, iterator, criterion):
    evaluate_parameters_dict = {"loss": 0, "acc": 0, "precision": 0, "recall": 0, "fscore": 0}
    with torch.no_grad():
        for batch in iterator:
            qa_similary, qhidden = model(batch.question, batch.answer)
            loss = criterion(qa_similary, batch.label)
            acc = binary_accuracy(qa_similary, batch.label)
            precision, recall, fscore, _ = precision_recall_fscore_support \
                (y_true=batch.label.cpu(), y_pred=np.argmax(qa_similary.cpu(), axis=1))
            evaluate_parameters_dict["loss"] += loss.item()
            evaluate_parameters_dict["acc"] += acc.item()
            evaluate_parameters_dict["precision"] = precision + evaluate_parameters_dict["precision"]
            evaluate_parameters_dict["recall"] = recall + evaluate_parameters_dict["recall"]
            evaluate_parameters_dict["fscore"] = fscore + evaluate_parameters_dict["fscore"]
        print("*************MODEL IS EVALUATING!****************")

        return evaluate_parameters_dict["loss"] / len(iterator), \
               evaluate_parameters_dict["acc"] / len(iterator), \
               evaluate_parameters_dict["precision"] / len(iterator), \
               evaluate_parameters_dict["recall"] / len(iterator), \
               evaluate_parameters_dict["fscore"] / len(iterator)

BEST_VALID_LOSS = float('inf')
for epoch in range(N_EPOCHS):
    start_time = time.time()
    train_loss, train_acc = train(model, dataset.train_iterator, optimizer, criterion)
    valid_loss, valid_acc, valid_precision, valid_recall, valid_fscore = \
        evaluate(model, dataset.val_iterator, criterion)
    end_time = time.time()

    epoch_mins, epoch_secs = epoch_time(start_time, end_time)

    if valid_loss < BEST_VALID_LOSS:
        BEST_VALID_LOSS = valid_loss,
                   MODEL_PATH + 'model_epoch{}_loss{}.pt'.format(epoch + 1, valid_loss))

    print(f'Epoch: {epoch + 1:02} | Epoch Time: {epoch_mins}m {epoch_secs}s')
    print(f'\tTrain Loss: {train_loss:.3f} | Train Acc: {train_acc * 100:.2f}%')
    print(f'\t Val. Loss: {valid_loss:.3f} |  Val. Acc: {valid_acc * 100:.2f}%')
    print(f'\t Val. Precision: {valid_precision}')
    print(f'\t Val. Recall: {valid_recall}')
    print(f'\t Val. F1_Score: {valid_fscore}')

    log_file.write(f'Epoch: {epoch + 1:02} | Epoch Time: {epoch_mins}m {epoch_secs}s\n')
    log_file.write(f'\tTrain Loss: {train_loss:.3f} | Train Acc: {train_acc * 100:.2f}%\n')
    log_file.write(f'\t Val. Loss: {valid_loss:.3f} |  Val. Acc: {valid_acc * 100:.2f}%\n')
    log_file.write(f'\t Val. Precision: {valid_precision}\n')
    log_file.write(f'\t Val. Recall: {valid_recall}\n')
    log_file.write(f'\t Val. F1_Score: {valid_fscore}\n')

# lookup = EmbeddingWrapper(model_embedding, , question_tokens, vocab_size)
# result = evaluate_sample(df_qa, lookup)
# result.to_csv('some_lstm.csv')

please help:(


This error means that precision calculated in the current iteration has 5 values but previously calculated precision which has been assigned to evaluate_parameters_dict['precision'] has 14 values.

A simple snippet that reproduces that error:

np.random.randn(5,) + np.random.randn(14, )

Which means you need to ensure that precision you are calculating in every iteration has the same size.
In your code you might need to double check the input of precision_recall_fscore_support method. I think the corresponding inputs are qa_similary and batch.label.


Hi and tnx for answering, i know that it see some difrent classes in every batch and make trouble in precision, but i don,t know how to solve it
The models output (qa_sinilarity) is in :

And the task is question answering by finding similarest question in dataset and by keras it obtain up yo 96% acc but in transfering the code into pytorch i face this problem,by this informations, can you help me more please?

Unfortunately, I do not know how Keras works, so I cannot help with that. But the issue you have is related to sklearn and numpy. If you are using same exact code (except model definition, etc) for evaluation precision, then I would say your implementation may not be identical.
Something you can try is to provide random inputs of different shapes to both model and check if they generate outputs with identical shapes. Possibly they do not as the sklearn code is shared between your two implementations.

This is to thank you for devoting your time to answering my questions and helping me

1 Like