kolovoza 26, 2020 —
                                          
Posted by Kensen Shi, Google Research
When manipulating tensors, one must keep track of multiple dimensions, tensor shape and DType compatibility, and of course mathematical correctness. Additionally, there are hundreds of TensorFlow operations, and finding the right ones to use can be a challenge.
Instead of coding your tensor manipulation directly, what if you could just demonstrate it through …
inputs = {
    'rows': [10, 20, 30],
    'cols': [1, 2, 3, 4],
}output = [[11, 12, 13, 14],
          [21, 22, 23, 24],
          [31, 32, 33, 34]]tf.add(cols, tf.expand_dims(rows, 1))[10, 50, 100, 1000] means that prices under $10 should fall into bucket 0, prices between $10 and $50 fall into bucket 1, and so on.# Input tensors
boundaries = [10, 50, 100, 1000]
prices = [15, 3, 50, 90, 100, 1001]# Output tensor
bucketed_prices = [1, 0, 2, 2, 3, 4]# Input-output example
inputs = {
    'boundaries': [10, 50, 100, 1000],
    'prices': [15, 3, 50, 90, 100, 1001],
}
output = [1, 0, 2, 2, 3, 4]tf.searchsorted(boundaries, prices, side='right')tf.searchsorted confirms that this code indeed performs the bucketing as desired.# Input tensor
scores = [[0.7, 0.2, 0.1],
          [0.4, 0.5, 0.1],
          [0.4, 0.4, 0.2],
          [0.3, 0.4, 0.3],
          [0.0, 0.0, 1.0]]
 
# Output tensor
top_scores = [[1, 0, 0],
              [0, 1, 0],
              [1, 0, 0],
              [0, 1, 0],
              [0, 0, 1]]scores, then only the first such largest element should be marked, so that every row of top_scores has exactly one entry of 1.tf.reduce_max, tf.argmax, and tf.maximum are relevant, but which one should you use? tf.reduce_max produces [0.7, 0.5, 0.4, 0.4, 1.0], tf.argmax produces [0, 1, 0, 1, 2], and tf.maximum isn’t right because it takes two arguments. None of these look close to our desired output.# Input-output example
inputs = {
    'scores': [[0.7, 0.2, 0.1],
               [0.4, 0.5, 0.1],
               [0.4, 0.4, 0.2],
               [0.3, 0.4, 0.3],
               [0.0, 0.0, 1.0]],
}
output = [[1, 0, 0],
          [0, 1, 0],
          [1, 0, 0],
          [0, 1, 0],
          [0, 0, 1]]tf.one_hot and tf.argmax in a short solution to this problem:tf.cast(tf.one_hot(tf.argmax(scores, axis=1), 3), tf.int32)# Input tensor
counts = [[0, 1, 0, 0],
          [0, 1, 1, 0],
          [1, 1, 1, 1]]
 
# Output tensor
normalized = [[0.0, 1.0, 0.0, 0.0],
              [0.0, 0.5, 0.5, 0.0],
              [0.25, 0.25, 0.25, 0.25]]tf.reduce_sum followed by tf.divide), writing the correct code is still nontrivial. A first attempt may look like this:# First attempt
normalized = tf.divide(counts, tf.reduce_sum(counts, axis=1))axis=0?counts and tf.reduce_sum(counts, axis=1) compatible for division, or do you need to reshape or transpose either of these?counts and tf.reduce_sum(counts, axis=1) are both tf.int32 tensors. Can tf.int32 tensors be divided, or do you need to cast them to a float DType first?tf.int32, tf.float32, or something else?# Input-output example
inputs = {
    'counts': [[0, 1, 0, 0],
               [0, 1, 1, 0],
               [1, 1, 1, 1]],
}
output = [[0.0, 1.0, 0.0, 0.0],
          [0.0, 0.5, 0.5, 0.0],
          [0.25, 0.25, 0.25, 0.25]]tf.cast(tf.divide(counts, tf.expand_dims(tf.reduce_sum(counts, axis=1), axis=1)), tf.float32)tf.expand_dims step is needed to make the shapes compatible for division, and the result of tf.divide must be cast to tf.float32 (in fact tf.divide returns a tf.float64 tensor when dividing two tf.int32 tensors). In this way, TF-Coder helps you write simple and correct code without painful debugging cycles.  
 
kolovoza 26, 2020
 —
                                  
Posted by Kensen Shi, Google Research
When manipulating tensors, one must keep track of multiple dimensions, tensor shape and DType compatibility, and of course mathematical correctness. Additionally, there are hundreds of TensorFlow operations, and finding the right ones to use can be a challenge.
Instead of coding your tensor manipulation directly, what if you could just demonstrate it through …