Scoring in Hyperparameter tuning fails because of variables with inconsistent number of samples

I’m doing a hyperparameter tuning using sklearn’s GridSearchCV.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> if cfg.tuning is True:
print("extractingY...n")
y = extract_Y(test_dataloader)
print("finished extractionn")
ht = hyp_tuning(model=model, optimizer=optimizer)
param_grid = {
'lr': [1e-2, 1.5e-2]
}
search = GridSearchCV(ht, param_grid, cv=5, scoring='precision')
#x is a "dummy" matrix because i use dataloader in fit and predict
x = [0 for _ in range(len(y))]
print(len(y))
result = search.fit(x, y)
print('Best: %f using %s' % (result.best_score_, result.best_params_))
</code>
<code> if cfg.tuning is True: print("extractingY...n") y = extract_Y(test_dataloader) print("finished extractionn") ht = hyp_tuning(model=model, optimizer=optimizer) param_grid = { 'lr': [1e-2, 1.5e-2] } search = GridSearchCV(ht, param_grid, cv=5, scoring='precision') #x is a "dummy" matrix because i use dataloader in fit and predict x = [0 for _ in range(len(y))] print(len(y)) result = search.fit(x, y) print('Best: %f using %s' % (result.best_score_, result.best_params_)) </code>
    if cfg.tuning is True:
        print("extractingY...n")
        y = extract_Y(test_dataloader)
        print("finished extractionn")
        ht = hyp_tuning(model=model, optimizer=optimizer)
        param_grid = {
            'lr': [1e-2, 1.5e-2]
        }
        search = GridSearchCV(ht, param_grid, cv=5, scoring='precision')
        #x is a "dummy" matrix because i use dataloader in fit and predict
        x = [0 for _ in range(len(y))]
        print(len(y))
        result = search.fit(x, y)
        print('Best: %f using %s' % (result.best_score_, result.best_params_))

