| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 39883ae commit efb1c24
7 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -29,14 +29,12 @@ public abstract class DisposableObject : IDisposable | |||
| 29 | 29 | protected IntPtr _handle; | |
| 30 | 30 | protected bool _disposed; | |
| 31 | 31 | ||
| 32 | - [SuppressMessage("ReSharper", "UnusedMember.Global")] | ||
| 33 | 32 | protected DisposableObject() | |
| 34 | 33 | { } | |
| 35 | 34 | ||
| 36 | 35 | protected DisposableObject(IntPtr handle) | |
| 37 | 36 | => _handle = handle; | |
| 38 | 37 | ||
| 39 | - [SuppressMessage("ReSharper", "InvertIf")] | ||
| 40 | 38 | private void Dispose(bool disposing) | |
| 41 | 39 | { | |
| 42 | 40 | if (_disposed) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -94,11 +94,5 @@ void copy_handle_data(Tensor target_t) | |||
| 94 | 94 | // c_api.TF_GraphSetOutputHandleShapesAndTypes(target_t.graph, target_t._as_tf_output(), 0, new IntPtr[0], new int[0], new DataType[0], tf.Status.Handle); | |
| 95 | 95 | } | |
| 96 | 96 | } | |
| 97 | - | ||
| 98 | - protected override void DisposeUnmanagedResources(IntPtr handle) | ||
| 99 | - { | ||
| 100 | - base.DisposeUnmanagedResources(handle); | ||
| 101 | - _eagerTensorHandle.Dispose(); | ||
| 102 | - } | ||
| 103 | 97 | } | |
| 104 | 98 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -43,44 +43,30 @@ void Init<T>(T value) where T : unmanaged | |||
| 43 | 43 | double val => new Tensor(val), | |
| 44 | 44 | _ => throw new NotImplementedException("") | |
| 45 | 45 | }; | |
| 46 | - _tensor.SetReferencedByNDArray(); | ||
| 47 | 46 | ||
| 48 | - var _handle = c_api.TFE_NewTensorHandle(_tensor, tf.Status.Handle); | ||
| 49 | - _tensor.SetEagerTensorHandle(_handle); | ||
| 47 | + _tensor.SetReferencedByNDArray(); | ||
| 50 | 48 | } | |
| 51 | 49 | ||
| 52 | 50 | void Init(Array value, Shape? shape = null) | |
| 53 | 51 | { | |
| 54 | 52 | _tensor = new Tensor(value, shape ?? value.GetShape()); | |
| 55 | 53 | _tensor.SetReferencedByNDArray(); | |
| 56 | - | ||
| 57 | - var _handle = c_api.TFE_NewTensorHandle(_tensor, tf.Status.Handle); | ||
| 58 | - _tensor.SetEagerTensorHandle(_handle); | ||
| 59 | 54 | } | |
| 60 | 55 | ||
| 61 | 56 | void Init(Shape shape, TF_DataType dtype = TF_DataType.TF_DOUBLE) | |
| 62 | 57 | { | |
| 63 | 58 | _tensor = new Tensor(shape, dtype: dtype); | |
| 64 | 59 | _tensor.SetReferencedByNDArray(); | |
| 65 | - | ||
| 66 | - var _handle = c_api.TFE_NewTensorHandle(_tensor, tf.Status.Handle); | ||
| 67 | - _tensor.SetEagerTensorHandle(_handle); | ||
| 68 | 60 | } | |
| 69 | 61 | ||
| 70 | 62 | void Init(Tensor value, Shape? shape = null) | |
| 71 | 63 | { | |
| 72 | - if (shape is not null) | ||
| 73 | - _tensor = new Tensor(value.TensorDataPointer, shape, value.dtype); | ||
| 74 | - else | ||
| 75 | - _tensor = value; | ||
| 76 | - | ||
| 77 | - if (_tensor.TensorDataPointer == IntPtr.Zero) | ||
| 78 | - _tensor = tf.get_default_session().eval(_tensor); | ||
| 64 | + // created tensor in graph mode | ||
| 65 | + if (value.TensorDataPointer == IntPtr.Zero) | ||
| 66 | + value = tf.defaultSession.eval(value); | ||
| 79 | 67 | ||
| 68 | + _tensor = new Tensor(value.TensorDataPointer, shape ?? value.shape, value.dtype); | ||
| 80 | 69 | _tensor.SetReferencedByNDArray(); | |
| 81 | - | ||
| 82 | - var _handle = c_api.TFE_NewTensorHandle(_tensor, tf.Status.Handle); | ||
| 83 | - _tensor.SetEagerTensorHandle(_handle); | ||
| 84 | 70 | } | |
| 85 | 71 | } | |
| 86 | 72 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -6,7 +6,7 @@ | |||
| 6 | 6 | ||
| 7 | 7 | namespace Tensorflow.NumPy | |
| 8 | 8 | { | |
| 9 | - public partial class NDArray : DisposableObject | ||
| 9 | + public partial class NDArray | ||
| 10 | 10 | { | |
| 11 | 11 | Tensor _tensor; | |
| 12 | 12 | public TF_DataType dtype => _tensor.dtype; | |
@@ -58,11 +58,5 @@ public override string ToString() | |||
| 58 | 58 | { | |
| 59 | 59 | return tensor_util.to_numpy_string(_tensor); | |
| 60 | 60 | } | |
| 61 | - | ||
| 62 | - protected override void DisposeUnmanagedResources(IntPtr handle) | ||
| 63 | - { | ||
| 64 | - _tensor.EagerTensorHandle.Dispose(); | ||
| 65 | - _tensor.Dispose(); | ||
| 66 | - } | ||
| 67 | 61 | } | |
| 68 | 62 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -212,9 +212,12 @@ public TF_Output _as_tf_output() | |||
| 212 | 212 | return _tf_output.Value; | |
| 213 | 213 | } | |
| 214 | 214 | ||
| 215 | - public void SetReferencedByNDArray() => isReferencedByNDArray = true; | ||
| 216 | - public void SetEagerTensorHandle(SafeTensorHandleHandle handle) => _eagerTensorHandle = handle; | ||
| 217 | - | ||
| 215 | + public void SetReferencedByNDArray() | ||
| 216 | + { | ||
| 217 | + isReferencedByNDArray = true; | ||
| 218 | + _eagerTensorHandle = c_api.TFE_NewTensorHandle(_handle, tf.Status.Handle); | ||
| 219 | + } | ||
| 220 | + | ||
| 218 | 221 | public Tensor MaybeMove() | |
| 219 | 222 | { | |
| 220 | 223 | var tensor = c_api.TF_TensorMaybeMove(_handle); | |
@@ -256,7 +259,6 @@ public override string ToString() | |||
| 256 | 259 | } | |
| 257 | 260 | } | |
| 258 | 261 | ||
| 259 | - [SuppressMessage("ReSharper", "ConvertIfStatementToSwitchStatement")] | ||
| 260 | 262 | protected override void DisposeUnmanagedResources(IntPtr handle) | |
| 261 | 263 | { | |
| 262 | 264 | if (dtype == TF_DataType.TF_STRING) | |
@@ -274,6 +276,9 @@ protected override void DisposeUnmanagedResources(IntPtr handle) | |||
| 274 | 276 | } | |
| 275 | 277 | ||
| 276 | 278 | c_api.TF_DeleteTensor(handle); | |
| 279 | + | ||
| 280 | + if (_eagerTensorHandle is not null) | ||
| 281 | + _eagerTensorHandle.Dispose(); | ||
| 277 | 282 | } | |
| 278 | 283 | ||
| 279 | 284 | public bool IsDisposed => _disposed; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -35,7 +35,9 @@ public static class tensor_util | |||
| 35 | 35 | /// <returns></returns> | |
| 36 | 36 | public static NDArray constant_value(Tensor tensor, bool partial = false) | |
| 37 | 37 | { | |
| 38 | - if (tensor is EagerTensor) | ||
| 38 | + if (tensor.IsReferencedByNDArray) | ||
| 39 | + return new NDArray(tensor); | ||
| 40 | + else if (tensor is EagerTensor) | ||
| 39 | 41 | return tensor.numpy(); | |
| 40 | 42 | ||
| 41 | 43 | NDArray ret = _ConstantValue(tensor, partial); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -10,11 +10,5 @@ public void TestInit() | |||
| 10 | 10 | { | |
| 11 | 11 | tf.compat.v1.disable_eager_execution(); | |
| 12 | 12 | } | |
| 13 | - | ||
| 14 | - [TestCleanup] | ||
| 15 | - public void TestClean() | ||
| 16 | - { | ||
| 17 | - tf.enable_eager_execution(); | ||
| 18 | - } | ||
| 19 | 13 | } | |
| 20 | 14 | } | |
| Back | FazBrowse Home | New Git URL |
0 commit comments