# Issues about modifying the source code

**URL:** https://community.konduit.ai/t/issues-about-modifying-the-source-code/1689
**Category:** DL4J
**Created:** [November 24, 2021, 11:41am UTC](https://community.konduit.ai/t/issues-about-modifying-the-source-code/1689 "2021-11-24T11:41:10Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![fubuki](https://avatars.discourse-cdn.com/v4/letter/f/9fc29f/32.png) [@fubuki](https://community.konduit.ai/u/fubuki)
#### Post date: [November 24, 2021, 11:41am UTC](https://community.konduit.ai/t/issues-about-modifying-the-source-code/1689/1 "2021-11-24T11:41:10Z")

</div>

Hi all, due to my research purpose, I need to split the forwarding and backwarding phase in training into two separate functions, which is all done in one function (i.e. fit() ) in DL4J. Can anyone give me some advices on how to achieve my idea? Sincerely appreciate for your help!

---

<div class="post-metadata">

### Author: ![agibsonccc](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/agibsonccc/32/697_2.png) [@agibsonccc](https://community.konduit.ai/u/agibsonccc)
#### Post date: [November 24, 2021, 11:59am UTC](https://community.konduit.ai/t/issues-about-modifying-the-source-code/1689/2 "2021-11-24T11:59:27Z")

</div>

@fubuki you can either try using samediff (the lower level api with more control) or use dl4j’s external errors to do your own backpropagation.  
In that case, you can call the gradient functions on your own.

Otherwise for the dl4j api ,a test example can be found here:

> <https://github.com/eclipse/deeplearning4j/blob/fc735d30023981ebbb0fafa55ea9520ec44292e0/deeplearning4j/deeplearning4j-core/src/test/java/org/deeplearning4j/nn/graph/TestComputationGraphNetwork.java>

---

<div class="post-metadata">

### Author: ![fubuki](https://avatars.discourse-cdn.com/v4/letter/f/9fc29f/32.png) [@fubuki](https://community.konduit.ai/u/fubuki)
#### Post date: [November 24, 2021, 1:01pm UTC](https://community.konduit.ai/t/issues-about-modifying-the-source-code/1689/3 "2021-11-24T13:01:44Z")

</div>

Thank you for your advice! For the first solution, do you mean that I can only use samediff api to achieve me idea without using DL4J?

---

<div class="post-metadata">

### Author: ![agibsonccc](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/agibsonccc/32/697_2.png) [@agibsonccc](https://community.konduit.ai/u/agibsonccc)
#### Post date: [November 24, 2021, 2:13pm UTC](https://community.konduit.ai/t/issues-about-modifying-the-source-code/1689/4 "2021-11-24T14:13:47Z")

</div>

@fubuki generally samediff is meant for lower level control of graphs similar to tensorflow/pytorch. You can look at:

> **[deeplearning4j-examples/samediff-examples at master ·...](https://github.com/eclipse/deeplearning4j-examples/tree/master/samediff-examples)**
>
> master/samediff-examples

Take a look at our quickstart as well:

> **[Quickstart](https://deeplearning4j.konduit.ai/samediff/tutorials/quickstart)**
>
> Samediff Quickstart

It’s up to you which route to go. Samediff is superceding dl4j as the main api long term though. Dl4j’s computation graph and multi layernetwork do have this functionality built in though via external errors as mentioned above. Samediff is capable of external errors as well though.

You may find more here:

> <https://github.com/eclipse/deeplearning4j/blob/12b8ff3514f3c996b7b7aa8cd7af3b1905da4102/nd4j/nd4j-backends/nd4j-tests/src/test/java/org/nd4j/autodiff/samediff/SameDiffTests.java#L2341-L2340>

---

<div class="post-metadata">

### Author: ![fubuki](https://avatars.discourse-cdn.com/v4/letter/f/9fc29f/32.png) [@fubuki](https://community.konduit.ai/u/fubuki)
#### Post date: [November 25, 2021, 1:23am UTC](https://community.konduit.ai/t/issues-about-modifying-the-source-code/1689/5 "2021-11-25T01:23:35Z")

</div>

I got it. Thank you for your guidance!

---

<div class="post-metadata">

### Author: ![fubuki](https://avatars.discourse-cdn.com/v4/letter/f/9fc29f/32.png) [@fubuki](https://community.konduit.ai/u/fubuki)
#### Post date: [November 25, 2021, 2:54am UTC](https://community.konduit.ai/t/issues-about-modifying-the-source-code/1689/6 "2021-11-25T02:54:07Z")

</div>

Hi, after referring to the github link you shared, I am still confused about the backward process in Samediff. Generally, for the backward process, it uses the forward output to perform the back propagation and updates the weights of the model using the gradient. I am not sure which the function / step is that updates the weights of the model.

---

<div class="post-metadata">

### Author: ![agibsonccc](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/agibsonccc/32/697_2.png) [@agibsonccc](https://community.konduit.ai/u/agibsonccc)
#### Post date: [November 25, 2021, 1:43pm UTC](https://community.konduit.ai/t/issues-about-modifying-the-source-code/1689/7 "2021-11-25T13:43:12Z")

</div>

@fubuki you would use external errors in combination with the fit function in that case. You can also use calculate gradients. You can configure a training configuration similar to other training examples after specifying external errors in the other examples listed.

---

<div class="post-metadata">

### Author: ![fubuki](https://avatars.discourse-cdn.com/v4/letter/f/9fc29f/32.png) [@fubuki](https://community.konduit.ai/u/fubuki)
#### Post date: [November 26, 2021, 2:24am UTC](https://community.konduit.ai/t/issues-about-modifying-the-source-code/1689/8 "2021-11-26T02:24:01Z")

</div>

I would try it out. Thank you for your help!

---

<div class="post-metadata">

### Author: ![fubuki](https://avatars.discourse-cdn.com/v4/letter/f/9fc29f/32.png) [@fubuki](https://community.konduit.ai/u/fubuki)
#### Post date: [December 1, 2021, 11:17am UTC](https://community.konduit.ai/t/issues-about-modifying-the-source-code/1689/9 "2021-12-01T11:17:50Z")

</div>

Hi, if I use calculateGradients() to get the gradients, should I update each variable by writing some codes like “var.get(‘w’).subi(grad.mul(lr))” ? Or is there any function I can use to update the variables?

---

<div class="post-metadata">

### Author: ![agibsonccc](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/agibsonccc/32/697_2.png) [@agibsonccc](https://community.konduit.ai/u/agibsonccc)
#### Post date: [December 2, 2021, 10:57am UTC](https://community.konduit.ai/t/issues-about-modifying-the-source-code/1689/10 "2021-12-02T10:57:09Z")

</div>

@fubuki You usually use use update like that yes.

---

<div class="post-metadata">

### Author: ![fubuki](https://avatars.discourse-cdn.com/v4/letter/f/9fc29f/32.png) [@fubuki](https://community.konduit.ai/u/fubuki)
#### Post date: [December 3, 2021, 1:50am UTC](https://community.konduit.ai/t/issues-about-modifying-the-source-code/1689/11 "2021-12-03T01:50:51Z")

</div>

Got it. Thank you a lot!

---

<div class="post-metadata">

### Author: ![fubuki](https://avatars.discourse-cdn.com/v4/letter/f/9fc29f/32.png) [@fubuki](https://community.konduit.ai/u/fubuki)
#### Post date: [December 6, 2021, 6:06am UTC](https://community.konduit.ai/t/issues-about-modifying-the-source-code/1689/12 "2021-12-06T06:06:47Z")

</div>

Hi! When I use the calculateGradients() and ExternalErrorFunction to execute the backward, it fails and reports that “No array was provided for required placeholder variable “input””. But I have already set values in the “input” variable. How should I handle this? Thank you!

---

<div class="post-metadata">

### Author: ![agibsonccc](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/agibsonccc/32/697_2.png) [@agibsonccc](https://community.konduit.ai/u/agibsonccc)
#### Post date: [December 7, 2021, 11:06am UTC](https://community.konduit.ai/t/issues-about-modifying-the-source-code/1689/13 "2021-12-07T11:06:23Z")

</div>

@fubuki that is saying that no gradient for input has been calculated not that you didn’t set the value for input. It sounds like there is an issue here somewhere. Could you file an issue with a reproducer for me to look at? Generally placeholders shouldn’t need gradients though. I will need to look at this in more detail.

It’s hard to tell if there is a bug here or not. Thanks!

---

<div class="post-metadata">

### Author: ![fubuki](https://avatars.discourse-cdn.com/v4/letter/f/9fc29f/32.png) [@fubuki](https://community.konduit.ai/u/fubuki)
#### Post date: [December 7, 2021, 11:42am UTC](https://community.konduit.ai/t/issues-about-modifying-the-source-code/1689/14 "2021-12-07T11:42:17Z")

</div>

What about providing you with the source code in Github (Test function included)? I will make some comments on the code and tell you what my program is trying to do. Thank you!

---

<div class="post-metadata">

### Author: ![agibsonccc](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/agibsonccc/32/697_2.png) [@agibsonccc](https://community.konduit.ai/u/agibsonccc)
#### Post date: [December 7, 2021, 11:43am UTC](https://community.konduit.ai/t/issues-about-modifying-the-source-code/1689/15 "2021-12-07T11:43:40Z")

</div>

@fubuki sure happy to review.

---

<div class="post-metadata">

### Author: ![fubuki](https://avatars.discourse-cdn.com/v4/letter/f/9fc29f/32.png) [@fubuki](https://community.konduit.ai/u/fubuki)
#### Post date: [December 7, 2021, 12:00pm UTC](https://community.konduit.ai/t/issues-about-modifying-the-source-code/1689/16 "2021-12-07T12:00:59Z")

</div>

@agibsonccc Here is the source code link: [GitHub - fubukishiro/FTPipeHD\_MC](https://github.com/fubukishiro/FTPipeHD_MC/)  
The error occurs in MNISTCNNTest/subModelTrainTest() function, which is in the line 118 of MNISTCNNTest.java. I made a TODO comment on that line.  
Forgive me to reuse some code from the SameDiff source code.  
Sincerely thank you for your help!

---

<div class="post-metadata">

### Author: ![fubuki](https://avatars.discourse-cdn.com/v4/letter/f/9fc29f/32.png) [@fubuki](https://community.konduit.ai/u/fubuki)
#### Post date: [December 7, 2021, 12:10pm UTC](https://community.konduit.ai/t/issues-about-modifying-the-source-code/1689/17 "2021-12-07T12:10:38Z")

</div>

@agibsonccc What my program want to do is to split a model into three sub-models. And it first forwards these three sub-models in a consecutive order and backwards them in a reverse order. For instance, the output of sub-model1 is the input of the sub-model2. The gradient of the input of subModel2 is the external error of sub-model1.

---

<div class="post-metadata">

### Author: ![fubuki](https://avatars.discourse-cdn.com/v4/letter/f/9fc29f/32.png) [@fubuki](https://community.konduit.ai/u/fubuki)
#### Post date: [December 10, 2021, 2:57am UTC](https://community.konduit.ai/t/issues-about-modifying-the-source-code/1689/18 "2021-12-10T02:57:04Z")

</div>

@agibsonccc Hi, have you figured out any issue in my implementation? Thanks!

---

<div class="post-metadata">

### Author: ![agibsonccc](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/agibsonccc/32/697_2.png) [@agibsonccc](https://community.konduit.ai/u/agibsonccc)
#### Post date: [December 10, 2021, 3:11am UTC](https://community.konduit.ai/t/issues-about-modifying-the-source-code/1689/19 "2021-12-10T03:11:07Z")

</div>

@fubuki sorry haven’t had bandwidth to do that. Assume anytime you ask me to look at source code it’s going to involve me cloning a repository, potentially correcting some code, me testing against multiple versions (potentially M1.1 and snapshots) in case I find issues. It’s not just a “scan the repo and magically find the problem”

These things are usually at least a 1-2 hour affair that requires dedicated time. I’ll try to get back to you early next week after I cleared my backlog out. If we’re lucky earlier.

---

<div class="post-metadata">

### Author: ![fubuki](https://avatars.discourse-cdn.com/v4/letter/f/9fc29f/32.png) [@fubuki](https://community.konduit.ai/u/fubuki)
#### Post date: [December 10, 2021, 3:41am UTC](https://community.konduit.ai/t/issues-about-modifying-the-source-code/1689/20 "2021-12-10T03:41:18Z")

</div>

@agibsonccc I totally understand that you are busy now. I will wait for you finishing your backlog. I have been always appreciating your help in my issue!

[Next page](https://community.konduit.ai/t/issues-about-modifying-the-source-code/1689.md?page=2)
