From 0b6e855439be0782ecacf4d3c51ec073c7e34a18 Mon Sep 17 00:00:00 2001 From: Oceania2018 Date: Mon, 12 Jul 2021 22:44:01 -0500 Subject: [PATCH] Shape as_int_list --- src/TensorFlowNET.Core/Data/MnistModelLoader.cs | 4 ++-- src/TensorFlowNET.Core/Gradients/gradients_util.cs | 2 +- .../NumPy/Implementation/NumPyImpl.Creation.cs | 5 +++++ src/TensorFlowNET.Core/Numpy/NDArray.Creation.cs | 6 ++++++ src/TensorFlowNET.Core/Numpy/Numpy.Creation.cs | 3 +++ src/TensorFlowNET.Core/Numpy/Numpy.cs | 3 --- src/TensorFlowNET.Core/Numpy/Shape.cs | 5 +++++ src/TensorFlowNET.Core/Operations/array_ops.cs | 6 ++++-- src/TensorFlowNET.Core/Tensors/tensor_util.cs | 8 ++++---- 9 files changed, 30 insertions(+), 12 deletions(-) diff --git a/src/TensorFlowNET.Core/Data/MnistModelLoader.cs b/src/TensorFlowNET.Core/Data/MnistModelLoader.cs index 73fb52f9..5be88bba 100644 --- a/src/TensorFlowNET.Core/Data/MnistModelLoader.cs +++ b/src/TensorFlowNET.Core/Data/MnistModelLoader.cs @@ -123,7 +123,7 @@ namespace Tensorflow bytestream.Read(buf, 0, buf.Length); - var data = np.frombuffer(buf, np.@byte.as_system_dtype()); + var data = np.frombuffer(buf, np.@byte); data = data.reshape((num_images, rows, cols, 1)); return data; @@ -148,7 +148,7 @@ namespace Tensorflow bytestream.Read(buf, 0, buf.Length); - var labels = np.frombuffer(buf, np.uint8.as_system_dtype()); + var labels = np.frombuffer(buf, np.uint8); if (one_hot) return DenseToOneHot(labels, num_classes); diff --git a/src/TensorFlowNET.Core/Gradients/gradients_util.cs b/src/TensorFlowNET.Core/Gradients/gradients_util.cs index f4c714ee..771887be 100644 --- a/src/TensorFlowNET.Core/Gradients/gradients_util.cs +++ b/src/TensorFlowNET.Core/Gradients/gradients_util.cs @@ -275,7 +275,7 @@ namespace Tensorflow if (y.dtype.is_complex()) throw new TypeAccessException($"Gradients of complex tensors must set grad_ys (y.dtype = {y.dtype})"); var shape = array_ops.shape(y); - var constant = constant_op.constant(y.dtype == TF_DataType.TF_DOUBLE ? (object)1.0 : (object)1.0f, name: $"grad_ys_{i}"); + var constant = constant_op.constant(1, y.dtype, name: $"grad_ys_{i}"); var fill = gen_array_ops.fill(shape, constant); new_grad_ys.append(fill); continue; diff --git a/src/TensorFlowNET.Core/NumPy/Implementation/NumPyImpl.Creation.cs b/src/TensorFlowNET.Core/NumPy/Implementation/NumPyImpl.Creation.cs index 32007ea5..ca49e19f 100644 --- a/src/TensorFlowNET.Core/NumPy/Implementation/NumPyImpl.Creation.cs +++ b/src/TensorFlowNET.Core/NumPy/Implementation/NumPyImpl.Creation.cs @@ -33,6 +33,11 @@ namespace Tensorflow.NumPy return new NDArray(tensor); } + public NDArray frombuffer(byte[] bytes, TF_DataType dtype) + { + throw new NotImplementedException(""); + } + public NDArray linspace(T start, T stop, int num = 50, bool endpoint = true, bool retstep = false, TF_DataType dtype = TF_DataType.TF_DOUBLE, int axis = 0) { diff --git a/src/TensorFlowNET.Core/Numpy/NDArray.Creation.cs b/src/TensorFlowNET.Core/Numpy/NDArray.Creation.cs index 2f89bc5d..61fc9258 100644 --- a/src/TensorFlowNET.Core/Numpy/NDArray.Creation.cs +++ b/src/TensorFlowNET.Core/Numpy/NDArray.Creation.cs @@ -18,6 +18,7 @@ namespace Tensorflow.NumPy public NDArray(Array value, Shape? shape = null) => Init(value, shape); public NDArray(Shape shape, TF_DataType dtype = TF_DataType.TF_DOUBLE) => Init(shape, dtype: dtype); public NDArray(Tensor value, Shape? shape = null) => Init(value, shape); + public NDArray(byte[] bytes, TF_DataType dtype) => Init(bytes, dtype); public static NDArray Scalar(T value) where T : unmanaged => value switch @@ -68,5 +69,10 @@ namespace Tensorflow.NumPy _tensor = new Tensor(value.TensorDataPointer, shape ?? value.shape, value.dtype); _tensor.SetReferencedByNDArray(); } + + void Init(byte[] bytes, TF_DataType dtype) + { + + } } } diff --git a/src/TensorFlowNET.Core/Numpy/Numpy.Creation.cs b/src/TensorFlowNET.Core/Numpy/Numpy.Creation.cs index b1251b2a..aa1371db 100644 --- a/src/TensorFlowNET.Core/Numpy/Numpy.Creation.cs +++ b/src/TensorFlowNET.Core/Numpy/Numpy.Creation.cs @@ -33,6 +33,9 @@ namespace Tensorflow.NumPy public static NDArray full(Shape shape, T fill_value) => new NDArray(tf.fill(tf.constant(shape), fill_value)); + public static NDArray frombuffer(byte[] bytes, TF_DataType dtype) + => tf.numpy.frombuffer(bytes, dtype); + public static NDArray linspace(T start, T stop, int num = 50, bool endpoint = true, bool retstep = false, TF_DataType dtype = TF_DataType.TF_DOUBLE, int axis = 0) where T : unmanaged => tf.numpy.linspace(start, stop, num: num, endpoint: endpoint, retstep: retstep, dtype: dtype, axis: axis); diff --git a/src/TensorFlowNET.Core/Numpy/Numpy.cs b/src/TensorFlowNET.Core/Numpy/Numpy.cs index 1f57c0df..174da10b 100644 --- a/src/TensorFlowNET.Core/Numpy/Numpy.cs +++ b/src/TensorFlowNET.Core/Numpy/Numpy.cs @@ -56,9 +56,6 @@ namespace Tensorflow.NumPy public static NDArray concatenate(NDArray[] arrays, int axis = 0) => throw new NotImplementedException(""); - public static NDArray frombuffer(byte[] bytes, Type dtype) - => throw new NotImplementedException(""); - public static NDArray frombuffer(byte[] bytes, string dtype) => throw new NotImplementedException(""); diff --git a/src/TensorFlowNET.Core/Numpy/Shape.cs b/src/TensorFlowNET.Core/Numpy/Shape.cs index f4176e38..9b87cd55 100644 --- a/src/TensorFlowNET.Core/Numpy/Shape.cs +++ b/src/TensorFlowNET.Core/Numpy/Shape.cs @@ -214,6 +214,11 @@ namespace Tensorflow return new Shape(new_dims.ToArray()); } + public int[] as_int_list() + { + return _dims.Select(x => (int)x).ToArray(); + } + public void assert_has_rank(int rank) { if (rank != ndim) diff --git a/src/TensorFlowNET.Core/Operations/array_ops.cs b/src/TensorFlowNET.Core/Operations/array_ops.cs index a38761f5..c3ed85ee 100644 --- a/src/TensorFlowNET.Core/Operations/array_ops.cs +++ b/src/TensorFlowNET.Core/Operations/array_ops.cs @@ -591,8 +591,10 @@ namespace Tensorflow var input_shape = input.shape; if (optimize && input.ndim > -1 && input_shape.IsFullyDefined) { - var nd = np.array(input.shape.dims).astype(out_type.as_system_dtype()); - return constant_op.constant(nd, name: name); + if(out_type == TF_DataType.TF_INT32) + return constant_op.constant(input.shape.as_int_list(), name: name); + else + return constant_op.constant(input.shape.dims, name: name); } } diff --git a/src/TensorFlowNET.Core/Tensors/tensor_util.cs b/src/TensorFlowNET.Core/Tensors/tensor_util.cs index f6effcf8..f93a27b5 100644 --- a/src/TensorFlowNET.Core/Tensors/tensor_util.cs +++ b/src/TensorFlowNET.Core/Tensors/tensor_util.cs @@ -60,11 +60,11 @@ namespace Tensorflow public static NDArray MakeNdarray(TensorProto tensor) { - var shape = tensor.TensorShape.Dim.Select(x => (int)x.Size).ToArray(); - int num_elements = np.prod(shape); - var tensor_dtype = tensor.Dtype.as_numpy_dtype(); + var shape = new Shape(tensor.TensorShape.Dim.Select(x => x.Size).ToArray()); + var num_elements = shape.size; + var tensor_dtype = tensor.Dtype.as_tf_dtype(); - if (shape.Length > 0 && tensor.TensorContent.Length > 0) + if (shape.ndim > 0 && tensor.TensorContent.Length > 0) { return np.frombuffer(tensor.TensorContent.ToByteArray(), tensor_dtype).reshape(shape); }