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.

Graph.Operation.cs 2.7 kB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using NumSharp.Core;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Runtime.InteropServices;
  6. using System.Text;
  7. namespace Tensorflow
  8. {
  9. public partial class Graph
  10. {
  11. public OpDef GetOpDef(string type)
  12. {
  13. using (var buffer = new Buffer())
  14. using (var status = new Status())
  15. {
  16. c_api.TF_GraphGetOpDef(_handle, type, buffer, status);
  17. return OpDef.Parser.ParseFrom(buffer.Data);
  18. }
  19. }
  20. public OperationDescription NewOperation(string opType, string opName)
  21. {
  22. return c_api.TF_NewOperation(_handle, opType, opName);
  23. }
  24. public ITensorOrOperation _get_operation_by_name_unsafe(string name)
  25. {
  26. return _nodes_by_name.ContainsKey(name) ? _nodes_by_name[name] : null;
  27. }
  28. public ITensorOrOperation _get_operation_by_tf_operation(IntPtr tf_oper)
  29. {
  30. var op_name = Marshal.PtrToStringAnsi(c_api.TF_OperationName(tf_oper));
  31. return _get_operation_by_name_unsafe(op_name);
  32. }
  33. public Operation _create_op_from_tf_operation(IntPtr c_op, bool compute_device = true)
  34. {
  35. var ret = new Operation(c_op);
  36. var name_key = ret.name.ToLower();
  37. if (!_names_in_use.ContainsKey(name_key))
  38. _names_in_use[name_key] = 1;
  39. _create_op_helper(ret, compute_device: compute_device);
  40. return ret;
  41. }
  42. /// <summary>
  43. /// Creates `Operations` in this graph for any new TF_Operations.
  44. ///
  45. /// This is useful for when TF_Operations are indirectly created by the C API
  46. /// outside of the Operation constructor (e.g. by TF_ImportGraphDef,
  47. /// TF_FinishWhile). This ensures there are corresponding Operations for all
  48. /// TF_Operations in the underlying TF_Graph.
  49. /// </summary>
  50. /// <param name="compute_devices"></param>
  51. /// <returns></returns>
  52. public IEnumerable<Operation> _add_new_tf_operations(bool compute_devices = true)
  53. {
  54. var new_ops = c_api_util.new_tf_operations(this)
  55. .Select(c_op => _create_op_from_tf_operation(c_op, compute_device: compute_devices))
  56. .ToArray();
  57. foreach(var op in new_ops)
  58. {
  59. var new_control_inputs = _control_dependencies_for_inputs(op.inputs)
  60. .Select(x => x as Operation)
  61. .ToArray();
  62. op._add_control_inputs(new_control_inputs);
  63. op._control_flow_post_processing();
  64. }
  65. return new_ops;
  66. }
  67. }
  68. }

tensorflow框架的.NET版本,提供了丰富的特性和API,可以借此很方便地在.NET平台下搭建深度学习训练与推理流程。