forked from SciSharp/TensorFlow.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTensor.cs
More file actions
326 lines (290 loc) · 10.8 KB
/
Tensor.cs
File metadata and controls
326 lines (290 loc) · 10.8 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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*****************************************************************************
Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
******************************************************************************/
using NumSharp;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using Tensorflow.Framework;
#if SERIALIZABLE
using Newtonsoft.Json;
#endif
namespace Tensorflow
{
/// <summary>
/// A tensor is a generalization of vectors and matrices to potentially higher dimensions.
/// Internally, TensorFlow represents tensors as n-dimensional arrays of base datatypes.
/// </summary>
[SuppressMessage("ReSharper", "ConvertToAutoProperty")]
public partial class Tensor : DisposableObject,
ITensorOrOperation,
_TensorLike,
ITensorOrTensorArray,
IPackable<Tensor>,
ICanBeFlattened
{
private readonly int _id;
private readonly Operation _op;
private readonly int _value_index;
private TF_Output? _tf_output;
private readonly TF_DataType _override_dtype;
#if SERIALIZABLE
[JsonIgnore]
#endif
public int Id => _id;
/// <summary>
/// The Graph that contains this tensor.
/// </summary>
#if SERIALIZABLE
[JsonIgnore]
#endif
public Graph graph => op?.graph;
/// <summary>
/// The Operation that produces this tensor as an output.
/// </summary>
#if SERIALIZABLE
[JsonIgnore]
#endif
public Operation op => _op;
#if SERIALIZABLE
[JsonIgnore]
#endif
public Tensor[] outputs => op.outputs;
/// <summary>
/// The string name of this tensor.
/// </summary>
public string name => $"{(op == null ? "<unnamed>" : $"{op.name}:{_value_index}")}";
/// <summary>
/// The index of this tensor in the outputs of its Operation.
/// </summary>
public int value_index => _value_index;
/// <summary>
/// The DType of elements in this tensor.
/// </summary>
public TF_DataType dtype => _handle == IntPtr.Zero ? _override_dtype : c_api.TF_TensorType(_handle);
#if SERIALIZABLE
[JsonIgnore]
#endif
public ulong bytesize => _handle == IntPtr.Zero ? 0 : c_api.TF_TensorByteSize(_handle);
#if SERIALIZABLE
[JsonIgnore]
#endif
public ulong itemsize => _handle == IntPtr.Zero ? 0 : c_api.TF_DataTypeSize(dtype);
#if SERIALIZABLE
[JsonIgnore]
#endif
public ulong size => _handle == IntPtr.Zero ? 0 : bytesize / itemsize;
#if SERIALIZABLE
[JsonIgnore]
#endif
public IntPtr buffer => _handle == IntPtr.Zero ? IntPtr.Zero : c_api.TF_TensorData(_handle);
public int num_consumers(TF_Output oper_out) => _handle == IntPtr.Zero ? 0 : c_api.TF_OperationOutputNumConsumers(oper_out);
#if SERIALIZABLE
[JsonIgnore]
#endif
public int NDims => rank;
/// <summary>
/// The name of the device on which this tensor will be produced, or null.
/// </summary>
#if SERIALIZABLE
[JsonIgnore]
#endif
public string Device => op.Device;
#if SERIALIZABLE
[JsonIgnore]
#endif
public int[] dims => shape;
/// <summary>
/// Used for keep other pointer when do implicit operating
/// </summary>
#if SERIALIZABLE
[JsonIgnore]
#endif
public object Tag { get; set; }
/// <summary>
/// Returns the shape of a tensor.
/// </summary>
/// <remarks>https://www.tensorflow.org/api_docs/python/tf/shape</remarks>
public int[] shape
{
get
{
var dims = new long[rank < 0 ? 0 : rank];
if (_handle == IntPtr.Zero)
{
using (var status = new Status())
{
c_api.TF_GraphGetTensorShape(op.graph, _as_tf_output(), dims, rank, status);
status.Check();
}
}
else
{
for (int i = 0; i < rank; i++)
dims[i] = c_api.TF_Dim(_handle, i);
}
return dims.Select(x => ((IConvertible) x).ToInt32(CultureInfo.InvariantCulture)).ToArray();
}
set
{
using (var status = new Status())
{
if (value == null)
c_api.TF_GraphSetTensorShape(graph, _as_tf_output(), null, -1, status);
else
c_api.TF_GraphSetTensorShape(graph, _as_tf_output(), value.Select(Convert.ToInt64).ToArray(), value.Length, status);
status.Check(true);
}
}
}
public int[] _shape_tuple()
{
return rank < 0 ? null : shape;
}
#if SERIALIZABLE
[JsonIgnore]
#endif
public TensorShape TensorShape => rank < 0 ? new TensorShape() : tensor_util.to_shape(shape);
/// <summary>
/// Updates the shape of this tensor.
/// </summary>
public void set_shape(TensorShape shape)
{
this.shape = shape.rank >= 0 ? shape.dims : null;
}
/// <summary>
/// Updates the shape of this tensor.
/// </summary>
public void set_shape(Tensor shape)
{
// ReSharper disable once MergeConditionalExpression
this.shape = shape is null ? null : shape.shape;
}
/// <summary>
/// number of dimensions <br></br>
/// -1 Unknown <br></br>
/// 0 Scalar (magnitude only) <br></br>
/// 1 Vector (magnitude and direction) <br></br>
/// 2 Matrix (table of numbers) <br></br>
/// 3 3-Tensor (cube of numbers) <br></br>
/// n n-Tensor (you get the idea)
/// </summary>
/// <remarks>https://www.tensorflow.org/api_docs/python/tf/rank</remarks>
public int rank
{
get
{
if (_handle == IntPtr.Zero)
{
using (var status = new Status())
{
var output = _as_tf_output();
int ndim = c_api.TF_GraphGetTensorNumDims(op.graph, output, status);
status.Check();
return ndim;
}
}
return c_api.TF_NumDims(_handle);
}
}
/// <summary>
/// Returns a list of Operations that consume this tensor.
/// </summary>
/// <returns></returns>
public Operation[] consumers()
{
var output = _as_tf_output();
var consumer_names = c_api.TF_OperationOutputConsumers_wrapper(output);
return consumer_names.Select(x => graph.OperationByName(x)).ToArray();
}
public TF_Output _as_tf_output()
{
if (!_tf_output.HasValue)
_tf_output = new TF_Output(op, value_index);
return _tf_output.Value;
}
public Tensor MaybeMove()
{
var tensor = c_api.TF_TensorMaybeMove(_handle);
return tensor;
}
/// <summary>
/// Evaluates this tensor in a `Session`.
/// </summary>
/// <param name="feed_dict">A dictionary that maps `Tensor` objects to feed values.</param>
/// <returns>A <see cref="NumSharp"/> array corresponding to the value of this tensor.</returns>
public NDArray eval(params FeedItem[] feed_dict)
{
return ops._eval_using_default_session(this, feed_dict, graph);
}
/// <summary>
/// Evaluates this tensor in a `Session`.
/// </summary>
/// <param name="feed_dict">A dictionary that maps `Tensor` objects to feed values.</param>
/// <param name="session">The `Session` to be used to evaluate this tensor.</param>
/// <returns>A <see cref="NumSharp"/> array corresponding to the value of this tensor.</returns>
public NDArray eval(Session session, params FeedItem[] feed_dict)
{
return ops._eval_using_default_session(this, feed_dict, graph, session);
}
public override string ToString()
{
// this can throw IndexOutOfRangeException
switch (rank)
{
case -1:
return $"tf.Tensor '{name}' shape=<unknown> dtype={dtype}";
case 0:
return $"tf.Tensor '{name}' shape=() dtype={dtype}";
default:
return $"tf.Tensor '{name}' shape=({string.Join(",", shape)}) dtype={dtype}";
}
}
/// <summary>
/// Dispose any managed resources.
/// </summary>
/// <remarks>Equivalent to what you would perform inside <see cref="DisposableObject.Dispose"/></remarks>
protected override void DisposeManagedResources()
{
AllocationReferenceHolder = null;
}
[SuppressMessage("ReSharper", "ConvertIfStatementToSwitchStatement")]
protected override void DisposeUnmanagedResources(IntPtr handle)
{
c_api.TF_DeleteTensor(handle);
if (AllocationHandle == null)
return;
if (AllocationType == AllocationType.GCHandle)
{
((GCHandle) AllocationHandle).Free();
AllocationHandle = null;
AllocationType = AllocationType.None;
} else if (AllocationType == AllocationType.Marshal)
{
Marshal.FreeHGlobal((IntPtr) AllocationHandle);
AllocationHandle = null;
AllocationType = AllocationType.None;
} else
throw new InvalidOperationException($"Tensor.AllocationHandle is not null ({AllocationHandle}) but AllocationType is not matched to a C# allocation type ({AllocationType}).");
}
#if SERIALIZABLE
[JsonIgnore]
#endif
public bool IsDisposed => _disposed;
// public int tensor_int_val { get; set; }
}
}