forked from SciSharp/TensorFlow.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTape.cs
More file actions
112 lines (96 loc) · 3.12 KB
/
Tape.cs
File metadata and controls
112 lines (96 loc) · 3.12 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Tensorflow.Util;
using static Tensorflow.Binding;
using static Tensorflow.tensorflow;
namespace Tensorflow.Gradients
{
public partial class Tape : ITape
{
int nesting_id;
static int tape_nesting_id_counter = 0;
bool persistent_;
bool watch_accessed_variables;
TensorTape tensor_tape_;
OpTape<BackwardFunction, TapeTensor> op_tape_;
/// <summary>
/// A deque-backed stack, whose element references are not invalidated by
/// pushes and pops at the back.
/// </summary>
Stack<AccumulatorCallState> call_state_;
public Tape(bool persistent, bool watch_accessed_variables)
{
this.persistent_ = persistent;
this.watch_accessed_variables = watch_accessed_variables;
tensor_tape_ = new TensorTape();
op_tape_ = new OpTape<BackwardFunction, TapeTensor>();
tensor_usage_ = new UnorderedMap<long, long>();
nesting_id = ++tape_nesting_id_counter;
tf.GetTapeSet().Add(this);
}
/// <summary>
/// Marks this tensor to be watched by the given tape.
/// </summary>
/// <param name="x"></param>
public void Watch(long tensor_id)
{
if (!CouldBackprop())
return;
tensor_tape_.emplace(tensor_id, -1);
}
public bool ShouldRecord(long[] tensor_ids, TF_DataType[] dtypes)
{
for (int i = 0; i < tensor_ids.Length; ++i)
{
if (tensor_tape_.find(tensor_ids[i]))
if (IsDtypeTrainable(dtypes[i]))
return true;
}
return false;
}
/// <summary>
/// Pops the given tape in the stack.
/// </summary>
/// <param name="tape"></param>
public void PopTape(ITape tape)
{
tf.GetTapeSet().Remove(tape);
}
public void VariableAccessed(ResourceVariable variable)
{
Watch(variable.Handle.Id);
}
public ResourceVariable[] WatchedVariables()
{
return null;
}
public bool IsDtypeTrainable(TF_DataType dtype)
{
switch (dtype)
{
case TF_DataType.TF_HALF:
case TF_DataType.TF_BFLOAT16:
case TF_DataType.TF_FLOAT:
case TF_DataType.TF_DOUBLE:
case TF_DataType.TF_COMPLEX64:
case TF_DataType.TF_COMPLEX128:
case TF_DataType.TF_RESOURCE:
case TF_DataType.TF_VARIANT:
return true;
default:
return false;
}
}
bool CouldForwardprop()
=> HasAccumulator();
bool CouldBackprop()
=> HasGradientTape();
bool HasAccumulator()
//return !GetAccumulatorSet()->empty();
=> false;
bool HasGradientTape()
=> tf.GetTapeSet().Count > 0;
}
}