Ajustar valor único dentro de Tensor-TensorFlow


Me avergüenza preguntar esto, pero ¿cómo se ajusta un solo valor dentro de un tensor? Supongamos que desea agregar ' 1 ' a solo un valor dentro de su tensor?

Hacerlo indexando no funciona:

TypeError: 'Tensor' object does not support item assignment

Un enfoque sería construir un tensor de forma idéntica de 0. Y luego ajustar un 1 en la posición que desee. Luego se sumarían los dos tensores. Una vez más, esto se encuentra con el mismo problema que antes.

He leído a través de la API docs varias veces y no sé cómo hacer esto. Gracias de antemano!

Author: LeavesBreathe, 2016-01-08

2 answers

ACTUALIZACIÓN: TensorFlow 1.0 incluye un tf.scatter_nd() operador, que se puede usar para crear delta a continuación sin crear un tf.SparseTensor.


¡Esto es realmente sorprendentemente complicado con las operaciones existentes! Tal vez alguien pueda sugerir una mejor manera de concluir lo siguiente, pero aquí hay una manera de hacerlo.

Digamos que tienes un tensor tf.constant():

c = tf.constant([[0.0, 0.0, 0.0],
                 [0.0, 0.0, 0.0],
                 [0.0, 0.0, 0.0]])

...y desea agregar 1.0 en la ubicación [1, 1]. Una forma de hacer esto es definir un tf.SparseTensor, delta, representando el cambio:

indices = [[1, 1]]  # A list of coordinates to update.

values = [1.0]  # A list of values corresponding to the respective
                # coordinate in indices.

shape = [3, 3]  # The shape of the corresponding dense tensor, same as `c`.

delta = tf.SparseTensor(indices, values, shape)

Entonces puede utilizar el tf.sparse_tensor_to_dense() op para hacer un tensor denso de delta y añadirlo a c:

result = c + tf.sparse_tensor_to_dense(delta)

sess = tf.Session()
sess.run(result)
# ==> array([[ 0.,  0.,  0.],
#            [ 0.,  1.,  0.],
#            [ 0.,  0.,  0.]], dtype=float32)
 58
Author: mrry,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-03-03 17:23:35

¿Qué tal tf.scatter_update(ref, indices, updates) o tf.scatter_add(ref, indices, updates)?

ref[indices[...], :] = updates
ref[indices[...], :] += updates

Ver esto .

 6
Author: Liping Liu,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-01-01 19:27:58