Skip to content

Commit ca3a775

Browse files
committed
Tensor indexing. SciSharp#368
1 parent b1bd05c commit ca3a775

2 files changed

Lines changed: 140 additions & 105 deletions

File tree

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
/*****************************************************************************
2+
Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
******************************************************************************/
16+
17+
using NumSharp;
18+
using System;
19+
using System.Collections.Generic;
20+
using System.Text;
21+
using static Tensorflow.Binding;
22+
23+
namespace Tensorflow
24+
{
25+
public partial class Tensor
26+
{
27+
public Tensor this[int idx]
28+
{
29+
get
30+
{
31+
return slice(idx);
32+
}
33+
}
34+
35+
public Tensor slice(Slice slice)
36+
{
37+
var slice_spec = new int[] { slice.Start.Value };
38+
var begin = new List<int>();
39+
var end = new List<int>();
40+
var strides = new List<int>();
41+
42+
var index = 0;
43+
var (new_axis_mask, shrink_axis_mask) = (0, 0);
44+
var (begin_mask, end_mask) = (0, 0);
45+
var ellipsis_mask = 0;
46+
47+
foreach (var s in slice_spec)
48+
{
49+
begin.Add(s);
50+
if (slice.Stop.HasValue)
51+
{
52+
end.Add(slice.Stop.Value);
53+
}
54+
else
55+
{
56+
end.Add(0);
57+
end_mask |= (1 << index);
58+
}
59+
60+
strides.Add(slice.Step);
61+
62+
index += 1;
63+
}
64+
65+
return tf_with(ops.name_scope(null, "strided_slice", new { begin, end, strides }), scope =>
66+
{
67+
string name = scope;
68+
if (begin != null)
69+
{
70+
var (packed_begin, packed_end, packed_strides) =
71+
(array_ops.stack(begin.ToArray()),
72+
array_ops.stack(end.ToArray()),
73+
array_ops.stack(strides.ToArray()));
74+
75+
return gen_array_ops.strided_slice(
76+
this,
77+
packed_begin,
78+
packed_end,
79+
packed_strides,
80+
begin_mask: begin_mask,
81+
end_mask: end_mask,
82+
shrink_axis_mask: shrink_axis_mask,
83+
new_axis_mask: new_axis_mask,
84+
ellipsis_mask: ellipsis_mask,
85+
name: name);
86+
}
87+
88+
throw new NotImplementedException("");
89+
});
90+
}
91+
92+
public Tensor slice(int start)
93+
{
94+
var slice_spec = new int[] { start };
95+
var begin = new List<int>();
96+
var end = new List<int>();
97+
var strides = new List<int>();
98+
99+
var index = 0;
100+
var (new_axis_mask, shrink_axis_mask) = (0, 0);
101+
var (begin_mask, end_mask) = (0, 0);
102+
var ellipsis_mask = 0;
103+
104+
foreach (var s in slice_spec)
105+
{
106+
begin.Add(s);
107+
end.Add(s + 1);
108+
strides.Add(1);
109+
shrink_axis_mask |= (1 << index);
110+
index += 1;
111+
}
112+
113+
return tf_with(ops.name_scope(null, "strided_slice", new { begin, end, strides }), scope =>
114+
{
115+
string name = scope;
116+
if (begin != null)
117+
{
118+
var (packed_begin, packed_end, packed_strides) =
119+
(array_ops.stack(begin.ToArray()),
120+
array_ops.stack(end.ToArray()),
121+
array_ops.stack(strides.ToArray()));
122+
123+
return gen_array_ops.strided_slice(
124+
this,
125+
packed_begin,
126+
packed_end,
127+
packed_strides,
128+
begin_mask: begin_mask,
129+
end_mask: end_mask,
130+
shrink_axis_mask: shrink_axis_mask,
131+
new_axis_mask: new_axis_mask,
132+
ellipsis_mask: ellipsis_mask,
133+
name: name);
134+
}
135+
136+
throw new NotImplementedException("");
137+
});
138+
}
139+
}
140+
}

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

