Skip to content

Commit d44f044

Browse files
committed
support n-dim indexing. SciSharp#368
1 parent e9feec4 commit d44f044

2 files changed

Lines changed: 78 additions & 3 deletions

File tree

‎src/TensorFlowNET.Core/TensorFlowNET.Core.csproj‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ Docs: https://tensorflownet.readthedocs.io</Description>
2727
6. Add Local Response Normalization.
2828
7. Add tf.image related APIs.
2929
8. Add tf.random_normal, tf.constant, tf.pad, tf.shape, tf.image.resize_nearest_neighbor.
30-
9. MultiThread is safe.</PackageReleaseNotes>
30+
9. MultiThread is safe.
31+
10. Support n-dim indexing for tensor.</PackageReleaseNotes>
3132
<LangVersion>7.3</LangVersion>
3233
<FileVersion>0.11.2.0</FileVersion>
3334
<PackageLicenseFile>LICENSE</PackageLicenseFile>

‎src/TensorFlowNET.Core/Tensors/Tensor.Index.cs‎

Lines changed: 76 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,92 @@ limitations under the License.
1717
using NumSharp;
1818
using System;
1919
using System.Collections.Generic;
20+
using System.Linq;
2021
using System.Text;
2122
using static Tensorflow.Binding;
2223

2324
namespace Tensorflow
2425
{
2526
public partial class Tensor
2627
{
27-
public Tensor this[int idx]
28+
public Tensor this[int idx] => slice(idx);
29+
30+
public Tensor this[params string[] slices]
2831
{
2932
get
3033
{
31-
return slice(idx);
34+
var slice_spec = slices.Select(x => x == null ? null : new Slice(x)).ToArray();
35+
var begin = new List<int>();
36+
var end = new List<int>();
37+
var strides = new List<int>();
38+
39+
var index = 0;
40+
var (new_axis_mask, shrink_axis_mask) = (0, 0);
41+
var (begin_mask, end_mask) = (0, 0);
42+
var ellipsis_mask = 0;
43+
44+
foreach (var s in slice_spec)
45+
{
46+
if(s == null)
47+
{
48+
begin.Add(0);
49+
end.Add(0);
50+
strides.Add(0);
51+
new_axis_mask |= (1 << index);
52+
}
53+
else
54+
{
55+
if (s.Start.HasValue)
56+
{
57+
begin.Add(s.Start.Value);
58+
}
59+
else
60+
{
61+
begin.Add(0);
62+
begin_mask |= (1 << index);
63+
}
64+
65+
if (s.Stop.HasValue)
66+
{
67+
end.Add(s.Stop.Value);
68+
}
69+
else
70+
{
71+
end.Add(0);
72+
end_mask |= (1 << index);
73+
}
74+
75+
strides.Add(s.Step);
76+
}
77+
78+
index += 1;
79+
}
80+
81+
return tf_with(ops.name_scope(null, "strided_slice", new { begin, end, strides }), scope =>
82+
{
83+
string name = scope;
84+
if (begin != null)
85+
{
86+
var (packed_begin, packed_end, packed_strides) =
87+
(array_ops.stack(begin.ToArray()),
88+
array_ops.stack(end.ToArray()),
89+
array_ops.stack(strides.ToArray()));
90+
91+
return gen_array_ops.strided_slice(
92+
this,
93+
packed_begin,
94+
packed_end,
95+
packed_strides,
96+
begin_mask: begin_mask,
97+
end_mask: end_mask,
98+
shrink_axis_mask: shrink_axis_mask,
99+
new_axis_mask: new_axis_mask,
100+
ellipsis_mask: ellipsis_mask,
101+
name: name);
102+
}
103+
104+
throw new NotImplementedException("");
105+
});
32106
}
33107
}
34108

0 commit comments

Comments
 (0)