-
Notifications
You must be signed in to change notification settings - Fork 125
feat: add keyword argument support for train_from_stream (#114)
#155
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
base: master
Are you sure you want to change the base?
Changes from 4 commits
9fe6225
7c8837f
069db71
0de3ae2
19bd731
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -268,9 +268,10 @@ def self.load_checkpoint(storage:, checkpoint_id:) | |
| # puts "#{progress.completed} documents processed" | ||
| # end | ||
| # | ||
| # @rbs (String | Symbol, IO, ?batch_size: Integer) { (Streaming::Progress) -> void } -> void | ||
| def train_from_stream(category, io, batch_size: Streaming::DEFAULT_BATCH_SIZE, &block) | ||
| @lsi.train_from_stream(category, io, batch_size: batch_size, &block) | ||
| # @rbs (?(String | Symbol | nil), ?IO?, ?batch_size: Integer, **IO) { (Streaming::Progress) -> void } -> void | ||
| def train_from_stream(category = nil, io = nil, batch_size: Streaming::DEFAULT_BATCH_SIZE, **categories, &block) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit — inconsistent block forwarding. KNN uses def train_from_stream(category = nil, io = nil, batch_size: Streaming::DEFAULT_BATCH_SIZE, **categories, &)
@lsi.train_from_stream(category, io, batch_size: batch_size, **categories, &)
synchronize { @dirty = true }
endAlso: when must-fix #3 is fixed, the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @cardmagic thanks, fixed!
Unfortunately, this has no effect. |
||
| # @type var categories: untyped | ||
| @lsi.train_from_stream(category, io, batch_size: batch_size, **categories, &block) | ||
| synchronize { @dirty = true } | ||
| end | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,8 +26,8 @@ module Streaming | |
| # Trains the classifier from an IO stream. | ||
| # Each line in the stream is treated as a separate document. | ||
| # | ||
| # @rbs (Symbol | String, IO, ?batch_size: Integer) { (Progress) -> void } -> void | ||
| def train_from_stream(category, io, batch_size: DEFAULT_BATCH_SIZE, &block) | ||
| # @rbs (?(Symbol | String | nil), ?IO?, ?batch_size: Integer, **Hash[Symbol, IO]) { (Progress) -> void } -> void | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Must-fix #3 — base-module type signature mismatches the implementations. In RBS,
The base stub should match: # @rbs (?(Symbol | String | nil), ?IO?, ?batch_size: Integer, **IO) { (Progress) -> void } -> voidThis may also let you drop the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @cardmagic thanks, fixed!
But |
||
| def train_from_stream(category = nil, io = nil, batch_size: DEFAULT_BATCH_SIZE, **categories, &block) | ||
| raise NotImplementedError, "#{self.class} must implement train_from_stream" | ||
| end | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| require_relative '../test_helper' | ||
| require 'stringio' | ||
|
|
||
| class KNNStreamingTest < Minitest::Test | ||
| def test_train_from_stream_basic | ||
| knn = Classifier::KNN.new | ||
| knn.train_from_stream(:spam, StringIO.new("buy now cheap\nfree money\nlimited offer\n")) | ||
|
|
||
| assert_equal 'spam', knn.classify('buy cheap free') | ||
| end | ||
|
|
||
| def test_train_from_stream_many_categories | ||
| knn = Classifier::KNN.new | ||
| knn.train_from_stream( | ||
| spam: StringIO.new("buy now cheap\nfree money\nlimited offer\n"), | ||
| ham: StringIO.new("hello friend\nmeeting tomorrow\nhello fellow\n") | ||
| ) | ||
|
|
||
| assert_equal 'spam', knn.classify('free offer') | ||
| assert_equal 'ham', knn.classify('hello') | ||
| end | ||
|
|
||
| def test_train_from_stream_invalid_io_type | ||
| knn = Classifier::KNN.new | ||
| assert_raises(StandardError) do | ||
| knn.train_from_stream(spam: Object.new) | ||
| end | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| require_relative '../test_helper' | ||
| require 'stringio' | ||
|
|
||
| class LogisticRegressionStreamingTest < Minitest::Test | ||
| def test_train_from_stream_basic | ||
| classifier = Classifier::LogisticRegression.new('Spam', 'Ham') | ||
| classifier.train_from_stream(:spam, StringIO.new("buy now cheap\nfree money\nlimited offer\n")) | ||
| classifier.fit | ||
|
|
||
| assert_equal 'Spam', classifier.classify('buy cheap free') | ||
| end | ||
|
|
||
| def test_train_from_stream_many_categories | ||
| classifier = Classifier::LogisticRegression.new('Spam', 'Ham') | ||
| classifier.train_from_stream( | ||
| spam: StringIO.new("buy now cheap\nfree money\nlimited offer\n"), | ||
| ham: StringIO.new("hello friend\nmeeting tomorrow\n") | ||
| ) | ||
| classifier.fit | ||
|
|
||
| assert_equal 'Spam', classifier.classify('buy free') | ||
| assert_equal 'Ham', classifier.classify('hello meeting') | ||
| end | ||
|
|
||
| def test_train_from_stream_invalid_io_type | ||
| classifier = Classifier::LogisticRegression.new('Spam', 'Ham') | ||
| assert_raises(StandardError) do | ||
| classifier.train_from_stream(spam: Object.new) | ||
| end | ||
| end | ||
| end |
Uh oh!
There was an error while loading. Please reload this page.