def test_log_vector_histogram(): # Construct a minimal SPN. h1 = Histogram([0., 1., 2.], [0.25, 0.75], [1, 1], scope=0) h2 = Histogram([0., 1., 2.], [0.45, 0.55], [1, 1], scope=1) h3 = Histogram([0., 1., 2.], [0.33, 0.67], [1, 1], scope=0) h4 = Histogram([0., 1., 2.], [0.875, 0.125], [1, 1], scope=1) p0 = Product(children=[h1, h2]) p1 = Product(children=[h3, h4]) spn = Sum([0.3, 0.7], [p0, p1]) inputs = np.column_stack(( np.random.randint(2, size=30), np.random.randint(2, size=30), )).astype("float64") if not CPUCompiler.isVectorizationSupported(): print("Test not supported by the compiler installation") return 0 # Execute the compiled Kernel. results = CPUCompiler(maxTaskSize=5).log_likelihood(spn, inputs, supportMarginal=False) # Compute the reference results using the inference from SPFlow. reference = log_likelihood(spn, inputs) reference = reference.reshape(30) # Check the computation results against the reference # Check in normal space if log-results are not very close to each other. assert np.all(np.isclose(results, reference)) or np.all(np.isclose(np.exp(results), np.exp(reference)))
def test_log_vector_categorical(): # Construct a minimal SPN c1 = Categorical(p=[0.35, 0.55, 0.1], scope=0) c2 = Categorical(p=[0.25, 0.625, 0.125], scope=1) c3 = Categorical(p=[0.5, 0.2, 0.3], scope=2) c4 = Categorical(p=[0.6, 0.15, 0.25], scope=3) c5 = Categorical(p=[0.7, 0.11, 0.19], scope=4) c6 = Categorical(p=[0.8, 0.14, 0.06], scope=5) p = Product(children=[c1, c2, c3, c4, c5, c6]) # Randomly sample input values. inputs = np.column_stack(( np.random.randint(3, size=30), np.random.randint(3, size=30), np.random.randint(3, size=30), np.random.randint(3, size=30), np.random.randint(3, size=30), np.random.randint(3, size=30), )).astype("int32") if not CPUCompiler.isVectorizationSupported(): print("Test not supported by the compiler installation") return 0 # Execute the compiled Kernel. results = CPUCompiler().log_likelihood(p, inputs, supportMarginal=False) # Compute the reference results using the inference from SPFlow. reference = log_likelihood(p, inputs) reference = reference.reshape(30) # Check the computation results against the reference # Check in normal space if log-results are not very close to each other. assert np.all(np.isclose(results, reference)) or np.all(np.isclose(np.exp(results), np.exp(reference)))
def test_cpu_gaussian(): # Construct a minimal SPN using two Gaussian leaves. g1 = Gaussian(mean=0.5, stdev=1, scope=0) g2 = Gaussian(mean=0.125, stdev=0.25, scope=1) g3 = Gaussian(mean=0.345, stdev=0.24, scope=2) g4 = Gaussian(mean=0.456, stdev=0.1, scope=3) g5 = Gaussian(mean=0.94, stdev=0.48, scope=4) g6 = Gaussian(mean=0.56, stdev=0.42, scope=5) g7 = Gaussian(mean=0.76, stdev=0.14, scope=6) g8 = Gaussian(mean=0.32, stdev=0.8, scope=7) g9 = Gaussian(mean=0.58, stdev=0.9, scope=8) g10 = Gaussian(mean=0.14, stdev=0.2, scope=9) p = Product(children=[g1, g2, g3, g4, g5, g6, g7, g8, g9, g10]) # Randomly sample input values from the two Gaussian (normal) distributions. inputs = np.column_stack( (np.random.normal(0.5, 1, 30), np.random.normal(0.125, 0.25, 30), np.random.normal(0.345, 0.24, 30), np.random.normal(0.456, 0.1, 30), np.random.normal(0.94, 0.48, 30), np.random.normal(0.56, 0.42, 30), np.random.normal(0.76, 0.14, 30), np.random.normal(0.32, 0.8, 30), np.random.normal(0.58, 0.9, 30), np.random.normal(0.14, 0.2, 30))).astype("float64") # Insert some NaN in random places into the input data. inputs.ravel()[np.random.choice(inputs.size, 10, replace=False)] = np.nan if not CPUCompiler.isVectorizationSupported(): print("Test not supported by the compiler installation") return 0 # Execute the compiled Kernel. results = CPUCompiler(computeInLogSpace=False, vectorize=False).log_likelihood(p, inputs, supportMarginal=True, batchSize=10) # Compute the reference results using the inference from SPFlow. reference = log_likelihood(p, inputs) reference = reference.reshape(30) # Check the computation results against the reference # Check in normal space if log-results are not very close to each other. assert np.all(np.isclose(results, reference)) or np.all( np.isclose(np.exp(results), np.exp(reference)))
def test_vector_fashion_mnist(): if not CPUCompiler.isVectorizationSupported(): print("Test not supported by the compiler installation") return 0 # Locate test resources located in same directory as this script. scriptPath = os.path.realpath(os.path.dirname(__file__)) # Deserialize model model = BinaryDeserializer( os.path.join( scriptPath, "nltcs_100_200_2_10_8_8_1_True.bin")).deserialize_from_file() spn = model.root inputs = np.genfromtxt(os.path.join(scriptPath, "input.csv"), delimiter=",", dtype="float64") reference = np.genfromtxt(os.path.join( scriptPath, "nltcs_100_200_2_10_8_8_1_True_output.csv"), delimiter=",", dtype="float64") reference = reference.reshape(10000) # Compile the kernel. compiler = CPUCompiler(vectorize=True, computeInLogSpace=True) kernel = compiler.compile_ll(spn=spn, batchSize=1, supportMarginal=False) # Execute the compiled Kernel. time_sum = 0 for i in range(len(reference)): # Check the computation results against the reference start = time.time() result = compiler.execute(kernel, inputs=np.array([inputs[i]])) time_sum = time_sum + time.time() - start if not np.isclose(result, reference[i]): print( f"\nevaluation #{i} failed: result: {result[0]:16.8f}, reference: {reference[i]:16.8f}" ) raise AssertionError() print(f"\nExecution of {len(reference)} samples took {time_sum} seconds.")
# This file is part of the SPNC project under the Apache License v2.0 by the # Embedded Systems and Applications Group, TU Darmstadt. # For the full copyright and license information, please view the LICENSE # file that was distributed with this source code. # SPDX-License-Identifier: Apache-2.0 # ============================================================================== import numpy as np import os import pytest import time from spnc.cpu import CPUCompiler from xspn.serialization.binary.BinarySerialization import BinaryDeserializer @pytest.mark.skipif(not CPUCompiler.isVectorizationSupported(), reason="CPU vectorization not supported") def test_vector_slp_fashion_mnist(): # Locate test resources located in same directory as this script. scriptPath = os.path.realpath(os.path.dirname(__file__)) # Deserialize model model = BinaryDeserializer( os.path.join(scriptPath, "fashion_mnist_200_100_4_5_10_9_1_True.bin") ).deserialize_from_file() spn = model.root inputs = np.genfromtxt(os.path.join(scriptPath, "input.csv"), delimiter=",", dtype="float64") reference = np.genfromtxt(os.path.join(
# file that was distributed with this source code. # SPDX-License-Identifier: Apache-2.0 # ============================================================================== import numpy as np from spn.structure.Base import Product, Sum from spn.structure.leaves.histogram.Histograms import Histogram from spn.algorithms.Inference import log_likelihood from spnc.cpu import CPUCompiler import pytest @pytest.mark.skipif(not CPUCompiler.isVectorizationSupported(), reason="CPU vectorization not supported") def test_log_vector_histogram(): # Construct a minimal SPN. h1 = Histogram([0., 1., 2.], [0.25, 0.75], [1, 1], scope=0) h2 = Histogram([0., 1., 2.], [0.45, 0.55], [1, 1], scope=1) h3 = Histogram([0., 1., 2.], [0.33, 0.67], [1, 1], scope=0) h4 = Histogram([0., 1., 2.], [0.875, 0.125], [1, 1], scope=1) p0 = Product(children=[h1, h2]) p1 = Product(children=[h3, h4]) spn = Sum([0.3, 0.7], [p0, p1]) inputs = np.column_stack(( np.random.randint(2, size=30), np.random.randint(2, size=30), )).astype("float64")