def _compute_scope(self, *value_scopes): if not self._values: raise StructureError("%s is missing input values." % self) value_scopes = self._gather_input_scopes(*value_scopes) if self._num_prods == 1: return [Scope.merge_scopes(chain.from_iterable(value_scopes))] value_scopes_list = [Scope.merge_scopes(pvs) for pvs in product(*[vs for vs in value_scopes])] return value_scopes_list
def _compute_valid(self, weight_scopes, latent_indicators_scopes, *value_scopes): # If already invalid, return None if (any(s is None for s in value_scopes) or (self._latent_indicators and latent_indicators_scopes is None)): return None flat_value_scopes, latent_indicators_scopes_, *value_scopes_ = self._get_flat_value_scopes( weight_scopes, latent_indicators_scopes, *value_scopes) # IndicatorLeaf if self._latent_indicators: # Verify number of IndicatorLeaf if len(latent_indicators_scopes_ ) != len(flat_value_scopes) * self._num_sums: raise StructureError( "Number of IndicatorLeaf (%s) and values (%s) does " "not match for %s" % (len(latent_indicators_scopes_), len(flat_value_scopes) * self._num_sums, self)) # Check if scope of all IndicatorLeaf is just one and the same variable if len(Scope.merge_scopes( latent_indicators_scopes_)) > self._num_sums: return None # Check sum for completeness wrt values first_scope = flat_value_scopes[0] if any(s != first_scope for s in flat_value_scopes[1:]): self.info("%s is not complete with input value scopes %s", self, flat_value_scopes) return None return self._compute_scope(weight_scopes, latent_indicators_scopes, *value_scopes)
def _compute_valid(self, weight_scopes, ivs_scopes, *value_scopes): if not self._values: raise StructureError("%s is missing input values" % self) _, ivs_scopes_, *value_scopes_ = self._gather_input_scopes(weight_scopes, ivs_scopes, *value_scopes) # If already invalid, return None if (any(s is None for s in value_scopes_) or (self._ivs and ivs_scopes_ is None)): return None flat_value_scopes = list(chain.from_iterable(value_scopes_)) # IVs if self._ivs: # Verify number of IVs if len(ivs_scopes_) != len(flat_value_scopes): raise StructureError("Number of IVs (%s) and values (%s) does " "not match for %s" % (len(ivs_scopes_), len(flat_value_scopes), self)) # Check if scope of all IVs is just one and the same variable if len(Scope.merge_scopes(ivs_scopes_)) > 1: return None # Check sum for completeness wrt values first_scope = flat_value_scopes[0] if any(s != first_scope for s in flat_value_scopes[1:]): self.__info("%s is not complete with input value scopes %s", self, flat_value_scopes) return None return self._compute_scope(weight_scopes, ivs_scopes, *value_scopes)
def _compute_valid(self, weight_scopes, ivs_scopes, *value_scopes): flat_value_scopes, ivs_scopes_, *value_scopes_ = self._get_flat_value_scopes( weight_scopes, ivs_scopes, *value_scopes) # If already invalid, return None if (any(s is None for s in value_scopes_) or (self._ivs and ivs_scopes_ is None)): return None # Split the flat value scopes based on value input sizes split_indices = np.cumsum(self._sum_sizes)[:-1] # IVs if self._ivs: # Verify number of IVs if len(ivs_scopes_) != len(flat_value_scopes): raise StructureError( "Number of IVs (%s) and values (%s) does " "not match for %s" % (len(ivs_scopes_), len(flat_value_scopes), self)) # Go over IVs involved for each sum. Scope size should be exactly one for iv_scopes_for_sum in np.split(ivs_scopes_, split_indices): if len(Scope.merge_scopes(iv_scopes_for_sum)) != 1: return None # Go over value input scopes for each sum being modeled. Within a single sum, the scope of # all the inputs should be the same for scope_slice in np.split(flat_value_scopes, split_indices): first_scope = scope_slice[0] if any(s != first_scope for s in scope_slice[1:]): self.info("%s is not complete with input value scopes %s", self, flat_value_scopes) return None return self._compute_scope(weight_scopes, ivs_scopes, *value_scopes)
def _compute_scope(self, weight_scopes, ivs_scopes, *value_scopes): if not self._values: raise StructureError("%s is missing input values" % self) _, ivs_scopes, *value_scopes = self._gather_input_scopes(weight_scopes, ivs_scopes, *value_scopes) flat_value_scopes = list(chain.from_iterable(value_scopes)) if self._ivs: flat_value_scopes.extend(ivs_scopes) return [Scope.merge_scopes(flat_value_scopes)]
def _compute_scope(self, *value_scopes): if not self._values: raise StructureError("%s is missing input values." % self) # Gather and flatten value scopes flat_value_scopes = list(chain.from_iterable(self._gather_input_scopes( *value_scopes))) # Divide gathered and flattened value scopes into sublists, one per # modeled product op. prod_input_sizes = np.cumsum(np.array(self._prod_input_sizes)).tolist() prod_input_sizes.insert(0, 0) value_scopes_lists = [flat_value_scopes[start:stop] for start, stop in zip(prod_input_sizes[:-1], prod_input_sizes[1:])] return [Scope.merge_scopes(vsl) for vsl in value_scopes_lists]
def _compute_scope(self, weight_scopes, ivs_scopes, *value_scopes): flat_value_scopes, ivs_scopes, *value_scopes = self._get_flat_value_scopes( weight_scopes, ivs_scopes, *value_scopes) if self._ivs: sublist_size = int(len(ivs_scopes) / self._num_sums) # Divide gathered ivs scopes into sublists, one per modelled Sum node. ivs_scopes_sublists = [ ivs_scopes[i:i + sublist_size] for i in range(0, len(ivs_scopes), sublist_size) ] return [ Scope.merge_scopes( flat_value_scopes + ivs_scopes_sublists[i] if self._ivs else flat_value_scopes) for i in range(self._num_sums) ]
def _compute_scope(self, weight_scopes, ivs_scopes, *value_scopes): flat_value_scopes, ivs_scopes, *value_scopes = self._get_flat_value_scopes( weight_scopes, ivs_scopes, *value_scopes) split_indices = np.cumsum(self._sum_sizes)[:-1] # Divide gathered value scopes into sublists, one per modelled Sum node value_scopes_sublists = [ arr.tolist() for arr in np.split(flat_value_scopes, split_indices) ] if self._ivs: # Divide gathered ivs scopes into sublists, one per modelled Sum node ivs_scopes_sublists = [ arr.tolist() for arr in np.split(ivs_scopes, split_indices) ] # Add respective ivs scope to value scope list of each Sum node for val, ivs in zip(value_scopes_sublists, ivs_scopes_sublists): val.extend(ivs) return [ Scope.merge_scopes(val_scope) for val_scope in value_scopes_sublists ]
def _compute_scope(self, *value_scopes): if not self._values: raise StructureError("%s is missing input values." % self) value_scopes = self._gather_input_scopes(*value_scopes) return [Scope.merge_scopes(chain.from_iterable(value_scopes))]
def _compute_scope(self, *value_scopes, check_valid=False): flat_value_scopes = self._gather_input_scopes(*value_scopes) value_scopes_grid = [ np.asarray(vs).reshape(self._spatial_dim_sizes + [-1]) for vs in flat_value_scopes ] value_scopes_concat = np.concatenate(value_scopes_grid, axis=2) dilate = self._dilation_rate kernel_size = self._kernel_size grid_dims = self._spatial_dim_sizes strides = self._strides input_channels = self._num_input_channels() pad_left, pad_right, pad_top, pad_bottom = self.pad_sizes() if any(p != 0 for p in [pad_right, pad_left, pad_top, pad_bottom]): padded_value_scopes_concat = np.empty( (pad_top + grid_dims[0] + pad_bottom, pad_left + grid_dims[1] + pad_right, input_channels), dtype=Scope) # Pad with empty scopes empty_scope = Scope.merge_scopes([]) padded_value_scopes_concat[:, :pad_left] = empty_scope padded_value_scopes_concat[:pad_top, :] = empty_scope padded_value_scopes_concat[-pad_bottom:, :] = empty_scope padded_value_scopes_concat[:, -pad_right:] = empty_scope padded_value_scopes_concat[ pad_top:pad_top + value_scopes_concat.shape[0], pad_left:pad_left + value_scopes_concat.shape[0]] = value_scopes_concat value_scopes_concat = padded_value_scopes_concat scope_list = [] kernel_size0, kernel_size1 = self._effective_kernel_size() # Reset grid dims as we might have padded the scopes grid_dims = value_scopes_concat.shape[:2] for row in range(0, grid_dims[0] - kernel_size0 + 1, strides[0]): row_indices = list(range(row, row + kernel_size0, dilate[0])) for col in range(0, grid_dims[1] - kernel_size1 + 1, strides[1]): col_indices = list(range(col, col + kernel_size1, dilate[1])) for channel in range(self._num_channels): single_scope = [] for im_row, kernel_row in zip(row_indices, range(kernel_size[0])): for im_col, kernel_col in zip(col_indices, range(kernel_size[1])): single_scope.append(value_scopes_concat[ im_row, im_col, self._sparse_connections[kernel_row, kernel_col, channel]]) # Ensure valid if check_valid: for sc1, sc2 in itertools.combinations( single_scope, 2): if sc1 & sc2: # Invalid if intersection not empty self.logger.warn( "{} is not decomposable".format(self)) return None scope_list.append(Scope.merge_scopes(single_scope)) return scope_list