diff --git a/src/TensorFlowNET.Core/Tensors/Tensor.Index.cs b/src/TensorFlowNET.Core/Tensors/Tensor.Index.cs index 1f4d6dd5..aa5df367 100644 --- a/src/TensorFlowNET.Core/Tensors/Tensor.Index.cs +++ b/src/TensorFlowNET.Core/Tensors/Tensor.Index.cs @@ -60,13 +60,9 @@ namespace Tensorflow } } - public Tensor this[Range slices] - => throw new NotImplementedException(""); - public Tensor this[params string[] slices] => this[slices.Select(x => new Slice(x)).ToArray()]; - public Tensor slice(Slice slice) { var slice_spec = new int[] { slice.Start.Value }; diff --git a/src/TensorFlowNET.Keras/Utils/KerasUtils.cs b/src/TensorFlowNET.Keras/Utils/KerasUtils.cs new file mode 100644 index 00000000..567bee91 --- /dev/null +++ b/src/TensorFlowNET.Keras/Utils/KerasUtils.cs @@ -0,0 +1,42 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Tensorflow.Keras.Utils +{ + public class KerasUtils + { + /// + /// Downloads a file from a URL if it not already in the cache. + /// + /// Name of the file + /// Original URL of the file + /// + /// + /// + /// + /// + /// + /// + /// + /// + public string get_file(string fname, string origin, + bool untar = false, + string md5_hash = null, + string file_hash = null, + string cache_subdir = "datasets", + string hash_algorithm = "auto", + bool extract = false, + string archive_format = "auto", + string cache_dir = null) + => data_utils.get_file(fname, origin, + untar: untar, + md5_hash: md5_hash, + file_hash: file_hash, + cache_subdir: cache_subdir, + hash_algorithm: hash_algorithm, + extract: extract, + archive_format: archive_format, + cache_dir: cache_dir); + } +} diff --git a/src/TensorFlowNET.Keras/Utils/RuntimeHelpers.cs b/src/TensorFlowNET.Keras/Utils/RuntimeHelpers.cs deleted file mode 100644 index 22f158c4..00000000 --- a/src/TensorFlowNET.Keras/Utils/RuntimeHelpers.cs +++ /dev/null @@ -1,39 +0,0 @@ -namespace System.Runtime.CompilerServices -{ - internal static class RuntimeHelpers - { - /// - /// Slices the specified array using the specified range. - /// - public static T[] GetSubArray(T[] array, Range range) - { - if (array == null) - { - throw new ArgumentNullException(nameof(array)); - } - - (int offset, int length) = range.GetOffsetAndLength(array.Length); - - if (default(T) != null || typeof(T[]) == array.GetType()) - { - // We know the type of the array to be exactly T[]. - - if (length == 0) - { - return Array.Empty(); - } - - var dest = new T[length]; - Array.Copy(array, offset, dest, 0, length); - return dest; - } - else - { - // The array is actually a U[] where U:T. - var dest = (T[])Array.CreateInstance(array.GetType().GetElementType(), length); - Array.Copy(array, offset, dest, 0, length); - return dest; - } - } - } -} \ No newline at end of file diff --git a/src/TensorFlowNET.Keras/Utils/data_utils.cs b/src/TensorFlowNET.Keras/Utils/data_utils.cs new file mode 100644 index 00000000..fda3a545 --- /dev/null +++ b/src/TensorFlowNET.Keras/Utils/data_utils.cs @@ -0,0 +1,37 @@ +using System; +using System.Linq; +using System.Collections.Generic; +using System.IO; +using System.Text; + +namespace Tensorflow.Keras.Utils +{ + public class data_utils + { + public static string get_file(string fname, string origin, + bool untar = false, + string md5_hash = null, + string file_hash = null, + string cache_subdir = "datasets", + string hash_algorithm = "auto", + bool extract = false, + string archive_format = "auto", + string cache_dir = null) + { + var datadir_base = cache_dir; + Directory.CreateDirectory(datadir_base); + + var datadir = Path.Combine(datadir_base, cache_subdir); + Directory.CreateDirectory(datadir); + + Web.Download(origin, datadir, fname); + + if (untar) + Compress.ExtractTGZ(Path.Combine(datadir_base, fname), datadir_base); + else if (extract) + Compress.ExtractGZip(Path.Combine(datadir_base, fname), datadir_base); + + return datadir; + } + } +} diff --git a/src/TensorFlowNET.Keras/Utils/layer_utils.cs b/src/TensorFlowNET.Keras/Utils/layer_utils.cs index 4166fd51..a7b1ef99 100644 --- a/src/TensorFlowNET.Keras/Utils/layer_utils.cs +++ b/src/TensorFlowNET.Keras/Utils/layer_utils.cs @@ -67,7 +67,7 @@ namespace Tensorflow.Keras.Utils line_length = 65; if (positions == null) positions = new[] { 0.45f, 0.85f, 1.0f }; - if (positions[^1] <= 1) + if (positions.Last() <= 1) positions = positions.Select(p => line_length * p).ToArray(); to_display = new[] { "Layer (type)", "Output Shape", "Param #" }; } @@ -77,7 +77,7 @@ namespace Tensorflow.Keras.Utils line_length = 98; if (positions == null) positions = new[] { 0.33f, 0.55f, 0.67f, 1.0f }; - if (positions[^1] <= 1) + if (positions.Last() <= 1) positions = positions.Select(p => line_length * p).ToArray(); to_display = new[] { "Layer (type)", "Output Shape", "Param #", "Connected to" }; @@ -118,7 +118,7 @@ namespace Tensorflow.Keras.Utils foreach (var i in range(fields.Length)) { if (i > 0) - line = line[0..^1] + " "; + line = line + " "; line += fields[i]; line = string.Join("", line.Take(positions[i])); line += string.Join("", range(positions[i] - len(line)).Select(x => " ")); diff --git a/src/TensorFlowNET.Core/Util/Range.cs b/test/TensorFlowNET.Keras.UnitTest/Range.cs similarity index 100% rename from src/TensorFlowNET.Core/Util/Range.cs rename to test/TensorFlowNET.Keras.UnitTest/Range.cs diff --git a/src/TensorFlowNET.Core/Util/RuntimeHelpers.cs b/test/TensorFlowNET.Keras.UnitTest/RuntimeHelpers.cs similarity index 100% rename from src/TensorFlowNET.Core/Util/RuntimeHelpers.cs rename to test/TensorFlowNET.Keras.UnitTest/RuntimeHelpers.cs diff --git a/test/TensorFlowNET.Keras.UnitTest/Tensorflow.Keras.UnitTest.csproj b/test/TensorFlowNET.Keras.UnitTest/Tensorflow.Keras.UnitTest.csproj index 95ead0c5..fa92d3b3 100644 --- a/test/TensorFlowNET.Keras.UnitTest/Tensorflow.Keras.UnitTest.csproj +++ b/test/TensorFlowNET.Keras.UnitTest/Tensorflow.Keras.UnitTest.csproj @@ -1,7 +1,7 @@ - netcoreapp3.1 + net5.0 false diff --git a/test/TensorFlowNET.Native.UnitTest/Tensorflow.Native.UnitTest.csproj b/test/TensorFlowNET.Native.UnitTest/Tensorflow.Native.UnitTest.csproj index 03797267..4d340c9b 100644 --- a/test/TensorFlowNET.Native.UnitTest/Tensorflow.Native.UnitTest.csproj +++ b/test/TensorFlowNET.Native.UnitTest/Tensorflow.Native.UnitTest.csproj @@ -1,7 +1,7 @@ - netcoreapp3.1 + net5.0 false diff --git a/test/TensorFlowNET.UnitTest/Dataset/DatasetTest.cs b/test/TensorFlowNET.UnitTest/Dataset/DatasetTest.cs index 746fea84..0a858ef9 100644 --- a/test/TensorFlowNET.UnitTest/Dataset/DatasetTest.cs +++ b/test/TensorFlowNET.UnitTest/Dataset/DatasetTest.cs @@ -119,7 +119,7 @@ namespace TensorFlowNET.UnitTest.Dataset long value = 0; var dataset = tf.data.Dataset.range(0, 2); - dataset = dataset.map(x => x + 10); + dataset = dataset.map(x => x[0] + 10); foreach (var item in dataset) { @@ -147,7 +147,7 @@ namespace TensorFlowNET.UnitTest.Dataset public void Cardinality() { var dataset = tf.data.Dataset.range(10); - dataset = dataset.map(x => x + 1); + dataset = dataset.map(x => x[0] + 1); var cardinality = dataset.dataset_cardinality(); Assert.AreEqual(new long[] { 10 }, cardinality.numpy()); } diff --git a/test/TensorFlowNET.UnitTest/Tensorflow.UnitTest.csproj b/test/TensorFlowNET.UnitTest/Tensorflow.UnitTest.csproj index bb3a16c8..70efb339 100644 --- a/test/TensorFlowNET.UnitTest/Tensorflow.UnitTest.csproj +++ b/test/TensorFlowNET.UnitTest/Tensorflow.UnitTest.csproj @@ -1,7 +1,7 @@  - netcoreapp3.1 + net5.0 false