Skip to content

Commit d75f354

Browse files
dedaleOceania2018
authored andcommitted
[F#] Report merge of BinaryTestClassification
1 parent c866ab7 commit d75f354

3 files changed

Lines changed: 17 additions & 14 deletions

File tree

src/TensorFlowNET.Examples.FSharp/ImageProcessing/MnistCnnKerasSubclass.fs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ module MnistCnnKerasSubclass =
7979

8080
// Fully connected layer.
8181
let fc1 = layers.Dense(1024) :> Layer
82-
// Apply Dropout (if is_training is False, dropout is not applied).
82+
// Apply Dropout (if training is False, dropout is not applied).
8383
let dropout = layers.Dropout(rate = 0.5f) :> Layer
8484

8585
// Output layer, class prediction.
@@ -99,19 +99,19 @@ module MnistCnnKerasSubclass =
9999
cnn
100100

101101
//let state = defaultArg state null
102-
//let is_training = defaultArg is_training false
103102

104-
override x.Call(inputs, state, is_training) =
103+
override x.Call(inputs, state, training) =
104+
let training = defaultArg (Option.ofNullable training) false
105105
let inputs = tf.reshape(inputs.asTensor, TensorShape (-1, 28, 28, 1)).asTensors
106106
let inputs = conv1.Apply(inputs)
107107
let inputs = maxpool1.Apply(inputs)
108108
let inputs = conv2.Apply(inputs)
109109
let inputs = maxpool2.Apply(inputs)
110110
let inputs = flatten.Apply(inputs)
111111
let inputs = fc1.Apply(inputs)
112-
let inputs = dropout.Apply(inputs, is_training = is_training)
112+
let inputs = dropout.Apply(inputs, training = training)
113113
let inputs = output.Apply(inputs)
114-
if not is_training then tf.nn.softmax(inputs.asTensor).asTensors else inputs
114+
if not training then tf.nn.softmax(inputs.asTensor).asTensors else inputs
115115

116116
let cross_entropy_loss x y =
117117
// Convert labels to int 64 for tf cross-entropy function.
@@ -123,7 +123,7 @@ module MnistCnnKerasSubclass =
123123

124124
let run_optimization (conv_net : ConvNet) (optimizer : OptimizerV2) (x : Tensor) y =
125125
use g = tf.GradientTape()
126-
let pred = conv_net.Apply(x.asTensors, is_training = true)
126+
let pred = conv_net.Apply(x.asTensors, training = true)
127127
let loss = cross_entropy_loss pred.asTensor y
128128

129129
// Compute gradients.

src/TensorFlowNET.Examples.FSharp/NeuralNetworks/FullyConnectedKeras.fs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ limitations under the License.
1616

1717
namespace TensorFlowNET.Examples.FSharp
1818

19+
open System
20+
1921
open NumSharp
2022
open Tensorflow
2123
open Tensorflow.Keras
@@ -93,11 +95,12 @@ module FullyConnectedKeras =
9395
x.Call(inputs, null, false)
9496

9597
// Set forward pass.
96-
override _.Call(inputs : Tensors, state : Tensor, is_training : bool) =
98+
override _.Call(inputs : Tensors, state : Tensor, training : Nullable<bool>) =
99+
let training = defaultArg (Option.ofNullable training) false
97100
let inputs = fc1.Apply(inputs)
98101
let inputs = fc2.Apply(inputs)
99102
let inputs = output.Apply(inputs)
100-
if not is_training then
103+
if not training then
101104
tf.nn.softmax(inputs.asTensor).asTensors
102105
else
103106
inputs
@@ -140,7 +143,7 @@ module FullyConnectedKeras =
140143
// Wrap computation inside a GradientTape for automatic differentiation.
141144
use g = tf.GradientTape()
142145
// Forward pass.
143-
let pred = neural_net.Apply(x.asTensors, is_training = true)
146+
let pred = neural_net.Apply(x.asTensors, training = true)
144147
let loss = cross_entropy_loss pred.asTensor y
145148

146149
// Compute gradients.
@@ -156,13 +159,13 @@ module FullyConnectedKeras =
156159
run_optimization batch_x batch_y
157160

158161
if step % display_step = 0 then
159-
let pred = neural_net.Apply(batch_x.asTensors, is_training = true)
162+
let pred = neural_net.Apply(batch_x.asTensors, training = true)
160163
let loss = cross_entropy_loss pred.asTensor batch_y
161164
let acc = get_accuracy pred.asTensor batch_y
162165
print($"step: {step}, loss: {float32 loss}, accuracy: {float32 acc}")
163166

164167
// Test model on validation set.
165-
let pred = neural_net.Apply(x_test.asTensor.asTensors, is_training = false)
168+
let pred = neural_net.Apply(x_test.asTensor.asTensors, training = false)
166169
let accuracy = float32 (get_accuracy pred.asTensor y_test.asTensor)
167170
print($"Test Accuracy: {accuracy}")
168171

src/TensorFlowNET.Examples.FSharp/TensorFlowNET.Examples.FSharp.fsproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@
3535
<PackageReference Include="Argu" Version="6.1.1" />
3636
<PackageReference Include="Colorful.Console" Version="1.2.15" />
3737
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
38-
<PackageReference Include="SciSharp.TensorFlow.Redist" Version="2.3.1" />
39-
<PackageReference Include="SharpCV" Version="0.6.0" />
38+
<PackageReference Include="SciSharp.TensorFlow.Redist" Version="2.4.1" />
39+
<PackageReference Include="SharpCV" Version="0.7.0" />
4040
<PackageReference Include="OpenCvSharp4.runtime.win" Version="4.4.0.20200915" />
4141
<PackageReference Include="System.Drawing.Common" Version="5.0.0" />
42-
<PackageReference Include="TensorFlow.Keras" Version="0.4.0" />
42+
<PackageReference Include="TensorFlow.Keras" Version="0.5.0" />
4343
</ItemGroup>
4444

4545
<ItemGroup>

0 commit comments

Comments
 (0)