function [grad] = mixgrad_gate(net, x, t, post)
%mixgrad_gate  Evaluate gradient of error function for the gating network in a mixture of experts model trained by EM.
%
%	GRAD = MIXGRAD_GATE(NET, X, T, POST) takes a gating network data
%	structure NET (which is a GLM) together with a matrix X of input 
%       vectors and a matrix T of target vectors, the posterior
%       probabilities POST, and evaluates the gradient GRAD of the error function
%       with respect to the network weights. The error function is the
%       gating network contribtion to the EM error error fucntion.
%
%       See also
%       mixgrad_em, mix, mixem, mixopt, glmfwd
%
%       Copyright: Perry Moerland

% Check arguments for consistency
errstring = consist(net, 'glm', x);
if ~isempty(errstring)
  error(errstring);
end

y = glmfwd(net, x);

% Compute gradients at gating network outputs: put the answer in deltas
deltas_gate = y-post;
 
% calculate gradients
gw1 = x'*deltas_gate;
gb1 =  sum(deltas_gate, 1);

grad = [gw1(:)', gb1];