| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 3052e1f commit d88fec4
14 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -513,10 +513,13 @@ public static Shape GetShape(this object data) | |||
| 513 | 513 | if (data is NDArray nd) | |
| 514 | 514 | return nd.shape; | |
| 515 | 515 | ||
| 516 | - if (data is Tensor tensor) | ||
| 516 | + else if (data is Tensor tensor) | ||
| 517 | 517 | return tensor.shape; | |
| 518 | 518 | ||
| 519 | - if (!data.GetType().IsArray) | ||
| 519 | + else if (data is Axis axis) | ||
| 520 | + return axis.IsScalar ? Shape.Scalar : new Shape(axis.axis); | ||
| 521 | + | ||
| 522 | + else if (!data.GetType().IsArray) | ||
| 520 | 523 | return Shape.Scalar; | |
| 521 | 524 | ||
| 522 | 525 | switch (data) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -17,7 +17,7 @@ public MnistDataSet(NDArray images, NDArray labels, TF_DataType dataType, bool r | |||
| 17 | 17 | ||
| 18 | 18 | NumOfExamples = (int)images.dims[0]; | |
| 19 | 19 | ||
| 20 | - images = images.reshape((images.dims[0], images.dims[1] * images.dims[2])); | ||
| 20 | + // images = images.reshape((images.dims[0], images.dims[1] * images.dims[2])); | ||
| 21 | 21 | images = images.astype(dataType); | |
| 22 | 22 | // for debug np.multiply performance | |
| 23 | 23 | var sw = new Stopwatch(); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -123,9 +123,7 @@ private NDArray ExtractImages(string file, int? limit = null) | |||
| 123 | 123 | ||
| 124 | 124 | bytestream.Read(buf, 0, buf.Length); | |
| 125 | 125 | ||
| 126 | - var data = np.frombuffer(buf, new Shape(buf.Length), np.@byte); | ||
| 127 | - data = data.reshape((num_images, rows, cols, 1)); | ||
| 128 | - | ||
| 126 | + var data = np.frombuffer(buf, (num_images, rows * cols), np.@byte); | ||
| 129 | 127 | return data; | |
| 130 | 128 | } | |
| 131 | 129 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -24,6 +24,7 @@ namespace Tensorflow | |||
| 24 | 24 | public record Axis(params int[] axis) | |
| 25 | 25 | { | |
| 26 | 26 | public int size => axis == null ? -1 : axis.Length; | |
| 27 | + public bool IsScalar { get; init; } | ||
| 27 | 28 | ||
| 28 | 29 | public int this[int index] => axis[index]; | |
| 29 | 30 | ||
@@ -34,7 +35,7 @@ public static implicit operator int(Axis axis) | |||
| 34 | 35 | => axis.axis[0]; | |
| 35 | 36 | ||
| 36 | 37 | public static implicit operator Axis(int axis) | |
| 37 | - => new Axis(axis); | ||
| 38 | + => new Axis(axis) { IsScalar = true }; | ||
| 38 | 39 | ||
| 39 | 40 | public static implicit operator Axis((int, int) axis) | |
| 40 | 41 | => new Axis(axis.Item1, axis.Item2); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -15,7 +15,7 @@ public NDArray this[params int[] indices] | |||
| 15 | 15 | Start = x, | |
| 16 | 16 | Stop = x + 1, | |
| 17 | 17 | IsIndex = true | |
| 18 | - })); | ||
| 18 | + }).ToArray()); | ||
| 19 | 19 | ||
| 20 | 20 | set => SetData(indices.Select(x => | |
| 21 | 21 | { | |
@@ -55,21 +55,58 @@ public NDArray this[NDArray mask] | |||
| 55 | 55 | } | |
| 56 | 56 | } | |
| 57 | 57 | ||
| 58 | - NDArray GetData(IEnumerable<Slice> slices) | ||
| 58 | + | ||
| 59 | + unsafe NDArray GetData(Slice[] slices) | ||
| 59 | 60 | { | |
| 60 | 61 | if (shape.IsScalar) | |
| 61 | 62 | return GetScalar(); | |
| 62 | 63 | ||
| 64 | + if (SliceHelper.AreAllIndex(slices, out var indices1)) | ||
| 65 | + { | ||
| 66 | + var newshape = ShapeHelper.GetShape(shape, slices); | ||
| 67 | + if (newshape.IsScalar) | ||
| 68 | + { | ||
| 69 | + var offset = ShapeHelper.GetOffset(shape, indices1); | ||
| 70 | + return GetScalar((ulong)offset); | ||
| 71 | + } | ||
| 72 | + else | ||
| 73 | + { | ||
| 74 | + return GetArrayData(newshape, indices1); | ||
| 75 | + } | ||
| 76 | + } | ||
| 77 | + else if (slices.Count() == 1) | ||
| 78 | + { | ||
| 79 | + var slice = slices[0]; | ||
| 80 | + if (slice.Step == 1) | ||
| 81 | + { | ||
| 82 | + var newshape = ShapeHelper.GetShape(shape, slice); | ||
| 83 | + var array = new NDArray(newshape, dtype: dtype); | ||
| 84 | + | ||
| 85 | + var new_dims = new int[shape.ndim]; | ||
| 86 | + new_dims[0] = slice.Start ?? 0; | ||
| 87 | + //for (int i = 1; i < shape.ndim; i++) | ||
| 88 | + //new_dims[i] = (int)shape.dims[i]; | ||
| 89 | + | ||
| 90 | + var offset = ShapeHelper.GetOffset(shape, new_dims); | ||
| 91 | + var src = (byte*)data + (ulong)offset * dtypesize; | ||
| 92 | + var dst = (byte*)array.data; | ||
| 93 | + var len = (ulong)newshape.size * dtypesize; | ||
| 94 | + | ||
| 95 | + System.Buffer.MemoryCopy(src, dst, len, len); | ||
| 96 | + | ||
| 97 | + return array; | ||
| 98 | + } | ||
| 99 | + } | ||
| 100 | + | ||
| 101 | + // default, performance is bad | ||
| 63 | 102 | var tensor = base[slices.ToArray()]; | |
| 64 | 103 | if (tensor.Handle == null) | |
| 65 | 104 | { | |
| 66 | 105 | if (tf.executing_eagerly()) | |
| 67 | 106 | tensor = tf.defaultSession.eval(tensor); | |
| 68 | - else | ||
| 69 | - return new NDArray(tensor); | ||
| 70 | 107 | } | |
| 71 | - | ||
| 72 | - return new NDArray(tensor); | ||
| 108 | + | ||
| 109 | + return new NDArray(tensor, tf.executing_eagerly()); | ||
| 73 | 110 | } | |
| 74 | 111 | ||
| 75 | 112 | unsafe T GetAtIndex<T>(params int[] indices) where T : unmanaged | |
@@ -78,17 +115,26 @@ unsafe T GetAtIndex<T>(params int[] indices) where T : unmanaged | |||
| 78 | 115 | return *((T*)data + offset); | |
| 79 | 116 | } | |
| 80 | 117 | ||
| 81 | - NDArray GetScalar() | ||
| 118 | + unsafe NDArray GetScalar(ulong offset = 0) | ||
| 82 | 119 | { | |
| 83 | 120 | var array = new NDArray(Shape.Scalar, dtype: dtype); | |
| 84 | - unsafe | ||
| 85 | - { | ||
| 86 | - var src = (byte*)data + dtypesize; | ||
| 87 | - System.Buffer.MemoryCopy(src, array.buffer.ToPointer(), bytesize, bytesize); | ||
| 88 | - } | ||
| 121 | + var src = (byte*)data + offset * dtypesize; | ||
| 122 | + System.Buffer.MemoryCopy(src, array.buffer.ToPointer(), dtypesize, dtypesize); | ||
| 89 | 123 | return array; | |
| 90 | 124 | } | |
| 91 | 125 | ||
| 126 | + unsafe NDArray GetArrayData(Shape newshape, int[] indices) | ||
| 127 | + { | ||
| 128 | + var offset = ShapeHelper.GetOffset(shape, indices); | ||
| 129 | + var len = (ulong)newshape.size * dtypesize; | ||
| 130 | + var array = new NDArray(newshape, dtype: dtype); | ||
| 131 | + | ||
| 132 | + var src = (byte*)data + (ulong)offset * dtypesize; | ||
| 133 | + System.Buffer.MemoryCopy(src, array.data.ToPointer(), len, len); | ||
| 134 | + | ||
| 135 | + return array; | ||
| 136 | + } | ||
| 137 | + | ||
| 92 | 138 | NDArray GetData(int[] indices, int axis = 0) | |
| 93 | 139 | { | |
| 94 | 140 | if (shape.IsScalar) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -5,7 +5,7 @@ | |||
| 5 | 5 | ||
| 6 | 6 | namespace Tensorflow.NumPy | |
| 7 | 7 | { | |
| 8 | - internal class ShapeHelper | ||
| 8 | + public class ShapeHelper | ||
| 9 | 9 | { | |
| 10 | 10 | public static long GetSize(Shape shape) | |
| 11 | 11 | { | |
@@ -41,6 +41,34 @@ public static long[] GetStrides(Shape shape) | |||
| 41 | 41 | return strides; | |
| 42 | 42 | } | |
| 43 | 43 | ||
| 44 | + public static Shape GetShape(Shape shape1, params Slice[] slices) | ||
| 45 | + { | ||
| 46 | + var new_dims = shape1.dims.ToArray(); | ||
| 47 | + slices = SliceHelper.AlignWithShape(shape1, slices); | ||
| 48 | + | ||
| 49 | + for (int i = 0; i < shape1.dims.Length; i++) | ||
| 50 | + { | ||
| 51 | + Slice slice = slices[i]; | ||
| 52 | + if (slice.Equals(Slice.All)) | ||
| 53 | + new_dims[i] = shape1.dims[i]; | ||
| 54 | + else if (slice.IsIndex) | ||
| 55 | + new_dims[i] = 1; | ||
| 56 | + else // range | ||
| 57 | + new_dims[i] = (slice.Stop ?? shape1.dims[i]) - (slice.Start ?? 0); | ||
| 58 | + } | ||
| 59 | + | ||
| 60 | + // strip first dim if is index | ||
| 61 | + var return_dims = new List<long>(); | ||
| 62 | + for (int i = 0; i< new_dims.Length; i++) | ||
| 63 | + { | ||
| 64 | + if (slices[i].IsIndex) | ||
| 65 | + continue; | ||
| 66 | + return_dims.add(new_dims[i]); | ||
| 67 | + } | ||
| 68 | + | ||
| 69 | + return new Shape(return_dims.ToArray()); | ||
| 70 | + } | ||
| 71 | + | ||
| 44 | 72 | public static bool Equals(Shape shape, object target) | |
| 45 | 73 | { | |
| 46 | 74 | switch (target) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,56 @@ | |||
| 1 | + using System; | ||
| 2 | + using System.Collections.Generic; | ||
| 3 | + using System.Linq; | ||
| 4 | + using System.Text; | ||
| 5 | + | ||
| 6 | + namespace Tensorflow.NumPy | ||
| 7 | + { | ||
| 8 | + public class SliceHelper | ||
| 9 | + { | ||
| 10 | + public static Slice[] AlignWithShape(Shape shape, Slice[] slices) | ||
| 11 | + { | ||
| 12 | + // align slices | ||
| 13 | + var ndim = shape.ndim; | ||
| 14 | + var new_slices = new List<Slice>(); | ||
| 15 | + var slice_index = 0; | ||
| 16 | + | ||
| 17 | + for (int i = 0; i < ndim; i++) | ||
| 18 | + { | ||
| 19 | + if (slice_index > slices.Length - 1) | ||
| 20 | + { | ||
| 21 | + new_slices.Add(Slice.All); | ||
| 22 | + continue; | ||
| 23 | + } | ||
| 24 | + | ||
| 25 | + if (slices[slice_index] == Slice.All) | ||
| 26 | + { | ||
| 27 | + new_slices.Add(Slice.All); | ||
| 28 | + for (int j = 0; j < ndim - slices.Length; j++) | ||
| 29 | + { | ||
| 30 | + new_slices.Add(Slice.All); | ||
| 31 | + i++; | ||
| 32 | + } | ||
| 33 | + } | ||
| 34 | + else | ||
| 35 | + { | ||
| 36 | + new_slices.Add(slices[slice_index]); | ||
| 37 | + } | ||
| 38 | + slice_index++; | ||
| 39 | + } | ||
| 40 | + | ||
| 41 | + return new_slices.ToArray(); | ||
| 42 | + } | ||
| 43 | + | ||
| 44 | + public static bool AreAllIndex(Slice[] slices, out int[] indices) | ||
| 45 | + { | ||
| 46 | + indices = new int[slices.Length]; | ||
| 47 | + for (int i = 0; i< slices.Length; i++) | ||
| 48 | + { | ||
| 49 | + indices[i] = slices[i].Start ?? 0; | ||
| 50 | + if (!slices[i].IsIndex) | ||
| 51 | + return false; | ||
| 52 | + } | ||
| 53 | + return true; | ||
| 54 | + } | ||
| 55 | + } | ||
| 56 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -28,7 +28,7 @@ public NDArray(byte[] bytes, Shape shape, TF_DataType dtype) | |||
| 28 | 28 | public NDArray(IntPtr address, Shape shape, TF_DataType dtype) | |
| 29 | 29 | : base(address, shape, dtype) { NewEagerTensorHandle(); } | |
| 30 | 30 | ||
| 31 | - public NDArray(Tensor tensor) : base(tensor.Handle) | ||
| 31 | + public NDArray(Tensor tensor, bool eval = true) : base(tensor.Handle) | ||
| 32 | 32 | { | |
| 33 | 33 | if (_handle is null) | |
| 34 | 34 | { | |
@@ -53,9 +53,12 @@ public static NDArray Scalar<T>(T value) where T : unmanaged | |||
| 53 | 53 | ||
| 54 | 54 | void NewEagerTensorHandle() | |
| 55 | 55 | { | |
| 56 | - _id = ops.uid(); | ||
| 57 | - _eagerTensorHandle = c_api.TFE_NewTensorHandle(_handle, tf.Status.Handle); | ||
| 58 | - tf.Status.Check(true); | ||
| 56 | + if(_handle is not null) | ||
| 57 | + { | ||
| 58 | + _id = ops.uid(); | ||
| 59 | + _eagerTensorHandle = c_api.TFE_NewTensorHandle(_handle, tf.Status.Handle); | ||
| 60 | + tf.Status.Check(true); | ||
| 61 | + } | ||
| 59 | 62 | } | |
| 60 | 63 | } | |
| 61 | 64 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -115,11 +115,12 @@ public class Slice | |||
| 115 | 115 | /// <param name="start">Start index of the slice, null means from the start of the array</param> | |
| 116 | 116 | /// <param name="stop">Stop index (first index after end of slice), null means to the end of the array</param> | |
| 117 | 117 | /// <param name="step">Optional step to select every n-th element, defaults to 1</param> | |
| 118 | - public Slice(int? start = null, int? stop = null, int step = 1) | ||
| 118 | + public Slice(int? start = null, int? stop = null, int step = 1, bool isIndex = false) | ||
| 119 | 119 | { | |
| 120 | 120 | Start = start; | |
| 121 | 121 | Stop = stop; | |
| 122 | 122 | Step = step; | |
| 123 | + IsIndex = isIndex; | ||
| 123 | 124 | } | |
| 124 | 125 | ||
| 125 | 126 | public Slice(string slice_notation) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -8,7 +8,7 @@ namespace Tensorflow | |||
| 8 | 8 | public sealed class SafeStringTensorHandle : SafeTensorHandle | |
| 9 | 9 | { | |
| 10 | 10 | Shape _shape; | |
| 11 | - SafeTensorHandle _handle; | ||
| 11 | + IntPtr _handle; | ||
| 12 | 12 | const int TF_TSRING_SIZE = 24; | |
| 13 | 13 | ||
| 14 | 14 | protected SafeStringTensorHandle() | |
@@ -18,7 +18,7 @@ protected SafeStringTensorHandle() | |||
| 18 | 18 | public SafeStringTensorHandle(SafeTensorHandle handle, Shape shape) | |
| 19 | 19 | : base(handle.DangerousGetHandle()) | |
| 20 | 20 | { | |
| 21 | - _handle = handle; | ||
| 21 | + _handle = c_api.TF_TensorData(handle); | ||
| 22 | 22 | _shape = shape; | |
| 23 | 23 | } | |
| 24 | 24 | ||
@@ -28,15 +28,10 @@ protected override bool ReleaseHandle() | |||
| 28 | 28 | print($"Delete StringTensorHandle 0x{handle.ToString("x16")}"); | |
| 29 | 29 | #endif | |
| 30 | 30 | ||
| 31 | - long size = 1; | ||
| 32 | - foreach (var s in _shape.dims) | ||
| 33 | - size *= s; | ||
| 34 | - var tstr = c_api.TF_TensorData(_handle); | ||
| 35 | - | ||
| 36 | - for (int i = 0; i < size; i++) | ||
| 31 | + for (int i = 0; i < _shape.size; i++) | ||
| 37 | 32 | { | |
| 38 | - c_api.TF_StringDealloc(tstr); | ||
| 39 | - tstr += TF_TSRING_SIZE; | ||
| 33 | + c_api.TF_StringDealloc(_handle); | ||
| 34 | + _handle += TF_TSRING_SIZE; | ||
| 40 | 35 | } | |
| 41 | 36 | ||
| 42 | 37 | SetHandle(IntPtr.Zero); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments