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 2.8 kB

6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 static Tensorflow.Python;
  14. namespace Tensorflow
  15. {
  16. /// <summary>
  17. /// Variable scope object to carry defaults to provide to `get_variable`
  18. /// </summary>
  19. public class VariableScope
  20. {
  21. public bool use_resource { get; set; }
  22. private _ReuseMode _reuse;
  23. public bool resue;
  24. private TF_DataType _dtype;
  25. string _name;
  26. public string name => _name;
  27. public string _name_scope { get; set; }
  28. public string original_name_scope => _name_scope;
  29. public VariableScope(bool reuse,
  30. string name = "",
  31. string name_scope = "",
  32. TF_DataType dtype = TF_DataType.TF_FLOAT)
  33. {
  34. _name = name;
  35. _name_scope = name_scope;
  36. _reuse = _ReuseMode.AUTO_REUSE;
  37. _dtype = dtype;
  38. }
  39. public RefVariable get_variable(_VariableStore var_store,
  40. string name,
  41. TensorShape shape = null,
  42. TF_DataType dtype = TF_DataType.DtInvalid,
  43. object initializer = null, // IInitializer or Tensor
  44. bool? trainable = null,
  45. bool? use_resource = null,
  46. bool validate_shape = true,
  47. VariableSynchronization synchronization = VariableSynchronization.Auto,
  48. VariableAggregation aggregation= VariableAggregation.None)
  49. {
  50. string full_name = !string.IsNullOrEmpty(this.name) ? this.name + "/" + name : name;
  51. return tf_with(ops.name_scope(null), scope =>
  52. {
  53. if (dtype == TF_DataType.DtInvalid)
  54. dtype = _dtype;
  55. return var_store.get_variable(full_name,
  56. shape: shape,
  57. dtype: dtype,
  58. initializer: initializer,
  59. reuse: resue,
  60. trainable: trainable,
  61. synchronization: synchronization,
  62. aggregation: aggregation);
  63. });
  64. }
  65. }
  66. }