# LostTech.TensorFlow : API Documentation

Type WALSMatrixFactorization

Namespace tensorflow.contrib.factorization

Parent Estimator

Interfaces IWALSMatrixFactorization

An Estimator for Weighted Matrix Factorization, using the WALS method.

WALS (Weighted Alternating Least Squares) is an algorithm for weighted matrix factorization. It computes a low-rank approximation of a given sparse (n x m) matrix `A`, by a product of two matrices, `U * V^T`, where `U` is a (n x k) matrix and `V` is a (m x k) matrix. Here k is the rank of the approximation, also called the embedding dimension. We refer to `U` as the row factors, and `V` as the column factors. See tensorflow/contrib/factorization/g3doc/wals.md for the precise problem formulation.

The training proceeds in sweeps: during a row_sweep, we fix `V` and solve for `U`. During a column sweep, we fix `U` and solve for `V`. Each one of these problems is an unconstrained quadratic minimization problem and can be solved exactly (it can also be solved in mini-batches, since the solution decouples across rows of each matrix). The alternating between sweeps is achieved by using a hook during training, which is responsible for keeping track of the sweeps and running preparation ops at the beginning of each sweep. It also updates the global_step variable, which keeps track of the number of batches processed since the beginning of training. The current implementation assumes that the training is run on a single machine, and will fail if `config.num_worker_replicas` is not equal to one. Training is done by calling `self.fit(input_fn=input_fn)`, where `input_fn` provides two tensors: one for rows of the input matrix, and one for rows of the transposed input matrix (i.e. columns of the original matrix). Note that during a row sweep, only row batches are processed (ignoring column batches) and vice-versa. Also note that every row (respectively every column) of the input matrix must be processed at least once for the sweep to be considered complete. In particular, training will not make progress if some rows are not generated by the `input_fn`.

For prediction, given a new set of input rows `A'`, we compute a corresponding set of row factors `U'`, such that `U' * V^T` is a good approximation of `A'`. We call this operation a row projection. A similar operation is defined for columns. Projection is done by calling `self.get_projections(input_fn=input_fn)`, where `input_fn` satisfies the constraints given below.

The input functions must satisfy the following constraints: Calling `input_fn` must return a tuple `(features, labels)` where `labels` is None, and `features` is a dict containing the following keys:

TRAIN: * `WALSMatrixFactorization.INPUT_ROWS`: float32 SparseTensor (matrix). Rows of the input matrix to process (or to project). * `WALSMatrixFactorization.INPUT_COLS`: float32 SparseTensor (matrix). Columns of the input matrix to process (or to project), transposed.

INFER: * `WALSMatrixFactorization.INPUT_ROWS`: float32 SparseTensor (matrix). Rows to project. * `WALSMatrixFactorization.INPUT_COLS`: float32 SparseTensor (matrix). Columns to project. * `WALSMatrixFactorization.PROJECT_ROW`: Boolean Tensor. Whether to project the rows or columns. * `WALSMatrixFactorization.PROJECTION_WEIGHTS` (Optional): float32 Tensor (vector). The weights to use in the projection.

EVAL: * `WALSMatrixFactorization.INPUT_ROWS`: float32 SparseTensor (matrix). Rows to project. * `WALSMatrixFactorization.INPUT_COLS`: float32 SparseTensor (matrix). Columns to project. * `WALSMatrixFactorization.PROJECT_ROW`: Boolean Tensor. Whether to project the rows or columns.

### Public instance methods

#### IList<object>get_col_factors()

Returns the column factors of the model, loading them from checkpoint.

Should only be run after training.
##### Returns
`IList<object>`
A list of the column factors of the model.

#### objectget_col_factors_dyn()

Returns the column factors of the model, loading them from checkpoint.

Should only be run after training.
##### Returns
`object`
A list of the column factors of the model.

#### IEnumerator<object>get_projections(PythonFunctionContainer input_fn)

Computes the projections of the rows or columns given in input_fn.

Runs predict() with the given input_fn, and returns the results. Should only be run after training.
##### Parameters
`PythonFunctionContainer` input_fn
Input function which specifies the rows or columns to project.
##### Returns
`IEnumerator<object>`
A generator of the projected factors.

