ContrastEncoder: missing buildmatrix with vector-valued mode gives "Nothing is not callable" instead of BUILDFUNC_MUST_BE_SPECIFIED

Đang mở Phù hợp với người mới
#108 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
90/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
julia
Lĩnh vực
machine-learning

Hướng nghiên cứu

Bắt đầu bằng cách đọc src/encoders/contrast_encoder/contrast_encoder.jl xung quanh contrast_encoder_fit, đặc biệt là phần xác thực chế độ và lệnh gọi buildmatrix. Chạy các trường hợp tái hiện lỗi, sau đó cập nhật các bài kiểm thử hiện có trong test/encoders/contrast_encoder.jl để bao phủ các chế độ vectơ chứa :contrast hoặc :hypothesis và các chế độ AbstractVector{Symbol} không phải Vector. Công việc hoàn tất khi các trường hợp này ném ra ArgumentError dự kiến thay vì MethodError hoặc lỗi chế độ không được hỗ trợ, đồng thời hành vi hiện có đối với giá trị vô hướng vẫn được kiểm thử.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

When mode is given as a vector, the buildmatrix check in contrast_encoder_fit never runs, so forgetting buildmatrix for a :contrast / :hypothesis feature gives a confusing MethodError instead of the intended BUILDFUNC_MUST_BE_SPECIFIED message.

The check is:

https://github.com/JuliaAI/MLJTransforms.jl/blob/d3e3db364f30bef397eddeae7f30a5e7967757fc/src/encoders/contrast_encoder/contrast_encoder.jl#L97-L100

if mode in (:contrast, :hypothesis)
    buildmatrix === nothing && throw(ArgumentError(BUILDFUNC_MUST_BE_SPECIFIED))
end

For mode = [:contrast, :dummy] this is [:contrast, :dummy] in (:contrast, :hypothesis), which is false, so we fall through to buildmatrix(name, k) with buildmatrix === nothing at L109/L113.

Reproduction
using MLJTransforms, MLJBase, CategoricalArrays
X = (A = categorical(["a", "b", "c", "a"]), B = categorical(["x", "y", "x", "y"]))

# scalar mode: helpful error
try
    MLJTransforms.contrast_encoder_fit(X, [:A]; mode = :contrast, ignore = false)
catch e
    println("scalar mode  -> ", sprint(showerror, e))
end

# vector mode: check is skipped
try
    MLJTransforms.contrast_encoder_fit(X, [:A, :B]; mode = [:contrast, :dummy], ignore = false)
catch e
    println("vector mode  -> ", sprint(showerror, e))
end

# same through the MLJ interface
enc = ContrastEncoder(features = [:A, :B], mode = [:hypothesis, :dummy], ignore = false)
mach = machine(enc, X)
try
    fit!(mach, verbosity = 0)
catch e
    println("machine      -> ", sprint(showerror, e)[1:min(end, 300)])
end

Output:

scalar mode  -> ArgumentError: In ContrastEncoder with mode=:contrast or mode=:hypothesis, the `buildmatrix` argument must be specified.
vector mode  -> MethodError: objects of type Nothing are not callable.
In case you did not try calling it explicitly, check if a Nothing has been passed as an argument to a method that expects a callable instead.
The object of type `Nothing` exists, but no method is defined for this combination of argument types when trying to treat it as a callable object.
┌ Error: Problem fitting the machine machine(ContrastEncoder(features = [:A, :B], …), …). 
└ @ MLJBase ~/.julia/packages/MLJBase/lZsbe/src/machines.jl:758
[ Info: Running type checks... 
[ Info: Type checks okay. 
machine      -> MethodError: objects of type Nothing are not callable.
In case you did not try calling it explicitly, check if a Nothing has been passed as an argument to a method that expects a callable instead.
The object of type `Nothing` exists, but no method is defined for this combination of argument types wh

Expected: the vector case throws the same ArgumentError(BUILDFUNC_MUST_BE_SPECIFIED) as the scalar case.
Actual: MethodError: objects of type Nothing are not callable.

Related: non-Vector mode vectors

A nearby detail in the same block: the keyword is typed mode::Union{Symbol, AbstractVector{Symbol}} (and the struct allows AbstractVector{Symbol}), but the vector branch at L90 tests mode isa Vector{Symbol}. So any other AbstractVector{Symbol} skips the vector handling and the whole vector ends up treated as one mode:

modes = view([:sum, :dummy, :helmert], 1:2)   # AbstractVector{Symbol}, not a Vector
MLJTransforms.contrast_encoder_fit(X, [:A, :B]; mode = modes, ignore = false)
ArgumentError: Mode [:sum, :dummy] is not supported.
Possible fix

Something like:

mode_is_vector = mode isa AbstractVector{Symbol}
...
modes = mode_is_vector ? mode : (mode,)
if any(in((:contrast, :hypothesis)), modes)
    buildmatrix === nothing && throw(ArgumentError(BUILDFUNC_MUST_BE_SPECIFIED))
end

plus a test next to the existing BUILDFUNC_MUST_BE_SPECIFIED one in test/encoders/contrast_encoder.jl (which currently only covers the scalar mode = :contrast case). Happy to open a PR if this sounds right.

Versions
  • MLJTransforms dev at d3e3db3 (v0.2.4)
  • Julia 1.13.3, MLJBase from the registry, Linux x86_64
Ngôn ngữ chính
Julia
Star
1
Fork
5
Merge trung bình
1 ngày 20 phút
Pull request đã merge (30 ngày)
7

Chuẩn bị môi trường

Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của JuliaAI/MLJTransforms.jl

Tất cả issue của JuliaAI/MLJTransforms.jl

Issue tương tự

Thêm issue về Julia

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.