# How to access IdDict with keys?

**URL:** <https://discourse.julialang.org/t/how-to-access-iddict-with-keys/67006>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [August 25, 2021, 9:37pm UTC](https://discourse.julialang.org/t/how-to-access-iddict-with-keys/67006 "2021-08-25T21:37:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![DiracFermion1](https://avatars.discourse-cdn.com/v4/letter/d/2acd7d/32.png) [@DiracFermion1](https://discourse.julialang.org/u/DiracFermion1)\
**Post date:** [August 25, 2021, 9:37pm UTC](https://discourse.julialang.org/t/how-to-access-iddict-with-keys/67006/1 "2021-08-25T21:37:18Z")

</div>

Hi, I am trying to access the content of following object named `grads`:

```julia
IdDict{Any, Any} with 5 entries:
  :(Main.tmp_test_in_1) => Float32[0.0401479 0.0100483 … -0.0744916 -0.0; 0…
  :(Main.tmp_param) => RefValue{Any}((d_in = nothing, d_hidden = nothin…
  :(Main.n_ode) => (model = nothing, p = nothing, re = nothing, tsp…
  Float32[-0.144463, 0.184… => Float32[-0.00789103, 0.00715096, 0.00734046, 0.0…
  Param(25, 16, 8, 10, 3, … => RefValue{Any}((d_in = nothing, d_hidden = nothin…

```

and when I access values with a key like this:

`grads[:(Main.tmp_test_in_1)]`

I got following error:

```julia
KeyError: key :(Main.tmp_test_in_1) not found

```

I also tested `grads[:tmp_test_in_1]`, but I got the same error. Does anyone know what the keys should be and how to access value in this case?

Thank you!

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [August 25, 2021, 10:00pm UTC](https://discourse.julialang.org/t/how-to-access-iddict-with-keys/67006/2 "2021-08-25T22:00:46Z")

</div>

Why are you using an IdDict? The keys for that are based on object identity (`===`) but you are creating new objects for the getindex call.

---

<div class="post-metadata">

**Author:** ![ToucheSir](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/touchesir/32/14411_2.png) [@ToucheSir](https://discourse.julialang.org/u/ToucheSir)\
**Post date:** [August 25, 2021, 10:38pm UTC](https://discourse.julialang.org/t/how-to-access-iddict-with-keys/67006/3 "2021-08-25T22:38:57Z")

</div>

The `IdDict` probably came from Zygote.

All of the keys you see as escaped symbols (e.g. `:(Main.tmp_test_in_1)`) are local or global variables that were in scope and participated in gradient calculation. So to index `grads` with them, use `grads[tmp_test_in_1]` directly. Ideally, however, you should be passing in anything you want to take the gradient of like this:

```julia
f(a, b) = ...
grad_arg1, grad_arg2 = gradient(f, arg1, arg2)

```

Or like this:

```julia
f(a, b) = ...
gs = gradient(() -> f(arg1, arg2), params(arg1, arg2))
grad_arg1, grad_arg2 = gs[arg1], gs[arg2]

```

This is covered in detail in [Basics · Flux](https://fluxml.ai/Flux.jl/stable/models/basics/). I would _highly recommend_ anyone new to Flux or Zygote to read through this first.
