merlin.models.tf.RetrievalModelV2#

class merlin.models.tf.RetrievalModelV2(*args, **kwargs)[source]#

Bases: merlin.models.tf.models.base.Model

__init__(*, query: Union[Encoder, tf.keras.layers.Layer], output: Union[ModelOutput, tf.keras.layers.Layer], candidate: Optional[Union[Encoder, tf.keras.layers.Layer]] = None, query_name='query', candidate_name='candidate', pre: Optional[tf.keras.layers.Layer] = None, post: Optional[tf.keras.layers.Layer] = None, **kwargs)[source]#

Methods

__init__(*, query, output[, candidate, ...])

add_loss(losses, **kwargs)

Add loss tensor(s), potentially dependent on layer inputs.

add_metric(value[, name])

Adds metric tensor to the layer.

add_update(updates)

Add update op(s), potentially dependent on layer inputs.

add_variable(*args, **kwargs)

Deprecated, do NOT use! Alias for add_weight.

add_weight([name, shape, dtype, ...])

Adds a new variable to the layer.

adjust_predictions_and_targets(predictions, ...)

Adjusts the predctions and targets, doing the following transformations if the target is provided: - Converts ragged targets (and their masks) to dense, so that they are compatible with most losses and metrics - Copies the targets mask to predictions mask, if defined - One-hot encode targets if their tf.rank(targets) == tf.rank(predictions)-1 - Ensures targets has the same shape and dtype as predicitnos

batch_predict(dataset, batch_size, **kwargs)

Batched prediction using the Dask.

build([input_shape])

Builds the model

build_from_config(config)

call(inputs[, targets, training, testing, ...])

call_train_test(x[, y, sample_weight, ...])

Apply the model's call method during Train or Test modes and prepare Prediction (v2) or PredictionOutput (v1 - depreciated) objects

candidate_embeddings([dataset, index])

compile([optimizer, loss, metrics, ...])

Configures the model for training. Example: `python model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=1e-3),               loss=tf.keras.losses.BinaryCrossentropy(),               metrics=[tf.keras.metrics.BinaryAccuracy(),                        tf.keras.metrics.FalseNegatives()]) ` :param optimizer: String (name of optimizer) or optimizer instance. See tf.keras.optimizers. :param loss: Optional[Union[str, Loss, Dict[str, Union[str, Loss]]]] = None losses : Optional[Union[str, Loss, Dict[str, Union[str, Loss]]]], optional {LOSS_PARAMETERS_DOCSTRINGS} See tf.keras.losses. A loss function is any callable with the signature loss = fn(y_true, y_pred), where y_true are the ground truth values, and y_pred are the model's predictions. y_true should have shape (batch_size, d0, .. dN) (except in the case of sparse loss functions such as sparse categorical crossentropy which expects integer arrays of shape (batch_size, d0, .. dN-1)). y_pred should have shape (batch_size, d0, .. dN). The loss function should return a float tensor. If a custom Loss instance is used and reduction is set to None, return value has shape (batch_size, d0, .. dN-1) i.e. per-sample or per-timestep loss values; otherwise, it is a scalar. If the model has multiple outputs, you can use a different loss on each output by passing a dictionary or a list of losses. The loss value that will be minimized by the model will then be the sum of all individual losses, unless loss_weights is specified. :param loss_weights: Optional list or dictionary specifying scalar coefficients (Python floats) to weight the loss contributions of different model outputs. The loss value that will be minimized by the model will then be the weighted sum of all individual losses, weighted by the loss_weights coefficients. If a list, it is expected to have a 1:1 mapping to the model's outputs (Keras sorts tasks by name). If a dict, it is expected to map output names (strings) to scalar coefficients. :param metrics: Optional[ Union[ MetricType, Sequence[MetricType], Sequence[Sequence[MetricType]], Dict[str, MetricType], Dict[str, Sequence[MetricType]], ] ], optional {METRICS_PARAMETERS_DOCSTRING} :param weighted_metrics: Optional[ Union[ MetricType, Sequence[MetricType], Sequence[Sequence[MetricType]], Dict[str, MetricType], Dict[str, Sequence[MetricType]], ] ], optional List of metrics to be evaluated and weighted by sample_weight or class_weight during training and testing. {METRICS_PARAMETERS_DOCSTRING} :param run_eagerly: Bool. Defaults to False. If True, this Model's logic will not be wrapped in a tf.function. Recommended to leave this as None unless your Model cannot be run inside a tf.function. run_eagerly=True is not supported when using tf.distribute.experimental.ParameterServerStrategy. :param steps_per_execution: Int. Defaults to 1. The number of batches to run during each tf.function call. Running multiple batches inside a single tf.function call can greatly improve performance on TPUs or small models with a large Python overhead. At most, one full epoch will be run each execution. If a number larger than the size of the epoch is passed, the execution will be truncated to the size of the epoch. Note that if steps_per_execution is set to N, Callback.on_batch_begin and Callback.on_batch_end methods will only be called every N batches (i.e. before/after each tf.function execution). :param jit_compile: If True, compile the model training step with XLA. [XLA](https://www.tensorflow.org/xla) is an optimizing compiler for machine learning. jit_compile is not enabled for by default. This option cannot be enabled with run_eagerly=True. Note that jit_compile=True is may not necessarily work for all models. For more information on supported operations please refer to the [XLA documentation](https://www.tensorflow.org/xla). Also refer to [known XLA issues](https://www.tensorflow.org/xla/known_issues) for more details. :param **kwargs: Arguments supported for backwards compatibility only.

