From 8cdb4a782887aefc2cbd2ba30f61df57e5202723 Mon Sep 17 00:00:00 2001 From: Oceania2018 Date: Sat, 19 Dec 2020 11:20:25 -0600 Subject: [PATCH] Add dataset cardinality test #695 --- README.md | 10 ++++++---- src/TensorFlowNET.Core/Data/DatasetV2.cs | 4 +--- src/TensorFlowNET.Core/Data/IDatasetV2.cs | 5 +++++ test/TensorFlowNET.UnitTest/Dataset/DatasetTest.cs | 9 +++++++++ 4 files changed, 21 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 2fd46c6b..b81c7724 100644 --- a/README.md +++ b/README.md @@ -16,13 +16,15 @@ ### Why TensorFlow in C# and F# ? -`SciSharp STACK`'s mission is to bring popular data science technology into the .NET world and to provide .NET developers with a powerful Machine Learning tool set without reinventing the wheel. Since the APIs are kept as similar as possible you can immediately adapt any existing Tensorflow code in C# or F# with a zero learning curve. Take a look at a comparison picture and see how comfortably a Tensorflow/Python script translates into a C# program with TensorFlow.NET. +`SciSharp STACK`'s mission is to bring popular data science technology into the .NET world and to provide .NET developers with a powerful Machine Learning tool set without reinventing the wheel. Since the APIs are kept as similar as possible you can immediately adapt any existing TensorFlow code in C# or F# with a zero learning curve. Take a look at a comparison picture and see how comfortably a TensorFlow/Python script translates into a C# program with TensorFlow.NET. ![pythn vs csharp](docs/assets/syntax-comparision.png) -SciSharp's philosophy allows a large number of machine learning code written in Python to be quickly migrated to .NET, enabling .NET developers to use cutting edge machine learning models and access a vast number of Tensorflow resources which would not be possible without this project. +SciSharp's philosophy allows a large number of machine learning code written in Python to be quickly migrated to .NET, enabling .NET developers to use cutting edge machine learning models and access a vast number of TensorFlow resources which would not be possible without this project. -In comparison to other projects, like for instance [TensorFlowSharp](https://www.nuget.org/packages/TensorFlowSharp/) which only provide Tensorflow's low-level C++ API and can only run models that were built using Python, Tensorflow.NET also implements Tensorflow's high level API where all the magic happens. This computation graph building layer is still under active development. Once it is completely implemented you can build new Machine Learning models in C# or F#. +In comparison to other projects, like for instance [TensorFlowSharp](https://www.nuget.org/packages/TensorFlowSharp/) which only provide TensorFlow's low-level C++ API and can only run models that were built using Python, Tensorflow.NET also implements TensorFlow's high level API where all the magic happens. This computation graph building layer is still under active development. Once it is completely implemented you can build new Machine Learning models in C# or F#. + +Go through the online docs [TensorFlow for .NET](https://scisharp.github.io/tensorflow-net-docs) before you get started with Machine Learning in .NET. ### How to use @@ -210,7 +212,7 @@ for step = 1 to (training_steps + 1) do printfn $"step: {step}, loss: {loss.numpy()}, W: {W.numpy()}, b: {b.numpy()}" ``` -Read the docs & book [The Definitive Guide to Tensorflow.NET](https://tensorflownet.readthedocs.io/en/latest/FrontCover.html) if you want to know more about TensorFlow for .NET under the hood. +Read the book [The Definitive Guide to Tensorflow.NET](https://tensorflownet.readthedocs.io/en/latest/FrontCover.html) if you want to know more about TensorFlow for .NET under the hood. ### Contribute: diff --git a/src/TensorFlowNET.Core/Data/DatasetV2.cs b/src/TensorFlowNET.Core/Data/DatasetV2.cs index 763baa31..00b04edf 100644 --- a/src/TensorFlowNET.Core/Data/DatasetV2.cs +++ b/src/TensorFlowNET.Core/Data/DatasetV2.cs @@ -132,9 +132,7 @@ namespace Tensorflow break; } - yield return results.Length == 2 - ? (results[0], results[1]) - : (null, results[0]); + yield return (results[0], results.Length == 1 ? null : results[1]); } } diff --git a/src/TensorFlowNET.Core/Data/IDatasetV2.cs b/src/TensorFlowNET.Core/Data/IDatasetV2.cs index 9a31ff51..77bc9169 100644 --- a/src/TensorFlowNET.Core/Data/IDatasetV2.cs +++ b/src/TensorFlowNET.Core/Data/IDatasetV2.cs @@ -75,6 +75,11 @@ namespace Tensorflow /// IDatasetV2 apply_options(); + /// + /// Returns the cardinality of `dataset`, if known. + /// + /// + /// Tensor dataset_cardinality(string name = null); } } diff --git a/test/TensorFlowNET.UnitTest/Dataset/DatasetTest.cs b/test/TensorFlowNET.UnitTest/Dataset/DatasetTest.cs index 9ad78482..417dac09 100644 --- a/test/TensorFlowNET.UnitTest/Dataset/DatasetTest.cs +++ b/test/TensorFlowNET.UnitTest/Dataset/DatasetTest.cs @@ -142,5 +142,14 @@ namespace TensorFlowNET.UnitTest.Dataset value++; } } + + [TestMethod, Ignore] + public void Cardinality() + { + var dataset = tf.data.Dataset.range(10); + dataset = dataset.map(x => x + 1); + var cardinality = dataset.dataset_cardinality(); + Assert.AreEqual(new long[] { 10 }, cardinality.numpy()); + } } }