Question

Je calcul et l'optimisation de certaines variables qui sont utilisées sur un processus externe, mais je reçois l'erreur « Non dégradé ».

Une version du code très simplifiée (non testé), mais vous pouvez obtenir l'idée:

def external_process (myvar):
    subprocess.call("process.sh", myvar)
    with open('result.json', 'r') as f:
        result = json.load(data, f)
    return np.array(result["result"])

myvar = tf.Variable(1.0, dtype = 'float32', trainable = True)

loss = tf.reduce_sum( tf.py_func(external_process, [myvar], [tf.float32])[0] )

optimizer = tf.train.AdamOptimizer(0.05)
train_step = optimizer.minimize(loss)
sess.run(train_step)

J'ai vu cette discussion, mais je ne comprends pas pleinement: https://github.com / tensorflow / tensorflow / questions / 1095

Merci!

Était-ce utile?

La solution 2

La bonne réponse est: « Un processus externe n'est pas différentiables (à moins que vous connaissez chaque détail, ce qui est impossible dans ce cas), donc ce problème doit faire face comme un problème d'apprentissage de renforcement »

Autres conseils

https://www.tensorflow.org/versions /r0.9/api_docs/python/framework.html (recherche gradient_override_map) est un exemple sur gradient_override_map:

@tf.RegisterGradient("CustomSquare")
def _custom_square_grad(op, grad):
  # ...

with tf.Graph().as_default() as g:
  c = tf.constant(5.0)
  s_1 = tf.square(c)  # Uses the default gradient for tf.square.
  with g.gradient_override_map({"Square": "CustomSquare"}):
    s_2 = tf.square(s_2)  # Uses _custom_square_grad to compute the
                          # gradient of s_2.

Ainsi, une solution pourrait être:

@tf.RegisterGradient("ExternalGradient")
def _custom_external_grad(unused_op, grad):
    # I don't know yet how to compute a gradient
    # From Tensorflow documentation:
    return grad, tf.neg(grad)

def external_process (myvar):
    subprocess.call("process.sh", myvar)
    with open('result.json', 'r') as f:
        result = json.load(data, f)
    return np.array(result["result"])

myvar = tf.Variable(1.0, dtype = 'float32', trainable = True)

g = tf.get_default_graph()
with g.gradient_override_map({"PyFunc": "ExternalGradient"}):
    external_data =  tf.py_func(external_process, [myvar], [tf.float32])[0]

loss =  tf.reduce_sum(external_data)

optimizer = tf.train.AdamOptimizer(0.05)
train_step = optimizer.minimize(loss)
sess.run(train_step)
Licencié sous: CC-BY-SA avec attribution
scroll top