예제 #1
0
def overwrite_plot_spn(spn, plotfile):
    import os
    try:
        os.remove(plotfile)
    except OSError as err:
        pass
    plot_spn(spn, plotfile)
예제 #2
0
def plot():
    spn = create_SPN()
    spn_marg = marginalize()

    from spn.io.Graphics import plot_spn

    plot_spn(spn, "basicspn.png")
    plot_spn(spn_marg, "marginalspn.png")
예제 #3
0
def run_oSLRAU(dataset, update_after_no_min_batches, prune_after):

    data = get_data(dataset)
    data = np.where(np.isnan(data),
                    np.ma.array(data, mask=np.isnan(data)).mean(axis=0), data)

    from sklearn.model_selection import train_test_split
    train_data, test_data = train_test_split(data,
                                             test_size=0.33,
                                             random_state=42)

    # make first mini_batch from data
    mini_batch_size = 50
    first_mini_batch = data[0:mini_batch_size]

    n = first_mini_batch.shape[1]  # num of variables
    print(n)
    context = [Gaussian] * n
    ds_context = Context(
        parametric_types=context).add_domains(first_mini_batch)

    # Learn initial spn
    spn = learn_parametric(first_mini_batch, ds_context)
    plot_spn(spn, 'intitial_spn.pdf')
    print(np.mean(log_likelihood(spn, test_data)))

    oSLRAU_params = oSLRAUParams(mergebatch_threshold=128,
                                 corrthresh=0.1,
                                 mvmaxscope=1,
                                 equalweight=True,
                                 currVals=True)
    no_of_minibatches = int(data.shape[0] / mini_batch_size)

    # update using oSLRAU
    for i in range(1, no_of_minibatches):
        mini_batch = data[i * mini_batch_size:(i + 1) * mini_batch_size]

        update_structure = False
        if update_after_no_min_batches // i == 0:
            print(i)
            update_structure = True
        spn = oSLRAU(spn, mini_batch, oSLRAU_params, update_structure)

        if i == prune_after:
            spn = Prune_oSLRAU(spn)

    print(np.mean(log_likelihood(spn, test_data)))
    plot_spn(spn, 'final_spn.pdf')
예제 #4
0
    def _fit(self, var_types=None, **kwargs):
        if self._spn_type == None:
            raise Exception("No SPN-type provided")

        if var_types != None:
            self.var_types = var_types
        else:
            var_types = self.var_types

        df = self.data.copy()
        # Exchange all object columns for their codes as SPFLOW cannot deal with Strings
        for key, value in self._categorical_variables.items():
            df[key] = value['categorical'].codes

        self._nameToVarType = var_types

        # Check if variable types are given
        if self._nameToVarType is None:
            raise ValueError("missing argument 'var_types'")

        self._initial_names = self.names.copy()
        self._initial_names_count = len(self._initial_names)
        self._initial_names_to_index = {
            self._initial_names[i]: i
            for i in range(self._initial_names_count)
        }

        # Initialize _state_mask with np.nan
        self._state_mask = np.array([
            np.nan for i in self._initial_names
        ]).reshape(-1, self._initial_names_count).astype(float)

        # Initialize _condition with np.nan
        self._condition = np.repeat(np.nan, self._initial_names_count).reshape(
            -1, self._initial_names_count).astype(float)

        self._marginalized = set()
        self._conditioned = set()

        try:
            var_types = [self._nameToVarType[name] for name in self.names]
        except KeyError as err:
            raise ValueError(
                'missing var type information for dimension: {}.'.format(
                    err.args[0]))

        if self._spn_type == 'spn':
            context = Context(parametric_types=var_types).add_domains(
                df.values)
            self._spn = learn_parametric(df.values, context)

        elif self._spn_type == 'mspn':
            context = Context(meta_types=var_types).add_domains(df.values)
            self._spn = learn_mspn(df.values, context)
        else:
            raise Exception("Type of SPN not known: " + self._spn_type)

        # TODO: DEBUG OUTPUT for NIPS2020
        if self._spn:
            plot_spn(self._spn,
                     fname=Path(
                         f"../../bin/experiments/spn_graphs/{self.name}.pdf"))
            plot_spn_to_svg(
                self._spn,
                fname=Path(
                    f"../../bin/experiments/spn_graphs/{self.name}.svg"))
        return self._unbound_updater,