compile_from_config(config)

compute_loss([x, y, y_pred, sample_weight])

Compute the total loss, validate it, and return it.

compute_mask(inputs[, mask])

Computes an output mask tensor.

compute_metrics(prediction_outputs[, ...])

Overrides Model.compute_metrics() for some custom behaviour

compute_output_shape(input_shape)

Computes the output shape of the layer.

compute_output_signature(input_signature)

Compute the output tensor signature of the layer based on the inputs.

count_params()

Count the total number of scalars composing the weights.

evaluate([x, y, batch_size, verbose, ...])

evaluate_generator(generator[, steps, ...])

Evaluates the model on a data generator.

export(filepath)

Create a SavedModel artifact for inference (e.g.

finalize_state()

Finalizes the layers state after updating layer weights.

fit([x, y, batch_size, epochs, verbose, ...])

fit_generator(generator[, steps_per_epoch, ...])

Fits the model on data yielded batch-by-batch by a Python generator.

freeze_blocks(blocks)

Freeze all sub-blocks of given blocks recursively.

from_block(block, schema[, input_block, ...])

Create a model from a block

from_config(config[, custom_objects])

get_blocks_by_name(block_names)

Get blocks by given block_names, return a list of blocks Traverse(Iterate) the model to check each block (sub_block) by BFS

get_build_config()

get_compile_config()

get_config()

get_input_at(node_index)

Retrieves the input tensor(s) of a layer at a given node.

get_input_mask_at(node_index)

Retrieves the input mask tensor(s) of a layer at a given node.

get_input_shape_at(node_index)

Retrieves the input shape(s) of a layer at a given node.

get_layer([name, index])

Retrieves a layer based on either its name (unique) or index.

get_metrics_result()

Returns the model's metrics values as a dict.

get_output_at(node_index)

Retrieves the output tensor(s) of a layer at a given node.

get_output_mask_at(node_index)

Retrieves the output mask tensor(s) of a layer at a given node.

get_output_shape_at(node_index)

Retrieves the output shape(s) of a layer at a given node.

get_weight_paths()

Retrieve all the variables and their paths for the model.

get_weights()

Retrieves the weights of the model.

load(export_path)

Loads a model that was saved with model.save().

load_weights(filepath[, skip_mismatch, ...])

Loads all layer weights from a saved files.

make_predict_function([force])

Creates a function that executes one step of inference.

make_test_function([force])

Creates a function that executes one step of evaluation.

make_train_function([force])

Creates a function that executes one step of training.

metrics_results()

Logic to consolidate metrics results extracted from standard Keras Model.compute_metrics()

outputs_by_name()

outputs_by_target()

Method to index the model's prediction blocks by target names.

predict(x[, batch_size, verbose, steps, ...])

predict_generator(generator[, steps, ...])

Generates predictions for the input samples from a data generator.

predict_on_batch(x)

Returns predictions for a single batch of samples.

predict_step(data)

prediction_tasks_by_name()

prediction_tasks_by_target()

Method to index the model's prediction tasks by target names.

query_embeddings([dataset, index])

reset_metrics()

Resets the state of all the metrics in the model.

reset_states()

save(export_path[, include_optimizer, ...])

Saves the model to export_path as a Tensorflow Saved Model.

save_spec([dynamic_batch])

Returns the tf.TensorSpec of call args as a tuple (args, kwargs).

save_weights(filepath[, overwrite, ...])

Saves all layer weights.

set_weights(weights)

Sets the weights of the layer, from NumPy arrays.

summary([line_length, positions, print_fn, ...])

Prints a string summary of the network.

test_on_batch(x[, y, sample_weight, ...])

Test the model on a single batch of samples.

test_step(data)

Custom test step using the compute_loss method.

to_json(**kwargs)

Returns a JSON string containing the network configuration.

to_top_k_encoder([candidates, candidate_id, ...])

to_yaml(**kwargs)

Returns a yaml string containing the network configuration.

train_compute_metrics(outputs, compiled_metrics)

Returns metrics for the outputs of this step.

train_on_batch(x[, y, sample_weight, ...])

Runs a single gradient update on a single batch of data.

train_step(data)

Custom train step using the compute_loss method.

unfreeze_all_frozen_blocks()

Unfreeze all blocks (including blocks and sub-blocks) of this model recursively

unfreeze_blocks(blocks)

Unfreeze all sub-blocks of given blocks recursively

with_name_scope(method)

Decorator to automatically enter the module name scope.

Attributes

activity_regularizer

Optional regularizer function for the output of this layer.

candidate_encoder

compute_dtype

The dtype of the layer's computations.

distribute_reduction_method

The method employed to reduce per-replica values during training.

distribute_strategy

The tf.distribute.Strategy this model was created under.

dtype

The dtype of the layer weights.

dtype_policy

The dtype policy associated with this layer.

dynamic

Whether the layer is dynamic (eager-only); set in the constructor.

encoder

first

frozen_blocks

Get frozen blocks of model, only on which you called freeze_blocks before, the result dose not include those blocks frozen in other methods, for example, if you create the embedding and set the trainable as False, it would not be tracked by this property, but you can also call unfreeze_blocks on those blocks.

has_candidate_encoder

inbound_nodes

Return Functional API nodes upstream of this layer.

input

Retrieves the input tensor(s) of a layer.

input_mask

Retrieves the input mask tensor(s) of a layer.

input_schema

Get the input schema if it's defined.

input_shape

Retrieves the input shape(s) of a layer.

input_spec

InputSpec instance(s) describing the input format for this layer.

jit_compile

Specify whether to compile the model with XLA.

last

layers

losses

List of losses added using the add_loss() API.

metrics

Return metrics added using compile() or add_metric().

metrics_names

Returns the model's display labels for all outputs.

model_outputs

name

Name of the layer (string), set in the constructor.

name_scope

Returns a tf.name_scope instance for this class.

non_trainable_variables

non_trainable_weights

outbound_nodes

Return Functional API nodes downstream of this layer.

output

Retrieves the output tensor(s) of a layer.

output_mask

Retrieves the output mask tensor(s) of a layer.

output_shape

Retrieves the output shape(s) of a layer.

prediction_tasks

query_encoder

run_eagerly

Settable attribute indicating whether the model should run eagerly.

state_updates

Deprecated, do NOT use!

stateful

submodules

Sequence of all sub-modules.

supports_masking

Whether this layer supports computing a mask using compute_mask.

trainable

trainable_variables

trainable_weights

updates

variable_dtype

Alias of Layer.dtype, the dtype of the weights.

variables

Returns the list of all layer variables/weights.

weights

Returns the list of all layer variables/weights.

query_embeddings(dataset: Optional[merlin.io.dataset.Dataset] = None, index: Optional[Union[str, merlin.schema.schema.ColumnSchema, merlin.schema.schema.Schema, merlin.schema.tags.Tags]] = None, **kwargs) merlin.io.dataset.Dataset[source]#
candidate_embeddings(dataset: Optional[merlin.io.dataset.Dataset] = None, index: Optional[Union[str, merlin.schema.schema.ColumnSchema, merlin.schema.schema.Schema, merlin.schema.tags.Tags]] = None, **kwargs) merlin.io.dataset.Dataset[source]#
property encoder#
property has_candidate_encoder#
property query_encoder: Encoder#
property candidate_encoder: Encoder#
classmethod from_config(config, custom_objects=None)[source]#
get_config()[source]#
to_top_k_encoder(candidates: Optional[merlin.io.dataset.Dataset] = None, candidate_id=Tags.ITEM_ID, strategy: Union[str, keras.engine.base_layer.Layer] = 'brute-force-topk', k: int = 10, **kwargs)[source]#