Minli Xu
Minli Xu

Reputation: 33

In tensorflow, how to assign change part of a tensor with itself?

I would like to do something like this in tensorflow

some_tensor[0] *= 2

double the first number in this tensor. Is there a neat to do so? Thanks.

Upvotes: 0

Views: 69

Answers (1)

Vlad
Vlad

Reputation: 8595

Use tf.scatter_mul():

import tensorflow as tf
import numpy as np

v = tf.Variable(np.ones((2, ), np.float32), trainable=False)
mult_op = tf.scatter_mul(v,
                         indices=tf.constant([0]),
                         updates=tf.constant([2.]))

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    print(v.eval()) # [1. 1.]
    sess.run(mult_op)
    print(v.eval()) # [2. 1.]
    sess.run(mult_op)
    print(v.eval()) # [4. 1.]

Upvotes: 1

Related Questions