nematus icon indicating copy to clipboard operation
nematus copied to clipboard

TensorArray Not Used on line 240 of mrt_utils.py

Open CodeSmileBot opened this issue 2 years ago • 0 comments

Hello!

I found an AI-Specific Code smell in your project. The smell is called: TensorArray Not Used

You can find more information about it in this paper: https://dl.acm.org/doi/abs/10.1145/3522664.3528620.

According to the paper, the smell is described as follows:

Problem If the developer initializes an array using tf.constant() and tries to assign a new value to it in the loop to keep it growing, the code will run into an error. The developer can fix this error by the low-level tf.while_loop() API. However, it is inefficient coding in this way. A lot of intermediate tensors are built in this process.
Solution Using tf.TensorArray() for growing array in the loop is a better solution for this kind of problem in TensorFlow 2.
Impact Efficiency, Error-proneness

Example:

### TensorFlow
import tensorflow as tf
@tf.function
def fibonacci(n):
   a = tf.constant(1)
   b = tf.constant(1)
-    c = tf.constant([1, 1])
+    c = tf.TensorArray(tf.int32, n)
+    c = c.write(0, a)
+    c = c.write(1, b)

   for i in range(2, n):
       a, b = b, a + b
-       c = tf.concat([c, [b]], 0)
+		c = c.write(i, b)

-    return c
+	 return c.stack()

You can find the code related to this smell in this link: https://github.com/EdinburghNLP/nematus/blob/6419dbb91e7266429f85073595f299f68979b8bd/nematus/mrt_utils.py#L230-L250.

I also found instances of this smell in other files, such as:

File: https://github.com/EdinburghNLP/nematus/blob/master/nematus/layers.py#L122-L132 Line: 127 File: https://github.com/EdinburghNLP/nematus/blob/master/nematus/layers.py#L127-L137 Line: 132 .

I hope this information is helpful!

CodeSmileBot avatar Jul 07 '23 09:07 CodeSmileBot