diff --git a/src/TensorFlowNET.Core/NumPy/Implementation/RandomizedImpl.cs b/src/TensorFlowNET.Core/NumPy/Implementation/RandomizedImpl.cs index c72bd2de..9d594637 100644 --- a/src/TensorFlowNET.Core/NumPy/Implementation/RandomizedImpl.cs +++ b/src/TensorFlowNET.Core/NumPy/Implementation/RandomizedImpl.cs @@ -36,6 +36,11 @@ namespace Tensorflow.NumPy return new NDArray(tensor); } + [AutoNumPy] + public NDArray randn(params int[] shape) + => new NDArray(random_ops.random_normal(shape ?? Shape.Scalar)); + + [AutoNumPy] public NDArray normal(float loc = 0.0f, float scale = 1.0f, Shape size = null) => new NDArray(random_ops.random_normal(size ?? Shape.Scalar, mean: loc, stddev: scale)); } diff --git a/src/TensorFlowNET.Core/NumPy/NDArray.Implicit.cs b/src/TensorFlowNET.Core/NumPy/NDArray.Implicit.cs index 88dca940..53401a44 100644 --- a/src/TensorFlowNET.Core/NumPy/NDArray.Implicit.cs +++ b/src/TensorFlowNET.Core/NumPy/NDArray.Implicit.cs @@ -21,7 +21,7 @@ namespace Tensorflow.NumPy => nd.dtype == TF_DataType.TF_BOOL ? *(bool*)nd.data : NDArrayConverter.Scalar(nd); public unsafe static implicit operator byte(NDArray nd) - => nd.dtype == TF_DataType.TF_INT8 ? *(byte*)nd.data : NDArrayConverter.Scalar(nd); + => nd.dtype == TF_DataType.TF_UINT8 ? *(byte*)nd.data : NDArrayConverter.Scalar(nd); public unsafe static implicit operator int(NDArray nd) => nd.dtype == TF_DataType.TF_INT32 ? *(int*)nd.data : NDArrayConverter.Scalar(nd); diff --git a/src/TensorFlowNET.Core/NumPy/NDArrayConverter.cs b/src/TensorFlowNET.Core/NumPy/NDArrayConverter.cs index 921c213c..fbab95a9 100644 --- a/src/TensorFlowNET.Core/NumPy/NDArrayConverter.cs +++ b/src/TensorFlowNET.Core/NumPy/NDArrayConverter.cs @@ -10,14 +10,25 @@ namespace Tensorflow.NumPy public unsafe static T Scalar(NDArray nd) where T : unmanaged => nd.dtype switch { + TF_DataType.TF_UINT8 => Scalar(*(byte*)nd.data), TF_DataType.TF_FLOAT => Scalar(*(float*)nd.data), TF_DataType.TF_INT64 => Scalar(*(long*)nd.data), _ => throw new NotImplementedException("") }; + static T Scalar(byte input) + => Type.GetTypeCode(typeof(T)) switch + { + TypeCode.Byte => (T)Convert.ChangeType(input, TypeCode.Byte), + TypeCode.Int32 => (T)Convert.ChangeType(input, TypeCode.Int32), + TypeCode.Single => (T)Convert.ChangeType(input, TypeCode.Single), + _ => throw new NotImplementedException("") + }; + static T Scalar(float input) => Type.GetTypeCode(typeof(T)) switch { + TypeCode.Byte => (T)Convert.ChangeType(input, TypeCode.Byte), TypeCode.Int32 => (T)Convert.ChangeType(input, TypeCode.Int32), TypeCode.Single => (T)Convert.ChangeType(input, TypeCode.Single), _ => throw new NotImplementedException("") @@ -26,6 +37,7 @@ namespace Tensorflow.NumPy static T Scalar(long input) => Type.GetTypeCode(typeof(T)) switch { + TypeCode.Byte => (T)Convert.ChangeType(input, TypeCode.Byte), TypeCode.Int32 => (T)Convert.ChangeType(input, TypeCode.Int32), TypeCode.Single => (T)Convert.ChangeType(input, TypeCode.Single), _ => throw new NotImplementedException("") diff --git a/src/TensorFlowNET.Core/NumPy/Numpy.Math.cs b/src/TensorFlowNET.Core/NumPy/Numpy.Math.cs index 39d02dcd..440a6faa 100644 --- a/src/TensorFlowNET.Core/NumPy/Numpy.Math.cs +++ b/src/TensorFlowNET.Core/NumPy/Numpy.Math.cs @@ -9,6 +9,9 @@ namespace Tensorflow.NumPy { public partial class np { + [AutoNumPy] + public static NDArray cos(NDArray x) => new NDArray(math_ops.cos(x)); + [AutoNumPy] public static NDArray exp(NDArray x) => new NDArray(tf.exp(x)); @@ -38,6 +41,12 @@ namespace Tensorflow.NumPy public static NDArray prod(params T[] array) where T : unmanaged => new NDArray(tf.reduce_prod(new NDArray(array))); + [AutoNumPy] + public static NDArray power(NDArray x, NDArray y) => new NDArray(tf.pow(x, y)); + + [AutoNumPy] + public static NDArray sin(NDArray x) => new NDArray(math_ops.sin(x)); + [AutoNumPy] public static NDArray sqrt(NDArray x) => new NDArray(tf.sqrt(x)); diff --git a/src/TensorFlowNET.Core/Operations/math_ops.cs b/src/TensorFlowNET.Core/Operations/math_ops.cs index 71992f35..97b9d13f 100644 --- a/src/TensorFlowNET.Core/Operations/math_ops.cs +++ b/src/TensorFlowNET.Core/Operations/math_ops.cs @@ -134,6 +134,9 @@ namespace Tensorflow }); } + public static Tensor cos(Tensor x, string name = null) + => tf.Context.ExecuteOp("Cos", name, new ExecuteOpArgs(x)); + public static Tensor saturate_cast(Tensor value, TF_DataType dtype, string name = null) { return tf_with(ops.name_scope(name, "saturate_cast", new[] { value }), name => @@ -373,6 +376,9 @@ namespace Tensorflow public static Tensor sign(T x, string name = null) => gen_math_ops.sign(x, name: name); + public static Tensor sin(Tensor x, string name = null) + => tf.Context.ExecuteOp("Sin", name, new ExecuteOpArgs(x)); + /// /// Returns (x - y)(x - y) element-wise. /// diff --git a/test/TensorFlowNET.UnitTest/NumPy/Randomize.Test.cs b/test/TensorFlowNET.UnitTest/NumPy/Randomize.Test.cs index 5916324f..55801f55 100644 --- a/test/TensorFlowNET.UnitTest/NumPy/Randomize.Test.cs +++ b/test/TensorFlowNET.UnitTest/NumPy/Randomize.Test.cs @@ -33,5 +33,15 @@ namespace TensorFlowNET.UnitTest.NumPy var x = np.random.normal(0, 0.1f, 1000); Equal(np.mean(x), 0f); } + + [TestMethod] + public void randn() + { + var x = np.random.randn(); + Assert.AreEqual(np.float32, x.dtype); + + x = np.random.randn(2, 4); + Equal(np.mean(x), 0f); + } } } diff --git a/test/TensorFlowNET.UnitTest/Numpy/Math.Test.cs b/test/TensorFlowNET.UnitTest/Numpy/Math.Test.cs index 072f0079..a0e6fa4e 100644 --- a/test/TensorFlowNET.UnitTest/Numpy/Math.Test.cs +++ b/test/TensorFlowNET.UnitTest/Numpy/Math.Test.cs @@ -43,5 +43,27 @@ namespace TensorFlowNET.UnitTest.NumPy var y = x / 2; Assert.AreEqual(y.dtype, np.float32); } + + [TestMethod] + public void sin() + { + var x = np.sin(np.pi / 2); + Assert.AreEqual(x, 1d); + } + + [TestMethod] + public void cos() + { + var x = np.cos(np.pi / 2); + Assert.AreEqual(x, 6.123233995736766e-17); + } + + [TestMethod] + public void power() + { + var x = np.arange(6); + var y = np.power(x, 3); + Assert.AreEqual(y, new[] { 0, 1, 8, 27, 64, 125 }); + } } } diff --git a/test/TensorFlowNET.UnitTest/Tensorflow.Binding.UnitTest.csproj b/test/TensorFlowNET.UnitTest/Tensorflow.Binding.UnitTest.csproj index 2aef756a..4c989e05 100644 --- a/test/TensorFlowNET.UnitTest/Tensorflow.Binding.UnitTest.csproj +++ b/test/TensorFlowNET.UnitTest/Tensorflow.Binding.UnitTest.csproj @@ -55,7 +55,7 @@ - +