14#include <unordered_map>
26inline std::mutex&
Mutex() {
static std::mutex m;
return m; }
27inline std::unordered_map<unsigned int, Snapshot>&
Snapshots() {
static std::unordered_map<unsigned int, Snapshot> m;
return m; }
31inline bool Read(
unsigned int entity,
Snapshot& out) { std::lock_guard<std::mutex> lock(
Mutex());
auto it=
Snapshots().find(entity);
if(it==
Snapshots().end())
return false; out=it->second;
return true; }
33inline void Clear(
unsigned int entity) { std::lock_guard<std::mutex> lock(
Mutex());
Snapshots().erase(entity); }
35 std::vector<std::vector<float>> result{input};
auto values=input;
36 for(
const auto& layer:net.
Layers()) {
37 if(values.size()!=layer.Inputs())
throw std::runtime_error(
"Neural input dimensions mismatch");
38 std::vector<float> next(layer.Outputs());
39 for(
size_t i=0;i<layer.Outputs();++i) {
40 float sum=layer.Biases()[i];
41 for(
size_t j=0;j<values.size();++j) sum+=layer.Weights()(i,j)*values[j];
44 result.push_back(next); values=std::move(next);
55 default:
return "Pong paddle (supervised)"; }
68 int epochs,
float learningRate,
bool resume,
bool overwrite) {
70 if (resume && sourceName.empty())
throw std::runtime_error(
"Select a source model to resume training");
71 if (std::filesystem::exists(output) && !overwrite)
72 throw std::runtime_error(
"Output already exists. Choose another Save As name or enable Overwrite Output");
78 if (net.
Layers().empty() || net.
Layers().front().Inputs()!=nIn || net.
Layers().back().Outputs()!=nOut)
79 throw std::runtime_error(
"Source model dimensions do not match the selected training preset");
91 std::vector<std::vector<float>> xs, ys;
93 for (
int x=-8;x<=8;++x)
for (
int z=-8;z<=8;++z) {
94 const float dx=x/8.f,dz=z/8.f,len=std::sqrt(dx*dx+dz*dz);
95 xs.push_back({dx,dz});
96 ys.push_back(len>1e-5f?std::vector<float>{dx/len,dz/len}:std::vector<float>{0.f,0.f});
100 for(
int target=-10;target<=10;++target)
for(
int self=-10;self<=10;++self) {
101 float t=target/10.f, p=self/10.f, d=t-p;
102 xs.push_back({t,p,std::clamp(d,-1.f,1.f)});
103 ys.push_back({std::clamp(d*3.f,-1.f,1.f)});
108 for(
int i=0;i<6000;++i) {
109 const float bx=
static_cast<float>((i*37)%199-99)/100.f;
110 const float by=
static_cast<float>((i*53)%199-99)/100.f;
111 const float vx=
static_cast<float>((i*17)%161-80)/100.f;
112 const float vy=
static_cast<float>((i*29)%161-80)/100.f;
113 const float py=
static_cast<float>((i*71)%199-99)/100.f;
114 const float dx=std::clamp((1.f-bx)*0.5f,0.f,1.f);
115 const float time=vx>0.05f ? std::min(3.f,(1.f-bx)/vx) : 0.f;
116 float intercept=by+vy*time;
118 while(intercept>1.f || intercept< -1.f) {
119 if(intercept>1.f) intercept=2.f-intercept;
120 if(intercept< -1.f) intercept=-2.f-intercept;
122 xs.push_back({bx,by,vx,vy,py,dx});
123 ys.push_back({std::clamp((intercept-py)*3.f,-1.f,1.f)});
127 options.
epochs=
static_cast<size_t>(std::clamp(epochs,1,20000));
128 options.
learningRate=std::clamp(learningRate,0.00001f,0.1f);
130 auto history=net.
Train(xs,ys,options);
131 std::filesystem::create_directories(output.parent_path());
132 net.
Save(output.string());
137 bool resume=
false,
bool overwrite=
false) {
Path resolution and validation for neural model assets.
Dense neural network inference, training and serialization.
CPU-based feed-forward neural network with native C++ training.
static NeuralNetwork Load(const std::string &path)
Loads a serialized network.
void Save(const std::string &path) const
Serializes the network to a model asset.
const std::vector< DenseLayer > & Layers() const
void AddDense(size_t inputs, size_t outputs, Activation activation)
Appends a dense layer to the network.
TrainingHistory Train(const std::vector< std::vector< float > > &inputs, const std::vector< std::vector< float > > &targets, const TrainingOptions &options={}, const std::function< bool(size_t, float)> &onEpoch={})
Trains on paired input and target samples.
std::vector< std::vector< float > > Activations(const Neural::NeuralNetwork &net, const std::vector< float > &input)
const char * PresetLabel(TrainingPreset p)
void Clear(unsigned int entity)
Removes diagnostics when an agent is destroyed or reset.
bool Read(unsigned int entity, Snapshot &out)
Copies the latest snapshot, if one exists for the entity.
TrainingPreset
Supported supervised training datasets and target mappings.
std::unordered_map< unsigned int, Snapshot > & Snapshots()
Neural::TrainingHistory TrainDirectionModel(const std::string &name, int epochs, float learningRate, bool resume=false, bool overwrite=false)
void Publish(unsigned int entity, Snapshot snapshot)
Publishes an entity snapshot for live AI debugging.
Neural::TrainingHistory TrainPreset(const std::string &outputName, const std::string &sourceName, TrainingPreset preset, int epochs, float learningRate, bool resume, bool overwrite)
Trains a preset model and saves it independently of the active model.
float Activate(float x, Activation a)
std::filesystem::path NeuralAssetPath(const std::string &name)
Per-epoch loss values returned by a training run.
Parameters controlling supervised neural network optimization.
A copy of neural agent inference state for editor diagnostics.
std::vector< float > inputs
std::vector< float > outputs
std::vector< std::vector< float > > activations