-
Notifications
You must be signed in to change notification settings - Fork 0
/
value.h
74 lines (62 loc) · 2.58 KB
/
value.h
1
2
3
4
5
6
7
8
9
10
11
12
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
#pragma once
#include <iostream>
#include <cmath>
#include <memory>
#include <vector>
#include <unordered_set>
#include <functional>
#include <algorithm>
#include <random>
#include <iomanip>
#include <span>
class Value : public std::enable_shared_from_this<Value> {
public:
float data;
float grad;
std::vector<std::shared_ptr<Value>> children;
std::function<void()> backprop;
Value(float data);
void backward();
void print_tree(int indent);
void print();
private:
void topological_sort(std::vector<std::shared_ptr<Value>>& sorted);
};
std::shared_ptr<Value> operator+(const std::shared_ptr<Value>& a, const std::shared_ptr<Value>& b);
std::shared_ptr<Value> operator+(const std::shared_ptr<Value>& a, float b);
std::shared_ptr<Value> operator+(float a, const std::shared_ptr<Value>& b);
std::shared_ptr<Value> operator-(const std::shared_ptr<Value>& a);
std::shared_ptr<Value> operator-(const std::shared_ptr<Value>& a, const std::shared_ptr<Value>& b);
std::shared_ptr<Value> operator-(const std::shared_ptr<Value>& a, float b);
std::shared_ptr<Value> operator-(float a, const std::shared_ptr<Value>& b);
std::shared_ptr<Value> operator*(const std::shared_ptr<Value>& a, const std::shared_ptr<Value>& b);
std::shared_ptr<Value> operator*(const std::shared_ptr<Value>& a, float b);
std::shared_ptr<Value> operator*(float a, const std::shared_ptr<Value>& b);
std::shared_ptr<Value> operator/(const std::shared_ptr<Value>& a, const std::shared_ptr<Value>& b);
std::shared_ptr<Value> operator/(const std::shared_ptr<Value>& a, float b);
std::shared_ptr<Value> operator/(float a, const std::shared_ptr<Value>& b);
std::shared_ptr<Value> tanh(const std::shared_ptr<Value>& a);
std::shared_ptr<Value> exp(const std::shared_ptr<Value>& a);
std::shared_ptr<Value> pow(const std::shared_ptr<Value>& a, float b);
class Neuron {
public:
Neuron(int numInputs);
void parameters(std::vector<std::shared_ptr<Value>>& params);
std::shared_ptr<Value> operator()(const std::vector<std::shared_ptr<Value>>& inputs);
std::vector<std::shared_ptr<Value>> weights;
std::shared_ptr<Value> bias;
};
class Layer {
public:
Layer(int numInputs, int numNeurons);
void parameters(std::vector<std::shared_ptr<Value>>& params);
std::vector<std::shared_ptr<Value>> operator()(const std::vector<std::shared_ptr<Value>>& inputs);
std::vector<Neuron> neurons;
};
class MLP {
public:
MLP(int numInputs, std::vector<int> numOutputs);
void parameters(std::vector<std::shared_ptr<Value>>& params);
std::vector<std::shared_ptr<Value>> operator()(const std::vector<std::shared_ptr<Value>>& inputs);
std::vector<Layer> layers;
};