-
Notifications
You must be signed in to change notification settings - Fork 16
⭐️ Entity embedder interface is here #286
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
0d738d3
⭐️ Entity embedder interface is here
EssamWisam 4ce351e
👨🔧 Zero dropout and deep copy
EssamWisam 757c186
Update entity_embedding.jl
EssamWisam d95fc8e
Update src/mlj_embedder_interface.jl
EssamWisam 9de68f3
Update src/mlj_embedder_interface.jl
EssamWisam 4e11f18
Update src/mlj_embedder_interface.jl
EssamWisam baf10d9
🌟 Keyword argument model for consistency
EssamWisam f121801
Merge branch 'entity-embedder' of https://github.com/FluxML/MLJFlux.j…
EssamWisam 8ac77e4
📖 Update docs
EssamWisam 0753e94
✏️ Better docs
EssamWisam 34a5af3
🚀 Update the interface
EssamWisam b879986
Update src/mlj_embedder_interface.jl
EssamWisam 9811554
Update src/mlj_embedder_interface.jl
EssamWisam File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| ### EntityEmbedder with MLJ Interface | ||
|
|
||
| # 1. Interface Struct | ||
| mutable struct EntityEmbedder{M <: MLJFluxModel} <: Unsupervised | ||
| model::M | ||
| end; | ||
|
|
||
|
|
||
| # 2. Constructor | ||
| function EntityEmbedder(model;) | ||
| return EntityEmbedder(model) | ||
| end; | ||
|
|
||
|
|
||
| # 4. Fitted parameters (for user access) | ||
| MMI.fitted_params(::EntityEmbedder, fitresult) = fitresult | ||
|
|
||
| # 5. Fit method | ||
| function MMI.fit(transformer::EntityEmbedder, verbosity::Int, X, y) | ||
| return MLJModelInterface.fit(transformer.model, verbosity, X, y) | ||
| end; | ||
|
|
||
|
|
||
| # 6. Transform method | ||
| function MMI.transform(transformer::EntityEmbedder, fitresult, Xnew) | ||
| Xnew_transf = MLJModelInterface.transform(transformer.model, fitresult, Xnew) | ||
| return Xnew_transf | ||
| end | ||
|
|
||
| # 8. Extra metadata | ||
| MMI.metadata_pkg( | ||
| EntityEmbedder, | ||
| package_name = "MLJTransforms", | ||
| package_uuid = "23777cdb-d90c-4eb0-a694-7c2b83d5c1d6", | ||
| package_url = "https://github.com/JuliaAI/MLJTransforms.jl", | ||
| is_pure_julia = true, | ||
| ) | ||
|
|
||
| MMI.metadata_model( | ||
| EntityEmbedder, | ||
| input_scitype = Table, | ||
| output_scitype = Table, | ||
ablaom marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| load_path = "MLJTransforms.EntityEmbedder", | ||
| ) | ||
|
|
||
| MMI.target_in_fit(::Type{<:EntityEmbedder}) = true | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
| """ | ||
| $(MMI.doc_header(EntityEmbedder)) | ||
|
|
||
ablaom marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| `EntityEmbedder` implements entity embeddings as in the "Entity Embeddings of Categorical Variables" paper by Cheng Guo, Felix Berkhahn. | ||
|
|
||
| # Training data | ||
|
|
||
| In MLJ (or MLJBase) bind an instance unsupervised `model` to data with | ||
|
|
||
| mach = machine(model, X, y) | ||
|
|
||
| Here: | ||
|
|
||
|
|
||
| - `X` is any table of input features (eg, a `DataFrame`). Features to be transformed must | ||
| have element scitype `Multiclass` or `OrderedFactor`. Use `schema(X)` to | ||
| check scitypes. | ||
|
|
||
| - `y` is the target, which can be any `AbstractVector` whose element | ||
| scitype is `Continuous` or `Count` for regression problems and | ||
ablaom marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| `Multiclass` or `OrderedFactor` for classification problems; check the scitype with `schema(y)` | ||
|
|
||
| Train the machine using `fit!(mach)`. | ||
|
|
||
| # Hyper-parameters | ||
|
|
||
| - `model`: The underlying deep learning model to be used for entity embedding. So far this supports `NeuralNetworkClassifier`, `NeuralNetworkRegressor`, and `MultitargetNeuralNetworkRegressor`. | ||
EssamWisam marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| # Operations | ||
|
|
||
| - `transform(mach, Xnew)`: Transform the categorical features of `Xnew` into dense `Continuous` vectors using the trained `MLJFlux.EntityEmbedderLayer` layer present in the network. | ||
| Check relevant documentation [here](https://fluxml.ai/MLJFlux.jl/dev/) and in particular, the `embedding_dims` hyperparameter. | ||
|
|
||
|
|
||
| # Examples | ||
|
|
||
| ```julia | ||
| using MLJFlux | ||
EssamWisam marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| using MLJ | ||
| using CategoricalArrays | ||
|
|
||
| # Setup some data | ||
| N = 200 | ||
| X = (; | ||
| Column1 = repeat(Float32[1.0, 2.0, 3.0, 4.0, 5.0], Int(N / 5)), | ||
| Column2 = categorical(repeat(['a', 'b', 'c', 'd', 'e'], Int(N / 5))), | ||
| Column3 = categorical(repeat(["b", "c", "d", "f", "f"], Int(N / 5)), ordered = true), | ||
| Column4 = repeat(Float32[1.0, 2.0, 3.0, 4.0, 5.0], Int(N / 5)), | ||
| Column5 = randn(Float32, N), | ||
| Column6 = categorical( | ||
| repeat(["group1", "group1", "group2", "group2", "group3"], Int(N / 5)), | ||
| ), | ||
| ) | ||
| y = categorical([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) # Classification | ||
|
|
||
| # Initiate model | ||
| NeuralNetworkClassifier = @load NeuralNetworkClassifier pkg=MLJFlux | ||
EssamWisam marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| clf = NeuralNetworkClassifier(embedding_dims=Dict(:Column2 => 2, :Column3 => 2)) | ||
|
|
||
| emb = EntityEmbedder(clf) | ||
|
|
||
| # Construct machine | ||
| mach = machine(emb, X, y) | ||
|
|
||
| # Train model | ||
| fit!(mach) | ||
|
|
||
| # Transform data using model to encode categorical columns | ||
| Xnew = transform(mach, X) | ||
| Xnew | ||
| ``` | ||
|
|
||
| See also | ||
| [`TargetEncoder`](@ref) | ||
| """ | ||
| EntityEmbedder | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.