pronobis / libspn-keras

Library for learning and inference with Sum-product Networks utilizing TensorFlow 2.x and Keras
Other
47 stars 9 forks source link

Stats of SPNs #26

Closed yannickl96 closed 3 years ago

yannickl96 commented 3 years ago

Hello,

does libspn-keras provide a function to directly obtain structural stats of SPNs (i.e. number of addition, multiplication and leaf nodes)? I only found the .summary() method which provides me the output shapes of each layer and the number of parameters in the examples. Any information would be appreciated!

Best, Yannick

jostosh commented 3 years ago

Hi Yannick,

There is no direct method for showing these stats. But it's not that complicated to do with a simple script. Assuming that you have an spn as an instance of tf.keras.models.Sequential or libspn_keras.models.SequentialSumProductNetwork you can use the following short script to print the number of sums, products and leaves:

import numpy as np

num_sums = 0
num_products = 0
num_leaves = 0

for layer in spn.layers:
  if isinstance(layer, spnk.layers.DenseSum):  # all sum-layers inherit from spnk.DenseSum
    if isinstance(layer, spnk.layers.RootSum):
      num_sums += 1
    else:
      num_sums += np.prod(layer.output_shape[1:])
  if isinstance(layer, spnk.layers.BaseLeaf):
    num_leaves += np.prod(layer.output_shape[1:])
  if isinstance(layer, (spnk.layers.DenseProduct, spnk.layers.ReduceProduct, spnk.layers.TemporalDenseProduct, spnk.layers.Conv2DProduct)):
    num_products += np.prod(layer.output_shape[1:])

print("num sums", num_sums)
print("num products", num_products)
print("num leaves", num_leaves)
yannickl96 commented 3 years ago

Hi Jos,

works like a charm! Thank you!