-
Notifications
You must be signed in to change notification settings - Fork 1
[Ready for review] Hessian for elementwise multiplication #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,10 @@ | ||
| #include "bivariate.h" | ||
| #include "subexpr.h" | ||
| #include <assert.h> | ||
| #include <math.h> | ||
| #include <stdio.h> | ||
| #include <stdlib.h> | ||
| #include <string.h> | ||
|
|
||
| // ------------------------------------------------------------------------------ | ||
| // Implementation of elementwise multiplication when both arguments are vectors. | ||
|
|
@@ -52,15 +56,151 @@ static void eval_jacobian(expr *node) | |
| x->value); | ||
| } | ||
|
|
||
| static void wsum_hess_init(expr *node) | ||
| { | ||
| expr *x = node->left; | ||
| expr *y = node->right; | ||
|
|
||
| /* for correctness x and y must be (1) different variables, | ||
| or (2) both must be linear operators */ | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we not have (Ax) * y? as long as they have the same shape? So not necessarily both linear operators?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right now we don't support this, because the second argument must be a "global" linear operator for the product rule to be correct. But we can form the B matrix representing y internally if only the left node is a linear operator. I'll add this as an issue so we do this later. |
||
| #ifndef DEBUG | ||
| if (x->var_id != NOT_A_VARIABLE && y->var_id != NOT_A_VARIABLE && | ||
| x->var_id == y->var_id) | ||
| { | ||
| fprintf(stderr, "Error: elementwise multiplication of a variable by itself " | ||
| "not supported.\n"); | ||
| exit(1); | ||
| } | ||
| else if ((x->var_id != NOT_A_VARIABLE && y->var_id == NOT_A_VARIABLE) || | ||
| (x->var_id == NOT_A_VARIABLE && y->var_id != NOT_A_VARIABLE)) | ||
| { | ||
| fprintf(stderr, "Error: elementwise multiplication of a variable by a " | ||
| "non-variable is not supported. (Both must be inserted " | ||
| "as linear operators)\n"); | ||
| exit(1); | ||
| } | ||
| #endif | ||
|
|
||
| /* both x and y are variables*/ | ||
| if (x->var_id != NOT_A_VARIABLE) | ||
| { | ||
| node->wsum_hess = new_csr_matrix(node->n_vars, node->n_vars, 2 * node->size); | ||
|
|
||
| int i, var1_id, var2_id; | ||
|
|
||
| if (x->var_id < y->var_id) | ||
| { | ||
| var1_id = x->var_id; | ||
| var2_id = y->var_id; | ||
| } | ||
| else | ||
| { | ||
| var1_id = y->var_id; | ||
| var2_id = x->var_id; | ||
| } | ||
|
|
||
| /* var1 rows of Hessian */ | ||
| for (i = 0; i < node->size; i++) | ||
| { | ||
| node->wsum_hess->p[var1_id + i] = i; | ||
| node->wsum_hess->i[i] = var2_id + i; | ||
| } | ||
|
|
||
| int nnz = node->size; | ||
|
|
||
| /* rows between var1 and var2 */ | ||
| for (i = var1_id + node->size; i < var2_id; i++) | ||
| { | ||
| node->wsum_hess->p[i] = nnz; | ||
| } | ||
|
|
||
| /* var2 rows of Hessian */ | ||
| for (i = 0; i < node->size; i++) | ||
| { | ||
| node->wsum_hess->p[var2_id + i] = nnz + i; | ||
| node->wsum_hess->i[nnz + i] = var1_id + i; | ||
| } | ||
|
|
||
| /* remaining rows */ | ||
| nnz += node->size; | ||
| for (i = var2_id + node->size; i <= node->n_vars; i++) | ||
| { | ||
| node->wsum_hess->p[i] = nnz; | ||
| } | ||
| } | ||
| else | ||
| { | ||
| /* both are linear operators */ | ||
| CSC_Matrix *A = ((linear_op_expr *) x)->A_csc; | ||
| CSC_Matrix *B = ((linear_op_expr *) y)->A_csc; | ||
|
|
||
| /* Allocate workspace for Hessian computation */ | ||
| elementwise_mult_expr *mul_node = (elementwise_mult_expr *) node; | ||
| CSR_Matrix *C; /* C = B^T diag(w) A */ | ||
| C = BTA_alloc(A, B); | ||
| node->iwork = (int *) malloc(C->m * sizeof(int)); | ||
|
|
||
| CSR_Matrix *CT = AT_alloc(C, node->iwork); | ||
| mul_node->CSR_work1 = C; | ||
| mul_node->CSR_work2 = CT; | ||
|
|
||
| /* Hessian is H = C + C^T where both are B->n x A->n, and can't be more than | ||
| * 2 * nnz(C) */ | ||
| assert(C->m == node->n_vars && C->n == node->n_vars); | ||
| node->wsum_hess = new_csr_matrix(C->m, C->n, 2 * C->nnz); | ||
| } | ||
| } | ||
|
|
||
| static void eval_wsum_hess(expr *node, const double *w) | ||
| { | ||
| expr *x = node->left; | ||
| expr *y = node->right; | ||
|
|
||
| /* both x and y are variables*/ | ||
| if (x->var_id != NOT_A_VARIABLE) | ||
| { | ||
| memcpy(node->wsum_hess->x, w, node->size * sizeof(double)); | ||
| memcpy(node->wsum_hess->x + node->size, w, node->size * sizeof(double)); | ||
| } | ||
| else | ||
| { | ||
| /* both are linear operators */ | ||
| CSC_Matrix *A = ((linear_op_expr *) x)->A_csc; | ||
| CSC_Matrix *B = ((linear_op_expr *) y)->A_csc; | ||
| CSR_Matrix *C = ((elementwise_mult_expr *) node)->CSR_work1; | ||
| CSR_Matrix *CT = ((elementwise_mult_expr *) node)->CSR_work2; | ||
|
|
||
| /* Compute C = B^T diag(w) A */ | ||
| BTDA_fill_values(A, B, w, C); | ||
|
|
||
| /* Compute CT = C^T = A^T diag(w) B */ | ||
| AT_fill_values(C, CT, node->iwork); | ||
|
|
||
| /* Hessian = C + CT = B^T diag(w) A + A^T diag(w) B */ | ||
| sum_csr_matrices(C, CT, node->wsum_hess); | ||
| } | ||
| } | ||
|
|
||
| static void free_type_data(expr *node) | ||
| { | ||
| free_csr_matrix(((elementwise_mult_expr *) node)->CSR_work1); | ||
| free_csr_matrix(((elementwise_mult_expr *) node)->CSR_work2); | ||
| } | ||
|
|
||
| expr *new_elementwise_mult(expr *left, expr *right) | ||
| { | ||
| expr *node = new_expr(left->d1, 1, left->n_vars); | ||
| elementwise_mult_expr *mul_node = | ||
| (elementwise_mult_expr *) calloc(1, sizeof(elementwise_mult_expr)); | ||
| expr *node = &mul_node->base; | ||
|
|
||
| init_expr(node, left->d1, left->d2, left->n_vars, forward, jacobian_init, | ||
| eval_jacobian, NULL, free_type_data); | ||
| node->wsum_hess_init = wsum_hess_init; | ||
| node->eval_wsum_hess = eval_wsum_hess; | ||
| node->left = left; | ||
| node->right = right; | ||
| expr_retain(left); | ||
| expr_retain(right); | ||
| node->forward = forward; | ||
| node->jacobian_init = jacobian_init; | ||
| node->eval_jacobian = eval_jacobian; | ||
|
|
||
| return node; | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.