Browse Source

np.random.randn.

tags/TensorFlowOpLayer
Oceania2018 4 years ago
parent
commit
5a3ec5a194
8 changed files with 66 additions and 2 deletions
  1. +5
    -0
      src/TensorFlowNET.Core/NumPy/Implementation/RandomizedImpl.cs
  2. +1
    -1
      src/TensorFlowNET.Core/NumPy/NDArray.Implicit.cs
  3. +12
    -0
      src/TensorFlowNET.Core/NumPy/NDArrayConverter.cs
  4. +9
    -0
      src/TensorFlowNET.Core/NumPy/Numpy.Math.cs
  5. +6
    -0
      src/TensorFlowNET.Core/Operations/math_ops.cs
  6. +10
    -0
      test/TensorFlowNET.UnitTest/NumPy/Randomize.Test.cs
  7. +22
    -0
      test/TensorFlowNET.UnitTest/Numpy/Math.Test.cs
  8. +1
    -1
      test/TensorFlowNET.UnitTest/Tensorflow.Binding.UnitTest.csproj

+ 5
- 0
src/TensorFlowNET.Core/NumPy/Implementation/RandomizedImpl.cs View File

@@ -36,6 +36,11 @@ namespace Tensorflow.NumPy
return new NDArray(tensor); 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) 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)); => new NDArray(random_ops.random_normal(size ?? Shape.Scalar, mean: loc, stddev: scale));
} }


+ 1
- 1
src/TensorFlowNET.Core/NumPy/NDArray.Implicit.cs View File

@@ -21,7 +21,7 @@ namespace Tensorflow.NumPy
=> nd.dtype == TF_DataType.TF_BOOL ? *(bool*)nd.data : NDArrayConverter.Scalar<bool>(nd); => nd.dtype == TF_DataType.TF_BOOL ? *(bool*)nd.data : NDArrayConverter.Scalar<bool>(nd);


public unsafe static implicit operator byte(NDArray nd) public unsafe static implicit operator byte(NDArray nd)
=> nd.dtype == TF_DataType.TF_INT8 ? *(byte*)nd.data : NDArrayConverter.Scalar<byte>(nd);
=> nd.dtype == TF_DataType.TF_UINT8 ? *(byte*)nd.data : NDArrayConverter.Scalar<byte>(nd);


public unsafe static implicit operator int(NDArray nd) public unsafe static implicit operator int(NDArray nd)
=> nd.dtype == TF_DataType.TF_INT32 ? *(int*)nd.data : NDArrayConverter.Scalar<int>(nd); => nd.dtype == TF_DataType.TF_INT32 ? *(int*)nd.data : NDArrayConverter.Scalar<int>(nd);


+ 12
- 0
src/TensorFlowNET.Core/NumPy/NDArrayConverter.cs View File

@@ -10,14 +10,25 @@ namespace Tensorflow.NumPy
public unsafe static T Scalar<T>(NDArray nd) where T : unmanaged public unsafe static T Scalar<T>(NDArray nd) where T : unmanaged
=> nd.dtype switch => nd.dtype switch
{ {
TF_DataType.TF_UINT8 => Scalar<T>(*(byte*)nd.data),
TF_DataType.TF_FLOAT => Scalar<T>(*(float*)nd.data), TF_DataType.TF_FLOAT => Scalar<T>(*(float*)nd.data),
TF_DataType.TF_INT64 => Scalar<T>(*(long*)nd.data), TF_DataType.TF_INT64 => Scalar<T>(*(long*)nd.data),
_ => throw new NotImplementedException("") _ => throw new NotImplementedException("")
}; };


