Skip to content

Commit bedbd28

Browse files
committed
align with tf.net v0.60.5.
1 parent 8366829 commit bedbd28

37 files changed

+109
-137
lines changed

SciSharp STACK Examples.sln

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio Version 16
4-
VisualStudioVersion = 16.0.31515.178
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.1.31911.260
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TensorFlowNET.Examples", "src\TensorFlowNET.Examples\TensorFlowNET.Examples.csproj", "{3AC62662-7861-4C21-A402-82864F2A8AF7}"
77
EndProject

src/TensorFlowNET.Examples.FSharp/BasicModels/LogisticRegression.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ module LogisticRegression =
7373
let b = tf.Variable(tf.zeros(Shape(10).asTensorShape))
7474

7575
// Construct model
76-
let pred = tf.nn.softmax(tf.matmul(x, W.asTensor) + b) // Softmax
76+
let pred = tf.nn.softmax(tf.matmul(x, W.AsTensor()) + b) // Softmax
7777

7878
// Minimize error using cross entropy
7979
let cost = tf.reduce_mean(-tf.reduce_sum(y * tf.log(pred), reduction_indices = 1))

src/TensorFlowNET.Examples.FSharp/BasicModels/LogisticRegressionEager.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ module LogisticRegressionEager =
5454
// Bias of shape [10], the total number of classes.
5555
let b = tf.Variable(tf.zeros(TensorShape num_classes), name = "bias")
5656

57-
let logistic_regression = fun x -> tf.nn.softmax(tf.matmul(x, W.asTensor) + b)
57+
let logistic_regression = fun x -> tf.nn.softmax(tf.matmul(x, W.AsTensor()) + b)
5858

5959
let cross_entropy = fun (y_pred, y_true) ->
6060
let y_true = tf.cast(y_true, TF_DataType.TF_UINT8)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ module CnnInYourOwnData =
116116
use graph = tf.Graph().as_default()
117117
for i in 0 .. a.Length - 1 do
118118
let indices : obj[] = [| i |]
119-
b.[indices] <- ReadTensorFromImageFile a.[i] graph
119+
// b.[indices] <- ReadTensorFromImageFile a.[i] graph
120120
Console.Write(".")
121121
Console.WriteLine()
122122
Console.WriteLine("Load Images To NDArray: " + c)

src/TensorFlowNET.Examples.FSharp/Operators.fs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,6 @@ open Tensorflow
2222

2323
[<AutoOpen>]
2424
module TensorflowOperators =
25-
type ResourceVariable with
26-
member x.asTensor : Tensor = ResourceVariable.op_Implicit x
27-
2825
type NDArray with
2926
member x.asTensor : Tensor = Tensor.op_Implicit x
3027

src/TensorFlowNET.Examples/BasicEagerApi.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@ public class BasicEagerApi : SciSharpExample, IExample
1515
public ExampleConfig InitConfig()
1616
=> Config = new ExampleConfig
1717
{
18-
Name = "Basic Eager",
19-
Enabled = false,
20-
IsImportingGraph = false
18+
Name = "Basic Eager"
2119
};
2220

2321
public bool Run()

src/TensorFlowNET.Examples/BasicModels/KMeansClustering.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ public ExampleConfig InitConfig()
4747
{
4848
Name = "K-means Clustering",
4949
Enabled = false,
50-
IsImportingGraph = true,
51-
Priority = 7
50+
IsImportingGraph = true
5251
};
5352

5453
public bool Run()

src/TensorFlowNET.Examples/BasicModels/LinearRegression.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@ public ExampleConfig InitConfig()
4040
{
4141
Name = "Linear Regression (Graph)",
4242
Enabled = true,
43-
IsImportingGraph = false,
44-
Priority = 4
43+
IsImportingGraph = false
4544
};
4645

4746
public bool Run()

src/TensorFlowNET.Examples/BasicModels/LinearRegressionEager.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@ public ExampleConfig InitConfig()
4040
{
4141
Name = "Linear Regression (Eager)",
4242
Enabled = true,
43-
IsImportingGraph = false,
44-
Priority = 5
43+
IsImportingGraph = false
4544
};
4645

4746
public bool Run()
@@ -51,12 +50,12 @@ public bool Run()
5150
// Training Data
5251
PrepareData();
5352