Lines changed: 0 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ limitations under the License.
2828
using NumSharp.Backends.Unmanaged;
2929
using NumSharp.Utilities;
3030
using Tensorflow.Framework;
31-
using static Tensorflow.Binding;
3231

3332
namespace Tensorflow
3433
{
@@ -436,110 +435,6 @@ public NDArray eval(Session session, params FeedItem[] feed_dict)
436435
return ops._eval_using_default_session(this, feed_dict, graph, session);
437436
}
438437

439-
public Tensor slice(Slice slice)
440-
{
441-
var slice_spec = new int[] {slice.Start.Value};
442-
var begin = new List<int>();
443-
var end = new List<int>();
444-
var strides = new List<int>();
445-
446-
var index = 0;
447-
var (new_axis_mask, shrink_axis_mask) = (0, 0);
448-
var (begin_mask, end_mask) = (0, 0);
449-
var ellipsis_mask = 0;
450-
451-
foreach (var s in slice_spec)
452-
{
453-
begin.Add(s);
454-
if (slice.Stop.HasValue)
455-
{
456-
end.Add(slice.Stop.Value);
457-
} else
458-
{
459-
end.Add(0);
460-
end_mask |= (1 << index);
461-
}
462-
463-
strides.Add(slice.Step);
464-
465-
index += 1;
466-
}
467-
468-
return tf_with(ops.name_scope(null, "strided_slice", new {begin, end, strides}), scope =>
469-
{
470-
string name = scope;
471-
if (begin != null)
472-
{
473-
var (packed_begin, packed_end, packed_strides) =
474-
(array_ops.stack(begin.ToArray()),
475-
array_ops.stack(end.ToArray()),
476-
array_ops.stack(strides.ToArray()));
477-
478-
return gen_array_ops.strided_slice(
479-
this,
480-
packed_begin,
481-
packed_end,
482-
packed_strides,
483-
begin_mask: begin_mask,
484-
end_mask: end_mask,
485-
shrink_axis_mask: shrink_axis_mask,
486-
new_axis_mask: new_axis_mask,
487-
ellipsis_mask: ellipsis_mask,
488-
name: name);
489-
}
490-
491-
throw new NotImplementedException("");
492-
});
493-
}
494-
495-
public Tensor slice(int start)
496-
{
497-
var slice_spec = new int[] {start};
498-
var begin = new List<int>();
499-
var end = new List<int>();
500-
var strides = new List<int>();
501-
502-
var index = 0;
503-
var (new_axis_mask, shrink_axis_mask) = (0, 0);
504-
var (begin_mask, end_mask) = (0, 0);
505-
var ellipsis_mask = 0;
506-
507-
foreach (var s in slice_spec)
508-
{
509-
begin.Add(s);
510-
end.Add(s + 1);
511-
strides.Add(1);
512-
shrink_axis_mask |= (1 << index);
513-
index += 1;
514-
}
515-
516-
return tf_with(ops.name_scope(null, "strided_slice", new {begin, end, strides}), scope =>
517-
{
518-
string name = scope;
519-
if (begin != null)
520-
{
521-
var (packed_begin, packed_end, packed_strides) =
522-
(array_ops.stack(begin.ToArray()),
523-
array_ops.stack(end.ToArray()),
524-
array_ops.stack(strides.ToArray()));
525-
526-
return gen_array_ops.strided_slice(
527-
this,
528-
packed_begin,
529-
packed_end,
530-
packed_strides,
531-
begin_mask: begin_mask,
532-
end_mask: end_mask,
533-
shrink_axis_mask: shrink_axis_mask,
534-
new_axis_mask: new_axis_mask,
535-
ellipsis_mask: ellipsis_mask,
536-
name: name);
537-
}
538-
539-
throw new NotImplementedException("");
540-
});
541-
}
542-
543438
public override string ToString()
544439
{
545440
// this can throw IndexOutOfRangeException

0 commit comments

Comments
 (0)