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.

VariableScope.cs 3.2 kB

6 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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.Collections.Generic;
  14. using static Tensorflow.Binding;
  15. namespace Tensorflow
  16. {
  17. /// <summary>
  18. /// Variable scope object to carry defaults to provide to `get_variable`
  19. /// </summary>
  20. public class VariableScope
  21. {
  22. public bool use_resource { get; set; }
  23. #pragma warning disable CS0414 // The field 'VariableScope._reuse' is assigned but its value is never used
  24. private _ReuseMode _reuse;
  25. #pragma warning restore CS0414 // The field 'VariableScope._reuse' is assigned but its value is never used
  26. public bool resue;
  27. private TF_DataType _dtype;
  28. string _name;
  29. public string name => _name;
  30. public string _name_scope { get; set; }
  31. public string original_name_scope => _name_scope;
  32. public VariableScope(bool reuse,
  33. string name = "",
  34. string name_scope = "",
  35. TF_DataType dtype = TF_DataType.TF_FLOAT)
  36. {
  37. _name = name;
  38. _name_scope = name_scope;
  39. _reuse = _ReuseMode.AUTO_REUSE;
  40. _dtype = dtype;
  41. }
  42. public IVariableV1 get_variable(_VariableStore var_store,
  43. string name,
  44. TensorShape shape = null,
  45. TF_DataType dtype = TF_DataType.DtInvalid,
  46. object initializer = null, // IInitializer or Tensor
  47. bool? trainable = null,
  48. List<string> collections = null,
  49. bool? use_resource = null,
  50. bool validate_shape = true,
  51. VariableSynchronization synchronization = VariableSynchronization.Auto,
  52. VariableAggregation aggregation= VariableAggregation.None)
  53. {
  54. string full_name = !string.IsNullOrEmpty(this.name) ? this.name + "/" + name : name;
  55. return tf_with(ops.name_scope(null), scope =>
  56. {
  57. if (dtype == TF_DataType.DtInvalid)
  58. dtype = _dtype;
  59. return var_store.get_variable(full_name,
  60. shape: shape,
  61. dtype: dtype,
  62. initializer: initializer,
  63. reuse: resue,
  64. trainable: trainable,
  65. collections: collections,
  66. synchronization: synchronization,
  67. aggregation: aggregation);
  68. });
  69. }
  70. public void reuse_variables()
  71. {
  72. _reuse = _ReuseMode.AUTO_REUSE;
  73. }
  74. }
  75. }