FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
TensorFlow.NET/src/TensorFlowNET.Core/Tensors/tensor_util.cs at master · lifemd/TensorFlow.NET · GitHub
lifemd
/
TensorFlow.NET
Public
forked from
SciSharp/TensorFlow.NET
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
TensorFlow.NET
/
src
/
TensorFlowNET.Core
/
Tensors
/
tensor_util.cs
Copy path
More file actions
More file actions
Latest commit
History
History
History
113 lines (97 loc) · 3.22 KB
Breadcrumbs
TensorFlow.NET
/
src
/
TensorFlowNET.Core
/
Tensors
/
tensor_util.cs
Copy path
File metadata and controls
113 lines (97 loc) · 3.22 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
using
NumSharp
.
Core
;
using
NumSharp
.
Core
.
Interfaces
;
using
System
;
using
System
.
Collections
.
Generic
;
using
System
.
Linq
;
using
System
.
Text
;
using
tensor_pb2
=
Tensorflow
;
namespace
Tensorflow
{
public
static
class
tensor_util
{
public
static
TensorProto
make_tensor_proto
(
NDArray
nd
,
bool
verify_shape
=
false
)
{
var
shape
=
nd
.
Storage
.
Shape
;
var
numpy_dtype
=
dtypes
.
as_dtype
(
nd
.
dtype
)
;
var
tensor_proto
=
new
tensor_pb2
.
TensorProto
{
Dtype
=
numpy_dtype
.
as_datatype_enum
(
)
,
TensorShape
=
shape
.
as_shape
(
nd
.
shape
)
.
as_proto
(
)
}
;
switch
(
nd
.
dtype
.
Name
)
{
case
"Int32"
:
tensor_proto
.
IntVal
.
AddRange
(
nd
.
Data
<
int
>
(
)
)
;
break
;
case
"Single"
:
tensor_proto
.
FloatVal
.
AddRange
(
nd
.
Data
<
float
>
(
)
)
;
break
;
case
"Double"
:
tensor_proto
.
DoubleVal
.
AddRange
(
nd
.
Data
<
double
>
(
)
)
;
break
;
case
"String"
:
tensor_proto
.
StringVal
.
AddRange
(
nd
.
Data
<
string
>
(
)
.
Select
(
x
=>
Google
.
Protobuf
.
ByteString
.
CopyFromUtf8
(
x
)
)
)
;
break
;
default
:
throw
new
Exception
(
"Not Implemented"
)
;
}
return
tensor_proto
;
}
public
static
NDArray
convert_to_numpy_ndarray
(
object
values
)
{
NDArray
nd
;
switch
(
values
)
{
case
NDArray
val
:
nd
=
val
;
break
;
case
int
val
:
nd
=
np
.
asarray
(
val
)
;
break
;
case
int
[
]
val
:
nd
=
np
.
array
(
val
)
;
break
;
case
float
val
:
nd
=
np
.
asarray
(
val
)
;
break
;
case
double
val
:
nd
=
np
.
asarray
(
val
)
;
break
;
case
string
val
:
nd
=
np
.
asarray
(
val
)
;
break
;
default
:
throw
new
Exception
(
"Not Implemented"
)
;
}
return
nd
;
}
public
static
TensorShapeProto
as_shape
(
long
[
]
dims
)
{
TensorShapeProto
shape
=
new
TensorShapeProto
(
)
;
for
(
int
i
=
0
;
i
<
dims
.
Length
;
i
++
)
{
var
dim
=
new
TensorShapeProto
.
Types
.
Dim
(
)
;
dim
.
Size
=
dims
[
i
]
;
dim
.
Name
=
$
"dim_
{
i
}
"
;
shape
.
Dim
.
Add
(
dim
)
;
}
return
shape
;
}
public
static
TensorShape
as_shape
(
this
IShape
shape
,
int
[
]
dims
)
{
return
new
TensorShape
(
dims
)
;
}
public
static
TensorShapeProto
as_proto
(
this
TensorShape
tshape
)
{
TensorShapeProto
shape
=
new
TensorShapeProto
(
)
;
for
(
int
i
=
0
;
i
<
tshape
.
NDim
;
i
++
)
{
var
dim
=
new
TensorShapeProto
.
Types
.
Dim
(
)
;
dim
.
Size
=
tshape
.
Dimensions
[
i
]
;
dim
.
Name
=
$
"dim_
{
i
}
"
;
shape
.
Dim
.
Add
(
dim
)
;
}
return
shape
;
}
}
}
Back
|
FazBrowse Home
|
New Git URL