| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent bcb28f3 commit 254ba33
24 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -12,13 +12,26 @@ public class MemoryMonitor | |||
| 12 | 12 | { | |
| 13 | 13 | public void WarmUp() | |
| 14 | 14 | { | |
| 15 | + var x1 = tf.Variable(10, name: "x"); | ||
| 16 | + | ||
| 17 | + tf.compat.v1.disable_eager_execution(); | ||
| 18 | + var input = np.array(4); | ||
| 19 | + var nd = tf.reshape(input, new int[] { 1, 1}); | ||
| 20 | + var z = nd[0, 0]; | ||
| 15 | 21 | while (true) | |
| 16 | 22 | { | |
| 17 | - var ones = np.ones((128, 128)); | ||
| 18 | - Thread.Sleep(1); | ||
| 23 | + var x = tf.placeholder(tf.float64, shape: (1024, 1024)); | ||
| 24 | + var log = tf.log(x); | ||
| 25 | + | ||
| 26 | + using (var sess = tf.Session()) | ||
| 27 | + { | ||
| 28 | + var ones = np.ones((1024, 1024), dtype: np.float64); | ||
| 29 | + var o = sess.run(log, new FeedItem(x, ones)); | ||
| 30 | + } | ||
| 31 | + // Thread.Sleep(1); | ||
| 19 | 32 | } | |
| 20 | 33 | ||
| 21 | - TensorShape shape = (1, 32, 32, 3); | ||
| 34 | + Shape shape = (1, 32, 32, 3); | ||
| 22 | 35 | np.arange(shape.size).astype(np.float32).reshape(shape.dims); | |
| 23 | 36 | ||
| 24 | 37 | print($"tensorflow native version: v{tf.VERSION}"); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -33,6 +33,9 @@ public Tensor log(Tensor x, string name = null) | |||
| 33 | 33 | public Tensor erf(Tensor x, string name = null) | |
| 34 | 34 | => math_ops.erf(x, name); | |
| 35 | 35 | ||
| 36 | + public Tensor sum(Tensor x, Axis? axis = null, string name = null) | ||
| 37 | + => math_ops.reduce_sum(x, axis: axis, name: name); | ||
| 38 | + | ||
| 36 | 39 | /// <summary> | |
| 37 | 40 | /// | |
| 38 | 41 | /// </summary> | |
@@ -492,40 +495,21 @@ public Tensor reduce_all(Tensor input_tensor, Axis? axis = null, bool keepdims = | |||
| 492 | 495 | public Tensor reduce_prod(Tensor input_tensor, Axis? axis = null, bool keepdims = false, string name = null) | |
| 493 | 496 | => math_ops.reduce_prod(input_tensor, axis: axis, keepdims: keepdims, name: name); | |
| 494 | 497 | ||
| 495 | - /// <summary> | ||
| 496 | - /// Computes the sum of elements across dimensions of a tensor. | ||
| 497 | - /// </summary> | ||
| 498 | - /// <param name="input_tensors"></param> | ||
| 499 | - /// <param name="axis"></param> | ||
| 500 | - /// <param name="keepdims"></param> | ||
| 501 | - /// <param name="name"></param> | ||
| 502 | - /// <returns></returns> | ||
| 503 | - public Tensor reduce_sum(Tensor[] input_tensors, int? axis = null, bool keepdims = false, string name = null) | ||
| 504 | - => math_ops.reduce_sum(input_tensors, axis: axis, keepdims: keepdims, name: name); | ||
| 505 | - | ||
| 506 | 498 | /// <summary> | |
| 507 | 499 | /// Computes the sum of elements across dimensions of a tensor. | |
| 508 | 500 | /// </summary> | |
| 509 | 501 | /// <param name="input"></param> | |
| 510 | 502 | /// <param name="axis"></param> | |
| 511 | 503 | /// <returns></returns> | |
| 512 | - public Tensor reduce_sum(Tensor input, int? axis = null, int? reduction_indices = null, | ||
| 504 | + public Tensor reduce_sum(Tensor input, Axis? axis = null, Axis? reduction_indices = null, | ||
| 513 | 505 | bool keepdims = false, string name = null) | |
| 514 | 506 | { | |
| 515 | - if (!axis.HasValue && reduction_indices.HasValue && !keepdims) | ||
| 516 | - return math_ops.reduce_sum(input, reduction_indices.Value); | ||
| 517 | - else if (axis.HasValue && !reduction_indices.HasValue && !keepdims) | ||
| 518 | - return math_ops.reduce_sum(input, axis.Value); | ||
| 519 | - else if (axis.HasValue && !reduction_indices.HasValue && keepdims) | ||
| 520 | - return math_ops.reduce_sum(input, keepdims: keepdims, axis: axis.Value, name: name); | ||
| 507 | + if(keepdims) | ||
| 508 | + return math_ops.reduce_sum(input, axis: constant_op.constant(axis ?? reduction_indices), keepdims: keepdims, name: name); | ||
| 521 | 509 | else | |
| 522 | - return math_ops.reduce_sum(input, keepdims: keepdims, name: name); | ||
| 510 | + return math_ops.reduce_sum(input, axis: constant_op.constant(axis ?? reduction_indices)); | ||
| 523 | 511 | } | |
| 524 | 512 | ||
| 525 | - public Tensor reduce_sum(Tensor input, Shape axis, int? reduction_indices = null, | ||
| 526 | - bool keepdims = false, string name = null) | ||
| 527 | - => math_ops.reduce_sum(input, axis, keepdims: keepdims, name: name); | ||
| 528 | - | ||
| 529 | 513 | /// <summary> | |
| 530 | 514 | /// Computes the maximum of elements across dimensions of a tensor. | |
| 531 | 515 | /// </summary> | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -70,7 +70,7 @@ public static Tensor[] _SoftmaxGrad(Operation op, Tensor[] grads) | |||
| 70 | 70 | ||
| 71 | 71 | var softmax = op.outputs[0]; | |
| 72 | 72 | var mul = grad_softmax * softmax; | |
| 73 | - var sum_channels = math_ops.reduce_sum(mul, -1, keepdims: true); | ||
| 73 | + var sum_channels = math_ops.reduce_sum(mul, axis: constant_op.constant(-1), keepdims: true); | ||
| 74 | 74 | var sub = grad_softmax - sum_channels; | |
| 75 | 75 | return new Tensor[] { sub * softmax }; | |
| 76 | 76 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,4 +1,20 @@ | |||
| 1 | - using System; | ||
| 1 | + /***************************************************************************** | ||
| 2 | + Copyright 2021 Haiping Chen. All Rights Reserved. | ||
| 3 | + | ||
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 5 | + you may not use this file except in compliance with the License. | ||
| 6 | + You may obtain a copy of the License at | ||
| 7 | + | ||
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 | ||
| 9 | + | ||
| 10 | + Unless required by applicable law or agreed to in writing, software | ||
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, | ||
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 13 | + See the License for the specific language governing permissions and | ||
| 14 | + limitations under the License. | ||
| 15 | + ******************************************************************************/ | ||
| 16 | + | ||
| 17 | + using System; | ||
| 2 | 18 | using System.Collections.Generic; | |
| 3 | 19 | using System.Linq; | |
| 4 | 20 | using System.Text; | |
@@ -7,6 +23,8 @@ namespace Tensorflow | |||
| 7 | 23 | { | |
| 8 | 24 | public record Axis(params int[] axis) | |
| 9 | 25 | { | |
| 26 | + public int size => axis == null ? -1 : axis.Length; | ||
| 27 | + | ||
| 10 | 28 | public int this[int index] => axis[index]; | |
| 11 | 29 | ||
| 12 | 30 | public static implicit operator int[]?(Axis axis) | |
@@ -16,19 +34,22 @@ public static implicit operator Axis(int axis) | |||
| 16 | 34 | => new Axis(axis); | |
| 17 | 35 | ||
| 18 | 36 | public static implicit operator Axis((int, int) axis) | |
| 19 | - => new Axis(axis); | ||
| 37 | + => new Axis(axis.Item1, axis.Item2); | ||
| 20 | 38 | ||
| 21 | 39 | public static implicit operator Axis((int, int, int) axis) | |
| 22 | - => new Axis(axis); | ||
| 40 | + => new Axis(axis.Item1, axis.Item2, axis.Item3); | ||
| 23 | 41 | ||
| 24 | 42 | public static implicit operator Axis(int[] axis) | |
| 25 | 43 | => new Axis(axis); | |
| 26 | 44 | ||
| 27 | - public static implicit operator Axis(long[] shape) | ||
| 28 | - => new Axis(shape.Select(x => (int)x).ToArray()); | ||
| 45 | + public static implicit operator Axis(long[] axis) | ||
| 46 | + => new Axis(axis.Select(x => (int)x).ToArray()); | ||
| 47 | + | ||
| 48 | + public static implicit operator Axis(Shape axis) | ||
| 49 | + => new Axis(axis.dims.Select(x => (int)x).ToArray()); | ||
| 29 | 50 | ||
| 30 | - public static implicit operator Axis(Shape shape) | ||
| 31 | - => new Axis(shape.dims.Select(x => (int)x).ToArray()); | ||
| 51 | + public static implicit operator Tensor(Axis axis) | ||
| 52 | + => constant_op.constant(axis); | ||
| 32 | 53 | } | |
| 33 | 54 | } | |
| 34 | 55 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -6,12 +6,22 @@ namespace Tensorflow.NumPy | |||
| 6 | 6 | { | |
| 7 | 7 | public partial class NDArray | |
| 8 | 8 | { | |
| 9 | + public void Deconstruct(out byte blue, out byte green, out byte red) | ||
| 10 | + { | ||
| 11 | + blue = (byte)dims[0]; | ||
| 12 | + green = (byte)dims[1]; | ||
| 13 | + red = (byte)dims[2]; | ||
| 14 | + } | ||
| 15 | + | ||
| 9 | 16 | public static implicit operator NDArray(Array array) | |
| 10 | 17 | => new NDArray(array); | |
| 11 | 18 | ||
| 12 | 19 | public static implicit operator bool(NDArray nd) | |
| 13 | 20 | => nd._tensor.ToArray<bool>()[0]; | |
| 14 | 21 | ||
| 22 | + public static implicit operator byte(NDArray nd) | ||
| 23 | + => nd._tensor.ToArray<byte>()[0]; | ||
| 24 | + | ||
| 15 | 25 | public static implicit operator byte[](NDArray nd) | |
| 16 | 26 | => nd.ToByteArray(); | |
| 17 | 27 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -30,7 +30,22 @@ public NDArray this[params int[] index] | |||
| 30 | 30 | ||
| 31 | 31 | set | |
| 32 | 32 | { | |
| 33 | - | ||
| 33 | + var offset = ShapeHelper.GetOffset(shape, index); | ||
| 34 | + unsafe | ||
| 35 | + { | ||
| 36 | + if (dtype == TF_DataType.TF_BOOL) | ||
| 37 | + *((bool*)data + offset) = value; | ||
| 38 | + else if (dtype == TF_DataType.TF_UINT8) | ||
| 39 | + *((byte*)data + offset) = value; | ||
| 40 | + else if (dtype == TF_DataType.TF_INT32) | ||
| 41 | + *((int*)data + offset) = value; | ||
| 42 | + else if (dtype == TF_DataType.TF_INT64) | ||
| 43 | + *((long*)data + offset) = value; | ||
| 44 | + else if (dtype == TF_DataType.TF_FLOAT) | ||
| 45 | + *((float*)data + offset) = value; | ||
| 46 | + else if (dtype == TF_DataType.TF_DOUBLE) | ||
| 47 | + *((double*)data + offset) = value; | ||
| 48 | + } | ||
| 34 | 49 | } | |
| 35 | 50 | } | |
| 36 | 51 | ||
@@ -43,7 +58,13 @@ public NDArray this[params Slice[] slices] | |||
| 43 | 58 | ||
| 44 | 59 | set | |
| 45 | 60 | { | |
| 46 | - | ||
| 61 | + var pos = _tensor[slices]; | ||
| 62 | + var len = value.bytesize; | ||
| 63 | + unsafe | ||
| 64 | + { | ||
| 65 | + System.Buffer.MemoryCopy(value.data.ToPointer(), pos.TensorDataPointer.ToPointer(), len, len); | ||
| 66 | + } | ||
| 67 | + // _tensor[slices].assign(constant_op.constant(value)); | ||
| 47 | 68 | } | |
| 48 | 69 | } | |
| 49 | 70 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -10,18 +10,18 @@ namespace Tensorflow.NumPy | |||
| 10 | 10 | public partial class np | |
| 11 | 11 | { | |
| 12 | 12 | public static NDArray log(NDArray x) | |
| 13 | - => throw new NotImplementedException(""); | ||
| 13 | + => tf.log(x); | ||
| 14 | 14 | ||
| 15 | 15 | public static NDArray prod(NDArray array, Axis? axis = null, Type? dtype = null, bool keepdims = false) | |
| 16 | - => tf.reduce_prod(ops.convert_to_tensor(array), axis: axis); | ||
| 16 | + => tf.reduce_prod(array, axis: axis); | ||
| 17 | 17 | ||
| 18 | 18 | public static NDArray prod<T>(params T[] array) where T : unmanaged | |
| 19 | 19 | => tf.reduce_prod(ops.convert_to_tensor(array)); | |
| 20 | 20 | ||
| 21 | - public static NDArray multiply(in NDArray x1, in NDArray x2) | ||
| 22 | - => throw new NotImplementedException(""); | ||
| 21 | + public static NDArray multiply(NDArray x1, NDArray x2) | ||
| 22 | + => tf.multiply(x1, x2); | ||
| 23 | 23 | ||
| 24 | - public static NDArray sum(NDArray x1) | ||
| 25 | - => throw new NotImplementedException(""); | ||
| 24 | + public static NDArray sum(NDArray x1, Axis? axis = null) | ||
| 25 | + => tf.math.sum(x1, axis); | ||
| 26 | 26 | } | |
| 27 | 27 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,87 @@ | |||
| 1 | + using System; | ||
| 2 | + using System.Collections.Generic; | ||
| 3 | + using System.Linq; | ||
| 4 | + using System.Text; | ||
| 5 | + | ||
| 6 | + namespace Tensorflow.NumPy | ||
| 7 | + { | ||
| 8 | + internal class ShapeHelper | ||
| 9 | + { | ||
| 10 | + public static long GetSize(Shape shape) | ||
| 11 | + { | ||
| 12 | + // scalar | ||
| 13 | + if (shape.ndim == 0) | ||
| 14 | + return 1; | ||
| 15 | + | ||
| 16 | + var computed = 1L; | ||
| 17 | + for (int i = 0; i < shape.ndim; i++) | ||
| 18 | + { | ||
| 19 | + var val = shape.dims[i]; | ||
| 20 | + if (val == 0) | ||
| 21 | + return 0; | ||
| 22 | + else if (val < 0) | ||
| 23 | + continue; | ||
| 24 | + computed *= val; | ||
| 25 | + } | ||
| 26 | + | ||
| 27 | + return computed; | ||
| 28 | + } | ||
| 29 | + | ||
| 30 | + public static long[] GetStrides(Shape shape) | ||
| 31 | + { | ||
| 32 | + var strides = new long[shape.ndim]; | ||
| 33 | + | ||
| 34 | + if (shape.ndim == 0) | ||
| 35 | + return strides; | ||
| 36 | + | ||
| 37 | + strides[strides.Length - 1] = 1; | ||
| 38 | + for (int idx = strides.Length - 1; idx >= 1; idx--) | ||
| 39 | + strides[idx - 1] = strides[idx] * shape.dims[idx]; | ||
| 40 | + | ||
| 41 | + return strides; | ||
| 42 | + } | ||
| 43 | + | ||
| 44 | + public static bool Equals(Shape shape, object target) | ||
| 45 | + { | ||
| 46 | + switch (target) | ||
| 47 | + { | ||
| 48 | + case Shape shape1: | ||
| 49 | + if (shape.ndim == -1 && shape1.ndim == -1) | ||
| 50 | + return false; | ||
| 51 | + else if (shape.ndim != shape1.ndim) | ||
| 52 | + return false; | ||
| 53 | + return Enumerable.SequenceEqual(shape1.dims, shape.dims); | ||
| 54 | + case long[] shape2: | ||
| 55 | + if (shape.ndim != shape2.Length) | ||
| 56 | + return false; | ||
| 57 | + return Enumerable.SequenceEqual(shape.dims, shape2); | ||
| 58 | + default: | ||
| 59 | + return false; | ||
| 60 | + } | ||
| 61 | + } | ||
| 62 | + | ||
| 63 | + public static string ToString(Shape shape) | ||
| 64 | + { | ||
| 65 | + return shape.ndim switch | ||
| 66 | + { | ||
| 67 | + -1 => "<unknown>", | ||
| 68 | + 0 => "()", | ||
| 69 | + 1 => $"({shape.dims[0]},)", | ||
| 70 | + _ => $"({string.Join(", ", shape.dims).Replace("-1", "None")})" | ||
| 71 | + }; | ||
| 72 | + } | ||
| 73 | + | ||
| 74 | + public static long GetOffset(Shape shape, params int[] indices) | ||
| 75 | + { | ||
| 76 | + if (shape.ndim == 0 && indices.Length == 1) | ||
| 77 | + return indices[0]; | ||
| 78 | + | ||
| 79 | + long offset = 0; | ||
| 80 | + var strides = shape.strides; | ||
| 81 | + for (int i = 0; i < indices.Length; i++) | ||
| 82 | + offset += strides[i] * indices[i]; | ||
| 83 | + | ||
| 84 | + return offset; | ||
| 85 | + } | ||
| 86 | + } | ||
| 87 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,4 +1,20 @@ | |||
| 1 | - using System; | ||
| 1 | + /***************************************************************************** | ||
| 2 | + Copyright 2021 Haiping Chen. All Rights Reserved. | ||
| 3 | + | ||
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 5 | + you may not use this file except in compliance with the License. | ||
| 6 | + You may obtain a copy of the License at | ||
| 7 | + | ||
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 | ||
| 9 | + | ||
| 10 | + Unless required by applicable law or agreed to in writing, software | ||
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, | ||
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 13 | + See the License for the specific language governing permissions and | ||
| 14 | + limitations under the License. | ||
| 15 | + ******************************************************************************/ | ||
| 16 | + | ||
| 17 | + using System; | ||
| 2 | 18 | using System.Collections.Generic; | |
| 3 | 19 | using System.Linq; | |
| 4 | 20 | using System.Text; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,4 +1,20 @@ | |||
| 1 | - using System; | ||
| 1 | + /***************************************************************************** | ||
| 2 | + Copyright 2021 Haiping Chen. All Rights Reserved. | ||
| 3 | + | ||
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 5 | + you may not use this file except in compliance with the License. | ||
| 6 | + You may obtain a copy of the License at | ||
| 7 | + | ||
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 | ||
| 9 | + | ||
| 10 | + Unless required by applicable law or agreed to in writing, software | ||
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, | ||
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 13 | + See the License for the specific language governing permissions and | ||
| 14 | + limitations under the License. | ||
| 15 | + ******************************************************************************/ | ||
| 16 | + | ||
| 17 | + using System; | ||
| 2 | 18 | using System.Collections; | |
| 3 | 19 | using System.Collections.Generic; | |
| 4 | 20 | using System.Numerics; | |
| Back | FazBrowse Home | New Git URL |
0 commit comments