From 438a0f972010e39d994ee766d90fb6b32c4fd484 Mon Sep 17 00:00:00 2001 From: Meinrad Recheis Date: Sun, 28 Apr 2019 17:47:06 +0200 Subject: [PATCH] Xor: import graph --- graph/xor.meta | Bin 0 -> 11860 bytes test/TensorFlowNET.Examples/BasicEagerApi.cs | 1 + .../TensorFlowNET.Examples/BasicOperations.cs | 2 + test/TensorFlowNET.Examples/HelloWorld.cs | 2 + test/TensorFlowNET.Examples/IExample.cs | 6 ++ .../ImageRecognition.cs | 2 + .../InceptionArchGoogLeNet.cs | 2 + .../KMeansClustering.cs | 1 + .../LinearRegression.cs | 2 + .../LogisticRegression.cs | 2 + test/TensorFlowNET.Examples/MetaGraph.cs | 2 + .../NaiveBayesClassifier.cs | 2 + .../NamedEntityRecognition.cs | 2 + .../TensorFlowNET.Examples/NearestNeighbor.cs | 2 + test/TensorFlowNET.Examples/NeuralNetXor.cs | 58 +++++++++++++++++- .../TensorFlowNET.Examples/ObjectDetection.cs | 2 + .../Text/TextClassificationTrain.cs | 1 + .../TextClassificationWithMovieReviews.cs | 1 + 18 files changed, 87 insertions(+), 3 deletions(-) create mode 100644 graph/xor.meta diff --git a/graph/xor.meta b/graph/xor.meta new file mode 100644 index 0000000000000000000000000000000000000000..4406153b2e86911dcc4fb711aced58b3e835459d GIT binary patch literal 11860 zcmd5?%a7bh8Sip?db+=P*UV%lmAA7YNh2~dy(~m6?UEN+kT%&>GDNVTG@fqn#EQq= zX}iPBf_5Vi(jxI#A*4kdKtdc4LgK{Vzy-vCBPWD}v|KrG$-^=uG%g~1E^a z>W_Re$T*wGS%=lu3el?iOs(FSn_pO5p{vI>j18C@c~-~icOTpCKGpa7eTQk6DsTw3 z>z-%tyIkzhdM6S4)O1D`7lm?B)L`Uy;^#&qmS`|o#G!W2^sSEf$Q)Sl>_jD&{G1T* zpdlo43r;2c6R3L^^qZ#VQ^bF0==X=+U4}o84E>hp?;7(E+!8M1fAPEFUu#g`9gf^C zsu?R-fvYfcv+sI7TY{P}k}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OT5TP^ zS?q*BZCt2Jx)RT>$-Y*z>UR}>#tPBuVHY@R2Q#H*ifs}?oJ`I4WHrSiCSqImFFX0L z$T=aG3vCN#pn99^<}>`#a$1z%ESMdd*O$jKCaU!^DtLVpJRkirlPwUk3lhJde6 z2A~7S5+kBc!p!`%#=ZmwJsZ^h398)m zCG@7lp9p*=c{f&82+wPTp^`L96b@xms&hG3=M0>pCo<~9kT6P0HNkbmNHhs6S`lUP zQHOl)krckucr|fE8G#+$>x<(4F>mfp*FpI;fzt|k(YaH+a86(*Rti3` zKzbcjZ}oUXiokDEAfsKM@KJPj8@9>DD8t6@4W%C9bM(%Bg+^jUDzpn!X%sdKoU#3lnIHZ6I}0ia7cKk(r7g zev5>RkHO;uaW=K*MI$G1l^Tn%JF=KWJfZ8jBeulvxIrY1$|D}zR{tc_ph22y^B^T+ zNHzOQF~2x=5a&)Vd@2{ffg!Vb6mr9Wo`nL`&({X0Yc(U@}2_m@AvG2uzcGYvkQ6*3wGU*1I>wynZX3JX6p zOit@qIrIYJ3T&fxUcSdAHGfNwB-Ior%>01{pBrNWeo9dEU1hNmq@MgZGd#zdR%(dS zX5aXg=f`*~RcwlolwNsQ{J2vZEbQ~+UQ-yDw_(<^66H28ZVa=JMfBofLHMy`-2Wxx zTA|YSE$YtOPw_-OBa$XxowCIDEmn-Y#DCA-ExipoZ-R$TT7H;#Blr-P(fAHGbnaf% zbvCs63F8k0R`JZ+cKJt2*i#o@D|z%ILVrf!R?;1BZ_~Kuk0woS@N=s6b}M&+*51BK z;0>6MAa-_ep8RnflW&D{`jNBHHh0Viub+a}?6v{vWYZ1{VMT literal 0 HcmV?d00001 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";