forked from SciSharp/TensorFlow.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModel.cs
More file actions
141 lines (116 loc) · 4.57 KB
/
Model.cs
File metadata and controls
141 lines (116 loc) · 4.57 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
/*****************************************************************************
Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
******************************************************************************/
using Keras.Layers;
using NumSharp;
using System;
using System.Collections.Generic;
using Tensorflow;
using static Tensorflow.Binding;
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;
using (var sess = tf.Session(graph))
{
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;
}
}
}