Need help converting python code

def _normalize(self, x):
    """You must call this before padding."""
    # -> (1, seqlen)
    mean = tf.reduce_mean(x, axis=-1, keepdims=True)
    var = tf.math.reduce_variance(x, axis=-1, keepdims=True)
    return tf.squeeze((x - mean) / tf.sqrt(var +n  1e-5))

This is the python code I want to convert to scala any help is very appreciated

This looks like Tensorflow code. You’ll probably need a linear algebra library. I know of Breeze and there is a Scala wrapper for Tensorflow (but never used them myself), so you should look into those.

The syntax won’t differ much, you’ll have to add val before assignments, remove the return and add a type annotation for x, but which one will depend on which library you use. The function names will of course also be different.

As @crater2150 mentioned, this looks like tensorflow code so maybe best to use the scala wrapper mentioned.

More generally I find ScalaPy very useful to incrementally port Python code to Scala. See this project for an example of use: calling matplotlib

1 Like