import re


def snake(string):
  """Convert string to snake case

  Implementation must be consistent with tf.Module's camel_to_snake in
  github.com/tensorflow/tensorflow/blob/master/tensorflow/python/module/module.py
  """
  return re.sub(r"((?<=[a-z0-9])[A-Z]|(?!^)[A-Z](?=[a-z]))", r"_\1", string).lower()