code: def calculate_sparsity(weights):
      """Returns sparsity of the given weights tensor."""
      number_of_weights = np.size(weights)
      number_of_non_zero_weights = np.count_nonzero(weights)
      sparsity = (
          1.0 - float(number_of_non_zero_weights) / number_of_weights
          if number_of_non_zero_weights != 0
          else 1.0
      )
      return sparsity
  
file_path: model-optimization/tensorflow_model_optimization/python/core/sparsity/keras/tools/check_sparsity_m_by_n.py
imports: from __future__ import print_function
  from absl import app
  from absl import flags
  import numpy
  import tensorflow
  from tensorflow_model_optimization.python.core.sparsity.keras import pruning_utils
label_desc: Use image node_id 2 for calling a global function with example usage: calculate_sparsity(weights) and returns: sparsity