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.

tensorflow.cs 4.4 kB

4 years ago
6 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 Serilog;
  15. using Serilog.Core;
  16. using Tensorflow.Contexts;
  17. using Tensorflow.Eager;
  18. using Tensorflow.Gradients;
  19. namespace Tensorflow
  20. {
  21. public partial class tensorflow : ITensorFlowObject
  22. {
  23. public TF_DataType byte8 = TF_DataType.TF_UINT8;
  24. public TF_DataType int8 = TF_DataType.TF_INT8;
  25. public TF_DataType int16 = TF_DataType.TF_INT16;
  26. public TF_DataType int32 = TF_DataType.TF_INT32;
  27. public TF_DataType int64 = TF_DataType.TF_INT64;
  28. public TF_DataType float16 = TF_DataType.TF_HALF;
  29. public TF_DataType float32 = TF_DataType.TF_FLOAT;
  30. public TF_DataType float64 = TF_DataType.TF_DOUBLE;
  31. public TF_DataType @bool = TF_DataType.TF_BOOL;
  32. public TF_DataType chars = TF_DataType.TF_STRING;
  33. public TF_DataType @string = TF_DataType.TF_STRING;
  34. public delegate Tensor[] BackwardFunction(Tensor[] grads, long[] unneeded_gradients);
  35. public Status Status;
  36. public OpDefLibrary OpDefLib;
  37. public Context Context;
  38. public IEagerRunner Runner;
  39. public Logger Logger;
  40. public tensorflow()
  41. {
  42. Logger = new LoggerConfiguration()
  43. .MinimumLevel.Error()
  44. .WriteTo.Console()
  45. .CreateLogger();
  46. Status = new Status();
  47. Context = new Context(new ContextOptions(), Status);
  48. OpDefLib = new OpDefLibrary();
  49. ConstructThreadingObjects();
  50. InitGradientEnvironment();
  51. Runner = new EagerRunner();
  52. }
  53. public string VERSION => c_api.StringPiece(c_api.TF_Version());
  54. private void InitGradientEnvironment()
  55. {
  56. ops.RegisterFromAssembly();
  57. }
  58. public ResourceVariable Variable<T>(T data,
  59. bool trainable = true,
  60. bool validate_shape = true,
  61. bool use_resource = true,
  62. string name = null,
  63. TF_DataType dtype = TF_DataType.DtInvalid,
  64. VariableAggregation aggregation = VariableAggregation.None,
  65. int[] shape = null)
  66. => new ResourceVariable(data,
  67. trainable: trainable,
  68. validate_shape: validate_shape,
  69. name: name,
  70. dtype: dtype,
  71. aggregation: aggregation,
  72. shape: shape);
  73. public Tensor placeholder(TF_DataType dtype, TensorShape shape = null, string name = null)
  74. => array_ops.placeholder(dtype, shape, name);
  75. public void enable_eager_execution()
  76. => Context.eager_mode();
  77. public Session get_default_session()
  78. => ops.get_default_session();
  79. public Session Session()
  80. {
  81. return new Session().as_default();
  82. }
  83. public Session Session(Graph graph, ConfigProto config = null)
  84. {
  85. return new Session(graph, config: config).as_default();
  86. }
  87. public Session Session(ConfigProto config)
  88. {
  89. return new Session(null, config).as_default();
  90. }
  91. List<ITape> tape_set;
  92. public List<ITape> GetTapeSet()
  93. {
  94. if (tape_set == null)
  95. {
  96. tape_set = new List<ITape>();
  97. }
  98. return tape_set;
  99. }
  100. public void __init__()
  101. {
  102. }
  103. public void __enter__()
  104. {
  105. }
  106. public void __exit__()
  107. {
  108. }
  109. public void __del__()
  110. {
  111. }
  112. public void Dispose()
  113. {
  114. }
  115. }
  116. }