Repository navigation
Expand file tree
/
Copy path_VariableStore.cs
More file actions
196 lines (175 loc) · 6.52 KB
/
Copy path_VariableStore.cs
File metadata and controls
196 lines (175 loc) · 6.52 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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
using System;
using System.Collections.Generic;
using System.Text;
namespace Tensorflow
{
/// <summary>
/// Variable store that carries a number of named Variables.
/// </summary>
public class _VariableStore
{
private Dictionary<string, object> _vars;
private Dictionary<string, object> _partitioned_vars;
private bool _store_eager_variables;
public _VariableStore()
{
_vars = new Dictionary<string, object>();
_partitioned_vars = new Dictionary<string, object>();
_store_eager_variables = false;
}
public RefVariable get_variable(string name,
TensorShape shape = null,
TF_DataType dtype = TF_DataType.TF_FLOAT,
object initializer = null, // IInitializer or Tensor
bool? reuse = null,
bool? trainable = null,
bool validate_shape = true,
VariableSynchronization synchronization = VariableSynchronization.Auto,
VariableAggregation aggregation = VariableAggregation.None)
{
dtype = dtype.as_base_dtype();
trainable = variable_scope._get_trainable_value(synchronization, trainable);
return _true_getter(name,
shape: shape,
dtype: dtype,
initializer: initializer,
trainable: trainable,
validate_shape: validate_shape,
synchronization: synchronization,
aggregation: aggregation);
}
private RefVariable _true_getter(string name,
TensorShape shape = null,
TF_DataType dtype = TF_DataType.TF_FLOAT,
object initializer = null,
bool? trainable = null,
bool validate_shape = true,
VariableSynchronization synchronization = VariableSynchronization.Auto,
VariableAggregation aggregation = VariableAggregation.None)
{
bool is_scalar = !(shape is null) && shape.NDim == 0;
if (initializer is IInitializer init)
{
return _get_single_variable(name: name,
shape: shape,
dtype: dtype,
initializer: init,
trainable: trainable,
validate_shape: validate_shape,
synchronization: synchronization,
aggregation: aggregation);
}
else if (initializer is Tensor tensor)
{
return _get_single_variable(name: name,
shape: shape,
dtype: dtype,
initializer: tensor,
trainable: trainable,
validate_shape: validate_shape,
synchronization: synchronization,
aggregation: aggregation);
}
else
{
IInitializer init1 = null;
return _get_single_variable(name: name,
shape: shape,
dtype: dtype,
initializer: init1,
trainable: trainable,
validate_shape: validate_shape,
synchronization: synchronization,
aggregation: aggregation);
}
}
private RefVariable _get_single_variable(string name,
TensorShape shape = null,
TF_DataType dtype = TF_DataType.DtInvalid,
IInitializer initializer = null,
bool reuse = false,
bool? trainable = null,
bool validate_shape = false,
bool? use_resource = null,
VariableSynchronization synchronization = VariableSynchronization.Auto,
VariableAggregation aggregation = VariableAggregation.None)
{
bool initializing_from_value = false;
if (use_resource == null)
use_resource = false;
if (_vars.ContainsKey(name))
{
if (!reuse)
{
var var = _vars[name];
}
throw new NotImplementedException("_get_single_variable");
}
RefVariable v = null;
// Create the tensor to initialize the variable with default value.
if (initializer == null)
{
if (dtype.is_floating())
{
initializer = tf.glorot_uniform_initializer;
initializing_from_value = false;
}
}
// Create the variable.
ops.init_scope();
{
if (initializing_from_value)
{
}
else
{
Func<Tensor> init_val = () => initializer.call(shape, dtype);
var variable_dtype = dtype.as_base_dtype();
v = variable_scope.default_variable_creator(init_val,
name: name,
trainable: trainable,
dtype: TF_DataType.DtInvalid,
validate_shape: validate_shape,
synchronization: synchronization,
aggregation: aggregation);
}
}
_vars[name] = v;
return v;
}
private RefVariable _get_single_variable(string name,
TensorShape shape = null,
TF_DataType dtype = TF_DataType.DtInvalid,
Tensor initializer = null,
bool reuse = false,
bool? trainable = null,
bool validate_shape = false,
bool? use_resource = null,
VariableSynchronization synchronization = VariableSynchronization.Auto,
VariableAggregation aggregation = VariableAggregation.None)
{
if (use_resource == null)
use_resource = false;
if (_vars.ContainsKey(name))
{
if (!reuse)
{
var var = _vars[name];
}
throw new NotImplementedException("_get_single_variable");
}
RefVariable v = null;
// Create the variable.
ops.init_scope();
{
var init_val = initializer;
v = new RefVariable(init_val,
name: name,
validate_shape: validate_shape,
trainable: trainable.Value);
}
_vars[name] = v;
return v;
}
}
}