def _fprime(ds, zi, Xi=None, sample_weights=None, reg=None, return_func=False):
"""np.dot(D.T, X[i] - np.dot(D, zi)) + reg
Parameters
----------
ds : array, shape (n_atoms, n_times_atom)
The atoms
zi : array, shape (n_atoms * n_times_valid)
The activations
Xi : array, shape (n_times, ) or None
The data array for one trial
sample_weights : array, shape (n_times, ) or None
The sample weights for one trial
reg : float or None
The regularization constant
return_func : boolean
Returns also the objective function, used to speed up LBFGS solver
Returns
-------
(func) : float
The objective function
grad : array, shape (n_atoms * n_times_valid)
The gradient
"""
n_atoms, n_times_atom = ds.shape
zi_reshaped = zi.reshape((n_atoms, -1))
Dzi = _choose_convolve(zi_reshaped, ds)
if Xi is not None:
Dzi -= Xi
if sample_weights is not None:
if return_func:
# preserve Dzi, we don't want to apply the weights twice in func
wDzi = sample_weights * Dzi
else:
Dzi *= sample_weights
wDzi = Dzi
else:
wDzi = Dzi
if return_func:
func = 0.5 * np.dot(wDzi, Dzi.T)
if reg is not None:
func += reg * zi.sum()
# Now do the dot product with the transpose of D (D.T) which is
# the conv by the reversed filter (keeping valid mode)
grad = np.concatenate(
[signal.convolve(wDzi, d[::-1], 'valid') for d in ds])
# grad = -np.dot(D.T, X[i] - np.dot(D, zi))
if reg is not None:
grad += reg
if return_func:
return func, grad
else:
return grad
评论列表
文章目录