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

Mary_nj: <p>i have  an error and no body can solve it… is there any body to help?<br>
it is the erro<br>
"    evaluate_parameters_dict[“precision”] = precision + evaluate_parameters_dict[“precision”]<br>
ValueError: operands could not be broadcast together with shapes (5,) (14,)</p>
<p>"<br>
in this code:</p>
<pre><code class="lang-auto">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, \
    EMBEDDING_SIZE, HIDDEN_SIZE, DROPOUT, N_LAYERS, N_EPOCHS, TEST_DATA, LSTM_UNITS, MODEL_NAME

log_file = open(LOG_PATH, 'w')

dataset = Dataset(NEG_CSV, GLOVE_MODEL_PATH, TEST_DATA)
dataset.load_data()

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)
model.embeddings.weight.data.copy_(dataset.pretrain_embeddings)
model.embeddings.weight.data[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 = model.to(DEVICE)
criterion = criterion.to(DEVICE)


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
    model.train()
    iter_len = len(iterator)
    n_batch = 0
    for batch in iterator:
        n_batch += 1
        optimizer.zero_grad()
        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)
        loss.backward()
        optimizer.step()
        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)}')
            print('_______________________________________________')

            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')
            log_file.write('_______________________________________________\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}
    model.eval()
    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 &lt; BEST_VALID_LOSS:
        BEST_VALID_LOSS = valid_loss
        torch.save(model,
                   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}')
    print('_____________________________________________________________________\n')

    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')
    log_file.write('____________________________________________________________\n')
    log_file.flush()

# lookup = EmbeddingWrapper(model_embedding, , question_tokens, vocab_size)
# result = evaluate_sample(df_qa, lookup)
# result.to_csv('some_lstm.csv')
</code></pre>
<p>please help:(</p>

Nikronic: <p>Hi,</p>
<aside class="quote no-group" data-username="Mary_nj" data-post="1" data-topic="90034">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/mary_nj/48/12934_2.png" class="avatar"> Mary_nj:</div>
<blockquote>
<p>ValueError: operands could not be broadcast together with shapes (5,) (14,)</p>
</blockquote>
</aside>
<p>This error means that <code>precision</code> calculated in the current iteration has 5 values but previously calculated <code>precision</code> which has been assigned to <code>evaluate_parameters_dict['precision']</code> has 14 values.</p>
<p>A simple snippet that reproduces that error:</p>
<pre><code class="lang-auto">np.random.randn(5,) + np.random.randn(14, )
</code></pre>
<p>Which means you need to ensure that <code>precision</code> you are calculating in every iteration has the same size.<br>
In your code you might need to double check the input of <code>precision_recall_fscore_support</code> method. I think the corresponding inputs are <code>qa_similary</code> and <code>batch.label</code>.</p>
<p>Bests</p>

Mary_nj: <p>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<br>
The models output (qa_sinilarity) is in :</p><aside class="quote quote-modified" data-post="1" data-topic="90033">
  <div class="title">
    <div class="quote-controls"></div>
    <img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/mary_nj/48/12934_2.png" class="avatar">
    <a href="https://discuss.pytorch.org/t/migrating-from-keras-to-pytorch/90033">Migrating from keras to pytorch</a> 
  </div>
  <blockquote>
    hi every body. please help me. 
in just want to transfer a code from keras to pytorch 
first q: is this two models are same? if not what is exact equivalence code in pytorch:) 
so: 
is this code…? 
  question = layers.Input(shape=(None,), dtype='int32', name='question')
        answer = layers.Input(shape=(None,), dtype='int32', name='answer')

        embedding = layers.Embedding(
            mask_zero=True,
            input_dim=self.vocab_size,
            output_dim=self.embedding_size,
    …
  </blockquote>
</aside>

<p>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?</p>

Nikronic: <p>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 <code>precision</code>, then I would say your implementation may not be identical.<br>
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.</p>

Mary_nj: <p>This is to thank you for devoting your time to answering my questions and helping me</p>