예제 #5
0
        dataOut = blocked_images[i + 1]

        # assert data.shape[1] == dataIn.shape[1] + dataOut.shape[1], 'invalid column size'
        # assert data.shape[0] == dataIn.shape[0] == dataOut.shape[0], 'invalid row size'

        ds_context = Context(meta_types=[MetaType.DISCRETE] * dataOut.shape[1])
        ds_context.add_domains(dataOut)
        ds_context.parametric_types = [Conditional_Poisson] * dataOut.shape[1]

        scope = list(range(dataOut.shape[1]))
        cspn = learn_conditional(np.concatenate((dataOut, dataIn), axis=1),
                                 ds_context,
                                 scope,
                                 min_instances_slice=0.3 * len(data))
        cspn_army.append(cspn)
        plot_spn(cspn, "basicspn%s.png" % i)

        fileObject = open(path + "/cspn_block%s" % i, "wb")
        pickle.dump(cspn, fileObject)
        fileObject.close()

    from spn.structure.leaves.conditional.Sampling import add_conditional_sampling_support

    add_conditional_inference_support()
    add_conditional_sampling_support()

    from numpy.random.mtrand import RandomState
    from spn.algorithms.Sampling import sample_instances

    num_samples = 30
    num_half_image_pixels = downscaleto * downscaleto // 4
g1 = Gaussian(1, 1, scope=3)
g2 = Gaussian(1, 1, scope=5)
g3 = Gaussian(1, 1, scope=1)
g4 = Gaussian(1, 1, scope=4)
g5 = Gaussian(1, 1, scope=2)
g6 = Gaussian(1, 1, scope=6)

p11 = Product([g1, g2])
p12 = Product([g1, g2])
p13 = Product([g1, g2])
p21 = Product([g3, g4])
p22 = Product([g3, g4])
p23 = Product([g3, g4])
p31 = Product([g5, g6])
p32 = Product([g5, g6])
p33 = Product([g5, g6])

s1 = Sum([0.33, 0.33, 0.33], [p11, p12, p13])
s2 = Sum([0.33, 0.33, 0.33], [p21, p22, p23])
s3 = Sum([0.33, 0.33, 0.33], [p31, p32, p33])

root = Product([s1, s2, s3])

assign_ids(root)
rebuild_scopes_bottom_up(root)

import matplotlib.pyplot as plt

fig = plt.figure(figsize=(12, 6), dpi=180)
plot_spn(root, "spn.png")
예제 #7
0
        0]  # data[:, :horizontal_middle, :vertical_middle].reshape(len(data), -1)
    dataOut = blocked_images[
        1]  # data[:, :horizontal_middle, vertical_middle:].reshape(len(data), -1)

    ds_context = Context(meta_types=[MetaType.REAL] * dataOut.shape[1])
    ds_context.add_domains(dataOut)
    ds_context.parametric_types = [Conditional_Poisson] * dataOut.shape[1]

    scope = list(range(dataOut.shape[1]))
    print(np.shape(dataIn), np.shape(dataOut))

    cspn = learn_conditional(np.concatenate((dataOut, dataIn), axis=1),
                             ds_context,
                             scope,
                             min_instances_slice=0.4 * len(data))
    plot_spn(cspn, "basicspn.png")

    # start sampling
    from spn.structure.leaves.conditional.Sampling import add_conditional_sampling_support

    add_conditional_inference_support()
    add_conditional_sampling_support()

    from numpy.random.mtrand import RandomState
    from spn.algorithms.Sampling import sample_instances

    num_samples = 30
    num_half_image_pixels = downscaleto * downscaleto // 4
    samples_placeholder = np.array([[np.nan] * num_half_image_pixels] *
                                   num_samples).reshape(
                                       -1, num_half_image_pixels)
