| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 1fc3838 commit d674d51
10 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -129,7 +129,7 @@ Read the docs & book [The Definitive Guide to Tensorflow.NET](https://tensorflow | |||
| 129 | 129 | Run specific example in shell: | |
| 130 | 130 | ||
| 131 | 131 | ```cs | |
| 132 | - dotnet TensorFlowNET.Examples.dll "EXAMPLE NAME" | ||
| 132 | + dotnet TensorFlowNET.Examples.dll -ex "MNIST CNN" | ||
| 133 | 133 | ``` | |
| 134 | 134 | ||
| 135 | 135 | Example runner will download all the required files like training data and model pb files. | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -79,23 +79,12 @@ public static Tensor dropout(Tensor x, Tensor keep_prob = null, Tensor noise_sha | |||
| 79 | 79 | /// <param name="swap_memory"></param> | |
| 80 | 80 | /// <param name="time_major"></param> | |
| 81 | 81 | /// <returns>A pair (outputs, state)</returns> | |
| 82 | - public static (Tensor, Tensor) dynamic_rnn(RNNCell cell, Tensor inputs, TF_DataType dtype = TF_DataType.DtInvalid, | ||
| 83 | - bool swap_memory = false, bool time_major = false) | ||
| 84 | - { | ||
| 85 | - with(variable_scope("rnn"), scope => | ||
| 86 | - { | ||
| 87 | - VariableScope varscope = scope; | ||
| 88 | - var flat_input = nest.flatten(inputs); | ||
| 89 | - | ||
| 90 | - if (!time_major) | ||
| 91 | - { | ||
| 92 | - flat_input = flat_input.Select(x => ops.convert_to_tensor(x)).ToList(); | ||
| 93 | - //flat_input = flat_input.Select(x => _transpose_batch_time(x)).ToList(); | ||
| 94 | - } | ||
| 95 | - }); | ||
| 96 | - | ||
| 97 | - throw new NotImplementedException(""); | ||
| 98 | - } | ||
| 82 | + public static (Tensor, Tensor) dynamic_rnn(RNNCell cell, Tensor inputs, | ||
| 83 | + int? sequence_length = null, TF_DataType dtype = TF_DataType.DtInvalid, | ||
| 84 | + int? parallel_iterations = null, bool swap_memory = false, bool time_major = false) | ||
| 85 | + => rnn.dynamic_rnn(cell, inputs, sequence_length: sequence_length, dtype: dtype, | ||
| 86 | + parallel_iterations: parallel_iterations, swap_memory: swap_memory, | ||
| 87 | + time_major: time_major); | ||
| 99 | 88 | ||
| 100 | 89 | public static Tensor elu(Tensor features, string name = null) | |
| 101 | 90 | => gen_nn_ops.elu(features, name: name); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -27,6 +27,8 @@ public class BasicRNNCell : LayerRNNCell | |||
| 27 | 27 | int _num_units; | |
| 28 | 28 | Func<Tensor, string, Tensor> _activation; | |
| 29 | 29 | ||
| 30 | + protected override int state_size => _num_units; | ||
| 31 | + | ||
| 30 | 32 | public BasicRNNCell(int num_units, | |
| 31 | 33 | Func<Tensor, string, Tensor> activation = null, | |
| 32 | 34 | bool? reuse = null, | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,117 @@ | |||
| 1 | + /***************************************************************************** | ||
| 2 | + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. | ||
| 3 | + | ||
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 5 | + you may not use this file except in compliance with the License. | ||
| 6 | + You may obtain a copy of the License at | ||
| 7 | + | ||
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 | ||
| 9 | + | ||
| 10 | + Unless required by applicable law or agreed to in writing, software | ||
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, | ||
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 13 | + See the License for the specific language governing permissions and | ||
| 14 | + limitations under the License. | ||
| 15 | + ******************************************************************************/ | ||
| 16 | + | ||
| 17 | + using System; | ||
| 18 | + using System.Collections.Generic; | ||
| 19 | + using System.Text; | ||
| 20 | + using System.Linq; | ||
| 21 | + using static Tensorflow.Python; | ||
| 22 | + using Tensorflow.Util; | ||
| 23 | + | ||
| 24 | + namespace Tensorflow.Operations | ||
| 25 | + { | ||
| 26 | + internal class rnn | ||
| 27 | + { | ||
| 28 | + public static (Tensor, Tensor) dynamic_rnn(RNNCell cell, Tensor inputs, | ||
| 29 | + int? sequence_length = null, Tensor initial_state = null, | ||
| 30 | + TF_DataType dtype = TF_DataType.DtInvalid, | ||
| 31 | + int? parallel_iterations = null, bool swap_memory = false, bool time_major = false) | ||
| 32 | + { | ||
| 33 | + with(tf.variable_scope("rnn"), scope => | ||
| 34 | + { | ||
| 35 | + VariableScope varscope = scope; | ||
| 36 | + var flat_input = nest.flatten(inputs); | ||
| 37 | + | ||
| 38 | + if (!time_major) | ||
| 39 | + { | ||
| 40 | + flat_input = flat_input.Select(x => ops.convert_to_tensor(x)).ToList(); | ||
| 41 | + flat_input = flat_input.Select(x => _transpose_batch_time(x)).ToList(); | ||
| 42 | + } | ||
| 43 | + | ||
| 44 | + parallel_iterations = parallel_iterations ?? 32; | ||
| 45 | + | ||
| 46 | + if (sequence_length.HasValue) | ||
| 47 | + throw new NotImplementedException("dynamic_rnn sequence_length has value"); | ||
| 48 | + | ||
| 49 | + var batch_size = _best_effort_input_batch_size(flat_input); | ||
| 50 | + | ||
| 51 | + if (initial_state != null) | ||
| 52 | + { | ||
| 53 | + var state = initial_state; | ||
| 54 | + } | ||
| 55 | + else | ||
| 56 | + { | ||
| 57 | + cell.get_initial_state(batch_size: batch_size, dtype: dtype); | ||
| 58 | + } | ||
| 59 | + }); | ||
| 60 | + | ||
| 61 | + throw new NotImplementedException(""); | ||
| 62 | + } | ||
| 63 | + | ||
| 64 | + /// <summary> | ||
| 65 | + /// Transposes the batch and time dimensions of a Tensor. | ||
| 66 | + /// </summary> | ||
| 67 | + /// <param name="x"></param> | ||
| 68 | + /// <returns></returns> | ||
| 69 | + public static Tensor _transpose_batch_time(Tensor x) | ||
| 70 | + { | ||
| 71 | + var x_static_shape = x.TensorShape; | ||
| 72 | + if (x_static_shape.NDim == 1) | ||
| 73 | + return x; | ||
| 74 | + | ||
| 75 | + var x_rank = array_ops.rank(x); | ||
| 76 | + var con1 = new object[] | ||
| 77 | + { | ||
| 78 | + new []{1, 0 }, | ||
| 79 | + math_ops.range(2, x_rank) | ||
| 80 | + }; | ||
| 81 | + var x_t = array_ops.transpose(x, array_ops.concat(con1, 0)); | ||
| 82 | + | ||
| 83 | + var dims = new int[] { x_static_shape.Dimensions[1], x_static_shape.Dimensions[0] } | ||
| 84 | + .ToList(); | ||
| 85 | + dims.AddRange(x_static_shape.Dimensions.Skip(2)); | ||
| 86 | + var shape = new TensorShape(dims.ToArray()); | ||
| 87 | + | ||
| 88 | + x_t.SetShape(shape); | ||
| 89 | + | ||
| 90 | + return x_t; | ||
| 91 | + } | ||
| 92 | + | ||
| 93 | + /// <summary> | ||
| 94 | + /// Get static input batch size if available, with fallback to the dynamic one. | ||
| 95 | + /// </summary> | ||
| 96 | + /// <param name="flat_input"></param> | ||
| 97 | + /// <returns></returns> | ||
| 98 | + private static Tensor _best_effort_input_batch_size(List<Tensor> flat_input) | ||
| 99 | + { | ||
| 100 | + foreach(var input_ in flat_input) | ||
| 101 | + { | ||
| 102 | + var shape = input_.TensorShape; | ||
| 103 | + if (shape.NDim < 0) | ||
| 104 | + continue; | ||
| 105 | + if (shape.NDim < 2) | ||
| 106 | + throw new ValueError($"Expected input tensor {input_.name} to have rank at least 2"); | ||
| 107 | + | ||
| 108 | + var batch_size = shape.Dimensions[1]; | ||
| 109 | + if (batch_size > -1) | ||
| 110 | + throw new ValueError("_best_effort_input_batch_size batch_size > -1"); | ||
| 111 | + //return batch_size; | ||
| 112 | + } | ||
| 113 | + | ||
| 114 | + return array_ops.shape(flat_input[0]).slice(1); | ||
| 115 | + } | ||
| 116 | + } | ||
| 117 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -17,6 +17,8 @@ limitations under the License. | |||
| 17 | 17 | using System; | |
| 18 | 18 | using System.Collections.Generic; | |
| 19 | 19 | using System.Text; | |
| 20 | + using Tensorflow.Util; | ||
| 21 | + using static Tensorflow.Python; | ||
| 20 | 22 | ||
| 21 | 23 | namespace Tensorflow | |
| 22 | 24 | { | |
@@ -48,6 +50,7 @@ public abstract class RNNCell : Layers.Layer | |||
| 48 | 50 | /// difference between TF and Keras RNN cell. | |
| 49 | 51 | /// </summary> | |
| 50 | 52 | protected bool _is_tf_rnn_cell = false; | |
| 53 | + protected virtual int state_size { get; } | ||
| 51 | 54 | ||
| 52 | 55 | public RNNCell(bool trainable = true, | |
| 53 | 56 | string name = null, | |
@@ -59,5 +62,41 @@ public RNNCell(bool trainable = true, | |||
| 59 | 62 | { | |
| 60 | 63 | _is_tf_rnn_cell = true; | |
| 61 | 64 | } | |
| 65 | + | ||
| 66 | + public virtual Tensor get_initial_state(Tensor inputs = null, Tensor batch_size = null, TF_DataType dtype = TF_DataType.DtInvalid) | ||
| 67 | + { | ||
| 68 | + if (inputs != null) | ||
| 69 | + throw new NotImplementedException("get_initial_state input is not null"); | ||
| 70 | + | ||
| 71 | + return zero_state(batch_size, dtype); | ||
| 72 | + } | ||
| 73 | + | ||
| 74 | + /// <summary> | ||
| 75 | + /// Return zero-filled state tensor(s). | ||
| 76 | + /// </summary> | ||
| 77 | + /// <param name="batch_size"></param> | ||
| 78 | + /// <param name="dtype"></param> | ||
| 79 | + /// <returns></returns> | ||
| 80 | + public Tensor zero_state(Tensor batch_size, TF_DataType dtype) | ||
| 81 | + { | ||
| 82 | + Tensor output = null; | ||
| 83 | + var state_size = this.state_size; | ||
| 84 | + with(ops.name_scope($"{this.GetType().Name}ZeroState", values: new { batch_size }), delegate | ||
| 85 | + { | ||
| 86 | + output = _zero_state_tensors(state_size, batch_size, dtype); | ||
| 87 | + }); | ||
| 88 | + | ||
| 89 | + return output; | ||
| 90 | + } | ||
| 91 | + | ||
| 92 | + private Tensor _zero_state_tensors(int state_size, Tensor batch_size, TF_DataType dtype) | ||
| 93 | + { | ||
| 94 | + nest.map_structure(x => | ||
| 95 | + { | ||
| 96 | + throw new NotImplementedException(""); | ||
| 97 | + }, state_size); | ||
| 98 | + | ||
| 99 | + throw new NotImplementedException(""); | ||
| 100 | + } | ||
| 62 | 101 | } | |
| 63 | 102 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -512,6 +512,14 @@ public static IEnumerable<object> map_structure(Func<object[], object> func, par | |||
| 512 | 512 | return _yield_value(pack_sequence_as(structure[0], mapped_flat_structure)).ToList(); | |
| 513 | 513 | } | |
| 514 | 514 | ||
| 515 | + public static Tensor map_structure<T>(Func<T, Tensor> func, T structure) | ||
| 516 | + { | ||
| 517 | + var flat_structure = flatten(structure); | ||
| 518 | + var mapped_flat_structure = flat_structure.Select(func).ToList(); | ||
| 519 | + | ||
| 520 | + return pack_sequence_as(structure, mapped_flat_structure) as Tensor; | ||
| 521 | + } | ||
| 522 | + | ||
| 515 | 523 | /// <summary> | |
| 516 | 524 | /// Same as map_structure, but with only one structure (no combining of multiple structures) | |
| 517 | 525 | /// </summary> | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2,7 +2,7 @@ | |||
| 2 | 2 | ||
| 3 | 3 | <PropertyGroup> | |
| 4 | 4 | <OutputType>Exe</OutputType> | |
| 5 | - <TargetFramework>netcoreapp3.0</TargetFramework> | ||
| 5 | + <TargetFramework>netcoreapp2.2</TargetFramework> | ||
| 6 | 6 | </PropertyGroup> | |
| 7 | 7 | ||
| 8 | 8 | <ItemGroup> | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -30,7 +30,7 @@ namespace TensorFlowNET.Examples.ImageProcess | |||
| 30 | 30 | /// </summary> | |
| 31 | 31 | public class DigitRecognitionRNN : IExample | |
| 32 | 32 | { | |
| 33 | - public bool Enabled { get; set; } = false; | ||
| 33 | + public bool Enabled { get; set; } = true; | ||
| 34 | 34 | public bool IsImportingGraph { get; set; } = false; | |
| 35 | 35 | ||
| 36 | 36 | public string Name => "MNIST RNN"; | |
@@ -95,7 +95,7 @@ public void Train(Session sess) | |||
| 95 | 95 | var init = tf.global_variables_initializer(); | |
| 96 | 96 | sess.run(init); | |
| 97 | 97 | ||
| 98 | - float loss_val = 100.0f; | ||
| 98 | + float loss_val = 100.0f; | ||
| 99 | 99 | float accuracy_val = 0f; | |
| 100 | 100 | ||
| 101 | 101 | foreach (var epoch in range(epochs)) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -29,23 +29,36 @@ class Program | |||
| 29 | 29 | { | |
| 30 | 30 | static void Main(string[] args) | |
| 31 | 31 | { | |
| 32 | + int finished = 0; | ||
| 32 | 33 | var errors = new List<string>(); | |
| 33 | 34 | var success = new List<string>(); | |
| 35 | + | ||
| 36 | + var parsedArgs = ParseArgs(args); | ||
| 37 | + | ||
| 34 | 38 | var examples = Assembly.GetEntryAssembly().GetTypes() | |
| 35 | 39 | .Where(x => x.GetInterfaces().Contains(typeof(IExample))) | |
| 36 | 40 | .Select(x => (IExample)Activator.CreateInstance(x)) | |
| 37 | 41 | .Where(x => x.Enabled) | |
| 38 | 42 | .OrderBy(x => x.Name) | |
| 39 | 43 | .ToArray(); | |
| 40 | 44 | ||
| 45 | + if (parsedArgs.ContainsKey("ex")) | ||
| 46 | + examples = examples.Where(x => x.Name == parsedArgs["ex"]).ToArray(); | ||
| 47 | + | ||
| 41 | 48 | Console.WriteLine(Environment.OSVersion.ToString(), Color.Yellow); | |
| 42 | 49 | Console.WriteLine($"TensorFlow Binary v{tf.VERSION}", Color.Yellow); | |
| 43 | 50 | Console.WriteLine($"TensorFlow.NET v{Assembly.GetAssembly(typeof(TF_DataType)).GetName().Version}", Color.Yellow); | |
| 44 | 51 | ||
| 45 | 52 | for (var i = 0; i < examples.Length; i++) | |
| 46 | 53 | Console.WriteLine($"[{i}]: {examples[i].Name}"); | |
| 47 | - Console.Write($"Choose one example to run, hit [Enter] to run all: ", Color.Yellow); | ||
| 48 | - var key = Console.ReadLine(); | ||
| 54 | + | ||
| 55 | + var key = "0"; | ||
| 56 | + | ||
| 57 | + if (examples.Length > 1) | ||
| 58 | + { | ||
| 59 | + Console.Write($"Choose one example to run, hit [Enter] to run all: ", Color.Yellow); | ||
| 60 | + key = Console.ReadLine(); | ||
| 61 | + } | ||
| 49 | 62 | ||
| 50 | 63 | var sw = new Stopwatch(); | |
| 51 | 64 | for (var i = 0; i < examples.Length; i++) | |
@@ -72,14 +85,35 @@ static void Main(string[] args) | |||
| 72 | 85 | Console.WriteLine(ex); | |
| 73 | 86 | } | |
| 74 | 87 | ||
| 88 | + finished++; | ||
| 75 | 89 | Console.WriteLine($"{DateTime.UtcNow} Completed {example.Name}", Color.White); | |
| 76 | 90 | } | |
| 77 | 91 | ||
| 78 | 92 | success.ForEach(x => Console.WriteLine($"{x} is OK!", Color.Green)); | |
| 79 | 93 | errors.ForEach(x => Console.WriteLine($"{x} is Failed!", Color.Red)); | |
| 80 | 94 | ||
| 81 | - Console.WriteLine($"{examples.Length} examples are completed."); | ||
| 95 | + Console.WriteLine($"{finished} of {examples.Length} example(s) are completed."); | ||
| 82 | 96 | Console.ReadLine(); | |
| 83 | 97 | } | |
| 98 | + | ||
| 99 | + private static Dictionary<string, string> ParseArgs(string[] args) | ||
| 100 | + { | ||
| 101 | + var parsed = new Dictionary<string, string>(); | ||
| 102 | + | ||
| 103 | + for (int i = 0; i < args.Length; i++) | ||
| 104 | + { | ||
| 105 | + string key = args[i].Substring(1); | ||
| 106 | + switch (key) | ||
| 107 | + { | ||
| 108 | + case "ex": | ||
| 109 | + parsed.Add(key, args[++i]); | ||
| 110 | + break; | ||
| 111 | + default: | ||
| 112 | + break; | ||
| 113 | + } | ||
| 114 | + } | ||
| 115 | + | ||
| 116 | + return parsed; | ||
| 117 | + } | ||
| 84 | 118 | } | |
| 85 | 119 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -6,12 +6,6 @@ | |||
| 6 | 6 | <GeneratePackageOnBuild>false</GeneratePackageOnBuild> | |
| 7 | 7 | </PropertyGroup> | |
| 8 | 8 | ||
| 9 | - <ItemGroup> | ||
| 10 | - <Compile Remove="python\**" /> | ||
| 11 | - <EmbeddedResource Remove="python\**" /> | ||
| 12 | - <None Remove="python\**" /> | ||
| 13 | - </ItemGroup> | ||
| 14 | - | ||
| 15 | 9 | <ItemGroup> | |
| 16 | 10 | <PackageReference Include="Colorful.Console" Version="1.2.9" /> | |
| 17 | 11 | <PackageReference Include="Newtonsoft.Json" Version="12.0.2" /> | |
| Back | FazBrowse Home | New Git URL |
0 commit comments