def _multinomial_loss_grad(w, X, Y, alpha, sample_weight):
 n_classes = Y.shape[1]
 n_features = X.shape[1]
 fit_intercept = (w.size == (n_classes * (n_features + 1)))
 grad = np.zeros((n_classes, (n_features + bool(fit_intercept))))
 (loss, p, w) = _multinomial_loss(w, X, Y, alpha, sample_weight)
 sample_weight = sample_weight[:, np.newaxis]
 diff = (sample_weight * (p - Y))
 grad[:, :n_features] = safe_sparse_dot(diff.T, X)
 grad[:, :n_features] += (alpha * w)
 if fit_intercept:
  grad[:, (-1)] = diff.sum(axis=0)
 return (loss, grad.ravel(), p)

def _multinomial_loss_grad ( w , X , Y , alpha , sample_weight ) : n_classes = Y . shape [ 1 ] n_features = X . shape [ 1 ] fit_intercept = ( w . size == ( n_classes * ( n_features + 1 ) ) ) grad = np . zeros ( ( n_classes , ( n_features + bool ( fit_intercept ) ) ) ) ( loss , p , w ) = _multinomial_loss ( w , X , Y , alpha , sample_weight ) sample_weight = sample_weight [ : , np . newaxis ] diff = ( sample_weight * ( p - Y ) ) grad [ : , : n_features ] = safe_sparse_dot ( diff . T , X ) grad [ : , : n_features ] += ( alpha * w ) if fit_intercept : grad [ : , ( - 1 ) ] = diff . sum ( axis = 0 ) return ( loss , grad . ravel ( ) , p )
computes the multinomial loss .