Skip to content
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

String import/export #2

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions README.md
Expand Up @@ -56,6 +56,22 @@ Load the model from a file
model.load_model("model.bin")
```

Export the model to string

```ruby
require 'base64'
binary_string = model.export_model_to_binary_string
safe_encoded_string = Base64.encode64(binary_string) #BETTER, BUT OPTIONAL
```

Import the model from string

```ruby
require 'base64'
binary_string = Base64.decode64(safe_encoded_string) #BETTER, BUT OPTIONAL
model = model.import_model_from_binary_string(binary_string)
```

Train online

```ruby
Expand Down
16 changes: 13 additions & 3 deletions lib/vowpalwabbit/model.rb
Expand Up @@ -36,25 +36,35 @@ def coefs
coefs
end

def save_model(filename)
def export_model_to_binary_string
buffer_handle = ::FFI::MemoryPointer.new(:pointer)
output_data = ::FFI::MemoryPointer.new(:pointer)
output_size = ::FFI::MemoryPointer.new(:size_t)
FFI.VW_CopyModelData(handle, buffer_handle, output_data, output_size)
bin_str = output_data.read_pointer.read_string(output_size.read(:size_t))
FFI.VW_FreeIOBuf(buffer_handle.read_pointer)

bin_str
end

def save_model(filename)
bin_str = export_model_to_binary_string
File.binwrite(filename, bin_str)
nil
end

def load_model(filename)
bin_str = File.binread(filename)
def import_model_from_binary_string(bin_str)
model_data = ::FFI::MemoryPointer.new(:char, bin_str.bytesize)
model_data.put_bytes(0, bin_str)
@handle = FFI.VW_InitializeWithModel(param_str(@params), model_data, bin_str.bytesize)
nil
end

def load_model(filename)
bin_str = File.binread(filename)
import_model_from_binary_string(bin_str)
end

private

# TODO clean-up handle
Expand Down
2 changes: 1 addition & 1 deletion lib/vowpalwabbit/version.rb
@@ -1,3 +1,3 @@
module VowpalWabbit
VERSION = "0.1.1"
VERSION = "0.1.2"
end