RewardFunctionWrapper reward
Bases: Reward
Wrap a stateless function into a Reward.
You can use this to quickly build a reward from a function. The function needs
to have the signature fn(y_true, y_pred) 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 |
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
13 14 15 16 17 18 19 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 | |