FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
TensorFlow.NET/src/KerasNET.Core/Model.cs at master · lokinfey/TensorFlow.NET · GitHub
lokinfey
/
TensorFlow.NET
Public
forked from
SciSharp/TensorFlow.NET
Notifications
You must be signed in to change notification settings
Fork
0
Star
1
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
/
KerasNET.Core
/
Model.cs
Copy path
More file actions
More file actions
Latest commit
History
History
History
127 lines (106 loc) · 3.85 KB
Breadcrumbs
TensorFlow.NET
/
src
/
KerasNET.Core
/
Model.cs
Copy path
File metadata and controls
127 lines (106 loc) · 3.85 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
using
Keras
.
Layers
;
using
NumSharp
;
using
System
;
using
System
.
Collections
.
Generic
;
using
System
.
Text
;
using
Tensorflow
;
using
static
Keras
.
Keras
;
using
static
Tensorflow
.
Python
;
namespace
Keras
{
public
class
Model
{
public
Tensor
Flow
;
List
<
ILayer
>
layer_stack
;
public
TensorShape
InputShape
;
public
Model
(
)
{
layer_stack
=
new
List
<
ILayer
>
(
)
;
}
public
Model
Add
(
ILayer
layer
)
{
layer_stack
.
Add
(
layer
)
;
return
this
;
}
public
Model
Add
(
IEnumerable
<
ILayer
>
layers
)
{
layer_stack
.
AddRange
(
layers
)
;
return
this
;
}
public
Tensor
getFlow
(
)
{
try
{
return
Flow
;
}
catch
(
Exception
ex
)
{
return
null
;
}
}
public
(
Operation
,
Tensor
,
Tensor
)
make_graph
(
Tensor
features
,
Tensor
labels
)
{
// TODO : Creating Loss Functions And Optimizers.....
#region Model Layers Graph
/*
var stddev = 1 / Math.Sqrt(2);
var d1 = new Dense(num_hidden);
d1.__build__(features.getShape());
var hidden_activations = tf.nn.relu(d1.__call__(features));
var d1_output = d1.output_shape(features.getShape());
var d2 = new Dense(1);
d2.__build__(d1.output_shape(features.getShape()), seed: 17, stddev: (float)(1/ Math.Sqrt(num_hidden)));
var logits = d2.__call__(hidden_activations);
var predictions = tf.sigmoid(tf.squeeze(logits));
*/
#endregion
#region Model Graph Form Layer Stack
var
flow_shape
=
features
.
TensorShape
;
Flow
=
features
;
for
(
int
i
=
0
;
i
<
layer_stack
.
Count
;
i
++
)
{
layer_stack
[
i
]
.
__build__
(
flow_shape
)
;
flow_shape
=
layer_stack
[
i
]
.
output_shape
(
flow_shape
)
;
Flow
=
layer_stack
[
i
]
.
__call__
(
Flow
)
;
}
var
predictions
=
tf
.
sigmoid
(
tf
.
squeeze
(
Flow
)
)
;
#endregion
#region loss and optimizer
var
loss
=
tf
.
reduce_mean
(
tf
.
square
(
predictions
-
tf
.
cast
(
labels
,
tf
.
float32
)
)
,
name
:
"loss"
)
;
var
gs
=
tf
.
Variable
(
0
,
trainable
:
false
,
name
:
"global_step"
)
;
var
train_op
=
tf
.
train
.
GradientDescentOptimizer
(
0.2f
)
.
minimize
(
loss
,
global_step
:
gs
)
;
#endregion
return
(
train_op
,
loss
,
gs
)
;
}
public
float
train
(
int
num_steps
,
(
NDArray
,
NDArray
)
training_dataset
)
{
var
(
X
,
Y
)
=
training_dataset
;
var
x_shape
=
X
.
shape
;
var
batch_size
=
x_shape
[
0
]
;
var
graph
=
tf
.
Graph
(
)
.
as_default
(
)
;
var
features
=
tf
.
placeholder
(
tf
.
float32
,
new
TensorShape
(
batch_size
,
2
)
)
;
var
labels
=
tf
.
placeholder
(
tf
.
float32
,
new
TensorShape
(
batch_size
)
)
;
var
(
train_op
,
loss
,
gs
)
=
this
.
make_graph
(
features
,
labels
)
;
var
init
=
tf
.
global_variables_initializer
(
)
;
float
loss_value
=
0
;
with
(
tf
.
Session
(
graph
)
,
sess
=>
{
sess
.
run
(
init
)
;
var
step
=
0
;
while
(
step
<
num_steps
)
{
var
result
=
sess
.
run
(
new
ITensorOrOperation
[
]
{
train_op
,
gs
,
loss
}
,
new
FeedItem
(
features
,
X
)
,
new
FeedItem
(
labels
,
Y
)
)
;
loss_value
=
result
[
2
]
;
step
=
result
[
1
]
;
if
(
step
%
1000
==
0
)
Console
.
WriteLine
(
$
"Step
{
step
}
loss:
{
loss_value
}
"
)
;
}
Console
.
WriteLine
(
$
"Final loss:
{
loss_value
}
"
)
;
}
)
;
return
loss_value
;
}
}
}
Back
|
FazBrowse Home
|
New Git URL