from sklearn.metrics import mean_squared_error
def eval_fx(valid_loader, model, criterion):
model.eval()
total_loss = 0
total_hits = 0
total_samples = 0
total_examples = 0
final_outputs =
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
score = mean_squared_error(y_true, final_outputs)
The shape of y_true = [58] and final_outputs = [58, 53]
This is the error:
ValueError Traceback (most recent call last)
/tmp/ipykernel_27/2898308318.py in
39
40 # scoring
—> 41 score = mean_squared_error(y_true, final_outputs)
42
43
/opt/conda/lib/python3.7/site-packages/sklearn/metrics/_regression.py in mean_squared_error(y_true, y_pred, sample_weight, multioutput, squared)
437 “”"
438 y_type, y_true, y_pred, multioutput = _check_reg_targets(
→ 439 y_true, y_pred, multioutput
440 )
441 check_consistent_length(y_true, y_pred, sample_weight)
/opt/conda/lib/python3.7/site-packages/sklearn/metrics/_regression.py in _check_reg_targets(y_true, y_pred, multioutput, dtype)
105 raise ValueError(
106 “y_true and y_pred have different number of output ({0}!={1})”.format(
→ 107 y_true.shape[1], y_pred.shape[1]
108 )
109 )