The gradient for mean cosine error is makes sense with nonnegative weights.
to minimize the loss we use the limitedmemory Broyden-Fletcher-Goldfarb-Shanno algorithm for box constraints (L-BFGS-B) (Byrd et al., 1995), a quasi-Newton method that allows bounded optimization.