public WeightMatrix Dropout(WeightMatrix V, bool[] droppedMask) { var res = new WeightMatrix(V.Rows, V.Columns, 0); var N = V.Weight.Length; var V2 = V.Clone(); for (var i = 0; i < N; i++) { if (droppedMask[i]) { V2.Weight[i] = 0; } // drop! } res = V2; if (this.needs_backprop) { Action backward = () => { var chain_grad = res; V.Gradient = new double[N]; // zero out gradient wrt data for (var i = 0; i < N; i++) { if (!(droppedMask[i])) { V.Gradient[i] += chain_grad.Gradient[i]; // copy over the gradient } } }; this.backprop.Add(backward); } return(res); }
public WeightMatrix Dropout(WeightMatrix V, double drop_prob) { var res = new WeightMatrix(V.Rows, V.Columns, 0); var N = V.Weight.Length; bool[] dropped = new bool[V.Rows * V.Columns]; var V2 = V.Clone(); for (var i = 0; i < N; i++) { if (ra.NextDouble() < drop_prob) { V2.Weight[i] = 0; dropped[i] = true; } // drop! else { dropped[i] = false; } } res = V2; if (this.needs_backprop) { Action backward = () => { var chain_grad = res; V.Gradient = new double[N]; // zero out gradient wrt data for (var i = 0; i < N; i++) { if (!(dropped[i])) { V.Gradient[i] += chain_grad.Gradient[i]; // copy over the gradient } } }; this.backprop.Add(backward); } return(res); }