| @@ -72,6 +72,7 @@ Read the docs & book [The Definitive Guide to Tensorflow.NET](https://tensorflow | |||||
| * [Basic Operations](test/TensorFlowNET.Examples/BasicOperations.cs) | * [Basic Operations](test/TensorFlowNET.Examples/BasicOperations.cs) | ||||
| * [Image Recognition](test/TensorFlowNET.Examples/ImageRecognition.cs) | * [Image Recognition](test/TensorFlowNET.Examples/ImageRecognition.cs) | ||||
| * [Linear Regression](test/TensorFlowNET.Examples/LinearRegression.cs) | * [Linear Regression](test/TensorFlowNET.Examples/LinearRegression.cs) | ||||
| * [Logistic Regression](test/TensorFlowNET.Examples/LogisticRegression.cs) | |||||
| * [Text Classification](test/TensorFlowNET.Examples/TextClassificationWithMovieReviews.cs) | * [Text Classification](test/TensorFlowNET.Examples/TextClassificationWithMovieReviews.cs) | ||||
| * [CNN Text Classification](test/TensorFlowNET.Examples/CnnTextClassification.cs) | * [CNN Text Classification](test/TensorFlowNET.Examples/CnnTextClassification.cs) | ||||
| * [Naive Bayes Classification](test/TensorFlowNET.Examples/NaiveBayesClassifier.cs) | * [Naive Bayes Classification](test/TensorFlowNET.Examples/NaiveBayesClassifier.cs) | ||||
| @@ -17,7 +17,8 @@ | |||||
| <Description>Google's TensorFlow binding in .NET Standard. | <Description>Google's TensorFlow binding in .NET Standard. | ||||
| Docs: https://tensorflownet.readthedocs.io</Description> | Docs: https://tensorflownet.readthedocs.io</Description> | ||||
| <AssemblyVersion>0.5.0.0</AssemblyVersion> | <AssemblyVersion>0.5.0.0</AssemblyVersion> | ||||
| <PackageReleaseNotes>Add a lot of APIs to build neural networks model</PackageReleaseNotes> | |||||
| <PackageReleaseNotes>Add Logistic Regression to do MNIST. | |||||
| Add a lot of APIs to build neural networks model</PackageReleaseNotes> | |||||
| <LangVersion>7.2</LangVersion> | <LangVersion>7.2</LangVersion> | ||||
| <FileVersion>0.5.0.0</FileVersion> | <FileVersion>0.5.0.0</FileVersion> | ||||
| </PropertyGroup> | </PropertyGroup> | ||||
| @@ -12,6 +12,7 @@ namespace TensorFlowNET.Examples | |||||
| public class BasicEagerApi : IExample | public class BasicEagerApi : IExample | ||||
| { | { | ||||
| private Tensor a, b, c, d; | private Tensor a, b, c, d; | ||||
| public void Run() | public void Run() | ||||
| { | { | ||||
| // Set Eager API | // Set Eager API | ||||
| @@ -34,5 +35,9 @@ namespace TensorFlowNET.Examples | |||||
| // Full compatibility with Numpy | // Full compatibility with Numpy | ||||
| } | } | ||||
| public void PrepareData() | |||||
| { | |||||
| } | |||||
| } | } | ||||
| } | } | ||||
| @@ -96,5 +96,9 @@ namespace TensorFlowNET.Examples | |||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| public void PrepareData() | |||||
| { | |||||
| } | |||||
| } | } | ||||
| } | } | ||||
| @@ -33,5 +33,9 @@ namespace TensorFlowNET.Examples | |||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| public void PrepareData() | |||||
| { | |||||
| } | |||||
| } | } | ||||
| } | } | ||||
| @@ -11,5 +11,6 @@ namespace TensorFlowNET.Examples | |||||
| public interface IExample | public interface IExample | ||||
| { | { | ||||
| void Run(); | void Run(); | ||||
| void PrepareData(); | |||||
| } | } | ||||
| } | } | ||||
| @@ -78,7 +78,7 @@ namespace TensorFlowNET.Examples | |||||
| }); | }); | ||||
| } | } | ||||
| private void PrepareData() | |||||
| public void PrepareData() | |||||
| { | { | ||||
| Directory.CreateDirectory(dir); | Directory.CreateDirectory(dir); | ||||
| @@ -83,7 +83,7 @@ namespace TensorFlowNET.Examples | |||||
| }); | }); | ||||
| } | } | ||||
| private void PrepareData() | |||||
| public void PrepareData() | |||||
| { | { | ||||
| Directory.CreateDirectory(dir); | Directory.CreateDirectory(dir); | ||||
| @@ -19,14 +19,13 @@ namespace TensorFlowNET.Examples | |||||
| int training_epochs = 1000; | int training_epochs = 1000; | ||||
| int display_step = 50; | int display_step = 50; | ||||
| NDArray train_X, train_Y; | |||||
| int n_samples; | |||||
| public void Run() | public void Run() | ||||
| { | { | ||||
| // Training Data | // Training Data | ||||
| var train_X = np.array(3.3f, 4.4f, 5.5f, 6.71f, 6.93f, 4.168f, 9.779f, 6.182f, 7.59f, 2.167f, | |||||
| 7.042f, 10.791f, 5.313f, 7.997f, 5.654f, 9.27f, 3.1f); | |||||
| var train_Y = np.array(1.7f, 2.76f, 2.09f, 3.19f, 1.694f, 1.573f, 3.366f, 2.596f, 2.53f, 1.221f, | |||||
| 2.827f, 3.465f, 1.65f, 2.904f, 2.42f, 2.94f, 1.3f); | |||||
| var n_samples = train_X.shape[0]; | |||||
| PrepareData(); | |||||
| // tf Graph Input | // tf Graph Input | ||||
| var X = tf.placeholder(tf.float32); | var X = tf.placeholder(tf.float32); | ||||
| @@ -95,5 +94,14 @@ namespace TensorFlowNET.Examples | |||||
| Console.WriteLine($"Absolute mean square loss difference: {Math.Abs((float)training_cost - (float)testing_cost)}"); | Console.WriteLine($"Absolute mean square loss difference: {Math.Abs((float)training_cost - (float)testing_cost)}"); | ||||
| }); | }); | ||||
| } | } | ||||
| public void PrepareData() | |||||
| { | |||||
| train_X = np.array(3.3f, 4.4f, 5.5f, 6.71f, 6.93f, 4.168f, 9.779f, 6.182f, 7.59f, 2.167f, | |||||
| 7.042f, 10.791f, 5.313f, 7.997f, 5.654f, 9.27f, 3.1f); | |||||
| train_Y = np.array(1.7f, 2.76f, 2.09f, 3.19f, 1.694f, 1.573f, 3.366f, 2.596f, 2.53f, 1.221f, | |||||
| 2.827f, 3.465f, 1.65f, 2.904f, 2.42f, 2.94f, 1.3f); | |||||
| n_samples = train_X.shape[0]; | |||||
| } | |||||
| } | } | ||||
| } | } | ||||
| @@ -21,9 +21,11 @@ namespace TensorFlowNET.Examples | |||||
| private int batch_size = 100; | private int batch_size = 100; | ||||
| private int display_step = 1; | private int display_step = 1; | ||||
| Datasets mnist; | |||||
| public void Run() | public void Run() | ||||
| { | { | ||||
| var mnist = PrepareData(); | |||||
| PrepareData(); | |||||
| // tf Graph Input | // tf Graph Input | ||||
| var x = tf.placeholder(tf.float32, new TensorShape(-1, 784)); // mnist data image of shape 28*28=784 | var x = tf.placeholder(tf.float32, new TensorShape(-1, 784)); // mnist data image of shape 28*28=784 | ||||
| @@ -86,10 +88,9 @@ namespace TensorFlowNET.Examples | |||||
| }); | }); | ||||
| } | } | ||||
| private Datasets PrepareData() | |||||
| public void PrepareData() | |||||
| { | { | ||||
| var mnist = MnistDataSet.read_data_sets("logistic_regression", one_hot: true); | |||||
| return mnist; | |||||
| mnist = MnistDataSet.read_data_sets("logistic_regression", one_hot: true); | |||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| @@ -27,5 +27,9 @@ namespace TensorFlowNET.Examples | |||||
| logits: logits); | logits: logits); | ||||
| }); | }); | ||||
| } | } | ||||
| public void PrepareData() | |||||
| { | |||||
| } | |||||
| } | } | ||||
| } | } | ||||
| @@ -15,9 +15,9 @@ namespace TensorFlowNET.Examples | |||||
| public Normal dist { get; set; } | public Normal dist { get; set; } | ||||
| public void Run() | public void Run() | ||||
| { | { | ||||
| np.array<float>(1.0f, 1.0f); | |||||
| var X = np.array<float>(new float[][] { new float[] { 1.0f, 1.0f }, new float[] { 2.0f, 2.0f }, new float[] { -1.0f, -1.0f }, new float[] { -2.0f, -2.0f }, new float[] { 1.0f, -1.0f }, new float[] { 2.0f, -2.0f }, }); | |||||
| var y = np.array<int>(0,0,1,1,2,2); | |||||
| np.array(1.0f, 1.0f); | |||||
| var X = np.array(new float[][] { new float[] { 1.0f, 1.0f }, new float[] { 2.0f, 2.0f }, new float[] { -1.0f, -1.0f }, new float[] { -2.0f, -2.0f }, new float[] { 1.0f, -1.0f }, new float[] { 2.0f, -2.0f }, }); | |||||
| var y = np.array(0,0,1,1,2,2); | |||||
| fit(X, y); | fit(X, y); | ||||
| // Create a regular grid and classify each point | // Create a regular grid and classify each point | ||||
| } | } | ||||
| @@ -102,5 +102,10 @@ namespace TensorFlowNET.Examples | |||||
| // exp to get the actual probabilities | // exp to get the actual probabilities | ||||
| return tf.exp(log_prob); | return tf.exp(log_prob); | ||||
| } | } | ||||
| public void PrepareData() | |||||
| { | |||||
| } | |||||
| } | } | ||||
| } | } | ||||
| @@ -14,5 +14,10 @@ namespace TensorFlowNET.Examples | |||||
| { | { | ||||
| throw new NotImplementedException(); | throw new NotImplementedException(); | ||||
| } | } | ||||
| public void PrepareData() | |||||
| { | |||||
| throw new NotImplementedException(); | |||||
| } | |||||
| } | } | ||||
| } | } | ||||
| @@ -23,7 +23,7 @@ namespace TensorFlowNET.Examples.CnnTextClassification | |||||
| public void Run() | public void Run() | ||||
| { | { | ||||
| download_dbpedia(); | |||||
| PrepareData(); | |||||
| Console.WriteLine("Building dataset..."); | Console.WriteLine("Building dataset..."); | ||||
| var (x, y, alphabet_size) = DataHelpers.build_char_dataset("train", "vdcnn", CHAR_MAX_LEN); | var (x, y, alphabet_size) = DataHelpers.build_char_dataset("train", "vdcnn", CHAR_MAX_LEN); | ||||
| @@ -32,17 +32,9 @@ namespace TensorFlowNET.Examples.CnnTextClassification | |||||
| with(tf.Session(), sess => | with(tf.Session(), sess => | ||||
| { | { | ||||
| new VdCnn(alphabet_size, CHAR_MAX_LEN, NUM_CLASS); | new VdCnn(alphabet_size, CHAR_MAX_LEN, NUM_CLASS); | ||||
| }); | }); | ||||
| } | } | ||||
| public void download_dbpedia() | |||||
| { | |||||
| string url = "https://github.com/le-scientifique/torchDatasets/raw/master/dbpedia_csv.tar.gz"; | |||||
| Web.Download(url, dataDir, dataFileName); | |||||
| Compress.ExtractTGZ(Path.Join(dataDir, dataFileName), dataDir); | |||||
| } | |||||
| private (int[][], int[][], int[], int[]) train_test_split(int[][] x, int[] y, float test_size = 0.3f) | private (int[][], int[][], int[], int[]) train_test_split(int[][] x, int[] y, float test_size = 0.3f) | ||||
| { | { | ||||
| int len = x.Length; | int len = x.Length; | ||||
| @@ -75,5 +67,12 @@ namespace TensorFlowNET.Examples.CnnTextClassification | |||||
| return (train_x.ToArray(), valid_x.ToArray(), train_y.ToArray(), valid_y.ToArray()); | return (train_x.ToArray(), valid_x.ToArray(), train_y.ToArray(), valid_y.ToArray()); | ||||
| } | } | ||||
| public void PrepareData() | |||||
| { | |||||
| string url = "https://github.com/le-scientifique/torchDatasets/raw/master/dbpedia_csv.tar.gz"; | |||||
| Web.Download(url, dataDir, dataFileName); | |||||
| Compress.ExtractTGZ(Path.Join(dataDir, dataFileName), dataDir); | |||||
| } | |||||
| } | } | ||||
| } | } | ||||
| @@ -13,10 +13,11 @@ namespace TensorFlowNET.Examples | |||||
| { | { | ||||
| string dir = "text_classification_with_movie_reviews"; | string dir = "text_classification_with_movie_reviews"; | ||||
| string dataFile = "imdb.zip"; | string dataFile = "imdb.zip"; | ||||
| NDArray train_data, train_labels, test_data, test_labels; | |||||
| public void Run() | public void Run() | ||||
| { | { | ||||
| var((train_data, train_labels), (test_data, test_labels)) = PrepareData(); | |||||
| PrepareData(); | |||||
| Console.WriteLine($"Training entries: {train_data.size}, labels: {train_labels.size}"); | Console.WriteLine($"Training entries: {train_data.size}, labels: {train_labels.size}"); | ||||
| @@ -40,7 +41,7 @@ namespace TensorFlowNET.Examples | |||||
| model.add(keras.layers.Embedding(vocab_size, 16)); | model.add(keras.layers.Embedding(vocab_size, 16)); | ||||
| } | } | ||||
| private ((NDArray, NDArray), (NDArray, NDArray)) PrepareData() | |||||
| public void PrepareData() | |||||
| { | { | ||||
| Directory.CreateDirectory(dir); | Directory.CreateDirectory(dir); | ||||
| @@ -71,7 +72,11 @@ namespace TensorFlowNET.Examples | |||||
| var y_train = labels_train; | var y_train = labels_train; | ||||
| var y_test = labels_test; | var y_test = labels_test; | ||||
| return ((x_train, y_train), (x_test, y_test)); | |||||
| x_train = train_data; | |||||
| train_labels = y_train; | |||||
| test_data = x_test; | |||||
| test_labels = y_test; | |||||
| } | } | ||||
| private NDArray ReadData(string file) | private NDArray ReadData(string file) | ||||