However, the scoring always fails because of this error:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>C:UsersParluAppDataLocalProgramsPythonPython39libsite-packagessklearnmodel_selection_validation.py:982: UserWarning: Scoring failed. The score on this train-test partition for these parameters will be set to nan. Details:
Traceback (most recent call last):
File "C:UsersParluAppDataLocalProgramsPythonPython39libsite-packagessklearnmodel_selection_validation.py", line 971, in _score
scores = scorer(estimator, X_test, y_test, **score_params)
File "C:UsersParluAppDataLocalProgramsPythonPython39libsite-packagessklearnmetrics_scorer.py", line 279, in __call__
return self._score(partial(_cached_call, None), estimator, X, y_true, **_kwargs)
File "C:UsersParluAppDataLocalProgramsPythonPython39libsite-packagessklearnmetrics_scorer.py", line 376, in _score
return self._sign * self._score_func(y_true, y_pred, **scoring_kwargs)
File "C:UsersParluAppDataLocalProgramsPythonPython39libsite-packagessklearnutils_param_validation.py", line 213, in wrapper
return func(*args, **kwargs)
File "C:UsersParluAppDataLocalProgramsPythonPython39libsite-packagessklearnmetrics_classification.py", line 2190, in precision_score
p, _, _, _ = precision_recall_fscore_support(
File "C:UsersParluAppDataLocalProgramsPythonPython39libsite-packagessklearnutils_param_validation.py", line 186, in wrapper
return func(*args, **kwargs)
File "C:UsersParluAppDataLocalProgramsPythonPython39libsite-packagessklearnmetrics_classification.py", line 1775, in precision_recall_fscore_support
labels = _check_set_wise_labels(y_true, y_pred, average, labels, pos_label)
File "C:UsersParluAppDataLocalProgramsPythonPython39libsite-packagessklearnmetrics_classification.py", line 1547, in _check_set_wise_labels
y_type, y_true, y_pred = _check_targets(y_true, y_pred)
File "C:UsersParluAppDataLocalProgramsPythonPython39libsite-packagessklearnmetrics_classification.py", line 99, in _check_targets
check_consistent_length(y_true, y_pred)
File "C:UsersParluAppDataLocalProgramsPythonPython39libsite-packagessklearnutilsvalidation.py", line 460, in check_consistent_length
raise ValueError(
ValueError: Found input variables with inconsistent numbers of samples: [2403, 12014]
</code>
<code>C:UsersParluAppDataLocalProgramsPythonPython39libsite-packagessklearnmodel_selection_validation.py:982: UserWarning: Scoring failed. The score on this train-test partition for these parameters will be set to nan. Details: Traceback (most recent call last): File "C:UsersParluAppDataLocalProgramsPythonPython39libsite-packagessklearnmodel_selection_validation.py", line 971, in _score scores = scorer(estimator, X_test, y_test, **score_params) File "C:UsersParluAppDataLocalProgramsPythonPython39libsite-packagessklearnmetrics_scorer.py", line 279, in __call__ return self._score(partial(_cached_call, None), estimator, X, y_true, **_kwargs) File "C:UsersParluAppDataLocalProgramsPythonPython39libsite-packagessklearnmetrics_scorer.py", line 376, in _score return self._sign * self._score_func(y_true, y_pred, **scoring_kwargs) File "C:UsersParluAppDataLocalProgramsPythonPython39libsite-packagessklearnutils_param_validation.py", line 213, in wrapper return func(*args, **kwargs) File "C:UsersParluAppDataLocalProgramsPythonPython39libsite-packagessklearnmetrics_classification.py", line 2190, in precision_score p, _, _, _ = precision_recall_fscore_support( File "C:UsersParluAppDataLocalProgramsPythonPython39libsite-packagessklearnutils_param_validation.py", line 186, in wrapper return func(*args, **kwargs) File "C:UsersParluAppDataLocalProgramsPythonPython39libsite-packagessklearnmetrics_classification.py", line 1775, in precision_recall_fscore_support labels = _check_set_wise_labels(y_true, y_pred, average, labels, pos_label) File "C:UsersParluAppDataLocalProgramsPythonPython39libsite-packagessklearnmetrics_classification.py", line 1547, in _check_set_wise_labels y_type, y_true, y_pred = _check_targets(y_true, y_pred) File "C:UsersParluAppDataLocalProgramsPythonPython39libsite-packagessklearnmetrics_classification.py", line 99, in _check_targets check_consistent_length(y_true, y_pred) File "C:UsersParluAppDataLocalProgramsPythonPython39libsite-packagessklearnutilsvalidation.py", line 460, in check_consistent_length raise ValueError( ValueError: Found input variables with inconsistent numbers of samples: [2403, 12014] </code>
C:UsersParluAppDataLocalProgramsPythonPython39libsite-packagessklearnmodel_selection_validation.py:982: UserWarning: Scoring failed. The score on this train-test partition for these parameters will be set to nan. Details:
Traceback (most recent call last):
  File "C:UsersParluAppDataLocalProgramsPythonPython39libsite-packagessklearnmodel_selection_validation.py", line 971, in _score
    scores = scorer(estimator, X_test, y_test, **score_params)
  File "C:UsersParluAppDataLocalProgramsPythonPython39libsite-packagessklearnmetrics_scorer.py", line 279, in __call__
    return self._score(partial(_cached_call, None), estimator, X, y_true, **_kwargs)
  File "C:UsersParluAppDataLocalProgramsPythonPython39libsite-packagessklearnmetrics_scorer.py", line 376, in _score
    return self._sign * self._score_func(y_true, y_pred, **scoring_kwargs)
  File "C:UsersParluAppDataLocalProgramsPythonPython39libsite-packagessklearnutils_param_validation.py", line 213, in wrapper
    return func(*args, **kwargs)
  File "C:UsersParluAppDataLocalProgramsPythonPython39libsite-packagessklearnmetrics_classification.py", line 2190, in precision_score
    p, _, _, _ = precision_recall_fscore_support(
  File "C:UsersParluAppDataLocalProgramsPythonPython39libsite-packagessklearnutils_param_validation.py", line 186, in wrapper
    return func(*args, **kwargs)
  File "C:UsersParluAppDataLocalProgramsPythonPython39libsite-packagessklearnmetrics_classification.py", line 1775, in precision_recall_fscore_support
    labels = _check_set_wise_labels(y_true, y_pred, average, labels, pos_label)
  File "C:UsersParluAppDataLocalProgramsPythonPython39libsite-packagessklearnmetrics_classification.py", line 1547, in _check_set_wise_labels
    y_type, y_true, y_pred = _check_targets(y_true, y_pred)
  File "C:UsersParluAppDataLocalProgramsPythonPython39libsite-packagessklearnmetrics_classification.py", line 99, in _check_targets
    check_consistent_length(y_true, y_pred)
  File "C:UsersParluAppDataLocalProgramsPythonPython39libsite-packagessklearnutilsvalidation.py", line 460, in check_consistent_length
    raise ValueError(
ValueError: Found input variables with inconsistent numbers of samples: [2403, 12014]

I tried to print the length of y, but the terminal says that len(y) is 12014, not 2403 as the error says. I really have no idea of what the problem is.

I’m also attaching the function extract_Y

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>def extract_Y(dataloader):
all_labels = []
for data, lengths, targets in tqdm(dataloader):
targets = targets.view(-1)#.cuda()
mask = (targets != -1)
all_labels.extend(targets[mask].cpu().numpy())
return all_labels
</code>
<code>def extract_Y(dataloader): all_labels = [] for data, lengths, targets in tqdm(dataloader): targets = targets.view(-1)#.cuda() mask = (targets != -1) all_labels.extend(targets[mask].cpu().numpy()) return all_labels </code>
def extract_Y(dataloader):
    all_labels = []
    for data, lengths, targets in tqdm(dataloader):
        targets = targets.view(-1)#.cuda()
        mask = (targets != -1)
        all_labels.extend(targets[mask].cpu().numpy())
    return all_labels

New contributor

Parlu10 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật