Turn cost function into a matrix multiplication

938 Views Asked by At

I have a cost function

$$ E = \sum_{i,j} s_{i,j}(y_{i,j} - f_{i, j})^2 $$

where $$ f_{i,j} = \mathbf{u}_i^\top \mathbf{v}_j $$

I want to turn the objective function into a form including only matrices and vectors however I am struggling to do it.

I have turned the cost function into another form:

$$ E = \sum_{i=1}^n \sum_{j=1}^m s_{i,j}(y_{i,j} - f_{i,j})^2 $$

Although I have tried to create vectors (for the j counter) and then matrices (for the i counter) using the vectors, I don't think that this was the right approach.

1

There are 1 best solutions below

2
On

Let $(U,V)$ be the matrices whose columns consist of the $(u_i, v_j)$ vectors.

Then you can use the Frobenius (:) Product and the Hadamard ($\circ$) Product to write the cost function in matrix form $$\eqalign{ E &= S:(Y-U^TV)\circ(Y-U^TV) \cr\cr }$$ Since the matrix of all ones is the identity for the Hadamard product ($1\circ S=S$), and the Hadamard and Frobenius products are mutually commutative ($A\circ B:C=A:B\circ C$), the cost function can also be written as
$$\eqalign{ E &= 1:S\circ(Y-U^TV)\circ(Y-U^TV) \cr\cr }$$ I mention this because most computer languages have operations which map directly to this form.
For example, in Julia you can write

   E = sum(S .* (Y-U'*V) .* (Y-U'*V))

Matlab is very similar.