diff --git a/graph/xor.meta b/graph/xor.meta new file mode 100644 index 00000000..4406153b Binary files /dev/null and b/graph/xor.meta differ diff --git a/test/TensorFlowNET.Examples/BasicEagerApi.cs b/test/TensorFlowNET.Examples/BasicEagerApi.cs index f0ac68d4..0cf572ef 100644 --- a/test/TensorFlowNET.Examples/BasicEagerApi.cs +++ b/test/TensorFlowNET.Examples/BasicEagerApi.cs @@ -14,6 +14,7 @@ namespace TensorFlowNET.Examples public int Priority => 100; public bool Enabled { get; set; } = false; public string Name => "Basic Eager"; + public bool ImportGraph { get; set; } = false; private Tensor a, b, c, d; diff --git a/test/TensorFlowNET.Examples/BasicOperations.cs b/test/TensorFlowNET.Examples/BasicOperations.cs index 21082866..c8abadf3 100644 --- a/test/TensorFlowNET.Examples/BasicOperations.cs +++ b/test/TensorFlowNET.Examples/BasicOperations.cs @@ -15,6 +15,8 @@ namespace TensorFlowNET.Examples public bool Enabled { get; set; } = true; public int Priority => 2; public string Name => "Basic Operations"; + public bool ImportGraph { get; set; } = false; + private Session sess; diff --git a/test/TensorFlowNET.Examples/HelloWorld.cs b/test/TensorFlowNET.Examples/HelloWorld.cs index 717e4192..a7f47211 100644 --- a/test/TensorFlowNET.Examples/HelloWorld.cs +++ b/test/TensorFlowNET.Examples/HelloWorld.cs @@ -14,6 +14,8 @@ namespace TensorFlowNET.Examples public int Priority => 1; public bool Enabled { get; set; } = true; public string Name => "Hello World"; + public bool ImportGraph { get; set; } = false; + public bool Run() { diff --git a/test/TensorFlowNET.Examples/IExample.cs b/test/TensorFlowNET.Examples/IExample.cs index 3b31e58f..ed2521f6 100644 --- a/test/TensorFlowNET.Examples/IExample.cs +++ b/test/TensorFlowNET.Examples/IExample.cs @@ -14,11 +14,17 @@ namespace TensorFlowNET.Examples /// running order /// int Priority { get; } + /// /// True to run example /// bool Enabled { get; set; } + /// + /// Set true to import the computation graph instead of building it. + /// + bool ImportGraph { get; set; } + string Name { get; } /// diff --git a/test/TensorFlowNET.Examples/ImageRecognition.cs b/test/TensorFlowNET.Examples/ImageRecognition.cs index 4ff82168..6ba8d898 100644 --- a/test/TensorFlowNET.Examples/ImageRecognition.cs +++ b/test/TensorFlowNET.Examples/ImageRecognition.cs @@ -15,6 +15,8 @@ namespace TensorFlowNET.Examples public int Priority => 7; public bool Enabled { get; set; } = true; public string Name => "Image Recognition"; + public bool ImportGraph { get; set; } = false; + string dir = "ImageRecognition"; string pbFile = "tensorflow_inception_graph.pb"; diff --git a/test/TensorFlowNET.Examples/InceptionArchGoogLeNet.cs b/test/TensorFlowNET.Examples/InceptionArchGoogLeNet.cs index 052cf920..579311ff 100644 --- a/test/TensorFlowNET.Examples/InceptionArchGoogLeNet.cs +++ b/test/TensorFlowNET.Examples/InceptionArchGoogLeNet.cs @@ -22,6 +22,8 @@ namespace TensorFlowNET.Examples public bool Enabled { get; set; } = false; public int Priority => 100; public string Name => "Inception Arch GoogLeNet"; + public bool ImportGraph { get; set; } = false; + string dir = "label_image_data"; string pbFile = "inception_v3_2016_08_28_frozen.pb"; diff --git a/test/TensorFlowNET.Examples/KMeansClustering.cs b/test/TensorFlowNET.Examples/KMeansClustering.cs index b020a856..e12623cc 100644 --- a/test/TensorFlowNET.Examples/KMeansClustering.cs +++ b/test/TensorFlowNET.Examples/KMeansClustering.cs @@ -20,6 +20,7 @@ namespace TensorFlowNET.Examples public int Priority => 8; public bool Enabled { get; set; } = true; public string Name => "K-means Clustering"; + public bool ImportGraph { get; set; } = true; public int? train_size = null; public int validation_size = 5000; diff --git a/test/TensorFlowNET.Examples/LinearRegression.cs b/test/TensorFlowNET.Examples/LinearRegression.cs index a17d6e2b..448a0d1c 100644 --- a/test/TensorFlowNET.Examples/LinearRegression.cs +++ b/test/TensorFlowNET.Examples/LinearRegression.cs @@ -15,6 +15,8 @@ namespace TensorFlowNET.Examples public int Priority => 3; public bool Enabled { get; set; } = true; public string Name => "Linear Regression"; + public bool ImportGraph { get; set; } = false; + public int training_epochs = 1000; diff --git a/test/TensorFlowNET.Examples/LogisticRegression.cs b/test/TensorFlowNET.Examples/LogisticRegression.cs index 7f537778..2568890c 100644 --- a/test/TensorFlowNET.Examples/LogisticRegression.cs +++ b/test/TensorFlowNET.Examples/LogisticRegression.cs @@ -20,6 +20,8 @@ namespace TensorFlowNET.Examples public int Priority => 4; public bool Enabled { get; set; } = true; public string Name => "Logistic Regression"; + public bool ImportGraph { get; set; } = false; + public int training_epochs = 10; public int? train_size = null; diff --git a/test/TensorFlowNET.Examples/MetaGraph.cs b/test/TensorFlowNET.Examples/MetaGraph.cs index eb24996d..09563e1e 100644 --- a/test/TensorFlowNET.Examples/MetaGraph.cs +++ b/test/TensorFlowNET.Examples/MetaGraph.cs @@ -12,6 +12,8 @@ namespace TensorFlowNET.Examples public int Priority => 100; public bool Enabled { get; set; } = false; public string Name => "Meta Graph"; + public bool ImportGraph { get; set; } = true; + public bool Run() { diff --git a/test/TensorFlowNET.Examples/NaiveBayesClassifier.cs b/test/TensorFlowNET.Examples/NaiveBayesClassifier.cs index 46db7793..c520d51a 100644 --- a/test/TensorFlowNET.Examples/NaiveBayesClassifier.cs +++ b/test/TensorFlowNET.Examples/NaiveBayesClassifier.cs @@ -15,6 +15,8 @@ namespace TensorFlowNET.Examples public int Priority => 6; public bool Enabled { get; set; } = true; public string Name => "Naive Bayes Classifier"; + public bool ImportGraph { get; set; } = false; + public Normal dist { get; set; } public bool Run() diff --git a/test/TensorFlowNET.Examples/NamedEntityRecognition.cs b/test/TensorFlowNET.Examples/NamedEntityRecognition.cs index 3826e04d..a85b41e5 100644 --- a/test/TensorFlowNET.Examples/NamedEntityRecognition.cs +++ b/test/TensorFlowNET.Examples/NamedEntityRecognition.cs @@ -13,6 +13,8 @@ namespace TensorFlowNET.Examples public int Priority => 100; public bool Enabled { get; set; } = false; public string Name => "NER"; + public bool ImportGraph { get; set; } = false; + public bool Run() { diff --git a/test/TensorFlowNET.Examples/NearestNeighbor.cs b/test/TensorFlowNET.Examples/NearestNeighbor.cs index 2735e514..cc670314 100644 --- a/test/TensorFlowNET.Examples/NearestNeighbor.cs +++ b/test/TensorFlowNET.Examples/NearestNeighbor.cs @@ -22,6 +22,8 @@ namespace TensorFlowNET.Examples public int? TrainSize = null; public int ValidationSize = 5000; public int? TestSize = null; + public bool ImportGraph { get; set; } = false; + public bool Run() { diff --git a/test/TensorFlowNET.Examples/NeuralNetXor.cs b/test/TensorFlowNET.Examples/NeuralNetXor.cs index 92984af3..ea6a10a5 100644 --- a/test/TensorFlowNET.Examples/NeuralNetXor.cs +++ b/test/TensorFlowNET.Examples/NeuralNetXor.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Text; using NumSharp; using Tensorflow; +using TensorFlowNET.Examples.Utility; namespace TensorFlowNET.Examples { @@ -15,6 +16,7 @@ namespace TensorFlowNET.Examples public int Priority => 10; public bool Enabled { get; set; } = true; public string Name => "NN XOR"; + public bool ImportGraph { get; set; } = true; public int num_steps = 5000; @@ -38,7 +40,7 @@ namespace TensorFlowNET.Examples // Shape [4] var predictions = tf.sigmoid(tf.squeeze(logits)); - var loss = tf.reduce_mean(tf.square(predictions - tf.cast(labels, tf.float32))); + var loss = tf.reduce_mean(tf.square(predictions - tf.cast(labels, tf.float32)), name:"loss"); var gs = tf.Variable(0, trainable: false); var train_op = tf.train.GradientDescentOptimizer(0.2f).minimize(loss, global_step: gs); @@ -49,7 +51,53 @@ namespace TensorFlowNET.Examples public bool Run() { PrepareData(); + float loss_value = 0; + if (ImportGraph) + loss_value = RunWithImportedGraph(); + else + loss_value=RunWithBuiltGraph(); + return loss_value < 0.0627; + } + + private float RunWithImportedGraph() + { + var graph = tf.Graph().as_default(); + + tf.train.import_meta_graph("graph/xor.meta"); + + var features = graph.get_operation_by_name("Placeholder"); + var labels = graph.get_operation_by_name("Placeholder_1"); + Tensor loss = graph.get_operation_by_name("loss"); + + var init = tf.global_variables_initializer(); + float loss_value = 0; + // Start tf session + with(tf.Session(graph), sess => + { + sess.run(init); + var step = 0; + + var y_ = np.array(new int[] { 1, 0, 0, 1 }, dtype: np.int32); + while (step < num_steps) + { + // original python: + //_, step, loss_value = sess.run( + // [train_op, gs, loss], + // feed_dict={features: xy, labels: y_} + // ) + loss_value = sess.run(loss, new FeedItem(features, data), new FeedItem(labels, y_)); + step++; + if (step % 1000 == 0) + Console.WriteLine($"Step {step} loss: {loss_value}"); + } + Console.WriteLine($"Final loss: {loss_value}"); + }); + return loss_value; + } + + private float RunWithBuiltGraph() + { var graph = tf.Graph().as_default(); var features = tf.placeholder(tf.float32, new TensorShape(4, 2)); @@ -76,12 +124,12 @@ namespace TensorFlowNET.Examples // ) loss_value = sess.run(loss, new FeedItem(features, data), new FeedItem(labels, y_)); step++; - if (step%1000==0) + if (step % 1000 == 0) Console.WriteLine($"Step {step} loss: {loss_value}"); } Console.WriteLine($"Final loss: {loss_value}"); }); - return loss_value < 0.0627; + return loss_value; } public void PrepareData() @@ -93,6 +141,10 @@ namespace TensorFlowNET.Examples {0, 0 }, {0, 1 } }; + + // download graph meta data + string url = "https://raw.githubusercontent.com/SciSharp/TensorFlow.NET/master/graph/xor.meta"; + Web.Download(url, "graph", "kmeans.meta"); } } } diff --git a/test/TensorFlowNET.Examples/ObjectDetection.cs b/test/TensorFlowNET.Examples/ObjectDetection.cs index 10845b28..d4aac44b 100644 --- a/test/TensorFlowNET.Examples/ObjectDetection.cs +++ b/test/TensorFlowNET.Examples/ObjectDetection.cs @@ -18,6 +18,8 @@ namespace TensorFlowNET.Examples public int Priority => 11; public bool Enabled { get; set; } = true; public string Name => "Object Detection"; + public bool ImportGraph { get; set; } = false; + public float MIN_SCORE = 0.5f; string modelDir = "ssd_mobilenet_v1_coco_2018_01_28"; diff --git a/test/TensorFlowNET.Examples/Text/TextClassificationTrain.cs b/test/TensorFlowNET.Examples/Text/TextClassificationTrain.cs index f7e69fc6..ce06cbc5 100644 --- a/test/TensorFlowNET.Examples/Text/TextClassificationTrain.cs +++ b/test/TensorFlowNET.Examples/Text/TextClassificationTrain.cs @@ -18,6 +18,7 @@ namespace TensorFlowNET.Examples.CnnTextClassification public bool Enabled { get; set; }= false; public string Name => "Text Classification"; public int? DataLimit = null; + public bool ImportGraph { get; set; } = true; private string dataDir = "text_classification"; private string dataFileName = "dbpedia_csv.tar.gz"; diff --git a/test/TensorFlowNET.Examples/Text/TextClassificationWithMovieReviews.cs b/test/TensorFlowNET.Examples/Text/TextClassificationWithMovieReviews.cs index 9030daec..2df012ac 100644 --- a/test/TensorFlowNET.Examples/Text/TextClassificationWithMovieReviews.cs +++ b/test/TensorFlowNET.Examples/Text/TextClassificationWithMovieReviews.cs @@ -14,6 +14,7 @@ namespace TensorFlowNET.Examples public int Priority => 9; public bool Enabled { get; set; } = false; public string Name => "Movie Reviews"; + public bool ImportGraph { get; set; } = true; string dir = "text_classification_with_movie_reviews"; string dataFile = "imdb.zip";