# Reusing SD variables

**URL:** https://community.konduit.ai/t/reusing-sd-variables/2709
**Category:** SameDiff
**Created:** [July 29, 2023, 6:44pm UTC](https://community.konduit.ai/t/reusing-sd-variables/2709 "2023-07-29T18:44:55Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![adonnini](https://avatars.discourse-cdn.com/v4/letter/a/5f8ce5/32.png) [@adonnini](https://community.konduit.ai/u/adonnini)
#### Post date: [July 29, 2023, 6:44pm UTC](https://community.konduit.ai/t/reusing-sd-variables/2709/1 "2023-07-29T18:44:55Z")

</div>

Hi,  
I suspect the latest problem I have run into is another case of my not fully understanding SameDiff and misusing it.

I have a created a class to create the multi-head attention layer for encoder and decoder. At one point in my code, I instantiate this class twice as required by the implementation of the decoder layer.

When I do this, not surprisingly, execution of the code fails with  
`Another variable with the name <name of variable> already exists`  
error

Is the only solution for this problem to create two multi-head attention classes (within the same SameDiff)? How am I misusing samediff?

What do you suggest I do?

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: [July 30, 2023, 12:19pm UTC](https://community.konduit.ai/t/reusing-sd-variables/2709/2 "2023-07-30T12:19:17Z")

</div>

@adonnini you can’t use the same variable twice. You’d need to create a separate variable name. Consider adding a suffix if you want to set the output name. Samediff also has name scopes. Can you tell me a bit more about your use case? If you have a variable you’re trying to use twice, just use sd.getVariable(“yourName”) and that will give you the managed instance from the model.

---

<div class="post-metadata">

### Author: ![adonnini](https://avatars.discourse-cdn.com/v4/letter/a/5f8ce5/32.png) [@adonnini](https://community.konduit.ai/u/adonnini)
#### Post date: [July 30, 2023, 9:02pm UTC](https://community.konduit.ai/t/reusing-sd-variables/2709/3 "2023-07-30T21:02:26Z")

</div>

@agibsonccc Thanks. I need to set up self-attention twice once in encoder block, then twice in the decoder block. I am (slowly) trying to implement the “Attention is all you Need” for my application

---

<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: [July 30, 2023, 10:32pm UTC](https://community.konduit.ai/t/reusing-sd-variables/2709/4 "2023-07-30T22:32:35Z")

</div>

@adonnini you might want to check out: [GitHub - partarstu/transformers-in-java: Experimental project for AI and NLP based on Transformer Architecture](https://github.com/partarstu/transformers-in-java) by @partarstu as well

---

<div class="post-metadata">

### Author: ![adonnini](https://avatars.discourse-cdn.com/v4/letter/a/5f8ce5/32.png) [@adonnini](https://community.konduit.ai/u/adonnini)
#### Post date: [July 31, 2023, 10:31am UTC](https://community.konduit.ai/t/reusing-sd-variables/2709/5 "2023-07-31T10:31:13Z")

</div>

@agibsonccc I know of @[partarstu](https://community.konduit.ai/u/partarstu)’s project. I’ve installed it on my system and looked at it in some detail.

At this point, using his code as starting point seems to be more work than starting from scratch.

You make a good point. Before contacting you about this, I took a look at his implementation. If I understand it correctly, he has a full implementation of only the decoder block without encoder input. If I am right, this means that he does not have to deal with the question of two multi head attentions in a single block

But, I could be wrong.

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: [July 31, 2023, 10:59am UTC](https://community.konduit.ai/t/reusing-sd-variables/2709/6 "2023-07-31T10:59:36Z")

</div>

@adonnini feel free to use sd.getVariable(“yourName”) if you want access to the variable again and you don’t have a reference to it then.