예제 #8
0
'''
Created on March 29, 2018

@author: Alejandro Molina
'''
from spn.leaves.Histograms import Histogram_str_to_spn

from spn.experiments.FPGA.GenerateSPNs import fpga_count_ops
from spn.io.Graphics import plot_spn
from spn.io.Text import str_to_spn

if __name__ == '__main__':
    with open('../experiments/FPGA/spns/NIPS_30/eqq.txt', 'r') as myfile:
        eq = myfile.read()
    with open('../experiments/FPGA/spns/NIPS_30/all_data.txt', 'r') as myfile:
        words = myfile.readline().strip()
        words = words[2:]
        words = words.split(';')

    print(words)

    spn = str_to_spn(eq, words, Histogram_str_to_spn)

    print(spn)

    print(fpga_count_ops(spn))
    plot_spn(spn)


예제 #9
0
 def plot(self, name='images/spn.png'):
     plot_spn(self.spn, name)
예제 #10
0
        0]  #data[:, :horizontal_middle, :vertical_middle].reshape(len(data), -1)
    dataOut = blocked_images[
        1]  #data[:, :horizontal_middle, vertical_middle:].reshape(len(data), -1)

    ds_context = Context(meta_types=[MetaType.REAL] * dataOut.shape[1])
    ds_context.add_domains(dataOut)
    ds_context.parametric_types = [Conditional_Poisson] * dataOut.shape[1]

    scope = list(range(dataOut.shape[1]))
    print(np.shape(dataIn), np.shape(dataOut))

    cspn = learn_conditional(np.concatenate((dataOut, dataIn), axis=1),
                             ds_context,
                             scope,
                             min_instances_slice=0.4 * len(data))
    plot_spn(cspn, 'basicspn.png')

    # start sampling
    from spn.structure.leaves.conditional.Sampling import add_conditional_sampling_support
    add_conditional_inference_support()
    add_conditional_sampling_support()

    from numpy.random.mtrand import RandomState
    from spn.algorithms.Sampling import sample_instances
    num_samples = 30
    num_half_image_pixels = downscaleto * downscaleto // 4
    samples_placeholder = np.array([[np.nan] * num_half_image_pixels] *
                                   num_samples).reshape(
                                       -1, num_half_image_pixels)
    top_left_samples = sample_instances(spn, samples_placeholder,
                                        RandomState(123))
예제 #11
0
    return expectation


if __name__ == '__main__':
    
    data, feature_types = generate_gender_age_data(1000, 0)
    
    #Set identity_numeric=False, if you want to use PWLs
    spn_params = generate_spn_parameters(identity_numeric=True)
    
    root_node = build_spn(data, feature_types, spn_params, np.random.RandomState(1))

    
    from spn.io.Graphics import plot_spn
    plot_spn(root_node, "spn.pdf")
    
    #Import nodes
    from spn.experiments.AQP.leaves.identity.IdentityNumeric import IdentityNumeric
    from spn.structure.leaves.piecewise.PiecewiseLinear import PiecewiseLinear
    from spn.structure.leaves.parametric.Parametric import Categorical
    from spn.structure.Base import Sum, Product
    
    #Import conditions
    from spn.experiments.AQP.Ranges import NominalRange, NumericRange
    
    #Import inference
    from spn.algorithms import Inference
    from spn.algorithms.Inference import sum_likelihood, prod_likelihood
    
    
