Skip to content

Commit 3f6da21

Browse files
committed
Adjust the code structure of keras.unittest.
1 parent 4aab86b commit 3f6da21

30 files changed

+363
-446
lines changed

test/TensorFlowNET.Keras.UnitTest/Callbacks/EarlystoppingTest.cs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
11
using Microsoft.VisualStudio.TestTools.UnitTesting;
2-
using Tensorflow.Keras.UnitTest.Helpers;
3-
using static Tensorflow.Binding;
4-
using Tensorflow;
5-
using Tensorflow.Keras.Optimizers;
2+
using System.Collections.Generic;
63
using Tensorflow.Keras.Callbacks;
74
using Tensorflow.Keras.Engine;
8-
using System.Collections.Generic;
95
using static Tensorflow.KerasApi;
10-
using Tensorflow.Keras;
116

127

13-
namespace TensorFlowNET.Keras.UnitTest
8+
namespace Tensorflow.Keras.UnitTest.Callbacks
149
{
1510
[TestClass]
1611
public class EarlystoppingTest
@@ -31,7 +26,7 @@ public void Earlystopping()
3126
layers.Dense(10)
3227
});
3328

34-
29+
3530
model.summary();
3631

3732
model.compile(optimizer: keras.optimizers.RMSprop(1e-3f),
@@ -55,7 +50,7 @@ public void Earlystopping()
5550
var callbacks = new List<ICallback>();
5651
callbacks.add(earlystop);
5752

58-
model.fit(x_train[new Slice(0, 2000)], y_train[new Slice(0, 2000)], batch_size, num_epochs,callbacks:callbacks);
53+
model.fit(x_train[new Slice(0, 2000)], y_train[new Slice(0, 2000)], batch_size, num_epochs, callbacks: callbacks);
5954
}
6055

6156
}

test/TensorFlowNET.Keras.UnitTest/EagerModeTestBase.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
using Microsoft.VisualStudio.TestTools.UnitTesting;
22
using System;
3-
using Tensorflow;
4-
using Tensorflow.Keras;
53
using static Tensorflow.Binding;
64

