math icon indicating copy to clipboard operation
math copied to clipboard

tensor product

Open bob-carpenter opened this issue 8 years ago • 5 comments

Summary:

From @betanalpha C_{ij} = sum{m, n = 1}^{N} A_{imn} B_{jmn}

Description:

In Stan code:

matrix tensor_product(matrix[] A, matrix[] B) {
  matrix[size(A), size(B)] C;
  for (j in 1:J)
    for (i in 1:I)
      c[i, j] = sum(A[i] .* B[j]);
  return c;
}

with calling:

matrix[M, N] A[I];
matrix[M, N] B[J];
matrix[I, J] C = tensor_product(A, B);

We could write analytic derivatives for sum(matrix .* matrix) to cut down on memory usage. I don't know if we can piggyback on any of the Eigen tensor operations to make it more efficient. Lots of memory blocking issues here in how to do that sum and elementwise product efficiently.

Current Version:

v2.15.0

bob-carpenter avatar May 02 '17 15:05 bob-carpenter

Apparently the derivatives are really easy, dC/dA = B and dC/dB = A. Screen Shot 2021-01-01 at 5 45 38 AM

spinkney avatar Jan 01 '21 10:01 spinkney

Could we implement this by just as reshaping A into a row vector and B into a vector and matrix-multiplying them (calculating outer product)?

t4c1 avatar Jan 04 '21 07:01 t4c1

@t4c1 yes you can do that. Just to be clear, you mean that each I, J matrix is converted to a vector. So these two functions are equal (in R code)

k = 2; n=3; m = 4
A <- array(rnorm(k * m * n), c(n,m,k))
A

j = 3
B  <- array(rnorm(j * m * n), c(n,m,j))
B

tensor_product <- function(A, B) {
  I <- dim(A)[3]
  J <- dim(B)[3]
  c <- matrix(NA, I, J)
  for (j in 1:J) {
    for (i in 1:I) {
      c[i, j] <- sum(A[,,i] * B[,,j])
    }
  }
    return(c)
}

tensor_product_t4c1 <- function(A, B){
  I <- dim(A)[3]
  J <- dim(B)[3]
  c <- matrix(NA, I, J)
  for (j in 1:J) {
    for (i in 1:I) {
      c[i, j] <- matrix(c(A[,,i]), nrow = 1) %*%  matrix(c(B[,,j]), ncol = 1)
    }
  }
  return(c)
}

all.equal(tensor_product(A, B), tensor_product_t4c1(A, B) )
[1] TRUE

spinkney avatar Jan 04 '21 14:01 spinkney

was this never implemented?

mathDR avatar Nov 18 '23 17:11 mathDR

Nope, this was never implemented. It shouldn't be that hard to implement, but we don't have a good tensor data structure other than an array of matrices.

bob-carpenter avatar Nov 27 '23 19:11 bob-carpenter