RewardFunctionWrapper reward
Bases: Reward
Wrap a per-sample function as a Reward; bare functions in compile need none.
You can use this to quickly build a reward from a function. The function needs
to have the signature fn(y_true, y_pred) (add an x argument to also get
the program's inputs during fit / evaluate) and to be declared with
async def, since reward functions are awaited: a synchronous one raises a
TypeError here rather than failing later inside the training loop.
Example:
async def my_reward(y_true, y_pred):
# ...
return reward
program.compile(
reward=synalinks.rewards.RewardFunctionWrapper(fn=my_reward),
optimizer=synalinks.optimizers.RandomFewShot(),
)
Wrapping is optional: compile(reward=my_reward) accepts the bare function
and wraps it for you, naming the reward after the function. Reach for this
class explicitly when you need masks, a custom reduction, or extra keyword
arguments forwarded to fn:
async def length_under(y_true, y_pred, limit=100):
return 1.0 if len(y_pred.get("answer")) < limit else 0.0
program.compile(
reward=synalinks.rewards.RewardFunctionWrapper(
fn=length_under,
limit=200,
in_mask=["answer"],
),
optimizer=synalinks.optimizers.RandomFewShot(),
)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fn
|
callable
|
Async reward function to wrap, with signature
|
required |
reduction
|
str
|
Optional. How the per-sample rewards of a batch are
reduced, one of |
'mean'
|
name
|
str
|
Optional. string name of the reward instance. |
None
|
in_mask
|
list
|
Optional. list of keys to keep to compute the reward. |
None
|
out_mask
|
list
|
Optional. list of keys to remove to compute the reward. |
None
|
in_mask_pattern
|
str
|
Optional. Regex pattern; fields whose names match
are kept (combined with |
None
|
out_mask_pattern
|
str
|
Optional. Regex pattern; fields whose names match
are dropped (combined with |
None
|
**kwargs
|
keyword arguments
|
Keyword arguments to pass on to |
{}
|
Source code in synalinks/src/rewards/reward_wrappers.py
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 | |