Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
parameters: list
A list of ProbabilisticModels
Returns
-------
InputConnector
"""
if isinstance(parameters, list):
unnested_parameters = []
parameters_count = 0
for item in parameters:
input_parameters_from_item = item
if isinstance(item, list):
input_parameters_from_item = InputConnector.from_list(item)
elif isinstance(item, (Hyperparameter, ProbabilisticModel)):
input_parameters_from_item = InputConnector.from_model(item)
elif isinstance(item, Number):
input_parameters_from_item = InputConnector.from_number(item)
elif not isinstance(item, InputConnector):
raise TypeError('Unsupported type.')
unnested_parameters.append(input_parameters_from_item)
parameters_count += input_parameters_from_item.get_parameter_count()
# here, unnested_parameters is a list of InputConnector and parameters_count hold the total number of
# parameters in this list
input_parameters = InputConnector(parameters_count)
index = 0
for param in unnested_parameters:
for pi in range(0, param.get_parameter_count()):
input_parameters.set(index, param._models[pi], param._model_indices[pi])
def from_number(number):
"""
Convenient initializer that converts a number to a hyperparameter input parameter.
Parameters
----------
number
Returns
-------
InputConnector
"""
if isinstance(number, Number):
input_parameters = InputConnector(1)
input_parameters.set(0, Hyperparameter(number), 0)
return input_parameters
else:
raise TypeError('Unsupported type.')
if is_root:
models = self.model
for model in models:
# New parameters should only be set in case we are not at the root
if not is_root and not isinstance(model, ModelResultingFromOperation):
#new_output_values = np.array(parameters[index:index + model.get_output_dimension()])
new_output_values = np.array(parameters[index]).reshape(-1,)
if not model.set_output_values(new_output_values):
return [False, index]
index += 1 #model.get_output_dimension()
model.visited = True
# New parameters for all parents are set using a depth-first search
for parent in model.get_input_models():
if not parent.visited and not isinstance(parent, Hyperparameter):
is_set, index = self.set_parameters(parameters, models=[parent], index=index, is_root=False)
if not is_set:
# At the end of the algorithm, are flags are reset such that new methods can act on the graph freely
if is_root:
self._reset_flags()
return [False, index]
model.visited = True
# At the end of the algorithm, are flags are reset such that new methods can act on the graph freely
if(is_root):
self._reset_flags()
return [True, index]
Sets for an input parameter index the input model and the model index to use.
For convenience, model can also be a number, which is automatically casted to a hyper parameter.
Parameters
----------
index: int
Index of the input parameter to be set.
model: ProbabilisticModel, Number
The model to be set for the input parameter.
model_index: int
Index of model's output to be used as input parameter.
"""
if isinstance(model, Number):
model = Hyperparameter(model)
self._models[index] = model
self._model_indices[index] = model_index
if (self._models != None):
self._all_indices_specified = True
is_root: boolean
Specifies whether the current list of models is the list of overall root models
index: integer
The current index in depth-first search.
Returns
-------
list
The first entry corresponds to the mapping of the root model, as well as all its parents. The second entry corresponds to the next index in depth-first search.
"""
# Implement a dfs to discover all nodes of the model
mapping = []
for model in models:
if(not(model.visited) and not(isinstance(model, Hyperparameter))):
model.visited = True
# Only parameters that are neither root nor Hyperparameters are included in the mapping
if(not(is_root) and not(isinstance(model, ModelResultingFromOperation))):
for i in range(model.get_output_dimension()):
mapping.append((model, index))
index+=1
for parent in model.get_input_models():
parent_mapping, index = self.get_mapping([parent], is_root= False, index=index)
for element in parent_mapping:
mapping.append(element)
# Reset the flags of all models
if(is_root):
self._reset_flags()
def _check_parameters_at_initialization(self, parameters):
if(len(parameters)!=2):
raise IndexError('Gaussian should have exactly two input parameters.')
variance, index = parameters[1]
if(isinstance(variance, Hyperparameter) and variance.fixed_values[0]<0):
raise ValueError('Variance has to be larger than 0.')