| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 99fc016 commit 2001619
14 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -21,6 +21,7 @@ limitations under the License. | |||
| 21 | 21 | using static Tensorflow.Binding; | |
| 22 | 22 | using Google.Protobuf; | |
| 23 | 23 | using Tensorflow.Util; | |
| 24 | + using Tensorflow.NumPy; | ||
| 24 | 25 | ||
| 25 | 26 | namespace Tensorflow.Contexts | |
| 26 | 27 | { | |
@@ -40,8 +41,15 @@ public sealed partial class Context : IDisposable | |||
| 40 | 41 | public FunctionCallOptions FunctionCallOptions { get; } | |
| 41 | 42 | ||
| 42 | 43 | SafeContextHandle _handle; | |
| 43 | - public SafeContextHandle Handle => _handle; | ||
| 44 | - | ||
| 44 | + public SafeContextHandle Handle | ||
| 45 | + { | ||
| 46 | + get | ||
| 47 | + { | ||
| 48 | + if (_handle == null) | ||
| 49 | + ensure_initialized(); | ||
| 50 | + return _handle; | ||
| 51 | + } | ||
| 52 | + } | ||
| 45 | 53 | int? _seed; | |
| 46 | 54 | Random _rng; | |
| 47 | 55 | ||
@@ -142,7 +150,11 @@ public bool has_graph_arg(params object[] args) | |||
| 142 | 150 | bool has_graph_arg = !tf.Context.executing_eagerly(); | |
| 143 | 151 | foreach (var el in flatten_args) | |
| 144 | 152 | { | |
| 145 | - if (el is Tensor tensor && tensor.IsCreatedInGraphMode) | ||
| 153 | + if (el is NDArray) | ||
| 154 | + continue; | ||
| 155 | + else if (el is EagerTensor) | ||
| 156 | + continue; | ||
| 157 | + else if (el is Tensor) | ||
| 146 | 158 | { | |
| 147 | 159 | has_graph_arg = true; | |
| 148 | 160 | break; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,30 @@ | |||
| 1 | + using MethodBoundaryAspect.Fody.Attributes; | ||
| 2 | + using System; | ||
| 3 | + using System.Collections.Generic; | ||
| 4 | + using System.Linq; | ||
| 5 | + using Tensorflow.Eager; | ||
| 6 | + using Tensorflow.Functions; | ||
| 7 | + using static Tensorflow.Binding; | ||
| 8 | + | ||
| 9 | + namespace Tensorflow.NumPy | ||
| 10 | + { | ||
| 11 | + public sealed class AutoNumPyAttribute : OnMethodBoundaryAspect | ||
| 12 | + { | ||
| 13 | + bool _changedMode = false; | ||
| 14 | + | ||
| 15 | + public override void OnEntry(MethodExecutionArgs args) | ||
| 16 | + { | ||
| 17 | + if (!tf.executing_eagerly()) | ||
| 18 | + { | ||
| 19 | + tf.Context.eager_mode(); | ||
| 20 | + _changedMode = true; | ||
| 21 | + } | ||
| 22 | + } | ||
| 23 | + | ||
| 24 | + public override void OnExit(MethodExecutionArgs args) | ||
| 25 | + { | ||
| 26 | + if (_changedMode) | ||
| 27 | + tf.Context.restore_mode(); | ||
| 28 | + } | ||
| 29 | + } | ||
| 30 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,4 +1,4 @@ | |||
| 1 | - using System; | ||
| 1 | + using System; | ||
| 2 | 2 | using System.Collections; | |
| 3 | 3 | using System.Collections.Generic; | |
| 4 | 4 | using System.IO; | |
@@ -27,6 +27,33 @@ public NDArray load(string file) | |||
| 27 | 27 | return result.reshape(shape); | |
| 28 | 28 | } | |
| 29 | 29 | ||
| 30 | + public Array LoadMatrix(Stream stream) | ||
| 31 | + { | ||
| 32 | + using (var reader = new BinaryReader(stream, System.Text.Encoding.ASCII, leaveOpen: true)) | ||
| 33 | + { | ||
| 34 | + int bytes; | ||
| 35 | + Type type; | ||
| 36 | + int[] shape; | ||
| 37 | + if (!ParseReader(reader, out bytes, out type, out shape)) | ||
| 38 | + throw new FormatException(); | ||
| 39 | + | ||
| 40 | + Array matrix = Array.CreateInstance(type, shape); | ||
| 41 | + | ||
| 42 | + //if (type == typeof(String)) | ||
| 43 | + //return ReadStringMatrix(reader, matrix, bytes, type, shape); | ||
| 44 | + return ReadValueMatrix(reader, matrix, bytes, type, shape); | ||
| 45 | + } | ||
| 46 | + } | ||
| 47 | + | ||
| 48 | + public T Load<T>(Stream stream) | ||
| 49 | + where T : class, | ||
| 50 | + ICloneable, IList, ICollection, IEnumerable, IStructuralComparable, IStructuralEquatable | ||
| 51 | + { | ||
| 52 | + // if (typeof(T).IsArray && (typeof(T).GetElementType().IsArray || typeof(T).GetElementType() == typeof(string))) | ||
| 53 | + // return LoadJagged(stream) as T; | ||
| 54 | + return LoadMatrix(stream) as T; | ||
| 55 | + } | ||
| 56 | + | ||
| 30 | 57 | bool ParseReader(BinaryReader reader, out int bytes, out Type t, out int[] shape) | |
| 31 | 58 | { | |
| 32 | 59 | bytes = 0; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -8,12 +8,19 @@ namespace Tensorflow.NumPy | |||
| 8 | 8 | { | |
| 9 | 9 | public partial class NDArray | |
| 10 | 10 | { | |
| 11 | + [AutoNumPy] | ||
| 11 | 12 | public static NDArray operator +(NDArray lhs, NDArray rhs) => new NDArray(BinaryOpWrapper("add", lhs, rhs)); | |
| 13 | + [AutoNumPy] | ||
| 12 | 14 | public static NDArray operator -(NDArray lhs, NDArray rhs) => new NDArray(BinaryOpWrapper("sub", lhs, rhs)); | |
| 15 | + [AutoNumPy] | ||
| 13 | 16 | public static NDArray operator *(NDArray lhs, NDArray rhs) => new NDArray(BinaryOpWrapper("mul", lhs, rhs)); | |
| 17 | + [AutoNumPy] | ||
| 14 | 18 | public static NDArray operator /(NDArray lhs, NDArray rhs) => new NDArray(BinaryOpWrapper("div", lhs, rhs)); | |
| 19 | + [AutoNumPy] | ||
| 15 | 20 | public static NDArray operator >(NDArray lhs, NDArray rhs) => new NDArray(gen_math_ops.greater(lhs, rhs)); | |
| 21 | + [AutoNumPy] | ||
| 16 | 22 | public static NDArray operator <(NDArray lhs, NDArray rhs) => new NDArray(gen_math_ops.less(lhs, rhs)); | |
| 23 | + [AutoNumPy] | ||
| 17 | 24 | public static NDArray operator -(NDArray lhs) => new NDArray(gen_math_ops.neg(lhs)); | |
| 18 | 25 | } | |
| 19 | 26 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -9,10 +9,10 @@ namespace Tensorflow.NumPy | |||
| 9 | 9 | { | |
| 10 | 10 | public partial class np | |
| 11 | 11 | { | |
| 12 | - public static NDArray logical_or(NDArray x1, NDArray x2) | ||
| 13 | - => new NDArray(tf.logical_or(x1, x2)); | ||
| 12 | + [AutoNumPy] | ||
| 13 | + public static NDArray logical_or(NDArray x1, NDArray x2) => new NDArray(tf.logical_or(x1, x2)); | ||
| 14 | 14 | ||
| 15 | - public static NDArray logical_and(NDArray x1, NDArray x2) | ||
| 16 | - => new NDArray(tf.logical_and(x1, x2)); | ||
| 15 | + [AutoNumPy] | ||
| 16 | + public static NDArray logical_and(NDArray x1, NDArray x2) => new NDArray(tf.logical_and(x1, x2)); | ||
| 17 | 17 | } | |
| 18 | 18 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -8,12 +8,15 @@ namespace Tensorflow.NumPy | |||
| 8 | 8 | { | |
| 9 | 9 | public partial class np | |
| 10 | 10 | { | |
| 11 | + [AutoNumPy] | ||
| 11 | 12 | public static NDArray argmax(NDArray a, Axis axis = null) | |
| 12 | 13 | => new NDArray(math_ops.argmax(a, axis)); | |
| 13 | 14 | ||
| 15 | + [AutoNumPy] | ||
| 14 | 16 | public static NDArray argsort(NDArray a, Axis axis = null) | |
| 15 | 17 | => new NDArray(math_ops.argmax(a, axis ?? -1)); | |
| 16 | 18 | ||
| 19 | + [AutoNumPy] | ||
| 17 | 20 | public static NDArray unique(NDArray a) | |
| 18 | 21 | => throw new NotImplementedException(""); | |
| 19 | 22 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -9,10 +9,10 @@ namespace Tensorflow.NumPy | |||
| 9 | 9 | { | |
| 10 | 10 | public partial class np | |
| 11 | 11 | { | |
| 12 | - public static NDArray amin(NDArray x, int axis = 0) | ||
| 13 | - => new NDArray(tf.arg_min(x, axis)); | ||
| 12 | + [AutoNumPy] | ||
| 13 | + public static NDArray amin(NDArray x, int axis = 0) => new NDArray(tf.arg_min(x, axis)); | ||
| 14 | 14 | ||
| 15 | - public static NDArray amax(NDArray x, int axis = 0) | ||
| 16 | - => new NDArray(tf.arg_max(x, axis)); | ||
| 15 | + [AutoNumPy] | ||
| 16 | + public static NDArray amax(NDArray x, int axis = 0) => new NDArray(tf.arg_max(x, axis)); | ||
| 17 | 17 | } | |
| 18 | 18 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -8,10 +8,10 @@ namespace Tensorflow.NumPy | |||
| 8 | 8 | { | |
| 9 | 9 | public partial class np | |
| 10 | 10 | { | |
| 11 | - public static NDArray reshape(NDArray x1, Shape newshape) | ||
| 12 | - => x1.reshape(newshape); | ||
| 11 | + [AutoNumPy] | ||
| 12 | + public static NDArray reshape(NDArray x1, Shape newshape) => x1.reshape(newshape); | ||
| 13 | 13 | ||
| 14 | - public static NDArray squeeze(NDArray x1, Axis? axis = null) | ||
| 15 | - => new NDArray(array_ops.squeeze(x1, axis)); | ||
| 14 | + [AutoNumPy] | ||
| 15 | + public static NDArray squeeze(NDArray x1, Axis? axis = null) => new NDArray(array_ops.squeeze(x1, axis)); | ||
| 16 | 16 | } | |
| 17 | 17 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -9,31 +9,33 @@ namespace Tensorflow.NumPy | |||
| 9 | 9 | { | |
| 10 | 10 | public partial class np | |
| 11 | 11 | { | |
| 12 | - public static NDArray exp(NDArray x) | ||
| 13 | - => new NDArray(tf.exp(x)); | ||
| 12 | + [AutoNumPy] | ||
| 13 | + public static NDArray exp(NDArray x) => new NDArray(tf.exp(x)); | ||
| 14 | 14 | ||
| 15 | - public static NDArray log(NDArray x) | ||
| 16 | - => new NDArray(tf.log(x)); | ||
| 15 | + [AutoNumPy] | ||
| 16 | + public static NDArray log(NDArray x) => new NDArray(tf.log(x)); | ||
| 17 | 17 | ||
| 18 | - public static NDArray multiply(NDArray x1, NDArray x2) | ||
| 19 | - => new NDArray(tf.multiply(x1, x2)); | ||
| 18 | + [AutoNumPy] | ||
| 19 | + public static NDArray multiply(NDArray x1, NDArray x2) => new NDArray(tf.multiply(x1, x2)); | ||
| 20 | 20 | ||
| 21 | - public static NDArray maximum(NDArray x1, NDArray x2) | ||
| 22 | - => new NDArray(tf.maximum(x1, x2)); | ||
| 21 | + [AutoNumPy] | ||
| 22 | + public static NDArray maximum(NDArray x1, NDArray x2) => new NDArray(tf.maximum(x1, x2)); | ||
| 23 | 23 | ||
| 24 | - public static NDArray minimum(NDArray x1, NDArray x2) | ||
| 25 | - => new NDArray(tf.minimum(x1, x2)); | ||
| 24 | + [AutoNumPy] | ||
| 25 | + public static NDArray minimum(NDArray x1, NDArray x2) => new NDArray(tf.minimum(x1, x2)); | ||
| 26 | 26 | ||
| 27 | + [AutoNumPy] | ||
| 27 | 28 | public static NDArray prod(NDArray array, Axis? axis = null, Type? dtype = null, bool keepdims = false) | |
| 28 | 29 | => new NDArray(tf.reduce_prod(array, axis: axis)); | |
| 29 | 30 | ||
| 31 | + [AutoNumPy] | ||
| 30 | 32 | public static NDArray prod<T>(params T[] array) where T : unmanaged | |
| 31 | 33 | => new NDArray(tf.reduce_prod(new NDArray(array))); | |
| 32 | 34 | ||
| 33 | - public static NDArray sqrt(NDArray x) | ||
| 34 | - => new NDArray(tf.sqrt(x)); | ||
| 35 | + [AutoNumPy] | ||
| 36 | + public static NDArray sqrt(NDArray x) => new NDArray(tf.sqrt(x)); | ||
| 35 | 37 | ||
| 36 | - public static NDArray sum(NDArray x1, Axis? axis = null) | ||
| 37 | - => new NDArray(tf.math.sum(x1, axis)); | ||
| 38 | + [AutoNumPy] | ||
| 39 | + public static NDArray sum(NDArray x1, Axis? axis = null) => new NDArray(tf.math.sum(x1, axis)); | ||
| 38 | 40 | } | |
| 39 | 41 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -38,6 +38,7 @@ public NDIterator<T> AsIterator<T>(bool autoreset = false) where T : unmanaged | |||
| 38 | 38 | ||
| 39 | 39 | public bool HasNext() => throw new NotImplementedException(""); | |
| 40 | 40 | public T MoveNext<T>() => throw new NotImplementedException(""); | |
| 41 | + [AutoNumPy] | ||
| 41 | 42 | public NDArray reshape(Shape newshape) => new NDArray(tf.reshape(this, newshape)); | |
| 42 | 43 | public NDArray astype(TF_DataType dtype) => new NDArray(math_ops.cast(this, dtype)); | |
| 43 | 44 | public NDArray ravel() => throw new NotImplementedException(""); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments