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.

optimizer.py.cs 2.6 kB

6 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 Tensorflow.Framework;
  14. namespace Tensorflow
  15. {
  16. public class optimizer
  17. {
  18. public static _OptimizableVariable _get_processor(RefVariable v)
  19. {
  20. return new _RefVariableProcessor(v);
  21. }
  22. public static _OptimizableVariable _get_processor(ResourceVariable v)
  23. {
  24. return new _DenseResourceVariableProcessor(v);
  25. }
  26. }
  27. public class _RefVariableProcessor : _OptimizableVariable
  28. {
  29. private RefVariable _v;
  30. public _RefVariableProcessor(RefVariable v)
  31. {
  32. _v = v;
  33. }
  34. public Tensor target()
  35. {
  36. return _v._ref();
  37. }
  38. public Operation update_op(Optimizer optimizer, Tensor g)
  39. {
  40. Operation update_op = null;
  41. if (g.Tag == null)
  42. {
  43. update_op = optimizer._apply_dense(g, _v);
  44. }
  45. else if (g.Tag is IndexedSlices)
  46. {
  47. return optimizer._apply_sparse_duplicate_indices(g, _v);
  48. }
  49. return update_op;
  50. }
  51. }
  52. public class _DenseResourceVariableProcessor : _OptimizableVariable
  53. {
  54. private ResourceVariable _v;
  55. public _DenseResourceVariableProcessor(ResourceVariable v)
  56. {
  57. _v = v;
  58. }
  59. public Tensor target()
  60. {
  61. return _v.Handle;
  62. }
  63. public Operation update_op(Optimizer optimizer, Tensor g)
  64. {
  65. Operation update_op = null;
  66. if (g.Tag == null)
  67. {
  68. update_op = optimizer._apply_dense(g, _v);
  69. }
  70. else if (g.Tag is IndexedSlices)
  71. {
  72. return optimizer._apply_sparse_duplicate_indices(g, _v);
  73. }
  74. return update_op;
  75. }
  76. }
  77. }