Skip to content

Commit 5a3ec5a

Browse files
committed
np.random.randn.
1 parent e10e280 commit 5a3ec5a

File tree

8 files changed

+66
-2
lines changed

8 files changed

+66
-2
lines changed

src/TensorFlowNET.Core/NumPy/Implementation/RandomizedImpl.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ public NDArray randint(int low, int? high = null, Shape size = null, TF_DataType
3636
return new NDArray(tensor);
3737
}
3838

39+
[AutoNumPy]
40+
public NDArray randn(params int[] shape)
41+
=> new NDArray(random_ops.random_normal(shape ?? Shape.Scalar));
42+
43+
[AutoNumPy]
3944
public NDArray normal(float loc = 0.0f, float scale = 1.0f, Shape size = null)
4045
=> new NDArray(random_ops.random_normal(size ?? Shape.Scalar, mean: loc, stddev: scale));
4146
}

src/TensorFlowNET.Core/NumPy/NDArray.Implicit.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public unsafe static implicit operator bool(NDArray nd)
2121
=> nd.dtype == TF_DataType.TF_BOOL ? *(bool*)nd.data : NDArrayConverter.Scalar<bool>(nd);
2222

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

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

src/TensorFlowNET.Core/NumPy/NDArrayConverter.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,25 @@ public class NDArrayConverter
1010
public unsafe static T Scalar<T>(NDArray nd) where T : unmanaged
1111
=> nd.dtype switch
1212
{
13+
TF_DataType.TF_UINT8 => Scalar<T>(*(byte*)nd.data),
1314
TF_DataType.TF_FLOAT => Scalar<T>(*(float*)nd.data),
1415
TF_DataType.TF_INT64 => Scalar<T>(*(long*)nd.data),
1516
_ => throw new NotImplementedException("")
1617
};
1718

19+
static T Scalar<T>(byte input)
20+
=> Type.GetTypeCode(typeof(T)) switch
21+
{
22+
TypeCode.Byte => (T)Convert.ChangeType(input, TypeCode.Byte),
23+
TypeCode.Int32 => (T)Convert.ChangeType(input, TypeCode.Int32),
24+
TypeCode.Single => (T)Convert.ChangeType(input, TypeCode.Single),
25+
_ => throw new NotImplementedException("")
26+
};
27+
1828
static T Scalar<T>(float input)
1929
=> Type.GetTypeCode(typeof(T)) switch
2030
{
31+
TypeCode.Byte => (T)Convert.ChangeType(input, TypeCode.Byte),
2132
TypeCode.Int32 => (T)Convert.ChangeType(input, TypeCode.Int32),
2233
TypeCode.Single => (T)Convert.ChangeType(input, TypeCode.Single),
2334
_ => throw new NotImplementedException("")
@@ -26,6 +37,7 @@ static T Scalar<T>(float input)
2637
static T Scalar<T>(long input)
2738
=> Type.GetTypeCode(typeof(T)) switch
2839
{
40+
TypeCode.Byte => (T)Convert.ChangeType(input, TypeCode.Byte),
2941
TypeCode.Int32 => (T)Convert.ChangeType(input, TypeCode.Int32),
3042
TypeCode.Single => (T)Convert.ChangeType(input, TypeCode.Single),
3143
_ => throw new NotImplementedException("")

src/TensorFlowNET.Core/NumPy/Numpy.Math.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ namespace Tensorflow.NumPy
99
{
1010
public partial class np
1111
{
12+
[AutoNumPy]
13+
public static NDArray cos(NDArray x) => new NDArray(math_ops.cos(x));
14+
1215
[AutoNumPy]
1316
public static NDArray exp(NDArray x) => new NDArray(tf.exp(x));
1417

@@ -38,6 +41,12 @@ public static NDArray prod(NDArray array, Axis? axis = null, Type? dtype = null,
3841
public static NDArray prod<T>(params T[] array) where T : unmanaged
3942
=> new NDArray(tf.reduce_prod(new NDArray(array)));
4043

44+
[AutoNumPy]
45+
public static NDArray power(NDArray x, NDArray y) => new NDArray(tf.pow(x, y));
46+
47+
[AutoNumPy]
48+
public static NDArray sin(NDArray x) => new NDArray(math_ops.sin(x));
49+
4150
[AutoNumPy]
4251
public static NDArray sqrt(NDArray x) => new NDArray(tf.sqrt(x));
4352

src/TensorFlowNET.Core/Operations/math_ops.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,9 @@ public static Tensor cast(Tensor x, TF_DataType dtype = TF_DataType.DtInvalid, s
134134
});
135135
}
136136

137+
public static Tensor cos(Tensor x, string name = null)
138+
=> tf.Context.ExecuteOp("Cos", name, new ExecuteOpArgs(x));
139+
137140
public static Tensor saturate_cast(Tensor value, TF_DataType dtype, string name = null)
138141
{
139142
return tf_with(ops.name_scope(name, "saturate_cast", new[] { value }), name =>
@@ -373,6 +376,9 @@ public static Tensor sigmoid<T>(T x, string name = null)
373376
public static Tensor sign<T>(T x, string name = null)
374377
=> gen_math_ops.sign(x, name: name);
375378

379+
public static Tensor sin(Tensor x, string name = null)
380+
=> tf.Context.ExecuteOp("Sin", name, new ExecuteOpArgs(x));
381+
376382
/// <summary>
377383
/// Returns (x - y)(x - y) element-wise.
378384
/// </summary>

test/TensorFlowNET.UnitTest/NumPy/Randomize.Test.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,15 @@ public void normal()
3333
var x = np.random.normal(0, 0.1f, 1000);
3434
Equal(np.mean(x), 0f);
3535
}
36+
37+
[TestMethod]
38+
public void randn()
39+
{
40+
var x = np.random.randn();
41+
Assert.AreEqual(np.float32, x.dtype);
42+
43+
x = np.random.randn(2, 4);
44+
Equal(np.mean(x), 0f);
45+
}
3646
}
3747
}

test/TensorFlowNET.UnitTest/Numpy/Math.Test.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,27 @@ public void divide()
4343
var y = x / 2;
4444
Assert.AreEqual(y.dtype, np.float32);
4545
}
46+
47+
[TestMethod]
48+
public void sin()
49+
{
50+
var x = np.sin(np.pi / 2);
51+
Assert.AreEqual(x, 1d);
52+
}
53+
54+
[TestMethod]
55+
public void cos()
56+
{
57+
var x = np.cos(np.pi / 2);
58+
Assert.AreEqual(x, 6.123233995736766e-17);
59+
}
60+
61+
[TestMethod]
62+
public void power()
63+
{
64+
var x = np.arange(6);
65+
var y = np.power(x, 3);
66+
Assert.AreEqual(y, new[] { 0, 1, 8, 27, 64, 125 });
67+
}
4668
}
4769
}

test/TensorFlowNET.UnitTest/Tensorflow.Binding.UnitTest.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
</ItemGroup>
5656

5757
<ItemGroup>
58-
<ProjectReference Include="..\..\src\TensorFlowNET.Recommenders\Tensorflow.Recommenders.csproj" />
58+
<ProjectReference Include="..\..\src\TensorFlowNET.Core\Tensorflow.Binding.csproj" />
5959
<ProjectReference Include="..\..\src\TensorFlowNET.Text\Tensorflow.Text.csproj" />
6060
</ItemGroup>
6161

0 commit comments

Comments
 (0)