forked from SciSharp/TensorFlow.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtensor_util.cs
More file actions
51 lines (48 loc) · 1.78 KB
/
tensor_util.cs
File metadata and controls
51 lines (48 loc) · 1.78 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
using NumSharp.Core;
using System;
using System.Collections.Generic;
using System.Text;
using tensor_pb2 = Tensorflow;
namespace Tensorflow
{
public static class tensor_util
{
public static TensorProto make_tensor_proto(object values, Type dtype = null)
{
NDArray nparray;
TensorProto tensor_proto = null;
TensorShape tensor_shape = new TensorShape(0);
switch (values)
{
case float val:
nparray = np.array(new float[] { val }, np.float32);
tensor_proto = new tensor_pb2.TensorProto
{
Dtype = DataType.DtFloat,
TensorShape = tensor_shape.as_shape().as_proto()
};
tensor_proto.FloatVal.Add(val);
break;
case double val:
nparray = np.array(new double[] { val }, np.float64);
tensor_proto = new tensor_pb2.TensorProto
{
Dtype = DataType.DtDouble,
TensorShape = tensor_shape.as_shape().as_proto()
};
tensor_proto.DoubleVal.Add(val);
break;
case string val:
nparray = np.array(new string[] { val }, np.chars);
tensor_proto = new tensor_pb2.TensorProto
{
Dtype = DataType.DtString,
TensorShape = tensor_shape.as_shape().as_proto()
};
tensor_proto.StringVal.Add(Google.Protobuf.ByteString.CopyFrom(val, Encoding.UTF8));
break;
}
return tensor_proto;
}
}
}