Tensorflow: tensor binarization












0















I want to transform this dataset in such a way that each tensor has a given size n and that a feature at index i of this new tensor is set to 1 if and only if there is a i in the original feature (modulo n).



I hope the following example will make things clearer



Let's suppose I have a dataset like:



t = tf.constant([
[0, 3, 4],
[12, 2 ,4]])
ds = tf.data.Dataset.from_tensors(t)


I want to get (if n = 9)



t = tf.constant([
[1, 0, 0, 1, 1, 0, 0, 0, 0], # index set to 1 are 0, 3 and 4
[0, 0, 1, 1, 1, 0, 0, 0, 0]]) # index set to 1 are 2, 4, and 12%9 = 3


I know how to apply the modulo to a tensor, but I don't find how to do the rest of the transformation
thanks










share|improve this question



























    0















    I want to transform this dataset in such a way that each tensor has a given size n and that a feature at index i of this new tensor is set to 1 if and only if there is a i in the original feature (modulo n).



    I hope the following example will make things clearer



    Let's suppose I have a dataset like:



    t = tf.constant([
    [0, 3, 4],
    [12, 2 ,4]])
    ds = tf.data.Dataset.from_tensors(t)


    I want to get (if n = 9)



    t = tf.constant([
    [1, 0, 0, 1, 1, 0, 0, 0, 0], # index set to 1 are 0, 3 and 4
    [0, 0, 1, 1, 1, 0, 0, 0, 0]]) # index set to 1 are 2, 4, and 12%9 = 3


    I know how to apply the modulo to a tensor, but I don't find how to do the rest of the transformation
    thanks










    share|improve this question

























      0












      0








      0








      I want to transform this dataset in such a way that each tensor has a given size n and that a feature at index i of this new tensor is set to 1 if and only if there is a i in the original feature (modulo n).



      I hope the following example will make things clearer



      Let's suppose I have a dataset like:



      t = tf.constant([
      [0, 3, 4],
      [12, 2 ,4]])
      ds = tf.data.Dataset.from_tensors(t)


      I want to get (if n = 9)



      t = tf.constant([
      [1, 0, 0, 1, 1, 0, 0, 0, 0], # index set to 1 are 0, 3 and 4
      [0, 0, 1, 1, 1, 0, 0, 0, 0]]) # index set to 1 are 2, 4, and 12%9 = 3


      I know how to apply the modulo to a tensor, but I don't find how to do the rest of the transformation
      thanks










      share|improve this question














      I want to transform this dataset in such a way that each tensor has a given size n and that a feature at index i of this new tensor is set to 1 if and only if there is a i in the original feature (modulo n).



      I hope the following example will make things clearer



      Let's suppose I have a dataset like:



      t = tf.constant([
      [0, 3, 4],
      [12, 2 ,4]])
      ds = tf.data.Dataset.from_tensors(t)


      I want to get (if n = 9)



      t = tf.constant([
      [1, 0, 0, 1, 1, 0, 0, 0, 0], # index set to 1 are 0, 3 and 4
      [0, 0, 1, 1, 1, 0, 0, 0, 0]]) # index set to 1 are 2, 4, and 12%9 = 3


      I know how to apply the modulo to a tensor, but I don't find how to do the rest of the transformation
      thanks







      python tensorflow






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 21 '18 at 14:37









      taktak004taktak004

      410524




      410524
























          1 Answer
          1






          active

          oldest

          votes


















          1














          That is similar to tf.one_hot, only for multiple values at the same time. Here is a way to do that:



          import tensorflow as tf

          def binarization(t, n):
          # One-hot encoding of each value
          t_1h = tf.one_hot(t % n, n, dtype=tf.bool, on_value=True, off_value=False)
          # Reduce across last dimension of the original tensor
          return tf.cast(tf.reduce_any(t_1h, axis=-2), t.dtype)

          # Test
          with tf.Graph().as_default(), tf.Session() as sess:
          t = tf.constant([
          [ 0, 3, 4],
          [12, 2, 4]
          ])
          t_m1h = binarization(t, 9)
          print(sess.run(t_m1h))


          Output:



          [[1 0 0 1 1 0 0 0 0]
          [0 0 1 1 1 0 0 0 0]]





          share|improve this answer


























          • Thanks, can you explain the axis=-2. I understand what you are doing, but I don't understand how you come up with -2.

            – taktak004
            Nov 21 '18 at 15:05






          • 1





            @taktak004 Right, so after you do tf.one_hot a new dimension is added at the end. However, you want to reduce across the last dimension of the input tensor (e.g. the "columns" in your example, 0, 3, 4 and 12, 2, 4. This dimension will not be the last one in t_1h (because that is the new one-hot dimension), but the previous-to-last one, which can be referred to with the -2. I hope that makes it clearer?

            – jdehesa
            Nov 21 '18 at 15:09











          • ok, and by puting -2 instead of 1, you make it more generic

            – taktak004
            Nov 21 '18 at 15:13






          • 1





            @taktak004 Yes exactly the idea was that it worked either with a vector or a matrix or whatever (well actually it would fail for a scalar I think). You could also add explicit parameters for the axis that is reduced and/or the position of the new axis, but that seemed overcomplicated.

            – jdehesa
            Nov 21 '18 at 15:18






          • 1





            I created a new question as I think the approch will be totally different actually :stackoverflow.com/questions/53450218/…

            – taktak004
            Nov 23 '18 at 16:35













          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',
          autoActivateHeartbeat: false,
          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%2f53414433%2ftensorflow-tensor-binarization%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          That is similar to tf.one_hot, only for multiple values at the same time. Here is a way to do that:



          import tensorflow as tf

          def binarization(t, n):
          # One-hot encoding of each value
          t_1h = tf.one_hot(t % n, n, dtype=tf.bool, on_value=True, off_value=False)
          # Reduce across last dimension of the original tensor
          return tf.cast(tf.reduce_any(t_1h, axis=-2), t.dtype)

          # Test
          with tf.Graph().as_default(), tf.Session() as sess:
          t = tf.constant([
          [ 0, 3, 4],
          [12, 2, 4]
          ])
          t_m1h = binarization(t, 9)
          print(sess.run(t_m1h))


          Output:



          [[1 0 0 1 1 0 0 0 0]
          [0 0 1 1 1 0 0 0 0]]





          share|improve this answer


























          • Thanks, can you explain the axis=-2. I understand what you are doing, but I don't understand how you come up with -2.

            – taktak004
            Nov 21 '18 at 15:05






          • 1





            @taktak004 Right, so after you do tf.one_hot a new dimension is added at the end. However, you want to reduce across the last dimension of the input tensor (e.g. the "columns" in your example, 0, 3, 4 and 12, 2, 4. This dimension will not be the last one in t_1h (because that is the new one-hot dimension), but the previous-to-last one, which can be referred to with the -2. I hope that makes it clearer?

            – jdehesa
            Nov 21 '18 at 15:09











          • ok, and by puting -2 instead of 1, you make it more generic

            – taktak004
            Nov 21 '18 at 15:13






          • 1





            @taktak004 Yes exactly the idea was that it worked either with a vector or a matrix or whatever (well actually it would fail for a scalar I think). You could also add explicit parameters for the axis that is reduced and/or the position of the new axis, but that seemed overcomplicated.

            – jdehesa
            Nov 21 '18 at 15:18






          • 1





            I created a new question as I think the approch will be totally different actually :stackoverflow.com/questions/53450218/…

            – taktak004
            Nov 23 '18 at 16:35


















          1














          That is similar to tf.one_hot, only for multiple values at the same time. Here is a way to do that:



          import tensorflow as tf

          def binarization(t, n):
          # One-hot encoding of each value
          t_1h = tf.one_hot(t % n, n, dtype=tf.bool, on_value=True, off_value=False)
          # Reduce across last dimension of the original tensor
          return tf.cast(tf.reduce_any(t_1h, axis=-2), t.dtype)

          # Test
          with tf.Graph().as_default(), tf.Session() as sess:
          t = tf.constant([
          [ 0, 3, 4],
          [12, 2, 4]
          ])
          t_m1h = binarization(t, 9)
          print(sess.run(t_m1h))


          Output:



          [[1 0 0 1 1 0 0 0 0]
          [0 0 1 1 1 0 0 0 0]]





          share|improve this answer


























          • Thanks, can you explain the axis=-2. I understand what you are doing, but I don't understand how you come up with -2.

            – taktak004
            Nov 21 '18 at 15:05






          • 1





            @taktak004 Right, so after you do tf.one_hot a new dimension is added at the end. However, you want to reduce across the last dimension of the input tensor (e.g. the "columns" in your example, 0, 3, 4 and 12, 2, 4. This dimension will not be the last one in t_1h (because that is the new one-hot dimension), but the previous-to-last one, which can be referred to with the -2. I hope that makes it clearer?

            – jdehesa
            Nov 21 '18 at 15:09











          • ok, and by puting -2 instead of 1, you make it more generic

            – taktak004
            Nov 21 '18 at 15:13






          • 1





            @taktak004 Yes exactly the idea was that it worked either with a vector or a matrix or whatever (well actually it would fail for a scalar I think). You could also add explicit parameters for the axis that is reduced and/or the position of the new axis, but that seemed overcomplicated.

            – jdehesa
            Nov 21 '18 at 15:18






          • 1





            I created a new question as I think the approch will be totally different actually :stackoverflow.com/questions/53450218/…

            – taktak004
            Nov 23 '18 at 16:35
















          1












          1








          1







          That is similar to tf.one_hot, only for multiple values at the same time. Here is a way to do that:



          import tensorflow as tf

          def binarization(t, n):
          # One-hot encoding of each value
          t_1h = tf.one_hot(t % n, n, dtype=tf.bool, on_value=True, off_value=False)
          # Reduce across last dimension of the original tensor
          return tf.cast(tf.reduce_any(t_1h, axis=-2), t.dtype)

          # Test
          with tf.Graph().as_default(), tf.Session() as sess:
          t = tf.constant([
          [ 0, 3, 4],
          [12, 2, 4]
          ])
          t_m1h = binarization(t, 9)
          print(sess.run(t_m1h))


          Output:



          [[1 0 0 1 1 0 0 0 0]
          [0 0 1 1 1 0 0 0 0]]





          share|improve this answer















          That is similar to tf.one_hot, only for multiple values at the same time. Here is a way to do that:



          import tensorflow as tf

          def binarization(t, n):
          # One-hot encoding of each value
          t_1h = tf.one_hot(t % n, n, dtype=tf.bool, on_value=True, off_value=False)
          # Reduce across last dimension of the original tensor
          return tf.cast(tf.reduce_any(t_1h, axis=-2), t.dtype)

          # Test
          with tf.Graph().as_default(), tf.Session() as sess:
          t = tf.constant([
          [ 0, 3, 4],
          [12, 2, 4]
          ])
          t_m1h = binarization(t, 9)
          print(sess.run(t_m1h))


          Output:



          [[1 0 0 1 1 0 0 0 0]
          [0 0 1 1 1 0 0 0 0]]






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 21 '18 at 15:08

























          answered Nov 21 '18 at 14:46









          jdehesajdehesa

          24.1k43554




          24.1k43554













          • Thanks, can you explain the axis=-2. I understand what you are doing, but I don't understand how you come up with -2.

            – taktak004
            Nov 21 '18 at 15:05






          • 1





            @taktak004 Right, so after you do tf.one_hot a new dimension is added at the end. However, you want to reduce across the last dimension of the input tensor (e.g. the "columns" in your example, 0, 3, 4 and 12, 2, 4. This dimension will not be the last one in t_1h (because that is the new one-hot dimension), but the previous-to-last one, which can be referred to with the -2. I hope that makes it clearer?

            – jdehesa
            Nov 21 '18 at 15:09











          • ok, and by puting -2 instead of 1, you make it more generic

            – taktak004
            Nov 21 '18 at 15:13






          • 1





            @taktak004 Yes exactly the idea was that it worked either with a vector or a matrix or whatever (well actually it would fail for a scalar I think). You could also add explicit parameters for the axis that is reduced and/or the position of the new axis, but that seemed overcomplicated.

            – jdehesa
            Nov 21 '18 at 15:18






          • 1





            I created a new question as I think the approch will be totally different actually :stackoverflow.com/questions/53450218/…

            – taktak004
            Nov 23 '18 at 16:35





















          • Thanks, can you explain the axis=-2. I understand what you are doing, but I don't understand how you come up with -2.

            – taktak004
            Nov 21 '18 at 15:05






          • 1





            @taktak004 Right, so after you do tf.one_hot a new dimension is added at the end. However, you want to reduce across the last dimension of the input tensor (e.g. the "columns" in your example, 0, 3, 4 and 12, 2, 4. This dimension will not be the last one in t_1h (because that is the new one-hot dimension), but the previous-to-last one, which can be referred to with the -2. I hope that makes it clearer?

            – jdehesa
            Nov 21 '18 at 15:09











          • ok, and by puting -2 instead of 1, you make it more generic

            – taktak004
            Nov 21 '18 at 15:13






          • 1





            @taktak004 Yes exactly the idea was that it worked either with a vector or a matrix or whatever (well actually it would fail for a scalar I think). You could also add explicit parameters for the axis that is reduced and/or the position of the new axis, but that seemed overcomplicated.

            – jdehesa
            Nov 21 '18 at 15:18






          • 1





            I created a new question as I think the approch will be totally different actually :stackoverflow.com/questions/53450218/…

            – taktak004
            Nov 23 '18 at 16:35



















          Thanks, can you explain the axis=-2. I understand what you are doing, but I don't understand how you come up with -2.

          – taktak004
          Nov 21 '18 at 15:05





          Thanks, can you explain the axis=-2. I understand what you are doing, but I don't understand how you come up with -2.

          – taktak004
          Nov 21 '18 at 15:05




          1




          1





          @taktak004 Right, so after you do tf.one_hot a new dimension is added at the end. However, you want to reduce across the last dimension of the input tensor (e.g. the "columns" in your example, 0, 3, 4 and 12, 2, 4. This dimension will not be the last one in t_1h (because that is the new one-hot dimension), but the previous-to-last one, which can be referred to with the -2. I hope that makes it clearer?

          – jdehesa
          Nov 21 '18 at 15:09





          @taktak004 Right, so after you do tf.one_hot a new dimension is added at the end. However, you want to reduce across the last dimension of the input tensor (e.g. the "columns" in your example, 0, 3, 4 and 12, 2, 4. This dimension will not be the last one in t_1h (because that is the new one-hot dimension), but the previous-to-last one, which can be referred to with the -2. I hope that makes it clearer?

          – jdehesa
          Nov 21 '18 at 15:09













          ok, and by puting -2 instead of 1, you make it more generic

          – taktak004
          Nov 21 '18 at 15:13





          ok, and by puting -2 instead of 1, you make it more generic

          – taktak004
          Nov 21 '18 at 15:13




          1




          1





          @taktak004 Yes exactly the idea was that it worked either with a vector or a matrix or whatever (well actually it would fail for a scalar I think). You could also add explicit parameters for the axis that is reduced and/or the position of the new axis, but that seemed overcomplicated.

          – jdehesa
          Nov 21 '18 at 15:18





          @taktak004 Yes exactly the idea was that it worked either with a vector or a matrix or whatever (well actually it would fail for a scalar I think). You could also add explicit parameters for the axis that is reduced and/or the position of the new axis, but that seemed overcomplicated.

          – jdehesa
          Nov 21 '18 at 15:18




          1




          1





          I created a new question as I think the approch will be totally different actually :stackoverflow.com/questions/53450218/…

          – taktak004
          Nov 23 '18 at 16:35







          I created a new question as I think the approch will be totally different actually :stackoverflow.com/questions/53450218/…

          – taktak004
          Nov 23 '18 at 16:35






















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53414433%2ftensorflow-tensor-binarization%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          'app-layout' is not a known element: how to share Component with different Modules

          android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

          WPF add header to Image with URL pettitions [duplicate]