#### objectget_projections_dyn(object input_fn)

Computes the projections of the rows or columns given in input_fn.

Runs predict() with the given input_fn, and returns the results. Should only be run after training.
##### Parameters
`object` input_fn
Input function which specifies the rows or columns to project.
##### Returns
`object`
A generator of the projected factors.

#### IList<object>get_row_factors()

Returns the row factors of the model, loading them from checkpoint.

Should only be run after training.
##### Returns
`IList<object>`
A list of the row factors of the model.

#### objectget_row_factors_dyn()

Returns the row factors of the model, loading them from checkpoint.

Should only be run after training.
##### Returns
`object`
A list of the row factors of the model.

### Public static methods

#### WALSMatrixFactorizationNewDyn(object num_rows, object num_cols, object embedding_dimension, ImplicitContainer<T> unobserved_weight, object regularization_coeff, ImplicitContainer<T> row_init, ImplicitContainer<T> col_init, ImplicitContainer<T> num_row_shards, ImplicitContainer<T> num_col_shards, ImplicitContainer<T> row_weights, ImplicitContainer<T> col_weights, ImplicitContainer<T> use_factors_weights_cache_for_training, ImplicitContainer<T> use_gramian_cache_for_training, object max_sweeps, object model_dir, object config)

Creates a model for matrix factorization using the WALS method.
##### Parameters
`object` num_rows
Total number of rows for input matrix.
`object` num_cols
Total number of cols for input matrix.
`object` embedding_dimension
Dimension to use for the factors.
`ImplicitContainer<T>` unobserved_weight
Weight of the unobserved entries of matrix.
`object` regularization_coeff
Weight of the L2 regularization term. Defaults to None, in which case the problem is not regularized.
`ImplicitContainer<T>` row_init
Initializer for row factor. Must be either: - A tensor: The row factor matrix is initialized to this tensor, - A numpy constant, - "random": The rows are initialized using a normal distribution.
`ImplicitContainer<T>` col_init
Initializer for column factor. See row_init.
`ImplicitContainer<T>` num_row_shards
Number of shards to use for the row factors.
`ImplicitContainer<T>` num_col_shards
Number of shards to use for the column factors.
`ImplicitContainer<T>` row_weights
Must be in one of the following three formats: - None: In this case, the weight of every entry is the unobserved_weight and the problem simplifies to ALS. Note that, in this case, col_weights must also be set to "None". - List of lists of non-negative scalars, of the form \\([[w_0, w_1,...], [w_k,... ], [...]]\\), where the number of inner lists equal to the number of row factor shards and the elements in each inner list are the weights for the rows of that shard. In this case, \\(w_ij = unonbserved_weight + row_weights[i] * col_weights[j]\\). - A non-negative scalar: This value is used for all row weights. Note that it is allowed to have row_weights as a list and col_weights as a scalar, or vice-versa.
`ImplicitContainer<T>` col_weights
See row_weights.
`ImplicitContainer<T>` use_factors_weights_cache_for_training
Boolean, whether the factors and weights will be cached on the workers before the updates start, during training. Defaults to True. Note that caching is disabled during prediction.
`ImplicitContainer<T>` use_gramian_cache_for_training
Boolean, whether the Gramians will be cached on the workers before the updates start, during training. Defaults to True. Note that caching is disabled during prediction.
`object` max_sweeps
integer, optional. Specifies the number of sweeps for which to train the model, where a sweep is defined as a full update of all the row factors (resp. column factors). If `steps` or `max_steps` is also specified in model.fit(), training stops when either of the steps condition or sweeps condition is met.
`object` model_dir
The directory to save the model results and log files.
`object` config
A Configuration object. See Estimator.

### Public fields

#### string PROJECT_ROW

 `return string`

#### string PROJECTION_WEIGHTS

 `return string`

#### string PROJECTION_RESULT

 `return string`

#### string COMPLETED_SWEEPS

 `return string`

#### string LOSS

 `return string`

#### string INPUT_ROWS

 `return string`

#### string RWSE

 `return string`

#### string INPUT_COLS

 `return string`