Esempio n. 1
0
def get_model(num_vars, logspace_accumulators, hard_em_backward,
              return_weighted_child_logits):

    init_sum0 = np.array([[[[0.6, 0.1, 0.1, 0.2], [0.1, 0.1, 0.6, 0.2]]],
                          [[[0.4, 0.3, 0.2, 0.1],
                            [0.1, 0.2, 0.3, 0.4]]]]).transpose((0, 1, 3, 2))

    init_sum1 = np.array([0.25, 0.15, 0.35, 0.25])

    sum_product_stack = keras.models.Sequential([
        DenseProduct(num_factors=2),
        DenseSum(num_sums=2,
                 logspace_accumulators=False,
                 backprop_mode=BackpropMode.HARD_EM,
                 accumulator_initializer=initializers.Constant(init_sum0)),
        DenseProduct(num_factors=2),
        Undecompose(),
        RootSum(logspace_accumulators=False,
                backprop_mode=BackpropMode.HARD_EM,
                accumulator_initializer=initializers.Constant(init_sum1)),
    ])

    # Use helper function to build the actual SPN
    return build_ratspn(num_vars=num_vars,
                        decomposer=RandomDecompositions(
                            num_decomps=1, permutations=[[0, 1, 2, 3]]),
                        leaf=IndicatorLeaf(num_components=2),
                        sum_product_stack=sum_product_stack), sum_product_stack
    def _build_depthwise(self, input_shape: Tuple[Optional[int], ...]) -> None:
        _, num_scopes_vertical, num_scopes_horizontal, num_channels_in = input_shape

        if num_scopes_vertical is None:
            raise ValueError(
                "Cannot build Conv2DProduct: unknown vertical dimension")
        if num_scopes_horizontal is None:
            raise ValueError(
                "Cannot build Conv2DProduct: unknown horizontal dimension")
        if num_channels_in is None:
            raise ValueError(
                "Cannot build Conv2DProduct: unknown channel dimension")

        self._spatial_dim_sizes = num_scopes_vertical, num_scopes_horizontal
        self.num_channels = num_channels_in

        sparse_kernels = self._create_sparse_kernels(1, 1)

        onehot_kernels = self._sparse_kernels_to_onehot(sparse_kernels, 1)

        self._onehot_kernels = self.add_weight(
            "onehot_kernel",
            initializer=initializers.Constant(onehot_kernels),
            trainable=False,
            shape=onehot_kernels.shape,
        )
Esempio n. 3
0
    def _build_depthwise(self, input_shape):
        num_batch, num_scopes_vertical, num_scopes_horizontal, num_channels_in = input_shape

        self._spatial_dim_sizes = num_scopes_vertical, num_scopes_horizontal
        self.num_channels = num_channels_in

        sparse_kernels = self._create_sparse_kernels(1, 1)

        onehot_kernels = self._sparse_kernels_to_onehot(sparse_kernels, 1)

        self._onehot_kernels = self.add_weight(
            "onehot_kernel",
            initializer=initializers.Constant(onehot_kernels),
            trainable=False,
            shape=onehot_kernels.shape)
Esempio n. 4
0
    def _build_onehot_kernels(self, input_shape):
        num_batch, num_scopes_vertical, num_scopes_horizontal, num_channels_in = input_shape

        self._spatial_dim_sizes = num_scopes_vertical, num_scopes_horizontal

        if self.num_channels is None:
            self.num_channels = int(num_channels_in**np.prod(self.kernel_size))

        sparse_kernels = self._create_sparse_kernels(num_channels_in,
                                                     self.num_channels)

        onehot_kernels = self._sparse_kernels_to_onehot(
            sparse_kernels, num_channels_in)

        self._onehot_kernels = self.add_weight(
            "onehot_kernel",
            initializer=initializers.Constant(onehot_kernels),
            trainable=False,
            shape=onehot_kernels.shape)