Class: Tangram::Model

Inherits:
Object
  • Object
show all
Defined in:
lib/tangram/tangram.rb

Overview

Use this class to load a model, make predictions, and log events to the app.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(c_model, options: nil) ⇒ Model

Returns a new instance of Model.


257
258
259
260
261
# File 'lib/tangram/tangram.rb', line 257

def initialize(c_model, options: nil)
  @tangram_url = options&.tangram_url.nil? ? 'https://app.tangram.dev' : options&.tangram_url
  @log_queue = []
  @model = FFI::AutoPointer.new(c_model.read_pointer, LibTangram.method(:tangram_model_delete))
end

Class Method Details

.from_bytes(bytes, options: nil) ⇒ Model

Load a model from bytes instead of a file. You should use this only if you already have a `.tangram` loaded into memory. Otherwise, use `Model.from_path`, which is faster because it memory maps the file.

Parameters:

  • bytes (String)

    The bytes for the .tangram model.

  • options (LoadModelOptions) (defaults to: nil)

    The options to use when loading the model.

Returns:


245
246
247
248
249
250
251
252
253
254
255
# File 'lib/tangram/tangram.rb', line 245

def self.from_bytes(bytes, options: nil)
  c_model = FFI::MemoryPointer.new(:pointer)
  c_err = LibTangram.tangram_model_from_bytes(bytes, bytes.size, c_model)
  unless err.null?
    c_err = FFI::AutoPointer.new(c_err, LibTangram.method(:tangram_error_delete))
    c_error_s = LibTangram::TangramStringView.new
    LibTangram.tangram_error_get_message(c_err, c_error_s)
    raise errors.into_string
  end
  new(c_model, options: options)
end

.from_path(path, options: nil) ⇒ Model

Load a model from the `.tangram` file at `path`.

Parameters:

  • path (String)

    The path to the `.tangram` file.

  • options (LoadModelOptions) (defaults to: nil)

    The options to use when loading the model.

Returns:


229
230
231
232
233
234
235
236
237
238
239
# File 'lib/tangram/tangram.rb', line 229

def self.from_path(path, options: nil)
  c_model = FFI::MemoryPointer.new(:pointer)
  c_err = LibTangram.tangram_model_from_path(path, c_model)
  unless c_err.null?
    c_err = FFI::AutoPointer.new(c_err, LibTangram.method(:tangram_error_delete))
    c_error_s = LibTangram::TangramStringView.new
    LibTangram.tangram_error_get_message(c_err, c_error_s)
    raise c_error_s.into_string
  end
  new(c_model, options: options)
end

Instance Method Details

#enqueue_log_prediction(identifier:, input:, output:, options: nil) ⇒ Object

Add a prediction event to the queue. Remember to call `flush_log_queue` at a later point to send the event to the app.

Parameters:

  • identifier (String, Number)

    This is a unique identifier for the prediction, which will associate it with a true value event and allow you to look it up in the app.

  • input (Hash{String, Symbol => String, Number})

    A single `PredictInput`.

  • output (PredictOutput)

    A single `PredictOutput`.

  • options (PredictOptions) (defaults to: nil)

    This is the same `predictOptions` value that you passed to `predict`.


307
308
309
310
311
312
313
314
315
# File 'lib/tangram/tangram.rb', line 307

def enqueue_log_prediction(identifier:, input:, output:, options: nil)
  event = prediction_event(
    identifier: identifier,
    input: input,
    output: output,
    options: options
  )
  log_queue.push(event)
end

#enqueue_log_true_value(identifier:, true_value:) ⇒ Object

Add a true value event to the queue. Remember to call `flush_log_queue` at a later point to send the event to the app.

Parameters:

  • identifier (String, Number)

    This is a unique identifier for the prediction, which will associate it with a true value event and allow you to look it up in the app.

  • true_value (String, Number)

    This is the true value for the prediction.


331
332
333
334
335
336
337
# File 'lib/tangram/tangram.rb', line 331

def enqueue_log_true_value(identifier:, true_value:)
  event = true_value_event(
    identifier: identifier,
    true_value: true_value
  )
  log_queue.push(event)
end

#flush_log_queueObject

Send all events in the queue to the app.


340
341
342
343
# File 'lib/tangram/tangram.rb', line 340

def flush_log_queue
  log_events(@log_queue)
  @log_queue = []
end

#idObject

Retrieve the model's id.


264
265
266
267
268
# File 'lib/tangram/tangram.rb', line 264

def id
  c_id = LibTangram::TangramStringView.new
  LibTangram.tangram_model_get_id(@model, c_id)
  c_id.into_string
end

#log_prediction(identifier:, input:, output:, options: nil) ⇒ Object

Send a prediction event to the app. If you want to batch events, you can use `enqueue_log_prediction` instead.

Parameters:

  • identifier (String, Number)

    This is a unique identifier for the prediction, which will associate it with a true value event and allow you to look it up in the app.

  • input (Hash{String, Symbol => String, Number})

    A single `PredictInput`.

  • output (PredictOutput)

    A single `PredictOutput`.

  • options (PredictOptions) (defaults to: nil)

    This is the same `predictOptions` value that you passed to `predict`.


292
293
294
295
296
297
298
299
300
# File 'lib/tangram/tangram.rb', line 292

def log_prediction(identifier:, input:, output:, options: nil)
  event = prediction_event(
    identifier: identifier,
    input: input,
    output: output,
    options: options
  )
  log_event(event)
end

#log_true_value(identifier:, true_value:) ⇒ Object

Send a true value event to the app. If you want to batch events, you can use `enqueue_log_true_value` instead.

Parameters:

  • identifier (String, Number)

    This is a unique identifier for the prediction, which will associate it with a true value event and allow you to look it up in the app.

  • true_value (String, Number)

    This is the true value for the prediction.


320
321
322
323
324
325
326
# File 'lib/tangram/tangram.rb', line 320

def log_true_value(identifier:, true_value:)
  event = true_value_event(
    identifier: identifier,
    true_value: true_value
  )
  log_event(event)
end

#predict(input, options: nil) ⇒ Array<RegressionPredictOutput, BinaryClassificationPredictOutput, MulticlassClassificationPredictOutput>, ...

Make a prediction!

Parameters:

  • input (Array<Hash{String, Symbol => String, Number}>, Hash{String, Symbol => String, Number})

    A predict input is either a single predict input which is a map from symbols or strings to strings or floats or an array of such maps. The keys should match the columns in the CSV file you trained your model with.

  • options (PredictOptions) (defaults to: nil)

    These are the predict options.

Returns:


274
275
276
277
278
279
280
281
282
283
284
285
# File 'lib/tangram/tangram.rb', line 274

def predict(input, options: nil)
  is_array = input.is_a?(Array)
  input = is_array ? input : [input]
  c_input_vec = new_predict_input_vec(input)
  c_options = new_predict_options(options)
  c_output_vec = FFI::MemoryPointer.new(:pointer)
  c_error = LibTangram.tangram_model_predict(@model, c_input_vec, c_options, c_output_vec)
  raise 'tangram error' unless c_error.null?
  c_output_vec = FFI::AutoPointer.new(c_output_vec.read_pointer, LibTangram.method(:tangram_predict_output_vec_delete))
  output = predict_output_vec_from_tangram_predict_output_vec(c_output_vec)
  is_array ? output : output[0]
end