You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

Layer.cs 7.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*****************************************************************************
  2. Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ******************************************************************************/
  13. using System;
  14. using System.Collections.Generic;
  15. using static Tensorflow.Binding;
  16. namespace Tensorflow.Layers
  17. {
  18. public class Layer : Keras.Layers.Layer
  19. {
  20. protected Graph _graph;
  21. protected VariableScope _scope;
  22. protected VariableScope _current_scope;
  23. protected bool? _reuse;
  24. protected bool _use_resource_variables;
  25. protected bool _keras_style;
  26. public Layer(bool trainable = true,
  27. string name = null,
  28. TF_DataType dtype = TF_DataType.DtInvalid,
  29. bool? _reuse = null) : base(trainable: trainable, name: name, dtype: dtype)
  30. {
  31. // For backwards compatibility, legacy layers do not use `ResourceVariable`
  32. // by default.
  33. this._use_resource_variables = false;
  34. this._reuse = _reuse;
  35. // Avoid an incorrect lint error
  36. _trainable_weights = new List<IVariableV1>();
  37. _non_trainable_weights = new List<IVariableV1>();
  38. this.built = false;
  39. _keras_style = false;
  40. }
  41. public virtual (Tensor, Tensor) apply(Tensor inputs, Tensor training = null)
  42. {
  43. var results = __call__(inputs, training: training);
  44. return (results[0], results[1]);
  45. }
  46. public Tensor[] __call__(Tensor inputs,
  47. Tensor training = null,
  48. Tensor state = null,
  49. VariableScope scope = null)
  50. {
  51. _set_scope(scope);
  52. _graph = ops._get_graph_from_inputs(new Tensor[] { inputs }, graph: _graph);
  53. variable_scope scope_context_manager = null;
  54. if (built)
  55. {
  56. scope_context_manager = tf.variable_scope(_scope,
  57. reuse: true,
  58. auxiliary_name_scope: false);
  59. }
  60. else
  61. {
  62. scope_context_manager = tf.variable_scope(_scope,
  63. reuse: _reuse,
  64. auxiliary_name_scope: false);
  65. }
  66. Tensor[] outputs = null;
  67. tf_with(scope_context_manager, scope2 =>
  68. {
  69. _current_scope = scope2;
  70. // Actually call layer
  71. outputs = base.__call__(new Tensor[] { inputs },
  72. training: training,
  73. state: state);
  74. });
  75. // Update global default collections.
  76. _add_elements_to_collection(_updates.ToArray(), new string[] { tf.GraphKeys.UPDATE_OPS });
  77. return outputs;
  78. }
  79. protected virtual void _add_elements_to_collection(Operation[] elements, string[] collection_list)
  80. {
  81. foreach(var name in collection_list)
  82. {
  83. var collection = ops.get_collection_ref<Operation>(name);
  84. foreach (var element in elements)
  85. if (!collection.Contains(element))
  86. collection.Add(element);
  87. }
  88. }
  89. /// <summary>
  90. /// Adds a new variable to the layer, or gets an existing one; returns it.
  91. /// </summary>
  92. /// <param name="name"></param>
  93. /// <param name="shape"></param>
  94. /// <param name="dtype"></param>
  95. /// <param name="initializer"></param>
  96. /// <param name="trainable"></param>
  97. /// <param name="synchronization"></param>
  98. /// <param name="aggregation"></param>
  99. /// <returns></returns>
  100. protected virtual IVariableV1 add_weight(string name,
  101. int[] shape,
  102. TF_DataType dtype = TF_DataType.DtInvalid,
  103. IInitializer initializer = null,
  104. bool? trainable = null,
  105. VariableSynchronization synchronization = VariableSynchronization.Auto,
  106. VariableAggregation aggregation = VariableAggregation.None)
  107. {
  108. var default_graph = ops.get_default_graph();
  109. Graph init_graph = null;
  110. IVariableV1[] existing_variables = null;
  111. if (synchronization == VariableSynchronization.OnRead)
  112. trainable = false;
  113. else if (!trainable.HasValue)
  114. trainable = true;
  115. if (default_graph.building_function)
  116. {
  117. throw new NotImplementedException("add_weight");
  118. }
  119. else
  120. {
  121. init_graph = default_graph;
  122. existing_variables = variables.global_variables().ToArray();
  123. }
  124. if(dtype == TF_DataType.DtInvalid)
  125. dtype = TF_DataType.TF_FLOAT;
  126. _set_scope();
  127. var reuse = built || (_reuse != null && _reuse.Value);
  128. return tf_with(tf.variable_scope(_scope,
  129. reuse: reuse,
  130. auxiliary_name_scope: false), scope =>
  131. {
  132. _current_scope = scope;
  133. return tf_with(ops.name_scope(_name_scope()), delegate
  134. {
  135. var variable = base.add_weight(name,
  136. shape,
  137. dtype: dtype,
  138. initializer: initializer,
  139. trainable: trainable,
  140. getter: (name1, shape1, dtype1, initializer1, trainable1) =>
  141. tf.get_variable(name1,
  142. shape: new TensorShape(shape1),
  143. dtype: dtype1,
  144. initializer: initializer1,
  145. trainable: trainable1)
  146. );
  147. //if (init_graph != null)
  148. //var trainable_variables = variables.trainable_variables();
  149. return variable;
  150. });
  151. });
  152. }
  153. protected override string _name_scope()
  154. {
  155. return _current_scope.original_name_scope;
  156. }
  157. protected void _set_scope(VariableScope scope = null)
  158. {
  159. if (_scope == null)
  160. {
  161. if(_reuse.HasValue && _reuse.Value)
  162. {
  163. throw new NotImplementedException("_set_scope _reuse.HasValue");
  164. /*with(tf.variable_scope(scope == null ? _base_name : scope),
  165. captured_scope => _scope = captured_scope);*/
  166. }
  167. else
  168. {
  169. tf_with(tf.variable_scope(scope, default_name: _base_name), captured_scope =>
  170. {
  171. // convert variable_scope to VariableScope
  172. _scope = captured_scope;
  173. });
  174. }
  175. }
  176. }
  177. }
  178. }