Norms and Stats
Most policies train on normalised state and action. Knonik
computes the numbers you need once per dataset and saves them beside the
manifest as norm_stats.json. Your training run then reads a few kilobytes
instead of scanning the dataset.
For every column of every numeric stream (for example state and action), the
file holds:
| Statistic | Key |
|---|---|
| Number of rows | count |
| Column width | dim |
| Mean, standard deviation | mean, std |
| Minimum, maximum | min, max |
| 1st and 99th percentiles | q01, q99 |
Camera and depth streams are not summarised.
Stats ship in the same wheel as ingest, export and the dataloader (see Overview → Install). They are included in Knonik Standard and use the ingest login:
knonik login --product ingest
1. At Pack Time
Add --stats to knonik pack.
The packer writes norm_stats.json next to hlp_manifest.json once the packs
are built:
knonik pack \
--input-dir /home/me/data/out/knonik_thread_velcro \
--output-dir /home/me/data/out/thread_velcro_hlp \
--dataset-name thread_velcro \
--fps 50 \
--stats
From Python, pass stats=True to pack:
from knonik.ingest import pack
pack(
input_dir="/home/me/data/out/knonik_thread_velcro",
output_dir="/home/me/data/out/thread_velcro_hlp",
dataset_name="thread_velcro",
fps=50,
stats=True,
)
2. For A Dataset Already Packed
knonik stats compute /home/me/data/out/thread_velcro_hlp
Wrote norm_stats.json from all 250 episodes:
action: 125000 rows x 7
state: 125000 rows x 14
The argument is the packed dataset directory or its hlp_manifest.json.
| Option | Default | Meaning |
|---|---|---|
--sample-episodes N | every episode | Read N randomly chosen episodes instead of all of them. The statistics are then those of the sample. |
--seed | 0 | Seed for --sample-episodes. The same seed picks the same episodes. |
--streams | every numeric stream | Comma-separated list of streams to summarise, e.g. state,action. |
--no-write | off | Print the statistics as JSON instead of writing norm_stats.json. |
To print a dataset's saved stats:
knonik stats show /home/me/data/out/thread_velcro_hlp
If the dataset's episodes have changed since the stats were computed, show
prints a note telling you to run knonik stats compute again.
Exact or sampled
By default every episode is read and every statistic is exact, the percentiles
included. Exact percentiles need every value in memory until the end, at
8 bytes per value. For example, 10 million frames of a 14-wide state is about
1.1 GB. For a dataset too big for that, use --sample-episodes. Episodes
are drawn at random (not the first N), so the sample covers the whole dataset.
A stream must keep the same width in every episode. Stats refuse a stream whose width changes and name it in the error.
3. From Python
from knonik import stats
result = stats.compute("/home/me/data/out/thread_velcro_hlp")
# result = stats.compute(path, sample_episodes=500, seed=0, streams=["state", "action"])
# result = stats.compute(path, write=False) # return without writing norm_stats.json
saved = stats.load("/home/me/data/out/thread_velcro_hlp") # None if there is none
fresh = stats.is_current("/home/me/data/out/thread_velcro_hlp") # False once episodes change
| Function | Returns |
|---|---|
stats.compute(dataset, sample_episodes=None, seed=0, streams=None, write=True) | The statistics, and writes norm_stats.json unless write=False. |
stats.load(dataset) | The saved norm_stats.json, or None. |
stats.is_current(dataset) | Whether the saved stats still describe the dataset's episodes. |
stats.iter_numeric(dataset, streams=None, sample_episodes=None, seed=0) | (episode_id, {stream: array (frames, width)}) per episode. |
iter_numeric gives you whole numeric trajectories. Use it for statistics
that need more than one frame at a time, such as stats over action chunks or
actions relative to the current state:
import numpy as np
from knonik import stats
chunks = []
for episode_id, streams in stats.iter_numeric("/home/me/data/out/thread_velcro_hlp",
streams=["action"]):
action = streams["action"]
chunks.append(action[16:] - action[:-16]) # e.g. 16-step action deltas
deltas = np.concatenate(chunks)
print(deltas.mean(0), deltas.std(0))
4. The File
norm_stats.json sits next to hlp_manifest.json:
{
"schema_version": 1,
"episodes": 250,
"episodes_total": 250,
"sampled": false,
"seed": 0,
"streams": {
"action": {
"count": 125000,
"dim": 7,
"mean": [0.012, -0.004, ...],
"std": [0.081, 0.077, ...],
"min": [-0.41, -0.39, ...],
"max": [ 0.44, 0.40, ...],
"q01": [-0.21, -0.20, ...],
"q99": [ 0.22, 0.21, ...],
"quantile_rows": 125000,
"quantiles": "exact"
}
},
"catalog_fingerprint": "..."
}
- ▸
episodes/episodes_total: episodes read, and episodes in the dataset.sampledistruewhen they differ. - ▸
quantile_rows/quantiles: how many rows the percentiles rest on, and whether they areexact. - ▸
catalog_fingerprint: identifies the episodes the stats were computed from.stats.is_currentandknonik stats showcompare it to the dataset.
5. Normalising A Batch
The dataloader returns raw values. Normalise in your training loop with whichever scheme your policy expects:
import numpy as np
from knonik import stats
norm = stats.load("/home/me/data/out/thread_velcro_hlp")["streams"]
def zscore(x, s):
return (x - np.asarray(s["mean"])) / (np.asarray(s["std"]) + 1e-8)
def to_unit_range(x, s):
# q01..q99 -> -1..1, clipped. Robust to outliers.
lo, hi = np.asarray(s["q01"]), np.asarray(s["q99"])
return np.clip(2 * (x - lo) / (hi - lo + 1e-8) - 1, -1, 1)
for batch in loader:
state = zscore(batch["state"], norm["state"]) # (B, T, state_dim)
action = to_unit_range(batch["action"], norm["action"]) # (B, T, action_dim)
# train(state, action, ...)
Undo the same scheme on the policy's output before sending actions to the robot.
6. Keep Them Current
The stats describe the episodes they were computed from. After you add, remove or
repack episodes, run knonik stats compute again (or pack with --stats).
stats.is_current(dataset) returns False until you do.