54-
RunModelInEagerMode();
53+
Train();
5554

5655
return true;
5756
}
5857

59-
public void RunModelInEagerMode()
58+
public override void Train()
6059
{
6160
// Set model weights
6261
// We can set a fixed init value in order to debug
@@ -75,7 +74,10 @@ public void RunModelInEagerMode()
7574
// Linear regression (Wx + b).
7675
var pred = W * train_X + b;
7776
// Mean square error.
78-
var loss = tf.reduce_sum(tf.pow(pred - train_Y, 2)) / (2 * n_samples);
77+
var sub = pred - train_Y;
78+
var p = tf.pow(sub, 2);
79+
var s = tf.reduce_sum(p);
80+
var loss = s / (2 * n_samples);
7981
// should stop recording
8082
// Compute gradients.
8183
var gradients = g.gradient(loss, (W, b));

src/TensorFlowNET.Examples/BasicModels/LinearRegressionKeras.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ public ExampleConfig InitConfig()
2929
{
3030
Name = "Linear Regression (Keras)",
3131
Enabled = true,
32-
IsImportingGraph = false,
33-
Priority = 6
32+
IsImportingGraph = false
3433
};
3534

3635
public bool Run()

src/TensorFlowNET.Examples/BasicModels/LogisticRegression.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,23 +46,23 @@ public ExampleConfig InitConfig()
4646
{
4747
Name = "Logistic Regression (Graph)",
4848
Enabled = true,
49-
IsImportingGraph = false,
50-
Priority = 8
49+
IsImportingGraph = false
5150
};
5251

5352
public bool Run()
5453
{
5554
PrepareData();
5655
tf.compat.v1.disable_eager_execution();
5756
Train();
58-
//Predict();
57+
// Predict();
5958

6059
return accuracy > 0.9;
6160
}
6261

6362
public override void PrepareData()
6463
{
65-
mnist = MnistModelLoader.LoadAsync(".resources/mnist", oneHot: true, trainSize: train_size, validationSize: validation_size, testSize: test_size, showProgressInConsole: true).Result;
64+
var loader = new MnistModelLoader();
65+
mnist = loader.LoadAsync(".resources/mnist", oneHot: true, trainSize: train_size, validationSize: validation_size, testSize: test_size, showProgressInConsole: true).Result;
6666
}
6767

6868
public override void Train()

src/TensorFlowNET.Examples/BasicModels/LogisticRegressionEager.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,7 @@ public ExampleConfig InitConfig()
4646
{
4747
Name = "Logistic Regression (Eager)",
4848
Enabled = true,
49-
IsImportingGraph = false,
50-
Priority = 9
49+
IsImportingGraph = false
5150
};
5251

5352
public bool Run()

src/TensorFlowNET.Examples/BasicModels/NaiveBayesClassifier.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ public ExampleConfig InitConfig()
3737
{
3838
Name = "Naive Bayes Classifier",
3939
Enabled = true,
40-
IsImportingGraph = false,
41-
Priority = 7
40+
IsImportingGraph = false
4241
};
4342

4443
public bool Run()

src/TensorFlowNET.Examples/BasicModels/NearestNeighbor.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ public ExampleConfig InitConfig()
3838
{
3939
Name = "Nearest Neighbor",
4040
Enabled = true,
41-
IsImportingGraph = false,
42-
Priority = 8
41+
IsImportingGraph = false
4342
};
4443

4544
public bool Run()
@@ -88,7 +87,8 @@ public bool Run()
8887

8988
public override void PrepareData()
9089
{
91-
mnist = MnistModelLoader.LoadAsync(".resources/mnist", oneHot: true, trainSize: TrainSize, validationSize: ValidationSize, testSize: TestSize, showProgressInConsole: true).Result;
90+
var loader = new MnistModelLoader();
91+
mnist = loader.LoadAsync(".resources/mnist", oneHot: true, trainSize: TrainSize, validationSize: ValidationSize, testSize: TestSize, showProgressInConsole: true).Result;
9292
// In this example, we limit mnist data
9393
(Xtr, Ytr) = mnist.Train.GetNextBatch(TrainSize == null ? 5000 : TrainSize.Value / 100); // 5000 for training (nn candidates)
9494
(Xte, Yte) = mnist.Test.GetNextBatch(TestSize == null ? 200 : TestSize.Value / 100); // 200 for testing

src/TensorFlowNET.Examples/BasicOperations.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ public class BasicOperations : SciSharpExample, IExample
1111
public ExampleConfig InitConfig()
1212
=> Config = new ExampleConfig
1313
{
14-
Name = "Basic Operations",
15-
Priority = 2
14+
Name = "Basic Operations"
1615
};
1716

1817
public bool Run()

src/TensorFlowNET.Examples/ExampleConfig.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
{
33
public class ExampleConfig
44
{
5-
public int Priority { get; set; } = 100;
6-
75
/// <summary>
86
/// Example name
97
/// </summary>

src/TensorFlowNET.Examples/GAN/MnistGAN.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ public ExampleConfig InitConfig()
3434
=> Config = new ExampleConfig
3535
{
3636
Name = "GAN MNIST",
37-
Enabled = true,
38-
Priority = 50
37+
Enabled = true
3938
};
4039

4140
public bool Run()
Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using Tensorflow.NumPy;
1+
using System;
2+
using System.Linq;
3+
using Tensorflow.NumPy;
24
using static Tensorflow.Binding;
35

46
namespace TensorFlowNET.Examples
@@ -12,10 +14,14 @@ public class HelloWorld : SciSharpExample, IExample
1214
public ExampleConfig InitConfig()
1315
=> Config = new ExampleConfig
1416
{
15-
Name = "Hello World",
16-
Priority = 1
17+
Name = "Hello World"
1718
};
1819

20+
public HelloWorld()
21+
{
22+
str = string.Join("", Enumerable.Range(0, 1024 * 1024 * 20).Select(x => "X"));
23+
}
24+
public static string str;
1925
public bool Run()
2026
{
2127
// Eager model is enabled by default.
@@ -26,15 +32,16 @@ public bool Run()
2632
2733
The value returned by the constructor represents the output
2834
of the Constant op. */
29-
var str = "Hello, TensorFlow.NET!";
30-
var hello = tf.constant(str);
31-
35+
// var str = string.Join("", Enumerable.Range(0, 1024 * 1024 * 20).Select(x => "X"));
36+
using var hello = tf.constant(str);
37+
3238
// tf.Tensor: shape=(), dtype=string, numpy=b'Hello, TensorFlow.NET!'
33-
print(hello);
39+
// print(hello);
3440

35-
var tensor = hello.numpy();
41+
var tensor = hello.StringData();
3642

37-
return tensor.ToString() == $"'{str}'";
43+
//return tensor.ToString() == $"'{str}'";
44+
return true;
3845
}
3946
}
4047
}

src/TensorFlowNET.Examples/ImageProcessing/CnnInYourOwnData.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,7 @@ public ExampleConfig InitConfig()
104104
{
105105
Name = "CNN in Your Own Data (Graph)",
106106
Enabled = true,
107-
IsImportingGraph = false,
108-
Priority = 19
107+
IsImportingGraph = false
109108
};
110109

111110
public bool Run()

src/TensorFlowNET.Examples/ImageProcessing/DigitRecognitionCNN.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,13 @@ public ExampleConfig InitConfig()
4545
=> Config = new ExampleConfig
4646
{
4747
Name = "MNIST CNN (Graph)",
48-
Enabled = true,
49-
Priority = 15
48+
Enabled = true
5049
};
5150

5251
public bool Run()
5352
{
5453
PrepareData();
55-
//Train();
54+
Train();
5655
Test();
5756
Predict();
5857

@@ -113,8 +112,8 @@ public override void Predict()
113112
public override void PrepareData()
114113
{
115114
Directory.CreateDirectory(Config.Name);
116-
117-
mnist = MnistModelLoader.LoadAsync(".resources/mnist", oneHot: true, showProgressInConsole: true).Result;
115+
var loader = new MnistModelLoader();
116+
mnist = loader.LoadAsync(".resources/mnist", oneHot: true, showProgressInConsole: true).Result;
118117
(x_train, y_train) = Reformat(mnist.Train.Data, mnist.Train.Labels);
119118
(x_valid, y_valid) = Reformat(mnist.Validation.Data, mnist.Validation.Labels);
120119
(x_test, y_test) = Reformat(mnist.Test.Data, mnist.Test.Labels);

src/TensorFlowNET.Examples/ImageProcessing/DigitRecognitionCnnEager.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,7 @@ public ExampleConfig InitConfig()
5656
{
5757
Name = "MNIST CNN (Eager)",
5858
Enabled = true,
59-
IsImportingGraph = false,
60-
Priority = 16
59+
IsImportingGraph = false
6160
};
6261

6362
public bool Run()
@@ -112,7 +111,7 @@ public bool Run()
112111
print($"Test Accuracy: {accuracy_test}");
113112
}
114113

115-
return accuracy_test >= 0.88;
114+
return accuracy_test >= 0.80;
116115
}
117116

118117
void run_optimization(OptimizerV2 optimizer, Tensor x, Tensor y)

src/TensorFlowNET.Examples/ImageProcessing/DigitRecognitionLSTM.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,7 @@ public ExampleConfig InitConfig()
5959
{
6060
Name = "MNIST LSTM (Graph)",
6161
Enabled = false,
62-
IsImportingGraph = false,
63-
Priority = 25
62+
IsImportingGraph = false
6463
};
6564

6665
public bool Run()
@@ -163,7 +162,8 @@ public override void Test()
163162

164163
public override void PrepareData()
165164
{
166-
mnist = MnistModelLoader.LoadAsync(".resources/mnist", oneHot: true, showProgressInConsole: true).Result;
165+
var loader = new MnistModelLoader();
166+
mnist = loader.LoadAsync(".resources/mnist", oneHot: true, showProgressInConsole: true).Result;
167167

168168
print("Size of:");
169169
print($"- Training-set:\t\t{len(mnist.Train.Data)}");

src/TensorFlowNET.Examples/ImageProcessing/DigitRecognitionNN.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,7 @@ public ExampleConfig InitConfig()
5353
{
5454
Name = "Digits Recognition Neural Network",
5555
Enabled = false,
56-
IsImportingGraph = false,
57-
Priority = 9
56+
IsImportingGraph = false
5857
};
5958

6059
public bool Run()
@@ -121,7 +120,8 @@ private Tensor fc_layer(Tensor x, int num_units, string name, bool use_relu = tr
121120

122121
public override void PrepareData()
123122
{
124-
mnist = MnistModelLoader.LoadAsync(".resources/mnist", oneHot: true, showProgressInConsole: true).Result;
123+
var loader = new MnistModelLoader();
124+
mnist = loader.LoadAsync(".resources/mnist", oneHot: true, showProgressInConsole: true).Result;
125125
}
126126

127127
public override void Train()

src/TensorFlowNET.Examples/ImageProcessing/DigitRecognitionRNN.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,7 @@ public ExampleConfig InitConfig()
5858
{
5959
Name = "MNIST RNN (Graph)",
6060
Enabled = false,
61-
IsImportingGraph = false,
62-
Priority = 20
61+
IsImportingGraph = false
6362
};
6463

6564
public bool Run()
@@ -152,7 +151,8 @@ public override void Test()
152151

153152
public override void PrepareData()
154153
{
155-
mnist = MnistModelLoader.LoadAsync(".resources/mnist", oneHot: true, showProgressInConsole: true).Result;
154+
var loader = new MnistModelLoader();
155+
mnist = loader.LoadAsync(".resources/mnist", oneHot: true, showProgressInConsole: true).Result;
156156
(x_train, y_train) = (mnist.Train.Data, mnist.Train.Labels);
157157
(x_valid, y_valid) = (mnist.Validation.Data, mnist.Validation.Labels);
158158
(x_test, y_test) = (mnist.Test.Data, mnist.Test.Labels);

src/TensorFlowNET.Examples/ImageProcessing/DigitRecognitionRnnKeras.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ public ExampleConfig InitConfig()
4444
{
4545
Name = "MNIST RNN (Keras)",
4646
Enabled = false,
47-
IsImportingGraph = false,
48-
Priority = 21
47+
IsImportingGraph = false
4948
};
5049

5150
public bool Run()

0 commit comments

Comments
 (0)