Java Code Examples for org.deeplearning4j.nn.multilayer.MultiLayerNetwork#backpropGradient()

The following examples show how to use org.deeplearning4j.nn.multilayer.MultiLayerNetwork#backpropGradient() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PLNetDyadRanker.java    From AILibs with GNU Affero General Public License v3.0 6 votes vote down vote up
private INDArray computeScaledGradient(final INDArray dyadMatrix) {
	int dyadRankingLength = dyadMatrix.rows();
	List<INDArray> activations = this.plNet.feedForward(dyadMatrix);
	INDArray output = activations.get(activations.size() - 1);
	output = output.transpose();
	INDArray deltaW = Nd4j.zeros(this.plNet.params().length());
	Gradient deltaWk = null;
	MultiLayerNetwork plNetClone = this.plNet.clone();
	for (int k = 0; k < dyadRankingLength; k++) {
		// compute derivative of loss w.r.t. k
		plNetClone.setInput(dyadMatrix.getRow(k));
		plNetClone.feedForward(true, false);
		INDArray lossGradient = PLNetLoss.computeLossGradient(output, k);
		// compute backprop gradient for weight updates w.r.t. k
		Pair<Gradient, INDArray> p = plNetClone.backpropGradient(lossGradient, null);
		deltaWk = p.getFirst();
		this.plNet.getUpdater().update(this.plNet, deltaWk, this.iteration, this.epoch, 1, LayerWorkspaceMgr.noWorkspaces());
		deltaW.addi(deltaWk.gradient());
	}

	return deltaW;
}
 
Example 2
Source File: PLNetDyadRanker.java    From AILibs with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Computes the gradient of the plNets' error function for a given instance. The
 * returned gradient is already scaled by the updater. The update procedure is
 * based on algorithm 2 in [1].
 *
 * @param instance
 *            The instance to compute the scaled gradient for.
 * @return The gradient for the given instance, multiplied by the updater's
 *         learning rate.
 */
private INDArray computeScaledGradient(final IDyadRankingInstance instance) {
	// init weight update vector
	INDArray dyadMatrix;
	List<INDArray> dyadList = new ArrayList<>(instance.getNumAttributes());
	for (IDyad dyad : instance) {
		INDArray dyadVector = this.dyadToVector(dyad);
		dyadList.add(dyadVector);
	}
	dyadMatrix = this.dyadRankingToMatrix(instance);
	List<INDArray> activations = this.plNet.feedForward(dyadMatrix);
	INDArray output = activations.get(activations.size() - 1);
	output = output.transpose();
	INDArray deltaW = Nd4j.zeros(this.plNet.params().length());
	Gradient deltaWk = null;
	MultiLayerNetwork plNetClone = this.plNet.clone();
	for (int k = 0; k < instance.getNumAttributes(); k++) {
		// compute derivative of loss w.r.t. k
		plNetClone.setInput(dyadList.get(k));
		plNetClone.feedForward(true, false);
		INDArray lossGradient = PLNetLoss.computeLossGradient(output, k);
		// compute backprop gradient for weight updates w.r.t. k
		Pair<Gradient, INDArray> p = plNetClone.backpropGradient(lossGradient, null);
		deltaWk = p.getFirst();
		this.plNet.getUpdater().update(this.plNet, deltaWk, this.iteration, this.epoch, 1, LayerWorkspaceMgr.noWorkspaces());
		deltaW.addi(deltaWk.gradient());
	}

	return deltaW;
}
 
Example 3
Source File: PLNetInputOptimizer.java    From AILibs with GNU Affero General Public License v3.0 5 votes vote down vote up
private static INDArray computeInputDerivative(PLNetDyadRanker plNet, INDArray input, InputOptimizerLoss loss) {
	MultiLayerNetwork net = plNet.getPlNet();

	INDArray output = net.output(input);
	INDArray lossGradient = Nd4j.create(new double[] { loss.lossGradient(output) });
	net.setInput(input);
	net.feedForward(false, false);
	Pair<Gradient, INDArray> p = net.backpropGradient(lossGradient, null);

	return p.getSecond();
}
 
Example 4
Source File: ValidateMKLDNN.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Test
public void compareBatchNormBackward() throws Exception {
    assumeTrue(Nd4j.getBackend().getClass().getName().toLowerCase().contains("native"));

    Nd4j.getRandom().setSeed(12345);
    INDArray in = Nd4j.rand(DataType.FLOAT, 1, 3, 15, 15);
    INDArray mean = in.mean(0, 2, 3).reshape(1,3);
    INDArray var = in.var(0, 2, 3).reshape(1,3);
    INDArray eps = Nd4j.rand(DataType.FLOAT, in.shape());
    INDArray gamma = Nd4j.rand(DataType.FLOAT, 1,3);
    INDArray beta = Nd4j.rand(DataType.FLOAT, 1,3);
    double e = 1e-3;

    INDArray dLdIn = in.ulike();
    INDArray dLdm = mean.ulike();
    INDArray dLdv = var.ulike();
    INDArray dLdg = gamma.ulike();
    INDArray dLdb = beta.ulike();


    MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder()
            .inferenceWorkspaceMode(WorkspaceMode.NONE)
            .trainingWorkspaceMode(WorkspaceMode.NONE)
            .list()
            .layer(new BatchNormalization.Builder().nIn(3).nOut(3).build())
            .build();
    MultiLayerNetwork net = new MultiLayerNetwork(conf);
    net.init();
    org.deeplearning4j.nn.layers.normalization.BatchNormalization bn = (org.deeplearning4j.nn.layers.normalization.BatchNormalization) net.getLayer(0);
    assertNotNull(bn.getHelper());
    System.out.println(bn.getHelper());

    net.output(in, true);
    bn.setInput(in, LayerWorkspaceMgr.noWorkspaces());
    Pair<Gradient,INDArray> pcudnn = net.backpropGradient(eps, LayerWorkspaceMgr.noWorkspaces());

    Field f = bn.getClass().getDeclaredField("helper");
    f.setAccessible(true);
    f.set(bn, null);
    assertNull(bn.getHelper());

    net.output(in, true);
    bn.setInput(in, LayerWorkspaceMgr.noWorkspaces());
    Pair<Gradient,INDArray> p = net.backpropGradient(eps, LayerWorkspaceMgr.noWorkspaces());

    INDArray dldin_dl4j = p.getSecond();
    INDArray dldin_helper = pcudnn.getSecond();

    assertTrue(dldin_dl4j.equalsWithEps(dldin_helper, 1e-5));
}