Getting the jacobian inside GradientTape

Aperta
#718 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 21 giorni

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
35/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Ferma
Stack tecnologico
python, tensorflow

Direzione di ricerca

Riproduci il problema con l’esempio Python fornito, iniziando da tfq.layers.PQC e dalla chiamata a GradientTape.jacobian; esamina l’operazione TfQAdjointGradient segnalata. Il lavoro è completato quando è possibile calcolare la matrice jacobiana all’interno del tape, mentre i gradienti delle operazioni successive rimangono disponibili per l’aggiornamento finale.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

kind/question

If I want to compute the jacobian inside of gradient tape (so I can use it downstream in optimization) classically I can do:

x = tf.random.normal([7, 1])
model = tf.keras.Sequential([
    tf.keras.layers.Input(shape=(1,)),
    tf.keras.layers.Dense(5),
])

with tf.GradientTape(persistent=True) as tape:
    y = model(x)
    j = tape.jacobian(y, model.trainable_variables[0])

But if I try to do that with a PQC in the mode, I get the following error: LookupError: No gradient defined for operation'TfqAdjointGradient' (op type: TfqAdjointGradient). In general every operation must have an associated `@tf.RegisterGradient` for correct autodiff, which this op is lacking. If you want to pretend this operation is a constant in your program, you may insert `tf.stop_gradient`. This can be useful to silence the error in cases where you know gradients are not needed, e.g. the forward pass of tf.custom_gradient. Please see more details in https://www.tensorflow.org/api_docs/python/tf/custom_gradient.

Example code:


x = tfq.convert_to_tensor([cirq.Circuit()] * 7)
qubits = cirq.GridQubit.rect(1, 2)
readouts = [cirq.Z(i) for i in qubits]
s = sympy.symbols("a b")
c = cirq.Circuit()
c += cirq.ry(s[0]).on(qubits[0])
c += cirq.ry(s[1]).on(qubits[1])

model = tf.keras.Sequential([
    tf.keras.layers.Input(shape=(), dtype=tf.string),
    tfq.layers.PQC(c, readouts),
])

with tf.GradientTape(persistent=True) as tape:
    y = model(x)
    j = tape.jacobian(y, model.trainable_variables[0])

If I put in a tf.stop_gradient(y) then I just get the jacobian to be none. Also I don't want to use a stop gradient because there are down stream tasks I want the gradient accumulating through for a final GD update. Is this is a known limitation or am I doing something wrong? In either case, what would your recommended path forward be?

Lingua principale
Python
Stelle
2.2k
Fork
675
Merge medio
15g 11h
PR unite (30g)
3

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di tensorflow/quantum

Tutte le issue di tensorflow/quantum

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.