本文整理匯總了Python中function.Function.make_float_function方法的典型用法代碼示例。如果您正苦於以下問題:Python Function.make_float_function方法的具體用法?Python Function.make_float_function怎麽用?Python Function.make_float_function使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類function.Function
的用法示例。
在下文中一共展示了Function.make_float_function方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: mutate_float
# 需要導入模塊: from function import Function [as 別名]
# 或者: from function.Function import make_float_function [as 別名]
def mutate_float(node, score_tree, eps=1e-1):
""" Takes a Node object and optimizes floats greedily.
Returns a new tree.
Args:
node: Node object to operate on
score_tree: function that takes a tree
and returns a fitness (as float)
eps: learning rate (as float) (default=1e-1)
Returns:
Node object
"""
# Copy the tree
new_tree = node.deepcopy()
# Find all floating leaves
floats = new_tree.all_floats()
if floats == []:
return None
has_changed = False
for f in floats:
value = f.func.func()
left_value = value - eps
right_value = value + eps
func = f.func
left_func = Function.make_float_function(left_value)
left_func = Function(left_func, 0, str(left_value))
right_func = Function.make_float_function(right_value)
right_func = Function(right_func, 0, str(right_value))
score = score_tree(new_tree)
f.func = left_func
left_score = score_tree(new_tree)
f.func = right_func
right_score = score_tree(new_tree)
max_ = max(left_score, score, right_score)
if abs(max_ - left_score) < 1e-10:
f.func = left_func
has_changed = True
elif abs(max_ - right_score) < 1e-10:
f.func = right_func
has_changed = True
else:
f.func = func
if not has_changed:
return None
return new_tree