예제 #12
0
def run_rspn(dataset, num_variables, num_latent_variables, num_latent_values,
             unroll, oSLRAU_params, update_after_no_min_batches, full_update,
             update_leaves, len_sequence_varies):

    data = get_data(dataset)

    from sklearn.model_selection import train_test_split
    train_data, test_data = train_test_split(data,
                                             test_size=0.33,
                                             random_state=42)
    print("train:", len(train_data))
    print("test:", len(test_data))

    rspn = RSPN(num_variables=num_variables,
                num_latent_variables=num_latent_variables,
                num_latent_values=num_latent_values)

    # make first mini_batch from data
    mini_batch_size = 10
    first_mini_batch = train_data[0:mini_batch_size]

    dat = first_mini_batch
    dat = dat[:, 0:num_variables]
    n = dat.shape[1]  # num of variables in each time step
    print(n)
    context = [Gaussian] * n
    ds_context = Context(parametric_types=context).add_domains(
        first_mini_batch[:, 0:num_variables])

    spn, initial_template_spn, top_spn = rspn.build_initial_template(
        first_mini_batch, ds_context, len_sequence_varies)

    plot_spn(spn, 'rspn_initial_spn.pdf')
    plot_spn(initial_template_spn, 'rspn_initial_template_spn.pdf')
    plot_spn(top_spn, 'rspn_top_spn.pdf')

    print(np.mean(rspn.log_likelihood(test_data, unroll, len_sequence_varies)))

    # Determine number of mini batches
    if not len_sequence_varies:
        no_of_minibatches = int(train_data.shape[0] / mini_batch_size)

    else:
        no_of_minibatches = int(len(train_data) / mini_batch_size)

    for i in range(1, no_of_minibatches):
        mini_batch = train_data[i * mini_batch_size:(i + 1) * mini_batch_size]

        update_template = False
        if i % update_after_no_min_batches == 0:
            print(i)
            update_template = True

        template_spn = rspn.learn_rspn(mini_batch, update_template,
                                       oSLRAU_params, unroll, full_update,
                                       update_leaves, len_sequence_varies)

    plot_spn(template_spn, 'rspn_final_template.pdf')
    print(np.mean(rspn.log_likelihood(test_data, unroll, len_sequence_varies)))

    unrolled_rspn_full = rspn.get_unrolled_rspn(rspn.get_len_sequence())
    plot_spn(unrolled_rspn_full, 'rspn_unrolled_full.pdf')

    unrolled_rspn = rspn.get_unrolled_rspn(2)
    plot_spn(unrolled_rspn, 'rspn_unrolled_2.pdf')
예제 #13
0
import matplotlib.pyplot as plt

from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score

# Prepare data
iris = datasets.load_iris()
X_train, X_test, y_train, y_test= train_test_split(iris.data, iris.target, test_size = 0.4, random_state = 42)
train_data_with_labels = np.insert(X_train, obj=X_train.shape[1], values=y_train, axis=1)
test_data_with_labels = np.insert(X_test, obj=X_test.shape[1], values=y_test, axis=1)

# Learn SPN
context = Context(parametric_types=[Gaussian, Gaussian, Gaussian, Gaussian, Categorical]).add_domains(train_data_with_labels)
spn_classification = learn_classifier(train_data_with_labels, context, learn_parametric, 4)

# Plot SPN
plot_spn(spn_classification, 'images/iris_spn.png')

# Predict
true_values = np.array(test_data_with_labels[:,-1])
items_to_predict = test_data_with_labels
items_to_predict[:, 4] = np.nan
predicted_values = mpe(spn_classification, test_data_with_labels)
predicted_labels = predicted_values[:, 4]

acc = accuracy_score(true_values, predicted_labels)
print(acc)


예제 #14
0
                           spn_learn_wrapper=learn_parametric,
                           label_idx=label_idx,
                           min_instances_slice=min_instances_slice,
                           threshold=threshold)

    spn = optimize_tf(spn, train_data)

    duration = time.time() - start_time
    print('\033[1mFinished training after %.3f sec.\033[0m' % duration)

    # Model performance evaluation
    spn_stats = get_structure_stats(spn)
    print(spn_stats)
    stats_file = open(plot_path + "/spn_stats.txt", "w+")
    stats_file.write(spn_stats)
    plot_spn(spn, plot_path + "/spn_struct.pdf")
    correct_test_preds, pred_test_labels = evaluate_spn_performance(spn, train_samples, train_labels, test_samples,
                                                                    test_labels, label_idx, stats_file)
    stats_file.close()

    correct_preds = np.array([test_samples[k] for k in range(0, len(test_samples)) if correct_test_preds[k] != 0])
    wrong_preds = np.array([test_samples[k] for k in range(0, len(test_samples)) if correct_test_preds[k] == 0])

    # Plot predictions
    plt.subplots(figsize=(5, 5))
    plt.scatter(correct_preds[:, 0], correct_preds[:, 1], label="Correctly predicted", c="darkgray", s=10)
    plt.scatter(wrong_preds[:, 0], wrong_preds[:, 1], label="Wrongly predicted", c="darkred", s=10)
    plt.xlabel('x')
    plt.ylabel('y')
    axes = plt.gca()
    axes.set_xlim([0, 128])
