Tutorial 4: Attaching a Dataset

In [1]:
import libspn as spn
import tensorflow as tf

Building a Test Graph with Random Weights

In [2]:
iv_x = spn.IVs(num_vars=2, num_vals=2, name="iv_x")
sum_11 = spn.Sum((iv_x, [0,1]), name="sum_11")
sum_12 = spn.Sum((iv_x, [0,1]), name="sum_12")
sum_21 = spn.Sum((iv_x, [2,3]), name="sum_21")
sum_22 = spn.Sum((iv_x, [2,3]), name="sum_22")
prod_1 = spn.Product(sum_11, sum_21, name="prod_1")
prod_2 = spn.Product(sum_11, sum_22, name="prod_2")
prod_3 = spn.Product(sum_12, sum_22, name="prod_3")
root = spn.Sum(prod_1, prod_2, prod_3, name="root")
iv_y = root.generate_ivs(name="iv_y")
spn.generate_weights(root, init_value=spn.ValueType.RANDOM_UNIFORM(0, 1))

Visualizing the SPN Graph

In [3]:
spn.display_spn_graph(root)

Specify Training Data

In [4]:
dataset=spn.CSVFileDataset('data.csv', num_vals=[2, 2], defaults=[[-1],[-1],[-1]],
                           num_labels=1, num_epochs=20, batch_size=8, shuffle=True, min_after_dequeue=8)
samples, labels = dataset.get_data()
iv_x.attach_feed(samples)
iv_y.attach_feed(labels)
[INFO] [spn.Dataset:get_data] Building dataset operations

Add Learning Ops

In [5]:
learner = spn.EMLearner(root, value_inference_type = spn.InferenceType.MARGINAL, initial_accum_value=10)
init_weights = spn.initialize_weights(root)
init_learning = learner.initialize()
learn = learner.learn()
likelihood = tf.reduce_mean(learner.value.values[root])

Run Learning

In [6]:
with spn.session() as (sess, run):
    sess.run(init_weights)
    sess.run(init_learning)
    likelihoods=[]
    try:    
        while run():
            likelihood_, _ = sess.run([likelihood, learn])
            print("Avg. Likelihood: %s" % (likelihood_))
            likelihoods+=[likelihood_]          
    except tf.errors.OutOfRangeError:
        print("Done!")
Avg. Likelihood: -1.60182
Avg. Likelihood: -1.34953
Avg. Likelihood: -1.379
Avg. Likelihood: -1.32074
Avg. Likelihood: -1.33961
Avg. Likelihood: -1.24907
Avg. Likelihood: -1.2773
Avg. Likelihood: -1.21422
Avg. Likelihood: -1.31709
Avg. Likelihood: -1.21426
Avg. Likelihood: -1.17128
Avg. Likelihood: -1.29712
Avg. Likelihood: -1.19819
Avg. Likelihood: -1.24228
Avg. Likelihood: -1.19567
Avg. Likelihood: -1.10348
Avg. Likelihood: -1.17327
Avg. Likelihood: -1.23069
Avg. Likelihood: -1.18122
Avg. Likelihood: -1.11965
Done!
In [7]:
# Plot training likelihoods
plt.plot(likelihoods)
plt.show()