| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 0cb07ec commit c0760ff
7 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -37,7 +37,7 @@ Import TF.NET. | |||
| 37 | 37 | using Tensorflow; | |
| 38 | 38 | ``` | |
| 39 | 39 | ||
| 40 | - Add two constants. | ||
| 40 | + Add two constants: | ||
| 41 | 41 | ```cs | |
| 42 | 42 | // Create a Constant op | |
| 43 | 43 | var a = tf.constant(4.0f); | |
@@ -50,7 +50,7 @@ using (var sess = tf.Session()) | |||
| 50 | 50 | } | |
| 51 | 51 | ``` | |
| 52 | 52 | ||
| 53 | - Feed placeholder. | ||
| 53 | + Feed placeholder: | ||
| 54 | 54 | ```cs | |
| 55 | 55 | // Create a placeholder op | |
| 56 | 56 | var a = tf.placeholder(tf.float32); | |
@@ -59,27 +59,86 @@ var c = tf.add(a, b); | |||
| 59 | 59 | ||
| 60 | 60 | using(var sess = tf.Session()) | |
| 61 | 61 | { | |
| 62 | - var feed_dict = new Dictionary<Tensor, object>(); | ||
| 63 | - feed_dict.Add(a, 3.0f); | ||
| 64 | - feed_dict.Add(b, 2.0f); | ||
| 65 | - | ||
| 66 | - var o = sess.run(c, feed_dict); | ||
| 62 | + var o = sess.run(c, new FeedItem(a, 3.0f), new FeedItem(b, 2.0f)); | ||
| 67 | 63 | } | |
| 68 | 64 | ``` | |
| 69 | 65 | ||
| 66 | + Linear Regression: | ||
| 67 | + | ||
| 68 | + ```c# | ||
| 69 | + // We can set a fixed init value in order to debug | ||
| 70 | + var W = tf.Variable(-0.06f, name: "weight"); | ||
| 71 | + var b = tf.Variable(-0.73f, name: "bias"); | ||
| 72 | + | ||
| 73 | + // Construct a linear model | ||
| 74 | + var pred = tf.add(tf.multiply(X, W), b); | ||
| 75 | + | ||
| 76 | + // Mean squared error | ||
| 77 | + var cost = tf.reduce_sum(tf.pow(pred - Y, 2.0f)) / (2.0f * n_samples); | ||
| 78 | + | ||
| 79 | + // Gradient descent | ||
| 80 | + // Note, minimize() knows to modify W and b because Variable objects are trainable=True by default | ||
| 81 | + var optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost); | ||
| 82 | + | ||
| 83 | + // Initialize the variables (i.e. assign their default value) | ||
| 84 | + var init = tf.global_variables_initializer(); | ||
| 85 | + | ||
| 86 | + // Start training | ||
| 87 | + with(tf.Session(), sess => | ||
| 88 | + { | ||
| 89 | + // Run the initializer | ||
| 90 | + sess.run(init); | ||
| 91 | + | ||
| 92 | + // Fit all training data | ||
| 93 | + for (int epoch = 0; epoch < training_epochs; epoch++) | ||
| 94 | + { | ||
| 95 | + foreach (var (x, y) in zip<float>(train_X, train_Y)) | ||
| 96 | + sess.run(optimizer, new FeedItem(X, x), new FeedItem(Y, y)); | ||
| 97 | + | ||
| 98 | + // Display logs per epoch step | ||
| 99 | + if ((epoch + 1) % display_step == 0) | ||
| 100 | + { | ||
| 101 | + var c = sess.run(cost, new FeedItem(X, train_X), new FeedItem(Y, train_Y)); | ||
| 102 | + Console.WriteLine($"Epoch: {epoch + 1} cost={c} " + $"W={sess.run(W)} b={sess.run(b)}"); | ||
| 103 | + } | ||
| 104 | + | ||
| 105 | + Console.WriteLine("Optimization Finished!"); | ||
| 106 | + var training_cost = sess.run(cost, new FeedItem(X, train_X), new FeedItem(Y, train_Y)); | ||
| 107 | + Console.WriteLine($"Training cost={training_cost} W={sess.run(W)} b={sess.run(b)}"); | ||
| 108 | + | ||
| 109 | + // Testing example | ||
| 110 | + var test_X = np.array(6.83f, 4.668f, 8.9f, 7.91f, 5.7f, 8.7f, 3.1f, 2.1f); | ||
| 111 | + var test_Y = np.array(1.84f, 2.273f, 3.2f, 2.831f, 2.92f, 3.24f, 1.35f, 1.03f); | ||
| 112 | + Console.WriteLine("Testing... (Mean square loss Comparison)"); | ||
| 113 | + | ||
| 114 | + var testing_cost = sess.run(tf.reduce_sum(tf.pow(pred - Y, 2.0f)) / (2.0f * test_X.shape[0]), new FeedItem(X, test_X), new FeedItem(Y, test_Y)); | ||
| 115 | + Console.WriteLine($"Testing cost={testing_cost}"); | ||
| 116 | + | ||
| 117 | + var diff = Math.Abs((float)training_cost - (float)testing_cost); | ||
| 118 | + Console.WriteLine($"Absolute mean square loss difference: {diff}"); | ||
| 119 | + } | ||
| 120 | + }); | ||
| 121 | + ``` | ||
| 122 | + | ||
| 123 | + | ||
| 124 | + | ||
| 70 | 125 | Read the docs & book [The Definitive Guide to Tensorflow.NET](https://tensorflownet.readthedocs.io/en/latest/FrontCover.html). | |
| 71 | 126 | ||
| 72 | 127 | ### More examples: | |
| 73 | 128 | ||
| 74 | 129 | * [Hello World](test/TensorFlowNET.Examples/HelloWorld.cs) | |
| 75 | 130 | * [Basic Operations](test/TensorFlowNET.Examples/BasicOperations.cs) | |
| 76 | - * [Image Recognition](test/TensorFlowNET.Examples/ImageRecognition.cs) | ||
| 77 | 131 | * [Linear Regression](test/TensorFlowNET.Examples/LinearRegression.cs) | |
| 78 | 132 | * [Logistic Regression](test/TensorFlowNET.Examples/LogisticRegression.cs) | |
| 79 | 133 | * [Nearest Neighbor](test/TensorFlowNET.Examples/NearestNeighbor.cs) | |
| 134 | + * [Naive Bayes Classification](test/TensorFlowNET.Examples/NaiveBayesClassifier.cs) | ||
| 135 | + * [Image Recognition](test/TensorFlowNET.Examples/ImageRecognition.cs) | ||
| 136 | + * [K-means Clustering](test/TensorFlowNET.Examples/KMeansClustering.cs) | ||
| 137 | + * [NN XOR](test/TensorFlowNET.Examples/NeuralNetXor.cs) | ||
| 138 | + * [Object Detection](test/TensorFlowNET.Examples/ObjectDetection.cs) | ||
| 80 | 139 | * [Text Classification](test/TensorFlowNET.Examples/TextClassificationWithMovieReviews.cs) | |
| 81 | 140 | * [CNN Text Classification](test/TensorFlowNET.Examples/CnnTextClassification.cs) | |
| 82 | - * [Naive Bayes Classification](test/TensorFlowNET.Examples/NaiveBayesClassifier.cs) | ||
| 141 | + | ||
| 83 | 142 | * [Named Entity Recognition](test/TensorFlowNET.Examples/NamedEntityRecognition.cs) | |
| 84 | 143 | ||
| 85 | 144 | ### Contribute: | |
@@ -94,7 +153,7 @@ You can: | |||
| 94 | 153 | * Debug one of the unit tests that is marked as Ignored to get it to work (can be challenging) | |
| 95 | 154 | * Debug one of the not yet working examples and get it to work (hard) | |
| 96 | 155 | ||
| 97 | - How to debug unit tests: | ||
| 156 | + ### How to debug unit tests: | ||
| 98 | 157 | ||
| 99 | 158 | The best way to find out why a unit test is failing is to single step it in C# and its pendant Python at the same time to see where the flow of execution digresses or where variables exhibit different values. Good Python IDEs like PyCharm let you single step into the tensorflow library code. | |
| 100 | 159 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -44,7 +44,7 @@ More math/ linalg APIs.</PackageReleaseNotes> | |||
| 44 | 44 | ||
| 45 | 45 | <ItemGroup> | |
| 46 | 46 | <PackageReference Include="Google.Protobuf" Version="3.7.0" /> | |
| 47 | - <PackageReference Include="NumSharp" Version="0.10.0-alpha" /> | ||
| 47 | + <PackageReference Include="NumSharp" Version="0.10.0-beta" /> | ||
| 48 | 48 | </ItemGroup> | |
| 49 | 49 | ||
| 50 | 50 | <ItemGroup> | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -76,7 +76,7 @@ private float RunWithImportedGraph() | |||
| 76 | 76 | var init = tf.global_variables_initializer(); | |
| 77 | 77 | float loss_value = 0; | |
| 78 | 78 | // Start tf session | |
| 79 | - with<Session>(tf.Session(graph), sess => | ||
| 79 | + with(tf.Session(graph), sess => | ||
| 80 | 80 | { | |
| 81 | 81 | sess.run(init); | |
| 82 | 82 | var step = 0; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -9,14 +9,19 @@ | |||
| 9 | 9 | ||
| 10 | 10 | namespace TensorFlowNET.Examples | |
| 11 | 11 | { | |
| 12 | - public class TextClassificationWithMovieReviews : Python, IExample | ||
| 12 | + /// <summary> | ||
| 13 | + /// This example classifies movie reviews as positive or negative using the text of the review. | ||
| 14 | + /// This is a binary—or two-class—classification, an important and widely applicable kind of machine learning problem. | ||
| 15 | + /// https://github.com/tensorflow/docs/blob/master/site/en/tutorials/keras/basic_text_classification.ipynb | ||
| 16 | + /// </summary> | ||
| 17 | + public class BinaryTextClassification : Python, IExample | ||
| 13 | 18 | { | |
| 14 | 19 | public int Priority => 9; | |
| 15 | - public bool Enabled { get; set; } = false; | ||
| 16 | - public string Name => "Movie Reviews"; | ||
| 17 | - public bool ImportGraph { get; set; } = true; | ||
| 20 | + public bool Enabled { get; set; } = true; | ||
| 21 | + public string Name => "Binary Text Classification"; | ||
| 22 | + public bool ImportGraph { get; set; } = true; | ||
| 18 | 23 | ||
| 19 | - string dir = "text_classification_with_movie_reviews"; | ||
| 24 | + string dir = "binary_text_classification"; | ||
| 20 | 25 | string dataFile = "imdb.zip"; | |
| 21 | 26 | NDArray train_data, train_labels, test_data, test_labels; | |
| 22 | 27 | ||
@@ -89,17 +94,18 @@ public void PrepareData() | |||
| 89 | 94 | private NDArray ReadData(string file) | |
| 90 | 95 | { | |
| 91 | 96 | var lines = File.ReadAllLines(file); | |
| 92 | - var nd = new NDArray(lines[0].StartsWith("[") ? typeof(object) : np.int32, new Shape(lines.Length)); | ||
| 97 | + var nd = new NDArray(lines[0].StartsWith("[") ? typeof(string) : np.int32, new Shape(lines.Length)); | ||
| 93 | 98 | ||
| 94 | 99 | if (lines[0].StartsWith("[")) | |
| 95 | 100 | { | |
| 96 | 101 | for (int i = 0; i < lines.Length; i++) | |
| 97 | 102 | { | |
| 98 | - var matches = Regex.Matches(lines[i], @"\d+\s*"); | ||
| 103 | + /*var matches = Regex.Matches(lines[i], @"\d+\s*"); | ||
| 99 | 104 | var data = new int[matches.Count]; | |
| 100 | 105 | for (int j = 0; j < data.Length; j++) | |
| 101 | 106 | data[j] = Convert.ToInt32(matches[j].Value); | |
| 102 | - nd[i] = data.ToArray(); | ||
| 107 | + nd[i] = data.ToArray();*/ | ||
| 108 | + nd[i] = lines[i].Substring(1, lines[i].Length - 2).Replace(" ", string.Empty); | ||
| 103 | 109 | } | |
| 104 | 110 | } | |
| 105 | 111 | else | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -71,30 +71,12 @@ def decode_review(text): | |||
| 71 | 71 | ||
| 72 | 72 | history = model.fit(partial_x_train, | |
| 73 | 73 | partial_y_train, | |
| 74 | - epochs=40, | ||
| 74 | + epochs=20, | ||
| 75 | 75 | batch_size=512, | |
| 76 | 76 | validation_data=(x_val, y_val), | |
| 77 | 77 | verbose=1) | |
| 78 | 78 | ||
| 79 | 79 | results = model.evaluate(test_data, test_labels) | |
| 80 | - | ||
| 81 | - # serialize model to JSON | ||
| 82 | - model_json = model.to_json() | ||
| 83 | - with open("model.json", "w") as json_file: | ||
| 84 | - json_file.write(model_json) | ||
| 85 | - # serialize weights to HDF5 | ||
| 86 | - model.save_weights("model.h5") | ||
| 87 | - print("Saved model to disk") | ||
| 88 | - | ||
| 89 | - # load json and create model | ||
| 90 | - json_file = open('model.json', 'r') | ||
| 91 | - loaded_model_json = json_file.read() | ||
| 92 | - json_file.close() | ||
| 93 | - loaded_model = model_from_json(loaded_model_json) | ||
| 94 | - # load weights into new model | ||
| 95 | - loaded_model.load_weights("model.h5") | ||
| 96 | - print("Loaded model from disk") | ||
| 97 | - | ||
| 98 | 80 | print(results) | |
| 99 | 81 | ||
| 100 | 82 | history_dict = history.history | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -105,7 +105,7 @@ public void TextClassificationTrain() | |||
| 105 | 105 | public void TextClassificationWithMovieReviews() | |
| 106 | 106 | { | |
| 107 | 107 | tf.Graph().as_default(); | |
| 108 | - new TextClassificationWithMovieReviews() { Enabled = true }.Run(); | ||
| 108 | + new BinaryTextClassification() { Enabled = true }.Run(); | ||
| 109 | 109 | } | |
| 110 | 110 | ||
| 111 | 111 | [TestMethod] | |
| Back | FazBrowse Home | New Git URL |
0 commit comments