use of org.deeplearning4j.nn.gradient.Gradient in project deeplearning4j by deeplearning4j.
the class RnnOutputLayer method backpropGradient.
@Override
public Pair<Gradient, INDArray> backpropGradient(INDArray epsilon) {
if (input.rank() != 3)
throw new UnsupportedOperationException("Input is not rank 3");
INDArray inputTemp = input;
this.input = TimeSeriesUtils.reshape3dTo2d(input);
Pair<Gradient, INDArray> gradAndEpsilonNext = super.backpropGradient(epsilon);
this.input = inputTemp;
INDArray epsilon2d = gradAndEpsilonNext.getSecond();
INDArray epsilon3d = TimeSeriesUtils.reshape2dTo3d(epsilon2d, input.size(0));
return new Pair<>(gradAndEpsilonNext.getFirst(), epsilon3d);
}
use of org.deeplearning4j.nn.gradient.Gradient in project deeplearning4j by deeplearning4j.
the class LayerVertex method doBackward.
@Override
public Pair<Gradient, INDArray[]> doBackward(boolean tbptt) {
if (!canDoBackward()) {
throw new IllegalStateException("Cannot do backward pass: all epsilons not set. Layer " + vertexName + " (idx " + vertexIndex + ") numInputs " + getNumInputArrays() + "; numOutputs " + getNumOutputConnections());
}
Pair<Gradient, INDArray> pair;
if (tbptt && layer instanceof RecurrentLayer) {
//Truncated BPTT for recurrent layers
pair = ((RecurrentLayer) layer).tbpttBackpropGradient(epsilon, graph.getConfiguration().getTbpttBackLength());
} else {
//Normal backprop
//epsTotal may be null for OutputLayers
pair = layer.backpropGradient(epsilon);
}
if (layerPreProcessor != null) {
INDArray eps = pair.getSecond();
eps = layerPreProcessor.backprop(eps, graph.batchSize());
pair.setSecond(eps);
}
//Layers always have single activations input -> always have single epsilon output during backprop
return new Pair<>(pair.getFirst(), new INDArray[] { pair.getSecond() });
}
use of org.deeplearning4j.nn.gradient.Gradient in project deeplearning4j by deeplearning4j.
the class ActivationLayer method backpropGradient.
@Override
public Pair<Gradient, INDArray> backpropGradient(INDArray epsilon) {
//TODO handle activation function params
INDArray delta = conf().getLayer().getActivationFn().backprop(input.dup(), epsilon).getFirst();
if (maskArray != null) {
delta.muliColumnVector(maskArray);
}
Gradient ret = new DefaultGradient();
return new Pair<>(ret, delta);
}
use of org.deeplearning4j.nn.gradient.Gradient in project deeplearning4j by deeplearning4j.
the class BaseLayer method error.
@Override
public Gradient error(INDArray errorSignal) {
INDArray W = getParam(DefaultParamInitializer.WEIGHT_KEY);
Gradient nextLayerGradient = new DefaultGradient();
INDArray wErrorSignal = errorSignal.mmul(W.transpose());
nextLayerGradient.gradientForVariable().put(DefaultParamInitializer.WEIGHT_KEY, wErrorSignal);
return nextLayerGradient;
}
use of org.deeplearning4j.nn.gradient.Gradient in project deeplearning4j by deeplearning4j.
the class BarnesHutTsne method gradient.
@Override
public Gradient gradient() {
if (yIncs == null)
yIncs = zeros(Y.shape());
if (gains == null)
gains = ones(Y.shape());
AtomicDouble sumQ = new AtomicDouble(0);
/* Calculate gradient based on barnes hut approximation with positive and negative forces */
INDArray posF = Nd4j.create(Y.shape());
INDArray negF = Nd4j.create(Y.shape());
if (tree == null)
tree = new SpTree(Y);
tree.computeEdgeForces(rows, cols, vals, N, posF);
for (int n = 0; n < N; n++) tree.computeNonEdgeForces(n, theta, negF.slice(n), sumQ);
INDArray dC = posF.subi(negF.divi(sumQ));
Gradient ret = new DefaultGradient();
ret.gradientForVariable().put(Y_GRAD, dC);
return ret;
}
Aggregations