popoto.fields.td_value_field¶
popoto.fields.td_value_field
¶
TDValueField — a Decimal column with an atomic TD(0) update.
Stores a learned value (a Q-value in reinforcement-learning terms) in the
model hash exactly as :class:~popoto.fields.shortcuts.DecimalField does, and
adds one operation the ordinary save path cannot express: an atomic
read-modify-write of that single hash field under the temporal-difference
rule
Q(s,a) <- Q(s,a) + alpha * [r + gamma * max Q(s',a') - Q(s,a)]
The read and the write happen inside one Lua script, so concurrent updates to
the same instance serialize at the server rather than racing through a
client-side HGET / compute / HSET round trip.
This field was extracted from popoto.recipes.policy_cache, which owned the
script directly (issue #647, part of the #630 series). A recipe composes
primitives; it should not own one.
Storage
The value lives in the model hash under the field's own name, encoded as
the __Decimal__ tagged dict every DecimalField uses. Nothing is
stored outside the hash. This matters beyond tidiness:
:class:~popoto.fields.decaying_sorted_field.DecayingSortedField reads a
base_score_field straight out of the member's hash in Lua and falls
back to 1.0 for any encoding it does not recognize, so the encoding
must stay exactly what DecimalField writes.
Valkey Compatibility
Core commands only — HGET, HSET, and cmsgpack inside the
script. No Redis-module commands (no BF., CMS., TOPK.,
TS., JSON.), so this field works identically on Redis and Valkey.
Example
from decimal import Decimal from popoto.fields.td_value_field import TDValueField
class Policy(Model): agent_id = KeyField() q_value = TDValueField(default=Decimal("0"))
policy = Policy(agent_id="agent-1") policy.save() td_error = TDValueField.td_update(policy, "q_value", reward=1.0)
TDValueField
¶
Bases: DecimalField
A Decimal column supporting atomic temporal-difference updates.
Declares no storage of its own and takes no extra constructor arguments —
it is a DecimalField in every respect except that it carries the
:meth:td_update operation. The learning rate and discount factor are
call-time arguments of that method, not field-level configuration: they are
experimental tuning constants (see
:class:popoto.fields.constants.Defaults), and field-declared defaults
would let two processes disagree about the gain schedule with no runtime
detection.
Note
The field must be named q_value on the model for :meth:td_update
to work; see :data:TD_UPDATE_HASH_FIELD.
Source code in src/popoto/fields/td_value_field.py
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 | |
td_update(model_instance, field_name, *, reward, max_future_q=0.0, alpha=Defaults.TD_ALPHA, gamma=Defaults.TD_GAMMA, pipeline=None)
classmethod
¶
Apply one TD(0) update to this instance's value, atomically.
The read-modify-write happens server-side in a single script invocation, so two concurrent calls on the same instance serialize rather than losing an update.
Note
The in-memory attribute is not refreshed. The script returns the TD error only; it does not return the new value, and neither recomputing it client-side nor re-reading it is acceptable (the first duplicates the arithmetic and encoding this field exists to own, the second adds a command). Reload the instance to observe the new value.
Note
When pipeline is given the update is queued on it and this
method returns None; the TD error is available in that
pipeline's results after execute().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_instance
|
Any
|
A saved model instance carrying a TDValueField. |
required |
field_name
|
str
|
Name of the TDValueField on the model. Must be
|
required |
reward
|
float
|
Observed reward signal. |
required |
max_future_q
|
float
|
Maximum value for the next state's best action. Default 0.0 (terminal — no future state). |
0.0
|
alpha
|
float
|
Learning rate. Defaults to |
TD_ALPHA
|
gamma
|
float
|
Discount factor. Defaults to |
TD_GAMMA
|
pipeline
|
Optional[Pipeline]
|
Optional Redis pipeline to queue the update on. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
float |
Optional[float]
|
The TD error — positive when the outcome beat the current |
Optional[float]
|
estimate, negative when it fell short. |
|
Optional[float]
|
pipeline. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the instance is unsaved, or |
Source code in src/popoto/fields/td_value_field.py
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 | |