}
@Override
protected void gradientUpdateMiniBatch(SGDTrainConfig config, DoubleMatrix x_samples, DoubleMatrix y_samples, SGDParam curr_param) {
int nbr_sample = x_samples.rows;
DoubleMatrix curr_w = ((HiddenLayerParam)curr_param).w;
DoubleMatrix curr_hbias = ((HiddenLayerParam)curr_param).hbias;
DoubleMatrix curr_vbias = ((HiddenLayerParam)curr_param).vbias;
/**
* reconstruct
*/
DoubleMatrix tilde_x = null;
DoubleMatrix y = null;
DoubleMatrix z = null;
if (config.isDoCorruption()) {
double p = 1 - config.getCorruption_level();
tilde_x = get_corrupted_input(x_samples, p);
y = tilde_x.mmul(curr_w.transpose()).addiRowVector(curr_hbias);
}
else {
y = x_samples.mmul(curr_w.transpose()).addiRowVector(curr_hbias);
}
MathUtil.sigmod(y);
z = y.mmul(curr_w).addiRowVector(curr_vbias);
MathUtil.sigmod(z);
/**
* gradient update
*/
DoubleMatrix L_vbias = x_samples.sub(z);
DoubleMatrix L_hbias = L_vbias.mmul(curr_w.transpose()).muli(y).muli(y.neg().addi(1));
DoubleMatrix delta_w = null;
if (config.isDoCorruption()) {
delta_w = L_hbias.transpose().mmul(tilde_x).addi(y.transpose().mmul(L_vbias));
} else {
delta_w = L_hbias.transpose().mmul(x_samples).addi(y.transpose().mmul(L_vbias));
}
if (config.isUseRegularization()) {
//only L2 for autoencoder
if (0 != config.getLamada2()) {
delta_w.subi(curr_w.mul(config.getLamada2()));
}
}
delta_w.divi(nbr_sample);
DoubleMatrix delta_hbias = L_hbias.columnSums().divi(nbr_sample);
DoubleMatrix delta_vbias = L_vbias.columnSums().divi(nbr_sample);
curr_w.addi(delta_w.muli(config.getLearningRate()));
curr_hbias.addi(delta_hbias.transpose().muli(config.getLearningRate()));
curr_vbias.addi(delta_vbias.transpose().muli(config.getLearningRate()));
}