-
Notifications
You must be signed in to change notification settings - Fork 194
Updates to improve the performance and logging of the transformer inf… #104
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 all commits
Commits
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
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
103 changes: 103 additions & 0 deletions
103
Core/src/main/java/org/tribuo/transform/transformations/IDFTransformation.java
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,103 @@ | ||
| package org.tribuo.transform.transformations; | ||
|
|
||
| import com.oracle.labs.mlrg.olcut.provenance.Provenance; | ||
| import java.util.Collections; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import org.tribuo.transform.TransformStatistics; | ||
| import org.tribuo.transform.Transformation; | ||
| import org.tribuo.transform.TransformationProvenance; | ||
| import org.tribuo.transform.Transformer; | ||
|
|
||
| /** | ||
| * A feature transformation that computes the IDF for features and then transforms | ||
| * them with a TF-IDF weighting. | ||
| */ | ||
| public class IDFTransformation implements Transformation { | ||
|
|
||
| private TransformationProvenance provenance; | ||
|
|
||
| @Override | ||
| public TransformStatistics createStats() { | ||
| return new IDFStatistics(); | ||
| } | ||
|
|
||
| @Override | ||
| public TransformationProvenance getProvenance() { | ||
| if(provenance == null) { | ||
| provenance = new IDFTransformationProvenance(); | ||
| } | ||
| return provenance; | ||
| } | ||
|
|
||
| private static class IDFStatistics implements TransformStatistics { | ||
|
|
||
| /** | ||
| * The document frequency for the feature that this statistic is | ||
| * associated with. This is a count of the number of examples that the | ||
| * feature occurs in. | ||
| */ | ||
| private int df; | ||
|
|
||
| /** | ||
| * The number of examples that the feature did not occur in. | ||
| */ | ||
| private int sparseObservances; | ||
|
|
||
|
|
||
| @Override | ||
| public void observeValue(double value) { | ||
| // | ||
| // One more document (i.e., an example) has this feature. | ||
| df++; | ||
| } | ||
|
|
||
| @Override | ||
| public void observeSparse() { | ||
| } | ||
|
|
||
| @Override | ||
| public void observeSparse(int count) { | ||
| sparseObservances = count; | ||
| } | ||
|
|
||
| @Override | ||
| public Transformer generateTransformer() { | ||
| return new IDFTransformer(df, df+sparseObservances); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| private static class IDFTransformer implements Transformer { | ||
|
|
||
| private double df; | ||
|
|
||
| private double N; | ||
|
|
||
| public IDFTransformer(int df, int N) { | ||
| this.df = df; | ||
| this.N = N; | ||
| } | ||
|
|
||
| @Override | ||
| public double transform(double tf) { | ||
| return Math.log(N / df) * (1 + Math.log(tf)); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| public final static class IDFTransformationProvenance implements TransformationProvenance { | ||
|
|
||
| @Override | ||
| public Map<String, Provenance> getConfiguredParameters() { | ||
| return Collections.unmodifiableMap(new HashMap<>()); | ||
| } | ||
|
|
||
| @Override | ||
| public String getClassName() { | ||
| return IDFTransformation.class.getName(); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| } |
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.