From 9ab30660bc2c7e143f45e3dd3dcafdc3efeef28d Mon Sep 17 00:00:00 2001 From: Oceania2018 Date: Wed, 8 May 2019 18:10:11 -0500 Subject: [PATCH] rename Text to TextProcess. --- .../AudioProcess/README.md | 1 + test/TensorFlowNET.Examples/MetaGraph.cs | 43 --- .../{ => Models}/KMeansClustering.cs | 0 .../{ => Models}/LinearRegression.cs | 0 .../{ => Models}/LogisticRegression.cs | 0 .../{ => Models}/NaiveBayesClassifier.cs | 0 .../{ => Models}/NearestNeighbor.cs | 0 .../{ => Models}/NeuralNetXor.cs | 312 +++++++++--------- .../BinaryTextClassification.cs | 0 .../{Text => TextProcess}/DataHelpers.cs | 4 +- .../{Text => TextProcess}/NER/BiLstmCrfNer.cs | 0 .../{Text => TextProcess}/NER/CRF.cs | 0 .../{Text => TextProcess}/NER/LstmCrfNer.cs | 0 .../NamedEntityRecognition.cs | 0 .../TextClassificationTrain.cs | 0 .../{Text => TextProcess}/Word2Vec.cs | 0 .../cnn_models/ITextClassificationModel.cs | 28 +- .../{Text => TextProcess}/cnn_models/VdCnn.cs | 0 .../ExamplesTests/ExamplesTest.cs | 8 - test/TensorFlowNET.UnitTest/GraphTest.cs | 16 +- 20 files changed, 188 insertions(+), 224 deletions(-) create mode 100644 test/TensorFlowNET.Examples/AudioProcess/README.md delete mode 100644 test/TensorFlowNET.Examples/MetaGraph.cs rename test/TensorFlowNET.Examples/{ => Models}/KMeansClustering.cs (100%) rename test/TensorFlowNET.Examples/{ => Models}/LinearRegression.cs (100%) rename test/TensorFlowNET.Examples/{ => Models}/LogisticRegression.cs (100%) rename test/TensorFlowNET.Examples/{ => Models}/NaiveBayesClassifier.cs (100%) rename test/TensorFlowNET.Examples/{ => Models}/NearestNeighbor.cs (100%) rename test/TensorFlowNET.Examples/{ => Models}/NeuralNetXor.cs (97%) rename test/TensorFlowNET.Examples/{Text => TextProcess}/BinaryTextClassification.cs (100%) rename test/TensorFlowNET.Examples/{Text => TextProcess}/DataHelpers.cs (99%) rename test/TensorFlowNET.Examples/{Text => TextProcess}/NER/BiLstmCrfNer.cs (100%) rename test/TensorFlowNET.Examples/{Text => TextProcess}/NER/CRF.cs (100%) rename test/TensorFlowNET.Examples/{Text => TextProcess}/NER/LstmCrfNer.cs (100%) rename test/TensorFlowNET.Examples/{ => TextProcess}/NamedEntityRecognition.cs (100%) rename test/TensorFlowNET.Examples/{Text => TextProcess}/TextClassificationTrain.cs (100%) rename test/TensorFlowNET.Examples/{Text => TextProcess}/Word2Vec.cs (100%) rename test/TensorFlowNET.Examples/{Text => TextProcess}/cnn_models/ITextClassificationModel.cs (95%) rename test/TensorFlowNET.Examples/{Text => TextProcess}/cnn_models/VdCnn.cs (100%) diff --git a/test/TensorFlowNET.Examples/AudioProcess/README.md b/test/TensorFlowNET.Examples/AudioProcess/README.md new file mode 100644 index 00000000..5f282702 --- /dev/null +++ b/test/TensorFlowNET.Examples/AudioProcess/README.md @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/test/TensorFlowNET.Examples/MetaGraph.cs b/test/TensorFlowNET.Examples/MetaGraph.cs deleted file mode 100644 index b449ceca..00000000 --- a/test/TensorFlowNET.Examples/MetaGraph.cs +++ /dev/null @@ -1,43 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Runtime.InteropServices; -using System.Text; -using Tensorflow; -using static Tensorflow.Python; - -namespace TensorFlowNET.Examples -{ - public class MetaGraph : IExample - { - public int Priority => 100; - public bool Enabled { get; set; } = false; - public string Name => "Meta Graph"; - public bool ImportGraph { get; set; } = true; - - - public bool Run() - { - ImportMetaGraph("my-save-dir/"); - return false; - } - - private void ImportMetaGraph(string dir) - { - with(tf.Session(), sess => - { - var new_saver = tf.train.import_meta_graph(dir + "my-model-10000.meta"); - new_saver.restore(sess, dir + "my-model-10000"); - var labels = tf.constant(0, dtype: tf.int32, shape: new int[] { 100 }, name: "labels"); - var batch_size = tf.size(labels); - var logits = (tf.get_collection("logits") as List)[0] as Tensor; - var loss = tf.losses.sparse_softmax_cross_entropy(labels: labels, - logits: logits); - }); - } - - public void PrepareData() - { - } - } -} diff --git a/test/TensorFlowNET.Examples/KMeansClustering.cs b/test/TensorFlowNET.Examples/Models/KMeansClustering.cs similarity index 100% rename from test/TensorFlowNET.Examples/KMeansClustering.cs rename to test/TensorFlowNET.Examples/Models/KMeansClustering.cs diff --git a/test/TensorFlowNET.Examples/LinearRegression.cs b/test/TensorFlowNET.Examples/Models/LinearRegression.cs similarity index 100% rename from test/TensorFlowNET.Examples/LinearRegression.cs rename to test/TensorFlowNET.Examples/Models/LinearRegression.cs diff --git a/test/TensorFlowNET.Examples/LogisticRegression.cs b/test/TensorFlowNET.Examples/Models/LogisticRegression.cs similarity index 100% rename from test/TensorFlowNET.Examples/LogisticRegression.cs rename to test/TensorFlowNET.Examples/Models/LogisticRegression.cs diff --git a/test/TensorFlowNET.Examples/NaiveBayesClassifier.cs b/test/TensorFlowNET.Examples/Models/NaiveBayesClassifier.cs similarity index 100% rename from test/TensorFlowNET.Examples/NaiveBayesClassifier.cs rename to test/TensorFlowNET.Examples/Models/NaiveBayesClassifier.cs diff --git a/test/TensorFlowNET.Examples/NearestNeighbor.cs b/test/TensorFlowNET.Examples/Models/NearestNeighbor.cs similarity index 100% rename from test/TensorFlowNET.Examples/NearestNeighbor.cs rename to test/TensorFlowNET.Examples/Models/NearestNeighbor.cs diff --git a/test/TensorFlowNET.Examples/NeuralNetXor.cs b/test/TensorFlowNET.Examples/Models/NeuralNetXor.cs similarity index 97% rename from test/TensorFlowNET.Examples/NeuralNetXor.cs rename to test/TensorFlowNET.Examples/Models/NeuralNetXor.cs index 6593b4a4..9d62cfc2 100644 --- a/test/TensorFlowNET.Examples/NeuralNetXor.cs +++ b/test/TensorFlowNET.Examples/Models/NeuralNetXor.cs @@ -1,156 +1,156 @@ -using System; -using System.Collections.Generic; -using System.Text; -using NumSharp; -using Tensorflow; -using TensorFlowNET.Examples.Utility; -using static Tensorflow.Python; - -namespace TensorFlowNET.Examples -{ - /// - /// Simple vanilla neural net solving the famous XOR problem - /// https://github.com/amygdala/tensorflow-workshop/blob/master/workshop_sections/getting_started/xor/README.md - /// - public class NeuralNetXor : IExample - { - public int Priority => 10; - public bool Enabled { get; set; } = true; - public string Name => "NN XOR"; - public bool ImportGraph { get; set; } = false; - - public int num_steps = 10000; - - private NDArray data; - - private (Operation, Tensor, Tensor) make_graph(Tensor features,Tensor labels, int num_hidden = 8) - { - var stddev = 1 / Math.Sqrt(2); - var hidden_weights = tf.Variable(tf.truncated_normal(new int []{2, num_hidden}, seed:1, stddev: (float) stddev )); - - // Shape [4, num_hidden] - var hidden_activations = tf.nn.relu(tf.matmul(features, hidden_weights)); - - var output_weights = tf.Variable(tf.truncated_normal( - new[] {num_hidden, 1}, - seed: 17, - stddev: (float) (1 / Math.Sqrt(num_hidden)) - )); - - // Shape [4, 1] - var logits = tf.matmul(hidden_activations, output_weights); - - // Shape [4] - var predictions = tf.sigmoid(tf.squeeze(logits)); - 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); - - return (train_op, loss, gs); - } - - public bool Run() - { - PrepareData(); - float loss_value = 0; - if (ImportGraph) - loss_value = RunWithImportedGraph(); - else - loss_value = RunWithBuiltGraph(); - - return loss_value < 0.0628; - } - - private float RunWithImportedGraph() - { - var graph = tf.Graph().as_default(); - - tf.train.import_meta_graph("graph/xor.meta"); - - Tensor features = graph.get_operation_by_name("Placeholder"); - Tensor labels = graph.get_operation_by_name("Placeholder_1"); - Tensor loss = graph.get_operation_by_name("loss"); - Tensor train_op = graph.get_operation_by_name("train_op"); - Tensor global_step = graph.get_operation_by_name("global_step"); - - 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_} - // ) - var result = sess.run(new ITensorOrOperation[] { train_op, global_step, loss }, new FeedItem(features, data), 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; - } - - private float RunWithBuiltGraph() - { - var graph = tf.Graph().as_default(); - - var features = tf.placeholder(tf.float32, new TensorShape(4, 2)); - var labels = tf.placeholder(tf.int32, new TensorShape(4)); - - var (train_op, loss, gs) = make_graph(features, labels); - - 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) - { - var result = sess.run(new ITensorOrOperation[] { train_op, gs, loss }, new FeedItem(features, data), 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; - } - - public void PrepareData() - { - data = new float[,] - { - {1, 0 }, - {1, 1 }, - {0, 0 }, - {0, 1 } - }; - - if (ImportGraph) - { - // download graph meta data - string url = "https://raw.githubusercontent.com/SciSharp/TensorFlow.NET/master/graph/xor.meta"; - Web.Download(url, "graph", "xor.meta"); - } - } - } -} +using System; +using System.Collections.Generic; +using System.Text; +using NumSharp; +using Tensorflow; +using TensorFlowNET.Examples.Utility; +using static Tensorflow.Python; + +namespace TensorFlowNET.Examples +{ + /// + /// Simple vanilla neural net solving the famous XOR problem + /// https://github.com/amygdala/tensorflow-workshop/blob/master/workshop_sections/getting_started/xor/README.md + /// + public class NeuralNetXor : IExample + { + public int Priority => 10; + public bool Enabled { get; set; } = true; + public string Name => "NN XOR"; + public bool ImportGraph { get; set; } = false; + + public int num_steps = 10000; + + private NDArray data; + + private (Operation, Tensor, Tensor) make_graph(Tensor features,Tensor labels, int num_hidden = 8) + { + var stddev = 1 / Math.Sqrt(2); + var hidden_weights = tf.Variable(tf.truncated_normal(new int []{2, num_hidden}, seed:1, stddev: (float) stddev )); + + // Shape [4, num_hidden] + var hidden_activations = tf.nn.relu(tf.matmul(features, hidden_weights)); + + var output_weights = tf.Variable(tf.truncated_normal( + new[] {num_hidden, 1}, + seed: 17, + stddev: (float) (1 / Math.Sqrt(num_hidden)) + )); + + // Shape [4, 1] + var logits = tf.matmul(hidden_activations, output_weights); + + // Shape [4] + var predictions = tf.sigmoid(tf.squeeze(logits)); + 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); + + return (train_op, loss, gs); + } + + public bool Run() + { + PrepareData(); + float loss_value = 0; + if (ImportGraph) + loss_value = RunWithImportedGraph(); + else + loss_value = RunWithBuiltGraph(); + + return loss_value < 0.0628; + } + + private float RunWithImportedGraph() + { + var graph = tf.Graph().as_default(); + + tf.train.import_meta_graph("graph/xor.meta"); + + Tensor features = graph.get_operation_by_name("Placeholder"); + Tensor labels = graph.get_operation_by_name("Placeholder_1"); + Tensor loss = graph.get_operation_by_name("loss"); + Tensor train_op = graph.get_operation_by_name("train_op"); + Tensor global_step = graph.get_operation_by_name("global_step"); + + 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_} + // ) + var result = sess.run(new ITensorOrOperation[] { train_op, global_step, loss }, new FeedItem(features, data), 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; + } + + private float RunWithBuiltGraph() + { + var graph = tf.Graph().as_default(); + + var features = tf.placeholder(tf.float32, new TensorShape(4, 2)); + var labels = tf.placeholder(tf.int32, new TensorShape(4)); + + var (train_op, loss, gs) = make_graph(features, labels); + + 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) + { + var result = sess.run(new ITensorOrOperation[] { train_op, gs, loss }, new FeedItem(features, data), 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; + } + + public void PrepareData() + { + data = new float[,] + { + {1, 0 }, + {1, 1 }, + {0, 0 }, + {0, 1 } + }; + + if (ImportGraph) + { + // download graph meta data + string url = "https://raw.githubusercontent.com/SciSharp/TensorFlow.NET/master/graph/xor.meta"; + Web.Download(url, "graph", "xor.meta"); + } + } + } +} diff --git a/test/TensorFlowNET.Examples/Text/BinaryTextClassification.cs b/test/TensorFlowNET.Examples/TextProcess/BinaryTextClassification.cs similarity index 100% rename from test/TensorFlowNET.Examples/Text/BinaryTextClassification.cs rename to test/TensorFlowNET.Examples/TextProcess/BinaryTextClassification.cs diff --git a/test/TensorFlowNET.Examples/Text/DataHelpers.cs b/test/TensorFlowNET.Examples/TextProcess/DataHelpers.cs similarity index 99% rename from test/TensorFlowNET.Examples/Text/DataHelpers.cs rename to test/TensorFlowNET.Examples/TextProcess/DataHelpers.cs index 658a102a..bb5d5675 100644 --- a/test/TensorFlowNET.Examples/Text/DataHelpers.cs +++ b/test/TensorFlowNET.Examples/TextProcess/DataHelpers.cs @@ -43,8 +43,8 @@ namespace TensorFlowNET.Examples.CnnTextClassification x[i][j] = char_dict[""]; else x[i][j] = char_dict.ContainsKey(content[j].ToString()) ? char_dict[content[j].ToString()] : char_dict[""]; - } - + } + y[i] = int.Parse(parts[0]); } diff --git a/test/TensorFlowNET.Examples/Text/NER/BiLstmCrfNer.cs b/test/TensorFlowNET.Examples/TextProcess/NER/BiLstmCrfNer.cs similarity index 100% rename from test/TensorFlowNET.Examples/Text/NER/BiLstmCrfNer.cs rename to test/TensorFlowNET.Examples/TextProcess/NER/BiLstmCrfNer.cs diff --git a/test/TensorFlowNET.Examples/Text/NER/CRF.cs b/test/TensorFlowNET.Examples/TextProcess/NER/CRF.cs similarity index 100% rename from test/TensorFlowNET.Examples/Text/NER/CRF.cs rename to test/TensorFlowNET.Examples/TextProcess/NER/CRF.cs diff --git a/test/TensorFlowNET.Examples/Text/NER/LstmCrfNer.cs b/test/TensorFlowNET.Examples/TextProcess/NER/LstmCrfNer.cs similarity index 100% rename from test/TensorFlowNET.Examples/Text/NER/LstmCrfNer.cs rename to test/TensorFlowNET.Examples/TextProcess/NER/LstmCrfNer.cs diff --git a/test/TensorFlowNET.Examples/NamedEntityRecognition.cs b/test/TensorFlowNET.Examples/TextProcess/NamedEntityRecognition.cs similarity index 100% rename from test/TensorFlowNET.Examples/NamedEntityRecognition.cs rename to test/TensorFlowNET.Examples/TextProcess/NamedEntityRecognition.cs diff --git a/test/TensorFlowNET.Examples/Text/TextClassificationTrain.cs b/test/TensorFlowNET.Examples/TextProcess/TextClassificationTrain.cs similarity index 100% rename from test/TensorFlowNET.Examples/Text/TextClassificationTrain.cs rename to test/TensorFlowNET.Examples/TextProcess/TextClassificationTrain.cs diff --git a/test/TensorFlowNET.Examples/Text/Word2Vec.cs b/test/TensorFlowNET.Examples/TextProcess/Word2Vec.cs similarity index 100% rename from test/TensorFlowNET.Examples/Text/Word2Vec.cs rename to test/TensorFlowNET.Examples/TextProcess/Word2Vec.cs diff --git a/test/TensorFlowNET.Examples/Text/cnn_models/ITextClassificationModel.cs b/test/TensorFlowNET.Examples/TextProcess/cnn_models/ITextClassificationModel.cs similarity index 95% rename from test/TensorFlowNET.Examples/Text/cnn_models/ITextClassificationModel.cs rename to test/TensorFlowNET.Examples/TextProcess/cnn_models/ITextClassificationModel.cs index e9778bba..942f2e04 100644 --- a/test/TensorFlowNET.Examples/Text/cnn_models/ITextClassificationModel.cs +++ b/test/TensorFlowNET.Examples/TextProcess/cnn_models/ITextClassificationModel.cs @@ -1,14 +1,14 @@ -using System; -using System.Collections.Generic; -using System.Text; -using Tensorflow; - -namespace TensorFlowNET.Examples.Text.cnn_models -{ - interface ITextClassificationModel - { - Tensor is_training { get; } - Tensor x { get;} - Tensor y { get; } - } -} +using System; +using System.Collections.Generic; +using System.Text; +using Tensorflow; + +namespace TensorFlowNET.Examples.Text.cnn_models +{ + interface ITextClassificationModel + { + Tensor is_training { get; } + Tensor x { get;} + Tensor y { get; } + } +} diff --git a/test/TensorFlowNET.Examples/Text/cnn_models/VdCnn.cs b/test/TensorFlowNET.Examples/TextProcess/cnn_models/VdCnn.cs similarity index 100% rename from test/TensorFlowNET.Examples/Text/cnn_models/VdCnn.cs rename to test/TensorFlowNET.Examples/TextProcess/cnn_models/VdCnn.cs diff --git a/test/TensorFlowNET.UnitTest/ExamplesTests/ExamplesTest.cs b/test/TensorFlowNET.UnitTest/ExamplesTests/ExamplesTest.cs index b93f678b..bca6e64f 100644 --- a/test/TensorFlowNET.UnitTest/ExamplesTests/ExamplesTest.cs +++ b/test/TensorFlowNET.UnitTest/ExamplesTests/ExamplesTest.cs @@ -61,14 +61,6 @@ namespace TensorFlowNET.ExamplesTests new LogisticRegression() { Enabled = true, training_epochs=10, train_size = 500, validation_size = 100, test_size = 100 }.Run(); } - [Ignore] - [TestMethod] - public void MetaGraph() - { - tf.Graph().as_default(); - new MetaGraph() { Enabled = true }.Run(); - } - [Ignore] [TestMethod] public void NaiveBayesClassifier() diff --git a/test/TensorFlowNET.UnitTest/GraphTest.cs b/test/TensorFlowNET.UnitTest/GraphTest.cs index 0fc086a4..b83fd291 100644 --- a/test/TensorFlowNET.UnitTest/GraphTest.cs +++ b/test/TensorFlowNET.UnitTest/GraphTest.cs @@ -5,6 +5,7 @@ using System.Runtime.InteropServices; using System.Text; using Tensorflow; using Buffer = Tensorflow.Buffer; +using static Tensorflow.Python; namespace TensorFlowNET.UnitTest { @@ -417,6 +418,19 @@ namespace TensorFlowNET.UnitTest } - + public void ImportGraphMeta() + { + var dir = "my-save-dir/"; + with(tf.Session(), sess => + { + var new_saver = tf.train.import_meta_graph(dir + "my-model-10000.meta"); + new_saver.restore(sess, dir + "my-model-10000"); + var labels = tf.constant(0, dtype: tf.int32, shape: new int[] { 100 }, name: "labels"); + var batch_size = tf.size(labels); + var logits = (tf.get_collection("logits") as List)[0] as Tensor; + var loss = tf.losses.sparse_softmax_cross_entropy(labels: labels, + logits: logits); + }); + } } }