forked from SciSharp/TensorFlow.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdtypes.cs
More file actions
161 lines (140 loc) · 4.95 KB
/
dtypes.cs
File metadata and controls
161 lines (140 loc) · 4.95 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
using System;
using System.Collections.Generic;
using System.Text;
namespace Tensorflow
{
public static class dtypes
{
public static TF_DataType int8 = TF_DataType.TF_INT8;
public static TF_DataType float32 = TF_DataType.TF_FLOAT; // is that float32?
public static TF_DataType float16 = TF_DataType.TF_HALF;
public static Type as_numpy_datatype(this TF_DataType type)
{
switch (type)
{
case TF_DataType.TF_BOOL:
return typeof(bool);
case TF_DataType.TF_INT64:
return typeof(long);
case TF_DataType.TF_INT32:
return typeof(int);
case TF_DataType.TF_INT16:
return typeof(short);
case TF_DataType.TF_FLOAT:
return typeof(float);
case TF_DataType.TF_DOUBLE:
return typeof(double);
case TF_DataType.TF_STRING:
return typeof(string);
default:
return null;
}
}
public static TF_DataType as_dtype(Type type)
{
TF_DataType dtype = TF_DataType.DtInvalid;
switch (type.Name)
{
case "Boolean":
dtype = TF_DataType.TF_BOOL;
break;
case "Int32":
dtype = TF_DataType.TF_INT32;
break;
case "Int64":
dtype = TF_DataType.TF_INT64;
break;
case "Single":
dtype = TF_DataType.TF_FLOAT;
break;
case "Double":
dtype = TF_DataType.TF_DOUBLE;
break;
case "String":
dtype = TF_DataType.TF_STRING;
break;
case "Byte":
dtype = TF_DataType.TF_STRING;
break;
default:
throw new Exception("as_dtype Not Implemented");
}
return dtype;
}
public static DataType as_datatype_enum(this TF_DataType type)
{
DataType dtype = DataType.DtInvalid;
switch (type)
{
default:
Enum.TryParse(((int)type).ToString(), out dtype);
break;
}
return dtype;
}
public static TF_DataType as_base_dtype(this TF_DataType type)
{
return (int)type > 100 ?
(TF_DataType)Enum.Parse(typeof(TF_DataType), ((int)type - 100).ToString()) :
type;
}
public static int name(this TF_DataType type)
{
return (int)type;
}
public static Type as_numpy_dtype(this DataType type)
{
return type.as_tf_dtype().as_numpy_datatype();
}
public static DataType as_base_dtype(this DataType type)
{
return (int)type > 100 ?
(DataType)Enum.Parse(typeof(DataType), ((int)type - 100).ToString()) :
type;
}
public static TF_DataType as_tf_dtype(this DataType type)
{
TF_DataType dtype = TF_DataType.DtInvalid;
switch (type)
{
default:
Enum.TryParse(((int)type).ToString(), out dtype);
break;
}
return dtype;
}
public static TF_DataType as_ref(this TF_DataType type)
{
return (int)type < 100 ?
(TF_DataType)Enum.Parse(typeof(TF_DataType), ((int)type + 100).ToString()) :
type;
}
public static int max(this TF_DataType type)
{
switch (type)
{
case TF_DataType.TF_UINT8:
return 255;
default:
throw new NotImplementedException($"max {type.name()}");
}
}
public static bool is_complex(this TF_DataType type)
{
return type == TF_DataType.TF_COMPLEX || type == TF_DataType.TF_COMPLEX64 || type == TF_DataType.TF_COMPLEX128;
}
public static bool is_integer(this TF_DataType type)
{
return type == TF_DataType.TF_INT8 || type == TF_DataType.TF_INT16 || type == TF_DataType.TF_INT32 || type == TF_DataType.TF_INT64 ||
type == TF_DataType.TF_UINT8 || type == TF_DataType.TF_UINT16 || type == TF_DataType.TF_UINT32 || type == TF_DataType.TF_UINT64;
}
public static bool is_floating(this TF_DataType type)
{
return type == TF_DataType.TF_HALF || type == TF_DataType.TF_FLOAT || type == TF_DataType.TF_DOUBLE;
}
public static bool is_ref_dtype(this TF_DataType type)
{
return (int)type > 100;
}
}
}