forked from SciSharp/TensorFlow.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArray.Indexing.Test.cs
More file actions
179 lines (157 loc) · 6.36 KB
/
Copy pathArray.Indexing.Test.cs
File metadata and controls
179 lines (157 loc) · 6.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Tensorflow;
using Tensorflow.NumPy;
using static Tensorflow.Binding;
namespace TensorFlowNET.UnitTest.NumPy
{
/// <summary>
/// https://numpy.org/doc/stable/user/basics.indexing.html
/// </summary>
[TestClass]
public class ArrayIndexingTest : EagerModeTestBase
{
[TestMethod]
public void int_params()
{
var x = np.arange(24).reshape((2, 3, 4));
x[1, 2, 3] = 1;
var y = x[1, 2, 3];
Assert.AreEqual(y.shape, Shape.Scalar);
Assert.AreEqual(y, 1);
x[0, 0] = new[] { 3, 1, 1, 2 };
y = x[0, 0];
Assert.AreEqual(y.shape, 4);
Assert.AreEqual(y, new[] { 3, 1, 1, 2 });
y = x[0];
Assert.AreEqual(y.shape, (3, 4));
var z = np.arange(12).reshape((3, 4));
x[1] = z;
Assert.AreEqual(x[1], z);
}
[TestMethod]
public void slice_newaxis()
{
var x = np.arange(20).reshape((4, 5));
var y = x[np.newaxis, ":2"];
Assert.AreEqual(y.shape, (1, 2, 5));
}
[TestMethod]
public void slice_params()
{
var x = np.arange(12).reshape((3, 4));
var y = x[new Slice(0, 1), new Slice(2)];
Assert.AreEqual(y.shape, (1, 2));
Assert.AreEqual(y, np.array(new[] { 2, 3 }).reshape((1, 2)));
}
[TestMethod]
public void slice_string_params()
{
var x = np.arange(12).reshape((3, 4));
var y = x[Slice.ParseSlices("0:1,2:")];
Assert.AreEqual(y.shape, (1, 2));
Assert.AreEqual(y, np.array(new[] { 2, 3 }).reshape((1, 2)));
}
[TestMethod]
public void slice_out_bound()
{
var input_shape = tf.constant(new int[] { 1, 1 });
var input_shape_val = input_shape.numpy();
input_shape_val[(int)input_shape.size - 1] = 1;
input_shape.Dispose();
}
[TestMethod]
public void shape_helper_get_shape_3dim()
{
var x = np.arange(24).reshape((4, 3, 2));
var shape1 = ShapeHelper.GetShape(x.shape, new Slice(1, isIndex: true));
Assert.AreEqual(shape1, (3, 2));
var shape2 = ShapeHelper.GetShape(x.shape, new Slice(1));
Assert.AreEqual(shape2, (3, 3, 2));
var shape3 = ShapeHelper.GetShape(x.shape, new Slice(2), Slice.All);
Assert.AreEqual(shape3, (2, 3, 2));
var shape4 = ShapeHelper.GetShape(x.shape, new Slice(1, isIndex: true), new Slice(2));
Assert.AreEqual(shape4, (1, 2));
var shape5 = ShapeHelper.GetShape(x.shape, new Slice(1, isIndex: true), new Slice(1));
Assert.AreEqual(shape5, (2, 2));
var shape6 = ShapeHelper.GetShape(x.shape, new Slice(1), new Slice(1, isIndex: true), new Slice(1));
Assert.AreEqual(shape6, (3, 1));
}
[TestMethod]
public void shape_helper_get_shape_4dim()
{
var x = np.arange(120).reshape((4, 3, 2, 5));
var slices = new[] { new Slice(1, isIndex: true), new Slice(1), new Slice(0, isIndex: true), new Slice(1) };
var shape1 = ShapeHelper.GetShape(x.shape, slices);
Assert.AreEqual(shape1, (2, 4));
var shape2 = ShapeHelper.GetShape(x.shape, Slice.All);
Assert.AreEqual(shape2, (4, 3, 2, 5));
var shape3 = ShapeHelper.GetShape(x.shape, Slice.All, new Slice(0, isIndex: true));
Assert.AreEqual(shape3, (4, 3, 2));
}
[TestMethod]
public void iterating()
{
var array = np.array(new[,] { { 0, 3 }, { 2, 2 }, { 3, 1 } });
int i = 0;
foreach(var x in array)
{
if (i == 0)
Assert.AreEqual(x, new[] { 0, 3 });
else
Assert.AreEqual(x, array[i]);
i++;
}
}
[TestMethod]
public void slice_step_setter()
{
var array = np.arange(32).reshape((4, 8));
var s1 = array[Slice.All, new Slice(2, 5, 2)] + 1;
Assert.AreEqual(s1.shape, (4, 2));
var expected = new[] { 3, 5, 11, 13, 19, 21, 27, 29 };
Assert.IsTrue(Enumerable.SequenceEqual(expected, s1.ToArray<int>()));
array[Slice.All, new Slice(2, 5, 2)] = s1;
Assert.AreEqual(array[0], new[] { 0, 1, 3, 3, 5, 5, 6, 7 });
Assert.AreEqual(array[1], new[] { 8, 9, 11, 11, 13, 13, 14, 15 });
Assert.AreEqual(array[2], new[] { 16, 17, 19, 19, 21, 21, 22, 23 });
Assert.AreEqual(array[3], new[] { 24, 25, 27, 27, 29, 29, 30, 31 });
}
[TestMethod]
public void slice_step_setter_diff_shape()
{
var array = np.arange(32).reshape((4, 8));
var s1 = np.array(new[] { 100, 200 });
array[Slice.All, new Slice(2, 5, 2)] = s1;
Assert.AreEqual(array[0], new[] { 0, 1, 100, 3, 200, 5, 6, 7 });
Assert.AreEqual(array[1], new[] { 8, 9, 100, 11, 200, 13, 14, 15 });
Assert.AreEqual(array[2], new[] { 16, 17, 100, 19, 200, 21, 22, 23 });
Assert.AreEqual(array[3], new[] { 24, 25, 100, 27, 200, 29, 30, 31 });
}
[TestMethod]
public void mask_2d_get_value()
{
var x = np.arange(25).reshape((5, 5));
var y = np.array(new[] { true, false, true, false, true });
var z = x[y];
Assert.AreEqual(z.shape, (3, 5));
Assert.AreEqual(z[0], new[] { 0, 1, 2, 3, 4 });
Assert.AreEqual(z[1], new[] { 10, 11, 12, 13, 14 });
Assert.AreEqual(z[2], new[] { 20, 21, 22, 23, 24 });
}
[TestMethod]
public void mask_2d_set_value()
{
var x = np.arange(25).reshape((5, 5));
var y = np.array(new[] {true, false, true, false, false});
x[y] = 0;
Assert.AreEqual(x[0], new[] { 0, 0, 0, 0, 0 });
Assert.AreEqual(x[1], new[] { 5, 6, 7, 8, 9 });
Assert.AreEqual(x[2], new[] { 0, 0, 0, 0, 0 });
Assert.AreEqual(x[3], new[] { 15, 16, 17, 18, 19 });
}
}
}