Assigning zero to tensor at indices specified in a list











up vote
2
down vote

favorite












I have to tensors, for example



A = tf.Tensor(
[[1.0986123 0.6931472 0. 0.6931472 0. ]
[0. 0. 0. 0. 0. ]
[3.7376697 3.7612002 3.7841897 3.8066626 3.8286414]], shape=(3, 5), dtype=float32)

B = tf.Tensor(
[[2 1]
[2 2]], shape=(2, 2), dtype=int64)


Tensor B holds indices in tensor A. I want to update every value in tensor A to zero that is listed in the index list B.



So, the expected result would be



tf.Tensor(
[[1.0986123 0.6931472 0. 0.6931472 0. ]
[0. 0. 0. 0. 0. ]
[3.7376697 0 0 3.8066626 3.8286414]], shape=(3, 5), dtype=float32)


So the entries at index [2,1] and [2, 2] are set to 0.



I looked at tf.assign but they can only be used for tf.Variable's. tf.boolean_mask would be a nice way to do it, but i do not know and could not find out how i can create a boolean mask with a list of indices.



I looked at the tensor flow functions i could find and related S/O answers but couldn't find a satisfying solution.










share|improve this question




























    up vote
    2
    down vote

    favorite












    I have to tensors, for example



    A = tf.Tensor(
    [[1.0986123 0.6931472 0. 0.6931472 0. ]
    [0. 0. 0. 0. 0. ]
    [3.7376697 3.7612002 3.7841897 3.8066626 3.8286414]], shape=(3, 5), dtype=float32)

    B = tf.Tensor(
    [[2 1]
    [2 2]], shape=(2, 2), dtype=int64)


    Tensor B holds indices in tensor A. I want to update every value in tensor A to zero that is listed in the index list B.



    So, the expected result would be



    tf.Tensor(
    [[1.0986123 0.6931472 0. 0.6931472 0. ]
    [0. 0. 0. 0. 0. ]
    [3.7376697 0 0 3.8066626 3.8286414]], shape=(3, 5), dtype=float32)


    So the entries at index [2,1] and [2, 2] are set to 0.



    I looked at tf.assign but they can only be used for tf.Variable's. tf.boolean_mask would be a nice way to do it, but i do not know and could not find out how i can create a boolean mask with a list of indices.



    I looked at the tensor flow functions i could find and related S/O answers but couldn't find a satisfying solution.










    share|improve this question


























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      I have to tensors, for example



      A = tf.Tensor(
      [[1.0986123 0.6931472 0. 0.6931472 0. ]
      [0. 0. 0. 0. 0. ]
      [3.7376697 3.7612002 3.7841897 3.8066626 3.8286414]], shape=(3, 5), dtype=float32)

      B = tf.Tensor(
      [[2 1]
      [2 2]], shape=(2, 2), dtype=int64)


      Tensor B holds indices in tensor A. I want to update every value in tensor A to zero that is listed in the index list B.



      So, the expected result would be



      tf.Tensor(
      [[1.0986123 0.6931472 0. 0.6931472 0. ]
      [0. 0. 0. 0. 0. ]
      [3.7376697 0 0 3.8066626 3.8286414]], shape=(3, 5), dtype=float32)


      So the entries at index [2,1] and [2, 2] are set to 0.



      I looked at tf.assign but they can only be used for tf.Variable's. tf.boolean_mask would be a nice way to do it, but i do not know and could not find out how i can create a boolean mask with a list of indices.



      I looked at the tensor flow functions i could find and related S/O answers but couldn't find a satisfying solution.










      share|improve this question















      I have to tensors, for example



      A = tf.Tensor(
      [[1.0986123 0.6931472 0. 0.6931472 0. ]
      [0. 0. 0. 0. 0. ]
      [3.7376697 3.7612002 3.7841897 3.8066626 3.8286414]], shape=(3, 5), dtype=float32)

      B = tf.Tensor(
      [[2 1]
      [2 2]], shape=(2, 2), dtype=int64)


      Tensor B holds indices in tensor A. I want to update every value in tensor A to zero that is listed in the index list B.



      So, the expected result would be



      tf.Tensor(
      [[1.0986123 0.6931472 0. 0.6931472 0. ]
      [0. 0. 0. 0. 0. ]
      [3.7376697 0 0 3.8066626 3.8286414]], shape=(3, 5), dtype=float32)


      So the entries at index [2,1] and [2, 2] are set to 0.



      I looked at tf.assign but they can only be used for tf.Variable's. tf.boolean_mask would be a nice way to do it, but i do not know and could not find out how i can create a boolean mask with a list of indices.



      I looked at the tensor flow functions i could find and related S/O answers but couldn't find a satisfying solution.







      python tensorflow






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 4 at 15:51









      Sam Comber

      367212




      367212










      asked Nov 4 at 10:11









      Falco Winkler

      371618




      371618
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote













          You can use tf.scatter_nd_update for this. For example:



          A = tf.Variable(
          [[1.0986123, 0.6931472, 0. , 0.6931472, 0. ],
          [0. , 0. , 0. , 0. , 0. ],
          [3.7376697, 3.7612002, 3.7841897, 3.8066626, 3.8286414]], dtype=tf.float32)

          B = tf.Variable(
          [[2, 1],
          [2, 2]], dtype=tf.int64)

          C = tf.scatter_nd_update(A, B, tf.zeros(shape=tf.shape(B)[0]))

          with tf.Session() as sess:
          sess.run(tf.global_variables_initializer())
          print(sess.run(C))


          or



          A = tf.constant(
          [[1.0986123, 0.6931472, 0. , 0.6931472, 0. ],
          [0. , 0. , 0. , 0. , 0. ],
          [3.7376697, 3.7612002, 3.7841897, 3.8066626, 3.8286414]], dtype=tf.float32)

          B = tf.constant(
          [[2, 1],
          [2, 2]], dtype=tf.int64)

          AV = tf.Variable(A)

          C = tf.scatter_nd_update(AV, B, tf.zeros(shape=tf.shape(B)[0]))

          with tf.Session() as sess:
          sess.run(tf.global_variables_initializer())
          print(sess.run(C))





          share|improve this answer























          • Thanks! My tensors come from the Dataset api so they are not tf.Variable's. I figured i can create a variable from the incoming tensor, apply scatter_nd_update and then convert it into a tensor again. Hacky but it works: tf.convert_to_tensor(tf.scatter_nd_update(tf.contrib.eager.Variable(input_data), index_list, tf.zeros(shape=tf.shape(index_list)[0])))
            – Falco Winkler
            2 days ago










          • i would like to continue research if there is a solution without using variables, maybe with tf.boolean_mask and if i don't find something i can accept your answer
            – Falco Winkler
            2 days ago










          • I add code with constant instead variable. This is same behavior as tf.Tensor
            – Vladimir Bystricky
            2 days ago











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53139678%2fassigning-zero-to-tensor-at-indices-specified-in-a-list%23new-answer', 'question_page');
          }
          );

          Post as a guest
































          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote













          You can use tf.scatter_nd_update for this. For example:



          A = tf.Variable(
          [[1.0986123, 0.6931472, 0. , 0.6931472, 0. ],
          [0. , 0. , 0. , 0. , 0. ],
          [3.7376697, 3.7612002, 3.7841897, 3.8066626, 3.8286414]], dtype=tf.float32)

          B = tf.Variable(
          [[2, 1],
          [2, 2]], dtype=tf.int64)

          C = tf.scatter_nd_update(A, B, tf.zeros(shape=tf.shape(B)[0]))

          with tf.Session() as sess:
          sess.run(tf.global_variables_initializer())
          print(sess.run(C))


          or



          A = tf.constant(
          [[1.0986123, 0.6931472, 0. , 0.6931472, 0. ],
          [0. , 0. , 0. , 0. , 0. ],
          [3.7376697, 3.7612002, 3.7841897, 3.8066626, 3.8286414]], dtype=tf.float32)

          B = tf.constant(
          [[2, 1],
          [2, 2]], dtype=tf.int64)

          AV = tf.Variable(A)

          C = tf.scatter_nd_update(AV, B, tf.zeros(shape=tf.shape(B)[0]))

          with tf.Session() as sess:
          sess.run(tf.global_variables_initializer())
          print(sess.run(C))





          share|improve this answer























          • Thanks! My tensors come from the Dataset api so they are not tf.Variable's. I figured i can create a variable from the incoming tensor, apply scatter_nd_update and then convert it into a tensor again. Hacky but it works: tf.convert_to_tensor(tf.scatter_nd_update(tf.contrib.eager.Variable(input_data), index_list, tf.zeros(shape=tf.shape(index_list)[0])))
            – Falco Winkler
            2 days ago










          • i would like to continue research if there is a solution without using variables, maybe with tf.boolean_mask and if i don't find something i can accept your answer
            – Falco Winkler
            2 days ago










          • I add code with constant instead variable. This is same behavior as tf.Tensor
            – Vladimir Bystricky
            2 days ago















          up vote
          1
          down vote













          You can use tf.scatter_nd_update for this. For example:



          A = tf.Variable(
          [[1.0986123, 0.6931472, 0. , 0.6931472, 0. ],
          [0. , 0. , 0. , 0. , 0. ],
          [3.7376697, 3.7612002, 3.7841897, 3.8066626, 3.8286414]], dtype=tf.float32)

          B = tf.Variable(
          [[2, 1],
          [2, 2]], dtype=tf.int64)

          C = tf.scatter_nd_update(A, B, tf.zeros(shape=tf.shape(B)[0]))

          with tf.Session() as sess:
          sess.run(tf.global_variables_initializer())
          print(sess.run(C))


          or



          A = tf.constant(
          [[1.0986123, 0.6931472, 0. , 0.6931472, 0. ],
          [0. , 0. , 0. , 0. , 0. ],
          [3.7376697, 3.7612002, 3.7841897, 3.8066626, 3.8286414]], dtype=tf.float32)

          B = tf.constant(
          [[2, 1],
          [2, 2]], dtype=tf.int64)

          AV = tf.Variable(A)

          C = tf.scatter_nd_update(AV, B, tf.zeros(shape=tf.shape(B)[0]))

          with tf.Session() as sess:
          sess.run(tf.global_variables_initializer())
          print(sess.run(C))





          share|improve this answer























          • Thanks! My tensors come from the Dataset api so they are not tf.Variable's. I figured i can create a variable from the incoming tensor, apply scatter_nd_update and then convert it into a tensor again. Hacky but it works: tf.convert_to_tensor(tf.scatter_nd_update(tf.contrib.eager.Variable(input_data), index_list, tf.zeros(shape=tf.shape(index_list)[0])))
            – Falco Winkler
            2 days ago










          • i would like to continue research if there is a solution without using variables, maybe with tf.boolean_mask and if i don't find something i can accept your answer
            – Falco Winkler
            2 days ago










          • I add code with constant instead variable. This is same behavior as tf.Tensor
            – Vladimir Bystricky
            2 days ago













          up vote
          1
          down vote










          up vote
          1
          down vote









          You can use tf.scatter_nd_update for this. For example:



          A = tf.Variable(
          [[1.0986123, 0.6931472, 0. , 0.6931472, 0. ],
          [0. , 0. , 0. , 0. , 0. ],
          [3.7376697, 3.7612002, 3.7841897, 3.8066626, 3.8286414]], dtype=tf.float32)

          B = tf.Variable(
          [[2, 1],
          [2, 2]], dtype=tf.int64)

          C = tf.scatter_nd_update(A, B, tf.zeros(shape=tf.shape(B)[0]))

          with tf.Session() as sess:
          sess.run(tf.global_variables_initializer())
          print(sess.run(C))


          or



          A = tf.constant(
          [[1.0986123, 0.6931472, 0. , 0.6931472, 0. ],
          [0. , 0. , 0. , 0. , 0. ],
          [3.7376697, 3.7612002, 3.7841897, 3.8066626, 3.8286414]], dtype=tf.float32)

          B = tf.constant(
          [[2, 1],
          [2, 2]], dtype=tf.int64)

          AV = tf.Variable(A)

          C = tf.scatter_nd_update(AV, B, tf.zeros(shape=tf.shape(B)[0]))

          with tf.Session() as sess:
          sess.run(tf.global_variables_initializer())
          print(sess.run(C))





          share|improve this answer














          You can use tf.scatter_nd_update for this. For example:



          A = tf.Variable(
          [[1.0986123, 0.6931472, 0. , 0.6931472, 0. ],
          [0. , 0. , 0. , 0. , 0. ],
          [3.7376697, 3.7612002, 3.7841897, 3.8066626, 3.8286414]], dtype=tf.float32)

          B = tf.Variable(
          [[2, 1],
          [2, 2]], dtype=tf.int64)

          C = tf.scatter_nd_update(A, B, tf.zeros(shape=tf.shape(B)[0]))

          with tf.Session() as sess:
          sess.run(tf.global_variables_initializer())
          print(sess.run(C))


          or



          A = tf.constant(
          [[1.0986123, 0.6931472, 0. , 0.6931472, 0. ],
          [0. , 0. , 0. , 0. , 0. ],
          [3.7376697, 3.7612002, 3.7841897, 3.8066626, 3.8286414]], dtype=tf.float32)

          B = tf.constant(
          [[2, 1],
          [2, 2]], dtype=tf.int64)

          AV = tf.Variable(A)

          C = tf.scatter_nd_update(AV, B, tf.zeros(shape=tf.shape(B)[0]))

          with tf.Session() as sess:
          sess.run(tf.global_variables_initializer())
          print(sess.run(C))






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 2 days ago

























          answered Nov 4 at 15:24









          Vladimir Bystricky

          1,0731512




          1,0731512












          • Thanks! My tensors come from the Dataset api so they are not tf.Variable's. I figured i can create a variable from the incoming tensor, apply scatter_nd_update and then convert it into a tensor again. Hacky but it works: tf.convert_to_tensor(tf.scatter_nd_update(tf.contrib.eager.Variable(input_data), index_list, tf.zeros(shape=tf.shape(index_list)[0])))
            – Falco Winkler
            2 days ago










          • i would like to continue research if there is a solution without using variables, maybe with tf.boolean_mask and if i don't find something i can accept your answer
            – Falco Winkler
            2 days ago










          • I add code with constant instead variable. This is same behavior as tf.Tensor
            – Vladimir Bystricky
            2 days ago


















          • Thanks! My tensors come from the Dataset api so they are not tf.Variable's. I figured i can create a variable from the incoming tensor, apply scatter_nd_update and then convert it into a tensor again. Hacky but it works: tf.convert_to_tensor(tf.scatter_nd_update(tf.contrib.eager.Variable(input_data), index_list, tf.zeros(shape=tf.shape(index_list)[0])))
            – Falco Winkler
            2 days ago










          • i would like to continue research if there is a solution without using variables, maybe with tf.boolean_mask and if i don't find something i can accept your answer
            – Falco Winkler
            2 days ago










          • I add code with constant instead variable. This is same behavior as tf.Tensor
            – Vladimir Bystricky
            2 days ago
















          Thanks! My tensors come from the Dataset api so they are not tf.Variable's. I figured i can create a variable from the incoming tensor, apply scatter_nd_update and then convert it into a tensor again. Hacky but it works: tf.convert_to_tensor(tf.scatter_nd_update(tf.contrib.eager.Variable(input_data), index_list, tf.zeros(shape=tf.shape(index_list)[0])))
          – Falco Winkler
          2 days ago




          Thanks! My tensors come from the Dataset api so they are not tf.Variable's. I figured i can create a variable from the incoming tensor, apply scatter_nd_update and then convert it into a tensor again. Hacky but it works: tf.convert_to_tensor(tf.scatter_nd_update(tf.contrib.eager.Variable(input_data), index_list, tf.zeros(shape=tf.shape(index_list)[0])))
          – Falco Winkler
          2 days ago












          i would like to continue research if there is a solution without using variables, maybe with tf.boolean_mask and if i don't find something i can accept your answer
          – Falco Winkler
          2 days ago




          i would like to continue research if there is a solution without using variables, maybe with tf.boolean_mask and if i don't find something i can accept your answer
          – Falco Winkler
          2 days ago












          I add code with constant instead variable. This is same behavior as tf.Tensor
          – Vladimir Bystricky
          2 days ago




          I add code with constant instead variable. This is same behavior as tf.Tensor
          – Vladimir Bystricky
          2 days ago


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53139678%2fassigning-zero-to-tensor-at-indices-specified-in-a-list%23new-answer', 'question_page');
          }
          );

          Post as a guest




















































































          這個網誌中的熱門文章

          Tangent Lines Diagram Along Smooth Curve

          Yusuf al-Mu'taman ibn Hud

          Zucchini