From 0484aab0f973169d808305441c8aed1ec35dd4b6 Mon Sep 17 00:00:00 2001 From: Oceania2018 Date: Mon, 22 Apr 2019 21:34:09 -0500 Subject: [PATCH] optimize image dataset loading --- test/TensorFlowNET.Examples/LogisticRegression.cs | 11 ++++++++++- test/TensorFlowNET.Examples/NeuralNetXor.cs | 2 +- test/TensorFlowNET.Examples/Utility/DataSet.cs | 4 ++-- test/TensorFlowNET.Examples/Utility/MnistDataSet.cs | 2 +- 4 files changed, 14 insertions(+), 5 deletions(-) diff --git a/test/TensorFlowNET.Examples/LogisticRegression.cs b/test/TensorFlowNET.Examples/LogisticRegression.cs index 1646f149..71238bf6 100644 --- a/test/TensorFlowNET.Examples/LogisticRegression.cs +++ b/test/TensorFlowNET.Examples/LogisticRegression.cs @@ -1,6 +1,7 @@ using NumSharp; using System; using System.Collections.Generic; +using System.Diagnostics; using System.IO; using System.Linq; using System.Text; @@ -55,6 +56,8 @@ namespace TensorFlowNET.Examples // Initialize the variables (i.e. assign their default value) var init = tf.global_variables_initializer(); + var sw = new Stopwatch(); + return with(tf.Session(), sess => { // Run the initializer @@ -63,6 +66,8 @@ namespace TensorFlowNET.Examples // Training cycle foreach (var epoch in range(training_epochs)) { + sw.Start(); + var avg_cost = 0.0f; var total_batch = mnist.train.num_examples / batch_size; // Loop over all batches @@ -79,9 +84,13 @@ namespace TensorFlowNET.Examples avg_cost += c / total_batch; } + sw.Stop(); + // Display logs per epoch step if ((epoch + 1) % display_step == 0) - print($"Epoch: {(epoch + 1).ToString("D4")} cost= {avg_cost.ToString("G9")}"); + print($"Epoch: {(epoch + 1).ToString("D4")} cost= {avg_cost.ToString("G9")} elapse= {sw.ElapsedMilliseconds}ms"); + + sw.Reset(); } print("Optimization Finished!"); diff --git a/test/TensorFlowNET.Examples/NeuralNetXor.cs b/test/TensorFlowNET.Examples/NeuralNetXor.cs index a52fa3dd..92984af3 100644 --- a/test/TensorFlowNET.Examples/NeuralNetXor.cs +++ b/test/TensorFlowNET.Examples/NeuralNetXor.cs @@ -12,7 +12,7 @@ namespace TensorFlowNET.Examples /// public class NeuralNetXor : Python, IExample { - public int Priority => 2; + public int Priority => 10; public bool Enabled { get; set; } = true; public string Name => "NN XOR"; diff --git a/test/TensorFlowNET.Examples/Utility/DataSet.cs b/test/TensorFlowNET.Examples/Utility/DataSet.cs index 10e68fa7..6b6b8769 100644 --- a/test/TensorFlowNET.Examples/Utility/DataSet.cs +++ b/test/TensorFlowNET.Examples/Utility/DataSet.cs @@ -54,8 +54,8 @@ namespace TensorFlowNET.Examples.Utility // Get the rest examples in this epoch var rest_num_examples = _num_examples - start; - var images_rest_part = _images[np.arange(start, _num_examples)]; - var labels_rest_part = _labels[np.arange(start, _num_examples)]; + //var images_rest_part = _images[np.arange(start, _num_examples)]; + //var labels_rest_part = _labels[np.arange(start, _num_examples)]; // Shuffle the data if (shuffle) { diff --git a/test/TensorFlowNET.Examples/Utility/MnistDataSet.cs b/test/TensorFlowNET.Examples/Utility/MnistDataSet.cs index d4ee0824..41728813 100644 --- a/test/TensorFlowNET.Examples/Utility/MnistDataSet.cs +++ b/test/TensorFlowNET.Examples/Utility/MnistDataSet.cs @@ -102,7 +102,7 @@ namespace TensorFlowNET.Examples.Utility for(int row = 0; row < num_labels; row++) { var col = labels_dense.Data(row); - labels_one_hot.SetData(1, row, col); + labels_one_hot.SetData(1.0, row, col); } return labels_one_hot;