ValueError: y_true and y_pred have different number of output (1!=53)

username: Rexedoziem
cooked: <p>from sklearn.metrics import mean_squared_error<br>
def eval_fx(valid_loader, model, criterion):<br>
model.eval()<br>
total_loss = 0<br>
total_hits = 0<br>
total_samples = 0<br>
total_examples = 0<br>
final_outputs = <span class="chcklst-box fa fa-square-o fa-fw"></span></p>
<pre><code>with torch.no_grad():
    for batch in tqdm(valid_loader):
        batch = batch.to(device)
        batch_size = batch['user'].batch_size

        # Forward pass
        output = model(batch.x_dict, batch.edge_index_dict)

        # Access the 'user' tensor from the output dictionary
        user_output = output['user']

        # Ground truth labels and predicted scores
        y_true = batch['user'].y[:batch_size]
        y_pred = user_output[:batch_size]

        # Compute the loss
        loss = criterion(y_pred, y_true.long())
        total_loss += loss.item() * batch_size
        total_examples += batch_size

        final_outputs.append(y_pred.cpu())

        # Calculate hit ratio
        hit_ratio = hit_ratio_at_k(y_true, y_pred, k=10)
        total_hits += hit_ratio * batch_size
        total_samples += batch_size
    
# Calculate and print the average loss for the evaluation set
eval_loss = total_loss / total_examples

# Concatenate the tensors in the final_outputs list
final_outputs = torch.cat(final_outputs)
avg_hit_ratio = total_hits / total_samples

return eval_loss, final_outputs, y_true, avg_hit_ratio
</code></pre>
<p>score = mean_squared_error(y_true, final_outputs)</p>
<p>The shape of y_true = [58] and final_outputs = [58, 53]<br>
This is the error:<br>
ValueError                                Traceback (most recent call last)<br>
/tmp/ipykernel_27/2898308318.py in <br>
39<br>
40         # scoring<br>
—&gt; 41         score = mean_squared_error(y_true, final_outputs)<br>
42<br>
43</p>
<p>/opt/conda/lib/python3.7/site-packages/sklearn/metrics/_regression.py in mean_squared_error(y_true, y_pred, sample_weight, multioutput, squared)<br>
437     “”"<br>
438     y_type, y_true, y_pred, multioutput = _check_reg_targets(<br>
 → 439         y_true, y_pred, multioutput<br>
440     )<br>
441     check_consistent_length(y_true, y_pred, sample_weight)</p>
<p>/opt/conda/lib/python3.7/site-packages/sklearn/metrics/_regression.py in _check_reg_targets(y_true, y_pred, multioutput, dtype)<br>
105         raise ValueError(<br>
106             “y_true and y_pred have different number of output ({0}!={1})”.format(<br>
 → 107                 y_true.shape[1], y_pred.shape[1]<br>
108             )<br>
109         )</p>

username: Tony-Y
cooked: <p><code>final_outputs </code> is a tensor of probabilities with shape (N,C) where N is the batch size and C is the number of classes. So, you can get the predicted labels <code>y_pred</code> from <code>torch.argmax(final_outputs,  dim=1)</code>. Then,</p>
<pre data-code-wrap="python"><code class="lang-python">score = mean_squared_error(y_true, y_pred)
</code></pre>

username: Rexedoziem
cooked: <p>Thanks <a class="mention" href="/u/tony-y">@Tony-Y</a> for the clearance, but i’ve got a question. When using hitrate from torcheval, i noticed that the y_pred must be a 2D tensor (N, C) and the y_true (N, ). Does that mean that i can use the y_pred as it is without using the argmax of it for the hitrate. I just need clarity here? Thanks.</p>

username: Tony-Y
cooked: <p><a href="https://pytorch.org/torcheval/stable/generated/torcheval.metrics.functional.hit_rate.html#torcheval.metrics.functional.hit_rate" rel="noopener nofollow ugc">hit_rate</a> needs logits or probabilities.</p>