Repository navigation
Expand file tree
/
Copy pathEagerTensorV2.cs
More file actions
81 lines (68 loc) · 2.67 KB
/
Copy pathEagerTensorV2.cs
File metadata and controls
81 lines (68 loc) · 2.67 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
using NumSharp;
using System;
using System.Linq;
using Tensorflow.Eager;
using Tensorflow.Util;
using static Tensorflow.Binding;
namespace Tensorflow
{
public class EagerTensorV2 : DisposableObject
{
SafeTensorHandleHandle EagerTensorHandle;
public string Device
{
get
{
using var _ = EagerTensorHandle.Lease();
return c_api.StringPiece(c_api.TFE_TensorHandleDeviceName(EagerTensorHandle, tf.Status.Handle));
}
}
public EagerTensorV2(IntPtr handle)
{
EagerTensorHandle = c_api.TFE_EagerTensorHandle(handle);
_handle = c_api.TFE_TensorHandleResolve(EagerTensorHandle, tf.Status.Handle);
}
public unsafe EagerTensorV2(NDArray nd, string device_name = "")
{
if (nd.typecode == NPTypeCode.String)
throw new NotImplementedException("Support for NDArray of type string not implemented yet");
var arraySlice = nd.Unsafe.Storage.Shape.IsContiguous ? nd.GetData() : nd.CloneData();
_handle = c_api.TF_NewTensor(nd.dtype.as_dtype(),
nd.shape.Select(i => (long)i).ToArray(),
nd.ndim,
new IntPtr(arraySlice.Address),
nd.size * nd.dtypesize,
deallocator: (IntPtr dataPtr, long len, IntPtr args) =>
{
}, IntPtr.Zero);
EagerTensorHandle = c_api.TFE_NewTensorHandle(_handle, tf.Status.Handle);
}
/*public unsafe EagerTensorV2(float[,] value)
{
var dims = new long[] { value.Rank, value.Length / value.Rank };
fixed (float* pointer = &value[0, 0])
{
// The address stored in pointerToFirst
// is valid only inside this fixed statement block.
tensorHandle = c_api.TF_NewTensor(TF_DataType.TF_FLOAT,
dims,
value.Rank,
new IntPtr(pointer),
value.Length * sizeof(float),
deallocator: (IntPtr dataPtr, long len, IntPtr args) =>
{
}, IntPtr.Zero);
localTensorHandle = c_api.TFE_NewTensorHandle(tensorHandle, status);
_handle = c_api.TFE_EagerTensorFromHandle(tf.context, localTensorHandle);
}
}*/
protected override void DisposeManagedResources()
{
EagerTensorHandle.Dispose();
}
protected override void DisposeUnmanagedResources(IntPtr handle)
{
c_api.TF_DeleteTensor(_handle);
}
}
}