7-
namespace TensorFlowNET.Keras.UnitTest
5+
namespace Tensorflow.Keras.UnitTest
86
{
97
public class EagerModeTestBase
108
{

test/TensorFlowNET.Keras.UnitTest/GradientTest.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
using Microsoft.VisualStudio.TestTools.UnitTesting;
22
using System.Linq;
3-
using Tensorflow;
43
using Tensorflow.Keras.Engine;
4+
using Tensorflow.NumPy;
55
using static Tensorflow.Binding;
66
using static Tensorflow.KerasApi;
7-
using Tensorflow.NumPy;
8-
using System;
9-
using Tensorflow.Keras.Optimizers;
107

11-
namespace TensorFlowNET.Keras.UnitTest;
8+
namespace Tensorflow.Keras.UnitTest;
129

1310
[TestClass]
1411
public class GradientTest : EagerModeTestBase

test/TensorFlowNET.Keras.UnitTest/InitializerTest.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
using Microsoft.VisualStudio.TestTools.UnitTesting;
2-
using System;
3-
using System.Collections.Generic;
4-
using System.Linq;
5-
using System.Text;
6-
using TensorFlowNET.Keras.UnitTest;
72
using static Tensorflow.Binding;
83

9-
namespace TensorFlowNET.Keras.UnitTest;
4+
namespace Tensorflow.Keras.UnitTest;
105

116
[TestClass]
127
public class InitializerTest : EagerModeTestBase

test/TensorFlowNET.Keras.UnitTest/Layers/ActivationTest.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
using Microsoft.VisualStudio.TestTools.UnitTesting;
2-
using System;
3-
using System.Collections.Generic;
4-
using static Tensorflow.Binding;
52
using Tensorflow.NumPy;
3+
using static Tensorflow.Binding;
64
using static Tensorflow.KerasApi;
7-
using Tensorflow;
85

9-
namespace TensorFlowNET.Keras.UnitTest {
6+
namespace Tensorflow.Keras.UnitTest.Layers
7+
{
108
[TestClass]
119
public class ActivationTest : EagerModeTestBase
1210
{

test/TensorFlowNET.Keras.UnitTest/Layers/AttentionTest.cs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
using Microsoft.VisualStudio.TestTools.UnitTesting;
2-
using System;
3-
using System.Collections.Generic;
2+
using Tensorflow.Keras.Layers;
3+
using Tensorflow.Keras.Utils;
44
using Tensorflow.NumPy;
55
using static Tensorflow.Binding;
66
using static Tensorflow.KerasApi;
7-
using Tensorflow.Keras.Layers;
8-
using Tensorflow;
9-
using Tensorflow.Keras.ArgsDefinition;
10-
using Tensorflow.Keras.Utils;
117

12-
namespace TensorFlowNET.Keras.UnitTest
8+
namespace Tensorflow.Keras.UnitTest.Layers
139
{
1410
[TestClass]
1511
public class AttentionTest : EagerModeTestBase
@@ -118,7 +114,8 @@ public void test_calculate_scores_multi_dim_concat()
118114
} }, dtype: np.float32);
119115
var attention_layer = (Attention)keras.layers.Attention(score_mode: "concat");
120116
//attention_layer.concat_score_weight = 1;
121-
attention_layer.concat_score_weight = base_layer_utils.make_variable(new VariableArgs() {
117+
attention_layer.concat_score_weight = base_layer_utils.make_variable(new VariableArgs()
118+
{
122119
Name = "concat_score_weight",
123120
Shape = (1),
124121
DType = TF_DataType.TF_FLOAT,
@@ -156,7 +153,7 @@ public void test_masked_attention()
156153

157154
var query = keras.Input(shape: (4, 8));
158155
var value = keras.Input(shape: (2, 8));
159-
var mask_tensor = keras.Input(shape:(4, 2));
156+
var mask_tensor = keras.Input(shape: (4, 2));
160157
var attention_layer = keras.layers.MultiHeadAttention(num_heads: 2, key_dim: 2);
161158
attention_layer.Apply(new Tensor[] { query, value, mask_tensor });
162159

test/TensorFlowNET.Keras.UnitTest/Layers/CosineSimilarity.Test.cs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
using Microsoft.VisualStudio.TestTools.UnitTesting;
2-
using Tensorflow.NumPy;
3-
using Tensorflow;
42
using Tensorflow.Keras.Losses;
5-
using static Tensorflow.Binding;
3+
using Tensorflow.NumPy;
64
using static Tensorflow.KerasApi;
75

8-
namespace TensorFlowNET.Keras.UnitTest
6+
namespace Tensorflow.Keras.UnitTest.Layers
97
{
108
[TestClass]
119
public class CosineSimilarity
@@ -16,7 +14,7 @@ public class CosineSimilarity
1614
NDArray y_pred_float = new float[,] { { 1.0f, 0.0f }, { 1.0f, 1.0f } };
1715

1816
[TestMethod]
19-
17+
2018
public void _Default()
2119
{
2220
//>>> # Using 'auto'/'sum_over_batch_size' reduction type.
@@ -27,7 +25,7 @@ public void _Default()
2725
//>>> # loss = mean(sum(l2_norm(y_true) . l2_norm(y_pred), axis=1))
2826
//>>> # = -((0. + 0.) + (0.5 + 0.5)) / 2
2927
//-0.5
30-
var loss = keras.losses.CosineSimilarity(axis : 1);
28+
var loss = keras.losses.CosineSimilarity(axis: 1);
3129
var call = loss.Call(y_true_float, y_pred_float);
3230
Assert.AreEqual((NDArray)(-0.49999997f), call.numpy());
3331
}
@@ -41,7 +39,7 @@ public void _Sample_Weight()
4139
//- 0.0999
4240
var loss = keras.losses.CosineSimilarity();
4341
var call = loss.Call(y_true_float, y_pred_float, sample_weight: (NDArray)new float[] { 0.8f, 0.2f });
44-
Assert.AreEqual((NDArray) (- 0.099999994f), call.numpy());
42+
Assert.AreEqual((NDArray)(-0.099999994f), call.numpy());
4543
}
4644

4745
[TestMethod]
@@ -53,7 +51,7 @@ public void _SUM()
5351
//... reduction = tf.keras.losses.Reduction.SUM)
5452
//>>> cosine_loss(y_true, y_pred).numpy()
5553
//- 0.999
56-
var loss = keras.losses.CosineSimilarity(axis: 1,reduction : ReductionV2.SUM);
54+
var loss = keras.losses.CosineSimilarity(axis: 1, reduction: ReductionV2.SUM);
5755
var call = loss.Call(y_true_float, y_pred_float);
5856
Assert.AreEqual((NDArray)(-0.99999994f), call.numpy());
5957
}
@@ -67,7 +65,7 @@ public void _None()
6765
//... reduction = tf.keras.losses.Reduction.NONE)
6866
//>>> cosine_loss(y_true, y_pred).numpy()
6967
//array([-0., -0.999], dtype = float32)
70-
var loss = keras.losses.CosineSimilarity(axis :1, reduction: ReductionV2.NONE);
68+
var loss = keras.losses.CosineSimilarity(axis: 1, reduction: ReductionV2.NONE);
7169
var call = loss.Call(y_true_float, y_pred_float);
7270
Assert.AreEqual((NDArray)new float[] { -0f, -0.99999994f }, call.numpy());
7371
}

test/TensorFlowNET.Keras.UnitTest/Layers/Huber.Test.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
using Microsoft.VisualStudio.TestTools.UnitTesting;
2-
using Tensorflow.NumPy;
3-
using Tensorflow;
42
using Tensorflow.Keras.Losses;
5-
using static Tensorflow.Binding;
3+
using Tensorflow.NumPy;
64
using static Tensorflow.KerasApi;
75

8-
namespace TensorFlowNET.Keras.UnitTest
6+
namespace Tensorflow.Keras.UnitTest.Layers
97
{
108
[TestClass]
119
public class Huber
@@ -16,7 +14,7 @@ public class Huber
1614
NDArray y_pred_float = new float[,] { { 0.6f, 0.4f }, { 0.4f, 0.6f } };
1715

1816
[TestMethod]
19-
17+
2018
public void _Default()
2119
{
2220
//>>> # Using 'auto'/'sum_over_batch_size' reduction type.
@@ -49,7 +47,7 @@ public void _SUM()
4947
//... reduction = tf.keras.losses.Reduction.SUM)
5048
//>>> h(y_true, y_pred).numpy()
5149
//0.31
52-
var loss = keras.losses.Huber(reduction : ReductionV2.SUM);
50+
var loss = keras.losses.Huber(reduction: ReductionV2.SUM);
5351
var call = loss.Call(y_true_float, y_pred_float);
5452
Assert.AreEqual((NDArray)0.31f, call.numpy());
5553
}

test/TensorFlowNET.Keras.UnitTest/Layers/Layers.Convolution.Test.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
using Microsoft.VisualStudio.TestTools.UnitTesting;
22
using Tensorflow.NumPy;
3-
using Tensorflow;
4-
using Tensorflow.Operations;
53
using static Tensorflow.KerasApi;
64

7-
namespace TensorFlowNET.Keras.UnitTest
5+
namespace Tensorflow.Keras.UnitTest.Layers
86
{
97
[TestClass]
108
public class LayersConvolutionTest : EagerModeTestBase
@@ -14,7 +12,7 @@ public void BasicConv1D()
1412
{
1513
var filters = 8;
1614

17-
var conv = keras.layers.Conv1D(filters, kernel_size: 3, activation: "linear");
15+
var conv = keras.layers.Conv1D(filters, kernel_size: 3, activation: "linear");
1816

1917
var x = np.arange(256.0f).reshape((8, 8, 4));
2018
var y = conv.Apply(x);
Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,43 @@
11
using Microsoft.VisualStudio.TestTools.UnitTesting;
2-
using Tensorflow;
32
using Tensorflow.NumPy;
43
using static Tensorflow.Binding;
54
using static Tensorflow.KerasApi;
65

7-
namespace TensorFlowNET.Keras.UnitTest {
8-
[TestClass]
9-
public class LayersCroppingTest : EagerModeTestBase {
10-
[TestMethod]
11-
public void Cropping1D () {
12-
Shape input_shape = (1, 5, 2);
13-
var x = tf.zeros(input_shape);
14-
var cropping_1d = keras.layers.Cropping1D(new[] { 1, 2 });
15-
var y = cropping_1d.Apply(x);
16-
Assert.AreEqual((1, 2, 2), y.shape);
17-
}
6+
namespace Tensorflow.Keras.UnitTest.Layers
7+
{
8+
[TestClass]
9+
public class LayersCroppingTest : EagerModeTestBase
10+
{
11+
[TestMethod]
12+
public void Cropping1D()
13+
{
14+
Shape input_shape = (1, 5, 2);
15+
var x = tf.zeros(input_shape);
16+
var cropping_1d = keras.layers.Cropping1D(new[] { 1, 2 });
17+
var y = cropping_1d.Apply(x);
18+
Assert.AreEqual((1, 2, 2), y.shape);
19+
}
1820

19-
[TestMethod]
20-
public void Cropping2D () {
21-
Shape input_shape = (1, 5, 6, 1);
22-
NDArray cropping = new NDArray(new[,] { { 1, 2 }, { 1, 3 } });
23-
var x = tf.zeros(input_shape);
24-
var cropping_2d = keras.layers.Cropping2D(cropping);
25-
var y = cropping_2d.Apply(x);
26-
Assert.AreEqual((1, 2, 2, 1), y.shape);
27-
}
21+
[TestMethod]
22+
public void Cropping2D()
23+
{
24+
Shape input_shape = (1, 5, 6, 1);
25+
NDArray cropping = new NDArray(new[,] { { 1, 2 }, { 1, 3 } });
26+
var x = tf.zeros(input_shape);
27+
var cropping_2d = keras.layers.Cropping2D(cropping);
28+
var y = cropping_2d.Apply(x);
29+
Assert.AreEqual((1, 2, 2, 1), y.shape);
30+
}
2831

29-
[TestMethod]
30-
public void Cropping3D () {
31-
Shape input_shape = new Shape(1, 5, 6, 7, 1);
32-
NDArray cropping = new NDArray(new[,] { { 1, 2 }, { 1, 3 }, { 1, 4 } });
33-
var x = tf.zeros(input_shape);
34-
var cropping_3d = keras.layers.Cropping3D(cropping);
35-
var y = cropping_3d.Apply(x);
36-
Assert.AreEqual(new Shape(1, 2, 2, 2, 1), y.shape);
37-
}
38-
}
32+
[TestMethod]
33+
public void Cropping3D()
34+
{
35+
Shape input_shape = new Shape(1, 5, 6, 7, 1);
36+
NDArray cropping = new NDArray(new[,] { { 1, 2 }, { 1, 3 }, { 1, 4 } });
37+
var x = tf.zeros(input_shape);
38+
var cropping_3d = keras.layers.Cropping3D(cropping);
39+
var y = cropping_3d.Apply(x);
40+
Assert.AreEqual(new Shape(1, 2, 2, 2, 1), y.shape);
41+
}
42+
}
3943
}

test/TensorFlowNET.Keras.UnitTest/Layers/Layers.Merging.Test.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
using Microsoft.VisualStudio.TestTools.UnitTesting;
22
using Tensorflow.NumPy;
3-
using Tensorflow;
43
using static Tensorflow.KerasApi;
54

6-
namespace TensorFlowNET.Keras.UnitTest
5+
namespace Tensorflow.Keras.UnitTest.Layers
76
{
87
[TestClass]
98
public class LayersMergingTest : EagerModeTestBase

0 commit comments

Comments
 (0)