-
Notifications
You must be signed in to change notification settings - Fork 1
/
Variable.cs
142 lines (121 loc) · 3.92 KB
/
Variable.cs
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
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
using System;
using System.Collections.Generic;
using chainer.functions;
using MathNet.Numerics;
using MathNet.Numerics.LinearAlgebra;
namespace chainer
{
public class Variable
{
public Matrix<float> Value;
public Matrix<float> Grad = null;
public Matrix<float> CurrentGrad = null;
private Matrix<float> AddGradBuf = null;
private Function _creator;
private bool _isLeaf;
public Variable(Matrix<float> value)
{
Value = value;
_creator = null;
_isLeaf = true;
}
public void SetCreator(Function creator)
{
_creator = creator;
_isLeaf = false;
}
public void ClearGrad()
{
Grad = null;
CurrentGrad = null;
}
/// <summary>
/// Same as inputs[i].Grad += grad, but memory efficient
/// </summary>
public void AddGrad(Matrix<float> grad)
{
if (AddGradBuf == null)
{
AddGradBuf = Grad.Clone();
}
Grad.Add(grad, AddGradBuf);
Grad = AddGradBuf;
}
public void VolatilizeWithoutBackward()
{
if (_isLeaf) return;
var functionQueue = new LinkedList<Function>();
var visitedFunctions = new LinkedList<Function>();
functionQueue.AddLast(_creator);
while (functionQueue.Count > 0)
{
var targetFunction = functionQueue.First.Value;
functionQueue.RemoveFirst();
var inputs = targetFunction.Inputs;
foreach (var input in inputs)
{
if (!input._isLeaf)
{
functionQueue.AddLast(input._creator);
}
}
visitedFunctions.AddLast(targetFunction);
}
// mark as resuable
foreach (var function in visitedFunctions)
{
function.Reusable = true;
}
}
public void Backward()
{
if (CurrentGrad == null)
{
Grad = CurrentGrad =
Matrix<float>.Build.Dense(
Value.RowCount, Value.ColumnCount, 1f); // LossのGradは1 (自分自身)
}
if (_isLeaf) return;
var functionQueue = new LinkedList<Function>();
var visitedFunctions = new LinkedList<Function>();
functionQueue.AddLast(_creator);
while (functionQueue.Count > 0)
{
var targetFunction = functionQueue.First.Value;
functionQueue.RemoveFirst();
var inputs = targetFunction.Inputs;
var output = targetFunction.Output;
var input_grads = targetFunction.Backward(output);
for (int i = 0; i < inputs.Count; i++)
{
inputs[i].CurrentGrad = input_grads[i]; // backward用
if (inputs[i].Grad == null)
{
inputs[i].Grad = input_grads[i];
}
else
{
inputs[i].AddGrad(input_grads[i]);
}
}
foreach (var input in inputs)
{
if (!input._isLeaf)
{
functionQueue.AddLast(input._creator);
}
}
visitedFunctions.AddLast(targetFunction);
}
// mark as resuable
foreach (var function in visitedFunctions)
{
function.Reusable = true;
}
}
public static Variable operator +(Variable x, Variable y)
{
return Add.ForwardStatic(x, y);
}
}
}