| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent afaf0c8 commit 855eba9
21 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -448,6 +448,17 @@ public Tensor reduce_all(Tensor input_tensor, int[] axis = null, bool keepdims = | |||
| 448 | 448 | public Tensor reduce_prod(Tensor input_tensor, int[] axis = null, bool keepdims = false, string name = null) | |
| 449 | 449 | => math_ops.reduce_prod(input_tensor, axis: axis, keepdims: keepdims, name: name); | |
| 450 | 450 | ||
| 451 | + /// <summary> | ||
| 452 | + /// Computes the sum of elements across dimensions of a tensor. | ||
| 453 | + /// </summary> | ||
| 454 | + /// <param name="input_tensors"></param> | ||
| 455 | + /// <param name="axis"></param> | ||
| 456 | + /// <param name="keepdims"></param> | ||
| 457 | + /// <param name="name"></param> | ||
| 458 | + /// <returns></returns> | ||
| 459 | + public Tensor reduce_sum(Tensor[] input_tensors, int? axis = null, bool keepdims = false, string name = null) | ||
| 460 | + => math_ops.reduce_sum(input_tensors, axis: axis, keepdims: keepdims, name: name); | ||
| 461 | + | ||
| 451 | 462 | /// <summary> | |
| 452 | 463 | /// Computes the sum of elements across dimensions of a tensor. | |
| 453 | 464 | /// </summary> | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -9,13 +9,21 @@ public class Context : DisposableObject | |||
| 9 | 9 | ||
| 10 | 10 | public int default_execution_mode; | |
| 11 | 11 | public string device_name = ""; | |
| 12 | + bool _initialized = false; | ||
| 12 | 13 | ||
| 13 | 14 | public Context(ContextOptions opts, Status status) | |
| 14 | 15 | { | |
| 15 | 16 | _handle = c_api.TFE_NewContext(opts, status); | |
| 16 | 17 | status.Check(true); | |
| 17 | 18 | } | |
| 18 | 19 | ||
| 20 | + public void ensure_initialized() | ||
| 21 | + { | ||
| 22 | + if (_initialized) | ||
| 23 | + return; | ||
| 24 | + _initialized = true; | ||
| 25 | + } | ||
| 26 | + | ||
| 19 | 27 | /// <summary> | |
| 20 | 28 | /// Dispose any unmanaged resources related to given <paramref name="handle"/>. | |
| 21 | 29 | /// </summary> | |
@@ -27,5 +35,8 @@ protected sealed override void DisposeUnmanagedResources(IntPtr handle) | |||
| 27 | 35 | ||
| 28 | 36 | public static implicit operator IntPtr(Context ctx) | |
| 29 | 37 | => ctx._handle; | |
| 38 | + | ||
| 39 | + public static implicit operator TFE_Context(Context ctx) | ||
| 40 | + => new TFE_Context(ctx._handle); | ||
| 30 | 41 | } | |
| 31 | 42 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -17,6 +17,10 @@ protected sealed override void DisposeUnmanagedResources(IntPtr handle) | |||
| 17 | 17 | ||
| 18 | 18 | public static implicit operator IntPtr(ContextOptions opts) | |
| 19 | 19 | => opts._handle; | |
| 20 | + | ||
| 21 | + public static implicit operator TFE_ContextOptions(ContextOptions opts) | ||
| 22 | + => new TFE_ContextOptions(opts._handle); | ||
| 23 | + | ||
| 20 | 24 | } | |
| 21 | 25 | ||
| 22 | 26 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,12 +1,67 @@ | |||
| 1 | 1 | using System.Collections.Generic; | |
| 2 | + using System; | ||
| 3 | + using System.Linq; | ||
| 2 | 4 | ||
| 3 | 5 | namespace Tensorflow.Eager | |
| 4 | 6 | { | |
| 5 | 7 | public class Execute | |
| 6 | 8 | { | |
| 9 | + /// <summary> | ||
| 10 | + /// Execute a TensorFlow operation. | ||
| 11 | + /// </summary> | ||
| 12 | + /// <param name="op_name"> | ||
| 13 | + /// Name of the TensorFlow operation (see REGISTER_OP in C++ code) to | ||
| 14 | + /// execute. | ||
| 15 | + /// </param> | ||
| 16 | + /// <param name="num_outputs"> | ||
| 17 | + /// The number of outputs of the operation to fetch. | ||
| 18 | + /// </param> | ||
| 19 | + /// <param name="inputs"> | ||
| 20 | + /// A list of inputs to the operation. Each entry should be a Tensor, or | ||
| 21 | + /// a value which can be passed to the Tensor constructor to create one. | ||
| 22 | + /// </param> | ||
| 23 | + /// <param name="attrs"> | ||
| 24 | + /// A tuple with alternating string attr names and attr values for this | ||
| 25 | + /// operation. | ||
| 26 | + /// </param> | ||
| 27 | + /// <param name="ctx">The value of context.context().</param> | ||
| 28 | + /// <param name="name">Customized name for the operation.</param> | ||
| 29 | + /// <returns>List of output Tensor objects. The list is empty if there are no outputs</returns> | ||
| 30 | + public Tensor execute(Context ctx, string op_name, Tensor[] inputs, object[] attrs, string name = null) | ||
| 31 | + { | ||
| 32 | + ctx.ensure_initialized(); | ||
| 33 | + using (var status = new Status()) | ||
| 34 | + { | ||
| 35 | + var retVals = wrap_tfe_src.TFE_Py_Execute(ctx, ctx.device_name, op_name, inputs, attrs, 1, status); | ||
| 36 | + | ||
| 37 | + var t = c_api.TFE_TensorHandleResolve(retVals[0], status); | ||
| 38 | + status.Check(true); | ||
| 39 | + | ||
| 40 | + return new EagerTensor(t); | ||
| 41 | + } | ||
| 42 | + } | ||
| 43 | + | ||
| 44 | + public (TF_DataType, Tensor) args_to_matching_eager(Tensor[] l, Context ctx, TF_DataType default_dtype = TF_DataType.DtInvalid) | ||
| 45 | + { | ||
| 46 | + var dtype = default_dtype; | ||
| 47 | + if(dtype == TF_DataType.DtInvalid) | ||
| 48 | + { | ||
| 49 | + var tensor = ops.convert_to_tensor(l, dtype, preferred_dtype: default_dtype, ctx: ctx); | ||
| 50 | + | ||
| 51 | + if (dtype == TF_DataType.DtInvalid) | ||
| 52 | + dtype = tensor.dtype; | ||
| 53 | + | ||
| 54 | + return (dtype, tensor); | ||
| 55 | + } | ||
| 56 | + else | ||
| 57 | + { | ||
| 58 | + return (dtype, l[0]); | ||
| 59 | + } | ||
| 60 | + } | ||
| 61 | + | ||
| 7 | 62 | public void record_gradient(string op_name, InputList inputs, Dictionary<string, object> attrs, Tensor[] results, string name = null) | |
| 8 | 63 | { | |
| 9 | - pywrap_tfe_src.RecordGradient(op_name, inputs._inputs, attrs, results, name); | ||
| 64 | + wrap_tfe_src.RecordGradient(op_name, inputs._inputs, attrs, results, name); | ||
| 10 | 65 | } | |
| 11 | 66 | } | |
| 12 | 67 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,23 @@ | |||
| 1 | + using System; | ||
| 2 | + using System.Collections.Generic; | ||
| 3 | + using System.Text; | ||
| 4 | + | ||
| 5 | + namespace Tensorflow.Eager | ||
| 6 | + { | ||
| 7 | + public struct TFE_Context | ||
| 8 | + { | ||
| 9 | + IntPtr _handle; | ||
| 10 | + | ||
| 11 | + public TFE_Context(IntPtr handle) | ||
| 12 | + => _handle = handle; | ||
| 13 | + | ||
| 14 | + public static implicit operator TFE_Context(IntPtr handle) | ||
| 15 | + => new TFE_Context(handle); | ||
| 16 | + | ||
| 17 | + public static implicit operator IntPtr(TFE_Context tensor) | ||
| 18 | + => tensor._handle; | ||
| 19 | + | ||
| 20 | + public override string ToString() | ||
| 21 | + => $"TFE_Context {_handle}"; | ||
| 22 | + } | ||
| 23 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,23 @@ | |||
| 1 | + using System; | ||
| 2 | + using System.Collections.Generic; | ||
| 3 | + using System.Text; | ||
| 4 | + | ||
| 5 | + namespace Tensorflow.Eager | ||
| 6 | + { | ||
| 7 | + public struct TFE_ContextOptions | ||
| 8 | + { | ||
| 9 | + IntPtr _handle; | ||
| 10 | + | ||
| 11 | + public TFE_ContextOptions(IntPtr handle) | ||
| 12 | + => _handle = handle; | ||
| 13 | + | ||
| 14 | + public static implicit operator TFE_ContextOptions(IntPtr handle) | ||
| 15 | + => new TFE_ContextOptions(handle); | ||
| 16 | + | ||
| 17 | + public static implicit operator IntPtr(TFE_ContextOptions tensor) | ||
| 18 | + => tensor._handle; | ||
| 19 | + | ||
| 20 | + public override string ToString() | ||
| 21 | + => $"TFE_ContextOptions {_handle}"; | ||
| 22 | + } | ||
| 23 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,23 @@ | |||
| 1 | + using System; | ||
| 2 | + using System.Collections.Generic; | ||
| 3 | + using System.Text; | ||
| 4 | + | ||
| 5 | + namespace Tensorflow.Eager | ||
| 6 | + { | ||
| 7 | + public struct TFE_Executor | ||
| 8 | + { | ||
| 9 | + IntPtr _handle; | ||
| 10 | + | ||
| 11 | + public TFE_Executor(IntPtr handle) | ||
| 12 | + => _handle = handle; | ||
| 13 | + | ||
| 14 | + public static implicit operator TFE_Executor(IntPtr handle) | ||
| 15 | + => new TFE_Executor(handle); | ||
| 16 | + | ||
| 17 | + public static implicit operator IntPtr(TFE_Executor tensor) | ||
| 18 | + => tensor._handle; | ||
| 19 | + | ||
| 20 | + public override string ToString() | ||
| 21 | + => $"TFE_Executor {_handle}"; | ||
| 22 | + } | ||
| 23 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,23 @@ | |||
| 1 | + using System; | ||
| 2 | + using System.Collections.Generic; | ||
| 3 | + using System.Text; | ||
| 4 | + | ||
| 5 | + namespace Tensorflow.Eager | ||
| 6 | + { | ||
| 7 | + public struct TFE_Op | ||
| 8 | + { | ||
| 9 | + IntPtr _handle; | ||
| 10 | + | ||
| 11 | + public TFE_Op(IntPtr handle) | ||
| 12 | + => _handle = handle; | ||
| 13 | + | ||
| 14 | + public static implicit operator TFE_Op(IntPtr handle) | ||
| 15 | + => new TFE_Op(handle); | ||
| 16 | + | ||
| 17 | + public static implicit operator IntPtr(TFE_Op tensor) | ||
| 18 | + => tensor._handle; | ||
| 19 | + | ||
| 20 | + public override string ToString() | ||
| 21 | + => $"TFE_Op {_handle}"; | ||
| 22 | + } | ||
| 23 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,23 @@ | |||
| 1 | + using System; | ||
| 2 | + using System.Collections.Generic; | ||
| 3 | + using System.Text; | ||
| 4 | + | ||
| 5 | + namespace Tensorflow.Eager | ||
| 6 | + { | ||
| 7 | + public struct TFE_TensorHandle | ||
| 8 | + { | ||
| 9 | + IntPtr _handle; | ||
| 10 | + | ||
| 11 | + public TFE_TensorHandle(IntPtr handle) | ||
| 12 | + => _handle = handle; | ||
| 13 | + | ||
| 14 | + public static implicit operator TFE_TensorHandle(IntPtr handle) | ||
| 15 | + => new TFE_TensorHandle(handle); | ||
| 16 | + | ||
| 17 | + public static implicit operator IntPtr(TFE_TensorHandle tensor) | ||
| 18 | + => tensor._handle; | ||
| 19 | + | ||
| 20 | + public override string ToString() | ||
| 21 | + => $"TFE_TensorHandle {_handle}"; | ||
| 22 | + } | ||
| 23 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,5 +1,6 @@ | |||
| 1 | 1 | using System; | |
| 2 | 2 | using System.Runtime.InteropServices; | |
| 3 | + using Tensorflow.Eager; | ||
| 3 | 4 | using TFE_Executor = System.IntPtr; | |
| 4 | 5 | ||
| 5 | 6 | namespace Tensorflow | |
@@ -11,7 +12,7 @@ public partial class c_api | |||
| 11 | 12 | /// </summary> | |
| 12 | 13 | /// <returns>TFE_ContextOptions*</returns> | |
| 13 | 14 | [DllImport(TensorFlowLibName)] | |
| 14 | - public static extern IntPtr TFE_NewContextOptions(); | ||
| 15 | + public static extern TFE_ContextOptions TFE_NewContextOptions(); | ||
| 15 | 16 | ||
| 16 | 17 | /// <summary> | |
| 17 | 18 | /// Destroy an options object. | |
@@ -70,7 +71,7 @@ public partial class c_api | |||
| 70 | 71 | /// <param name="status">TF_Status*</param> | |
| 71 | 72 | /// <returns>TFE_Context*</returns> | |
| 72 | 73 | [DllImport(TensorFlowLibName)] | |
| 73 | - public static extern IntPtr TFE_NewContext(IntPtr opts, IntPtr status); | ||
| 74 | + public static extern TFE_Context TFE_NewContext(IntPtr opts, IntPtr status); | ||
| 74 | 75 | ||
| 75 | 76 | /// <summary> | |
| 76 | 77 | /// | |
@@ -88,7 +89,7 @@ public partial class c_api | |||
| 88 | 89 | /// <param name="num_retvals">int*</param> | |
| 89 | 90 | /// <param name="status">TF_Status*</param> | |
| 90 | 91 | [DllImport(TensorFlowLibName)] | |
| 91 | - public static extern void TFE_Execute(IntPtr op, IntPtr[] retvals, ref int num_retvals, IntPtr status); | ||
| 92 | + public static extern void TFE_Execute(TFE_Op op, IntPtr[] retvals, ref int num_retvals, IntPtr status); | ||
| 92 | 93 | ||
| 93 | 94 | /// <summary> | |
| 94 | 95 | /// | |
@@ -98,7 +99,7 @@ public partial class c_api | |||
| 98 | 99 | /// <param name="status">TF_Status*</param> | |
| 99 | 100 | /// <returns></returns> | |
| 100 | 101 | [DllImport(TensorFlowLibName)] | |
| 101 | - public static extern IntPtr TFE_NewOp(IntPtr ctx, string op_or_function_name, IntPtr status); | ||
| 102 | + public static extern TFE_Op TFE_NewOp(IntPtr ctx, string op_or_function_name, IntPtr status); | ||
| 102 | 103 | ||
| 103 | 104 | /// <summary> | |
| 104 | 105 | /// | |
@@ -150,7 +151,7 @@ public partial class c_api | |||
| 150 | 151 | /// <param name="device_name"></param> | |
| 151 | 152 | /// <param name="status"></param> | |
| 152 | 153 | [DllImport(TensorFlowLibName)] | |
| 153 | - public static extern void TFE_OpSetDevice(IntPtr op, string device_name, IntPtr status); | ||
| 154 | + public static extern void TFE_OpSetDevice(TFE_Op op, string device_name, IntPtr status); | ||
| 154 | 155 | ||
| 155 | 156 | /// <summary> | |
| 156 | 157 | /// | |
@@ -167,7 +168,7 @@ public partial class c_api | |||
| 167 | 168 | /// <param name="t">const tensorflow::Tensor&</param> | |
| 168 | 169 | /// <returns>TFE_TensorHandle*</returns> | |
| 169 | 170 | [DllImport(TensorFlowLibName)] | |
| 170 | - public static extern IntPtr TFE_NewTensorHandle(IntPtr t, IntPtr status); | ||
| 171 | + public static extern TFE_TensorHandle TFE_NewTensorHandle(IntPtr t, IntPtr status); | ||
| 171 | 172 | ||
| 172 | 173 | /// <summary> | |
| 173 | 174 | /// Sets the default execution mode (sync/async). Note that this can be | |
@@ -195,7 +196,7 @@ public partial class c_api | |||
| 195 | 196 | /// <param name="status">TF_Status*</param> | |
| 196 | 197 | /// <returns></returns> | |
| 197 | 198 | [DllImport(TensorFlowLibName)] | |
| 198 | - public static extern IntPtr TFE_TensorHandleResolve(IntPtr h, IntPtr status); | ||
| 199 | + public static extern TF_Tensor TFE_TensorHandleResolve(IntPtr h, IntPtr status); | ||
| 199 | 200 | ||
| 200 | 201 | /// <summary> | |
| 201 | 202 | /// This function will block till the operation that produces `h` has completed. | |
@@ -252,7 +253,7 @@ public partial class c_api | |||
| 252 | 253 | /// <param name="is_async"></param> | |
| 253 | 254 | /// <returns>TFE_Executor*</returns> | |
| 254 | 255 | [DllImport(TensorFlowLibName)] | |
| 255 | - public static extern IntPtr TFE_NewExecutor(bool is_async); | ||
| 256 | + public static extern TFE_Executor TFE_NewExecutor(bool is_async); | ||
| 256 | 257 | ||
| 257 | 258 | /// <summary> | |
| 258 | 259 | /// Deletes the eager Executor without waiting for enqueued nodes. Please call | |
| Back | FazBrowse Home | New Git URL |
0 commit comments