forked from SciSharp/TensorFlow.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNDArray.Index.cs
More file actions
214 lines (175 loc) · 6.71 KB
/
NDArray.Index.cs
File metadata and controls
214 lines (175 loc) · 6.71 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using static Tensorflow.Binding;
namespace Tensorflow.NumPy
{
public partial class NDArray
{
public NDArray this[params int[] indices]
{
get => GetData(indices.Select(x => new Slice
{
Start = x,
Stop = x + 1,
IsIndex = true
}).ToArray());
set => SetData(indices.Select(x =>
{
if(x < 0)
x = (int)dims[0] + x;
var slice = new Slice
{
Start = x,
Stop = x + 1,
IsIndex = true
};
return slice;
}), value);
}
public NDArray this[params Slice[] slices]
{
get => GetData(slices);
set => SetData(slices, value);
}
public NDArray this[NDArray mask]
{
get
{
if(mask.dtype == TF_DataType.TF_INT32)
return GetData(mask.ToArray<int>());
else if (mask.dtype == TF_DataType.TF_INT64)
return GetData(mask.ToArray<long>().Select(x => Convert.ToInt32(x)).ToArray());
throw new NotImplementedException("");
}
set
{
throw new NotImplementedException("");
}
}
unsafe NDArray GetData(Slice[] slices)
{
if (shape.IsScalar)
return GetScalar();
if (SliceHelper.AreAllIndex(slices, out var indices1))
{
var newshape = ShapeHelper.GetShape(shape, slices);
if (newshape.IsScalar)
{
var offset = ShapeHelper.GetOffset(shape, indices1);
return GetScalar((ulong)offset);
}
else
{
return GetArrayData(newshape, indices1);
}
}
else if (slices.Count() == 1)
{
var slice = slices[0];
if (slice.Step == 1)
{
var newshape = ShapeHelper.GetShape(shape, slice);
var array = new NDArray(newshape, dtype: dtype);
var new_dims = new int[shape.ndim];
new_dims[0] = slice.Start ?? 0;
//for (int i = 1; i < shape.ndim; i++)
//new_dims[i] = (int)shape.dims[i];
var offset = ShapeHelper.GetOffset(shape, new_dims);
var src = (byte*)data + (ulong)offset * dtypesize;
var dst = (byte*)array.data;
var len = (ulong)newshape.size * dtypesize;
System.Buffer.MemoryCopy(src, dst, len, len);
return array;
}
}
// default, performance is bad
var tensor = base[slices.ToArray()];
if (tensor.Handle == null)
{
if (tf.executing_eagerly())
tensor = tf.defaultSession.eval(tensor);
}
return new NDArray(tensor, tf.executing_eagerly());
}
unsafe T GetAtIndex<T>(params int[] indices) where T : unmanaged
{
var offset = (ulong)ShapeHelper.GetOffset(shape, indices);
return *((T*)data + offset);
}
unsafe NDArray GetScalar(ulong offset = 0)
{
var array = new NDArray(Shape.Scalar, dtype: dtype);
var src = (byte*)data + offset * dtypesize;
System.Buffer.MemoryCopy(src, array.buffer.ToPointer(), dtypesize, dtypesize);
return array;
}
unsafe NDArray GetArrayData(Shape newshape, int[] indices)
{
var offset = ShapeHelper.GetOffset(shape, indices);
var len = (ulong)newshape.size * dtypesize;
var array = new NDArray(newshape, dtype: dtype);
var src = (byte*)data + (ulong)offset * dtypesize;
System.Buffer.MemoryCopy(src, array.data.ToPointer(), len, len);
return array;
}
unsafe NDArray GetData(int[] indices, int axis = 0)
{
if (shape.IsScalar)
return GetScalar();
if(axis == 0)
{
var dims = shape.as_int_list();
dims[0] = indices.Length;
var array = np.ndarray(dims, dtype: dtype);
dims[0] = 1;
var len = new Shape(dims).size * dtype.get_datatype_size();
int dst_index = 0;
foreach (var pos in indices)
{
var src_offset = (ulong)ShapeHelper.GetOffset(shape, pos);
var dst_offset = (ulong)ShapeHelper.GetOffset(array.shape, dst_index++);
var src = (byte*)data + src_offset * dtypesize;
var dst = (byte*)array.data + dst_offset * dtypesize;
System.Buffer.MemoryCopy(src, dst, len, len);
}
return array;
}
else
throw new NotImplementedException("");
}
void SetData(IEnumerable<Slice> slices, NDArray array)
=> SetData(slices, array, -1, slices.Select(x => 0).ToArray());
void SetData(IEnumerable<Slice> slices, NDArray array, int currentNDim, int[] indices)
{
if (dtype != array.dtype)
throw new ArrayTypeMismatchException($"Required dtype {dtype} but {array.dtype} is assigned.");
if (!slices.Any())
return;
var slice = slices.First();
if (slices.Count() == 1)
{
if (slice.Step != 1)
throw new NotImplementedException("slice.step should == 1");
if (slice.Start < 0)
throw new NotImplementedException("slice.start should > -1");
indices[indices.Length - 1] = slice.Start ?? 0;
var offset = (ulong)ShapeHelper.GetOffset(shape, indices);
var bytesize = array.bytesize;
unsafe
{
var dst = (byte*)data + offset * dtypesize;
System.Buffer.MemoryCopy(array.data.ToPointer(), dst, bytesize, bytesize);
}
return;
}
currentNDim++;
for (var i = slice.Start ?? 0; i < slice.Stop; i++)
{
indices[currentNDim] = i;
SetData(slices.Skip(1), array, currentNDim, indices);
}
}
}
}