# Question on calculating gradient by external error

**URL:** https://community.konduit.ai/t/question-on-calculating-gradient-by-external-error/157
**Category:** DL4J
**Created:** [February 23, 2020, 12:00pm UTC](https://community.konduit.ai/t/question-on-calculating-gradient-by-external-error/157 "2020-02-23T12:00:19Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![RuralHunter](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/ruralhunter/32/184_2.png) [@RuralHunter](https://community.konduit.ai/u/RuralHunter)
#### Post date: [February 23, 2020, 12:00pm UTC](https://community.konduit.ai/t/question-on-calculating-gradient-by-external-error/157/1 "2020-02-23T12:00:20Z")

</div>

Hi, I have a question regarding using external error to calculate gradient as this example: [MultiLayerNetworkExternalErrors.java](https://github.com/eclipse/deeplearning4j-examples/blob/master/dl4j-examples/src/main/java/org/deeplearning4j/examples/misc/externalerrors/MultiLayerNetworkExternalErrors.java)

If my nn is like this:

```
MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder()
        .weightInit(WeightInit.XAVIER)
        .activation(Activation.RELU)
        .optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT)
        .updater(new Nesterovs(0.001,0.9))
        .list()
        .layer(0, new DenseLayer.Builder().nOut(20).build())
        .layer(1, new DenseLayer.Builder().activation(Activation.SOFTMAX).nOut(4).build())
        .setInputType(InputType.feedForward(1))
        .build();

```

To calculate the gradient, should I use the vanila error or the error after softmax activation in the net.backpropGradient(error,null) function?

---

<div class="post-metadata">

### Author: ![treo](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/treo/32/47_2.png) [@treo](https://community.konduit.ai/u/treo)
#### Post date: [February 23, 2020, 12:33pm UTC](https://community.konduit.ai/t/question-on-calculating-gradient-by-external-error/157/2 "2020-02-23T12:33:43Z")

</div>

The error is the result of your loss function. Your (external) loss function gets the result of the network after the activation, so you should be calculating it after the activation.
