function [grad] = mixmlpgrad_gate(net, x, t, post)
%mixmlpgrad_gate  Evaluate gradient of error function for the gating network in a gated experts model trained by EM.
%
%       GRAD = MIXMLPGRAD_GATE(NET, X, T, POST) takes a gating network data
%       structure NET (which is a MLP) 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, mixmlp, mixmlpem, mixopt, mlpfwd
%
%       Copyright: Perry Moerland


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

[y,z] = mlpfwd(net, x);

% Compute gradients at gating network outputs: put the answer in deltas
deltas_gate = y-post;
 
% calculate gradients
grad = mlpbkp(net,x,z,deltas_gate);