static T Scalar<T>(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<T>(float input) static T Scalar<T>(float input)
=> Type.GetTypeCode(typeof(T)) switch => Type.GetTypeCode(typeof(T)) switch
{ {
TypeCode.Byte => (T)Convert.ChangeType(input, TypeCode.Byte),
TypeCode.Int32 => (T)Convert.ChangeType(input, TypeCode.Int32), TypeCode.Int32 => (T)Convert.ChangeType(input, TypeCode.Int32),
TypeCode.Single => (T)Convert.ChangeType(input, TypeCode.Single), TypeCode.Single => (T)Convert.ChangeType(input, TypeCode.Single),
_ => throw new NotImplementedException("") _ => throw new NotImplementedException("")
@@ -26,6 +37,7 @@ namespace Tensorflow.NumPy
static T Scalar<T>(long input) static T Scalar<T>(long input)
=> Type.GetTypeCode(typeof(T)) switch => Type.GetTypeCode(typeof(T)) switch
{ {
TypeCode.Byte => (T)Convert.ChangeType(input, TypeCode.Byte),
TypeCode.Int32 => (T)Convert.ChangeType(input, TypeCode.Int32), TypeCode.Int32 => (T)Convert.ChangeType(input, TypeCode.Int32),
TypeCode.Single => (T)Convert.ChangeType(input, TypeCode.Single), TypeCode.Single => (T)Convert.ChangeType(input, TypeCode.Single),
_ => throw new NotImplementedException("") _ => throw new NotImplementedException("")


+ 9
- 0
src/TensorFlowNET.Core/NumPy/Numpy.Math.cs View File

@@ -9,6 +9,9 @@ namespace Tensorflow.NumPy
{ {
public partial class np public partial class np
{ {
[AutoNumPy]
public static NDArray cos(NDArray x) => new NDArray(math_ops.cos(x));

[AutoNumPy] [AutoNumPy]
public static NDArray exp(NDArray x) => new NDArray(tf.exp(x)); public static NDArray exp(NDArray x) => new NDArray(tf.exp(x));


@@ -38,6 +41,12 @@ namespace Tensorflow.NumPy
public static NDArray prod<T>(params T[] array) where T : unmanaged public static NDArray prod<T>(params T[] array) where T : unmanaged
=> new NDArray(tf.reduce_prod(new NDArray(array))); => 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] [AutoNumPy]
public static NDArray sqrt(NDArray x) => new NDArray(tf.sqrt(x)); public static NDArray sqrt(NDArray x) => new NDArray(tf.sqrt(x));




+ 6
- 0
src/TensorFlowNET.Core/Operations/math_ops.cs View File

@@ -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) 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 => return tf_with(ops.name_scope(name, "saturate_cast", new[] { value }), name =>
@@ -373,6 +376,9 @@ namespace Tensorflow
public static Tensor sign<T>(T x, string name = null) public static Tensor sign<T>(T x, string name = null)
=> gen_math_ops.sign(x, name: name); => gen_math_ops.sign(x, name: name);


public static Tensor sin(Tensor x, string name = null)
=> tf.Context.ExecuteOp("Sin", name, new ExecuteOpArgs(x));

/// <summary> /// <summary>
/// Returns (x - y)(x - y) element-wise. /// Returns (x - y)(x - y) element-wise.
/// </summary> /// </summary>


+ 10
- 0
test/TensorFlowNET.UnitTest/NumPy/Randomize.Test.cs View File

@@ -33,5 +33,15 @@ namespace TensorFlowNET.UnitTest.NumPy
var x = np.random.normal(0, 0.1f, 1000); var x = np.random.normal(0, 0.1f, 1000);
Equal(np.mean(x), 0f); 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);
}
} }
} }

+ 22
- 0
test/TensorFlowNET.UnitTest/Numpy/Math.Test.cs View File

@@ -43,5 +43,27 @@ namespace TensorFlowNET.UnitTest.NumPy
var y = x / 2; var y = x / 2;
Assert.AreEqual(y.dtype, np.float32); 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 });
}
} }
} }

+ 1
- 1
test/TensorFlowNET.UnitTest/Tensorflow.Binding.UnitTest.csproj View File

@@ -55,7 +55,7 @@
</ItemGroup> </ItemGroup>


<ItemGroup> <ItemGroup>
<ProjectReference Include="..\..\src\TensorFlowNET.Recommenders\Tensorflow.Recommenders.csproj" />
<ProjectReference Include="..\..\src\TensorFlowNET.Core\Tensorflow.Binding.csproj" />
<ProjectReference Include="..\..\src\TensorFlowNET.Text\Tensorflow.Text.csproj" /> <ProjectReference Include="..\..\src\TensorFlowNET.Text\Tensorflow.Text.csproj" />
</ItemGroup> </ItemGroup>




Loading…
Cancel
Save