예제 #15
0
from spn.algorithms.Marginalization import marginalize
from spn.structure.leaves.parametric.Parametric import Categorical
from spn.structure.Base import Sum, Product
from spn.structure.Base import assign_ids, rebuild_scopes_bottom_up

spn = 0.4 * (Categorical(p=[0.2, 0.8], scope=0) *
             (0.3 * (Categorical(p=[0.3, 0.7], scope=1) *
                     Categorical(p=[0.4, 0.6], scope=2))
            + 0.7 * (Categorical(p=[0.5, 0.5], scope=1) *
                     Categorical(p=[0.6, 0.4], scope=2)))) \
    + 0.6 * (Categorical(p=[0.2, 0.8], scope=0) *
             Categorical(p=[0.3, 0.7], scope=1) *
             Categorical(p=[0.4, 0.6], scope=2))
spn = marginalize(spn, [1,2])
print(to_JSON(spn) + "\n\n\n")
plot_spn(spn, 'spn1.png')

spn2 = Product(children=[Categorical(p=[0.5, 0.5], scope=0), Categorical(p=[0.2, 0.8], scope=2)])
print(to_JSON(spn2))
assign_ids(spn2)
rebuild_scopes_bottom_up(spn2)
plot_spn(spn2, 'basicspn.png')
print(to_JSON(spn2))

def getSpn1():
    spn = 0.4 * (Categorical(p=[0.2, 0.8], scope=0) *
             (0.3 * (Categorical(p=[0.3, 0.7], scope=1) *
                     Categorical(p=[0.4, 0.6], scope=2))
            + 0.7 * (Categorical(p=[0.5, 0.5], scope=1) *
                     Categorical(p=[0.6, 0.4], scope=2)))) \
    + 0.6 * (Categorical(p=[0.2, 0.8], scope=0) *
예제 #16
0
        dataOut = blocked_images[i + 1]

        # assert data.shape[1] == dataIn.shape[1] + dataOut.shape[1], 'invalid column size'
        # assert data.shape[0] == dataIn.shape[0] == dataOut.shape[0], 'invalid row size'

        ds_context = Context(meta_types=[MetaType.DISCRETE] * dataOut.shape[1])
        ds_context.add_domains(dataOut)
        ds_context.parametric_types = [Conditional_Poisson] * dataOut.shape[1]

        scope = list(range(dataOut.shape[1]))
        cspn = learn_conditional(np.concatenate((dataOut, dataIn), axis=1),
                                 ds_context,
                                 scope,
                                 min_instances_slice=0.3 * len(data))
        cspn_army.append(cspn)
        plot_spn(cspn, 'basicspn%s.png' % i)

        fileObject = open(path + "/cspn_block%s" % i, 'wb')
        pickle.dump(cspn, fileObject)
        fileObject.close()

    from spn.structure.leaves.conditional.Sampling import add_conditional_sampling_support
    add_conditional_inference_support()
    add_conditional_sampling_support()

    from numpy.random.mtrand import RandomState
    from spn.algorithms.Sampling import sample_instances
    num_samples = 30
    num_half_image_pixels = downscaleto * downscaleto // 4
    # block_samples_spn = sample_instances(spn, np.array([[np.nan] * num_half_image_pixels] * num_samples).reshape(-1, num_half_image_pixels), RandomState(123))
    annotation_spn = sample_instances(