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.

Context.AutoMode.cs 2.8 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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.Diagnostics;
  15. using System.Linq;
  16. using Tensorflow.Eager;
  17. using static Tensorflow.Binding;
  18. using Google.Protobuf;
  19. namespace Tensorflow.Contexts
  20. {
  21. /// <summary>
  22. /// Environment in which eager operations execute.
  23. /// </summary>
  24. public sealed partial class Context
  25. {
  26. public T RunInAutoMode<T>(Func<T> graphAction, Func<T> eagerAction, params object[] args)
  27. {
  28. if (tf.Context.has_graph_arg(args))
  29. {
  30. if (executing_eagerly())
  31. {
  32. graph_mode();
  33. var result = graphAction();
  34. restore_mode();
  35. return result;
  36. }
  37. else
  38. {
  39. return graphAction();
  40. }
  41. }
  42. else
  43. {
  44. if (tf.Context.executing_eagerly())
  45. {
  46. return eagerAction();
  47. }
  48. else
  49. {
  50. return graphAction();
  51. }
  52. }
  53. }
  54. // [DebuggerStepThrough]
  55. public Tensors RunInAutoMode2(Func<Tensors> graphAction,
  56. Func<Tensors> eagerAction,
  57. Action<Operation> recordGradient,
  58. Tensors tensors)
  59. {
  60. if (tf.Context.has_graph_arg(tensors))
  61. {
  62. if (executing_eagerly())
  63. {
  64. graph_mode();
  65. var result = graphAction();
  66. restore_mode();
  67. return result;
  68. }
  69. else
  70. {
  71. var result = graphAction();
  72. if (tf.Runner.MustRecordGradient())
  73. recordGradient(result[0].op);
  74. return result;
  75. }
  76. }
  77. else
  78. {
  79. return eagerAction();
  80. }
  81. }
  82. }
  83. }