forked from SciSharp/TensorFlow.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLayer.cs
More file actions
207 lines (178 loc) · 7.43 KB
/
Layer.cs
File metadata and controls
207 lines (178 loc) · 7.43 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
197
198
199
200
201
202
203
204
205
206
207
/*****************************************************************************
Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
******************************************************************************/
using System;
using System.Collections.Generic;
using static Tensorflow.Binding;
namespace Tensorflow.Layers
{
public class Layer : Keras.Layers.Layer
{
protected Graph _graph;
protected VariableScope _scope;
protected VariableScope _current_scope;
protected bool? _reuse;
protected bool _use_resource_variables;
protected bool _keras_style;
public Layer(bool trainable = true,
string name = null,
TF_DataType dtype = TF_DataType.DtInvalid,
bool? _reuse = null) : base(trainable: trainable, name: name, dtype: dtype)
{
// For backwards compatibility, legacy layers do not use `ResourceVariable`
// by default.
this._use_resource_variables = false;
this._reuse = _reuse;
// Avoid an incorrect lint error
_trainable_weights = new List<VariableV1>();
_non_trainable_weights = new List<VariableV1>();
this.built = false;
_keras_style = false;
}
public virtual (Tensor, Tensor) apply(Tensor inputs, Tensor training = null)
{
var results = __call__(inputs, training: training);
return (results[0], results[1]);
}
public Tensor[] __call__(Tensor inputs,
Tensor training = null,
Tensor state = null,
VariableScope scope = null)
{
_set_scope(scope);
_graph = ops._get_graph_from_inputs(new Tensor[] { inputs }, graph: _graph);
variable_scope scope_context_manager = null;
if (built)
{
scope_context_manager = tf.variable_scope(_scope,
reuse: true,
auxiliary_name_scope: false);
}
else
{
scope_context_manager = tf.variable_scope(_scope,
reuse: _reuse,
auxiliary_name_scope: false);
}
Tensor[] outputs = null;
tf_with(scope_context_manager, scope2 =>
{
_current_scope = scope2;
// Actually call layer
outputs = base.__call__(new Tensor[] { inputs },
training: training,
state: state);
});
// Update global default collections.
_add_elements_to_collection(_updates.ToArray(), new string[] { tf.GraphKeys.UPDATE_OPS });
return outputs;
}
protected virtual void _add_elements_to_collection(Operation[] elements, string[] collection_list)
{
foreach(var name in collection_list)
{
var collection = ops.get_collection_ref<Operation>(name);
foreach (var element in elements)
if (!collection.Contains(element))
collection.Add(element);
}
}
/// <summary>
/// Adds a new variable to the layer, or gets an existing one; returns it.
/// </summary>
/// <param name="name"></param>
/// <param name="shape"></param>
/// <param name="dtype"></param>
/// <param name="initializer"></param>
/// <param name="trainable"></param>
/// <param name="synchronization"></param>
/// <param name="aggregation"></param>
/// <returns></returns>
protected virtual VariableV1 add_weight(string name,
int[] shape,
TF_DataType dtype = TF_DataType.DtInvalid,
IInitializer initializer = null,
bool? trainable = null,
VariableSynchronization synchronization = VariableSynchronization.Auto,
VariableAggregation aggregation = VariableAggregation.None)
{
var default_graph = ops.get_default_graph();
Graph init_graph = null;
VariableV1[] existing_variables = null;
if (synchronization == VariableSynchronization.OnRead)
trainable = false;
else if (!trainable.HasValue)
trainable = true;
if (default_graph.building_function)
{
throw new NotImplementedException("add_weight");
}
else
{
init_graph = default_graph;
existing_variables = variables.global_variables().ToArray();
}
if(dtype == TF_DataType.DtInvalid)
dtype = TF_DataType.TF_FLOAT;
_set_scope();
var reuse = built || (_reuse != null && _reuse.Value);
return tf_with(tf.variable_scope(_scope,
reuse: reuse,
auxiliary_name_scope: false), scope =>
{
_current_scope = scope;
return tf_with(ops.name_scope(_name_scope()), delegate
{
var variable = base.add_weight(name,
shape,
dtype: dtype,
initializer: initializer,
trainable: trainable,
getter: (name1, shape1, dtype1, initializer1, trainable1) =>
tf.get_variable(name1,
shape: new TensorShape(shape1),
dtype: dtype1,
initializer: initializer1,
trainable: trainable1)
);
//if (init_graph != null)
//var trainable_variables = variables.trainable_variables();
return variable;
});
});
}
protected override string _name_scope()
{
return _current_scope.original_name_scope;
}
protected void _set_scope(VariableScope scope = null)
{
if (_scope == null)
{
if(_reuse.HasValue && _reuse.Value)
{
throw new NotImplementedException("_set_scope _reuse.HasValue");
/*with(tf.variable_scope(scope == null ? _base_name : scope),
captured_scope => _scope = captured_scope);*/
}
else
{
tf_with(tf.variable_scope(scope, default_name: _base_name), captured_scope =>
{
// convert variable_scope to VariableScope
_scope = captured_scope;
});
}
}
}
}
}