Learning over time¶
Most machine learning trains a model once on a large stored collection of data and then freezes it. UNaIVERSE follows the idea of Collectionless AI. Like a person, an agent learns over time from the stream of experience that reaches it and never builds a collection first. The data come from other agents (a teacher that shows examples, a person who corrects an answer, a sensor) and stay with whoever owns them. An agent can answer one request and learn from the next one in the same world because learning and using the model are not two separate phases.
process and learn¶
Two built-in actions run the processor.
process |
learn |
|
|---|---|---|
Reads the inputs (stdin) |
yes | yes |
| Runs the processor | yes | yes |
Writes the outputs (stdout) |
yes | yes |
Reads the targets (stdtar) and updates the weights |
no | yes |
learn first runs process on the same interaction and then takes a backward step against the targets that came with the inputs. It needs an interaction (a request of another agent) and the learning options described below. Without these options it logs that the processor has no learning skills and returns False. People learn by themselves and the agent of a person therefore returns True without running the processor.
Learning options¶
The processor learns with the optimizer and the losses of proc_opts:
import torch
from unaiverse.agent import Agent
from unaiverse.modules.networks import ResNet
net = ResNet(d_dim=5)
agent = Agent(proc=net,
proc_opts={"optimizer": torch.optim.Adam(net.parameters(), lr=1e-4),
"losses": [torch.nn.functional.binary_cross_entropy]})
| Key | Meaning |
|---|---|
optimizer |
A torch.optim.Optimizer over the parameters of the processor. |
losses |
One loss function per output of the processor in the order of proc_outputs. None marks an output that does not learn. |
The backward step runs in the worker thread of the processor like the forward pass (see Agents). The losses of each step are printed in the log of the agent.
A lesson from a teacher¶
In a world a teacher sends a learn request with two kinds of streams: (1) the inputs that go to stdin of the student and (2) the right answers that go to stdtar. The teacher of the example world animal_school gives a lesson of 40 samples with the transition below. The samples come from a playlist of streams that the teacher recorded:
{"on": {"action": "send",
"action_kwargs": {"action_name": "learn",
"streams": {"stdin": ["<playlist>"], "stdtar": ["<playlist>"]},
"num_steps": 40, "timeout": 160, "wait_completion": true}},
"goto": "student_finished_following"}
num_steps: 40makes the student runlearn40 times on one sample of the stream at a time (see Actions).wait_completion: truekeeps the teacher in its state until the student has finished.- The student has
learnin its own behavior as a request-only action ("ready": false) and learns only when a teacher asks.
The student receives the samples as a stream with one sample per step and never receives a dataset. Once the lesson is over it keeps nothing unless its own code stores the samples.
Evaluating¶
After a lesson the teacher can check what the student learned with the actions evaluate and compare_eval (see Actions). A typical exam sends process on streams that the student has not seen, compares what the student generated with the reference stream and chooses the next state of the behavior according to the result. In animal_school the teacher reaches the state good when the error is at most 0.65 (compare_eval with cmp="<=" and thres=0.65) and can then suggest a badge to the world (see Worlds).
Keeping what was learned¶
What an agent learned lives in the weights of its processor. agent.save() writes them with the state of the optimizer and agent.load() reads them back. A node saves them periodically with save_checkpoint_every and loads them at start with node.run(resume_from_checkpoint=True) (see Agents). Weights stay on the machine of the owner of the agent like the data the agent learned from.
Research models¶
The SDK includes networks designed for learning from a stream, such as continuous-time state-space networks and Continual Neural Units (see Models). Any PyTorch module works with learn as long as proc_opts gives it an optimizer and losses.