How to access IdDict with keys?

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

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:


I got following error:

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!

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.

1 Like

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:

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

Or like this:

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. I would highly recommend anyone new to Flux or Zygote to read through this first.