| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 8e054af commit a65d881
11 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -172,7 +172,8 @@ private static (Tensor, Tensor) _dynamic_rnn_loop(RNNCell cell, Tensor inputs, T | |||
| 172 | 172 | ||
| 173 | 173 | for (int i = 0; i < input_ta.Count; i++) | |
| 174 | 174 | { | |
| 175 | - var (ta, input_) = (input_ta[0], flat_input[0]); | ||
| 175 | + var (ta, input_) = (input_ta[i], flat_input[i]); | ||
| 176 | + ta.unstack(input_); | ||
| 176 | 177 | } | |
| 177 | 178 | } | |
| 178 | 179 | ||
@@ -185,16 +186,16 @@ private static (Tensor, Tensor) _dynamic_rnn_loop(RNNCell cell, Tensor inputs, T | |||
| 185 | 186 | ||
| 186 | 187 | Func<BodyItemInRnnWhileLoop, Tensor> cond = (item) => | |
| 187 | 188 | { | |
| 188 | - return time < loop_bound; | ||
| 189 | + return item.time < loop_bound; | ||
| 189 | 190 | }; | |
| 190 | 191 | ||
| 191 | 192 | // Take a time step of the dynamic RNN. | |
| 192 | 193 | Func<BodyItemInRnnWhileLoop, BodyItemInRnnWhileLoop> _time_step = (item) => | |
| 193 | 194 | { | |
| 194 | - return item; | ||
| 195 | + throw new NotImplementedException(""); | ||
| 195 | 196 | }; | |
| 196 | 197 | ||
| 197 | - control_flow_ops.while_loop<BodyItemInRnnWhileLoop>( | ||
| 198 | + control_flow_ops.while_loop( | ||
| 198 | 199 | cond: cond, | |
| 199 | 200 | body: _time_step, | |
| 200 | 201 | loop_vars: new BodyItemInRnnWhileLoop(time, output_ta.ToArray(), state), | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -30,10 +30,9 @@ public partial class Operation | |||
| 30 | 30 | /// </summary> | |
| 31 | 31 | public void _control_flow_post_processing() | |
| 32 | 32 | { | |
| 33 | - foreach(var input_tensor in inputs) | ||
| 33 | + foreach(Tensor input_tensor in inputs) | ||
| 34 | 34 | { | |
| 35 | - //TODO: implement below code dependency | ||
| 36 | - //control_flow_util.CheckInputFromValidContext(this, input_tensor.op); | ||
| 35 | + control_flow_util.CheckInputFromValidContext(this, input_tensor.op); | ||
| 37 | 36 | } | |
| 38 | 37 | ||
| 39 | 38 | if (_control_flow_context != null) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -23,6 +23,7 @@ limitations under the License. | |||
| 23 | 23 | using System.IO; | |
| 24 | 24 | using System.Linq; | |
| 25 | 25 | using Tensorflow.Util; | |
| 26 | + using static Tensorflow.Binding; | ||
| 26 | 27 | ||
| 27 | 28 | namespace Tensorflow | |
| 28 | 29 | { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -25,21 +25,25 @@ namespace Tensorflow.Operations | |||
| 25 | 25 | internal class _GraphTensorArray | |
| 26 | 26 | { | |
| 27 | 27 | internal TF_DataType _dtype; | |
| 28 | + public TF_DataType dtype => _dtype; | ||
| 28 | 29 | ||
| 29 | 30 | /// <summary> | |
| 30 | 31 | /// Used to keep track of what tensors the TensorArray should be | |
| 31 | 32 | /// colocated with. We choose to colocate the TensorArray with the | |
| 32 | 33 | /// first tensor written to it. | |
| 33 | 34 | /// </summary> | |
| 34 | 35 | bool _colocate_with_first_write_call; | |
| 36 | + public bool colocate_with_first_write_call => _colocate_with_first_write_call; | ||
| 35 | 37 | ||
| 36 | 38 | bool _infer_shape; | |
| 37 | - bool _dynamic_size; | ||
| 38 | - List<TensorShape> _element_shape; | ||
| 39 | + public bool infer_shape => _infer_shape; | ||
| 40 | + public bool _dynamic_size; | ||
| 41 | + public List<TensorShape> _element_shape; | ||
| 39 | 42 | ||
| 40 | - List<Tensor> _colocate_with; | ||
| 43 | + public List<Tensor> _colocate_with; | ||
| 41 | 44 | ||
| 42 | 45 | internal Tensor _handle; | |
| 46 | + public Tensor handle => _handle; | ||
| 43 | 47 | internal Tensor _flow; | |
| 44 | 48 | ||
| 45 | 49 | public _GraphTensorArray(TF_DataType dtype, Tensor size, bool? dynamic_size = null, | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -21,6 +21,7 @@ limitations under the License. | |||
| 21 | 21 | using Tensorflow.Operations.ControlFlows; | |
| 22 | 22 | using util = Tensorflow.control_flow_util; | |
| 23 | 23 | using static Tensorflow.Binding; | |
| 24 | + using Tensorflow.Util; | ||
| 24 | 25 | ||
| 25 | 26 | namespace Tensorflow | |
| 26 | 27 | { | |
@@ -251,12 +252,16 @@ public static Tensor _Identity(Tensor data, string name = null) | |||
| 251 | 252 | return gen_array_ops.identity(data, name: name); | |
| 252 | 253 | } | |
| 253 | 254 | ||
| 254 | - public static void _SetShapeInvariants(Tensor[] input_vars, Tensor[] enter_vars, TensorShape shapes = null) | ||
| 255 | + public static void _SetShapeInvariants(Tensor[] input_vars, Tensor[] enter_vars, TensorShape[] shapes = null) | ||
| 255 | 256 | { | |
| 256 | 257 | if (shapes == null) | |
| 257 | 258 | return; | |
| 258 | 259 | ||
| 259 | - throw new NotImplementedException("_SetShapeInvariants"); | ||
| 260 | + var flat_shapes = nest.flatten2(shapes); | ||
| 261 | + foreach (var (inp, var, shape) in zip(input_vars, enter_vars, flat_shapes)) | ||
| 262 | + { | ||
| 263 | + var.set_shape(shape); | ||
| 264 | + } | ||
| 260 | 265 | } | |
| 261 | 266 | ||
| 262 | 267 | /// <summary> | |
@@ -428,12 +433,12 @@ raise ValueError( | |||
| 428 | 433 | .Select(pair => merge(new Tensor[] { pair.Item1, pair.Item2 })) | |
| 429 | 434 | .ToArray(); | |
| 430 | 435 | ||
| 431 | - merges = _convert_flows_to_tensorarrays(new Tensor[] { (Tensor)orig_res_t }, merges); | ||
| 436 | + var merges2 = _convert_flows_to_tensorarrays(new ITensorOrTensorArray[] { (Tensor)orig_res_t }, merges); | ||
| 432 | 437 | ||
| 433 | 438 | ops.add_to_collection(tf.GraphKeys.COND_CONTEXT, context_t); | |
| 434 | 439 | ops.add_to_collection(tf.GraphKeys.COND_CONTEXT, context_f); | |
| 435 | 440 | ||
| 436 | - return merges[0]; | ||
| 441 | + return new Tensor(IntPtr.Zero); | ||
| 437 | 442 | }); | |
| 438 | 443 | } | |
| 439 | 444 | ||
@@ -473,22 +478,28 @@ public static Tensor[] cond<T>(Tensor pred, | |||
| 473 | 478 | var res_f_flat = res_f; | |
| 474 | 479 | ||
| 475 | 480 | var merges = zip(res_f_flat, res_t_flat) | |
| 476 | - .Select(pair => merge(new Tensor[] { pair.Item1, pair.Item2 })) | ||
| 481 | + .Select(pair => merge(new [] { pair.Item1, pair.Item2 })) | ||
| 477 | 482 | .ToArray(); | |
| 478 | 483 | ||
| 479 | - merges = _convert_flows_to_tensorarrays(orig_res_t, merges); | ||
| 484 | + var merges2 = _convert_flows_to_tensorarrays(orig_res_t.Select(x => (ITensorOrTensorArray)x).ToArray(), merges); | ||
| 480 | 485 | ||
| 481 | 486 | ops.add_to_collection(tf.GraphKeys.COND_CONTEXT, context_t); | |
| 482 | 487 | ops.add_to_collection(tf.GraphKeys.COND_CONTEXT, context_f); | |
| 483 | 488 | ||
| 484 | - return merges; | ||
| 489 | + return new[] { new Tensor(IntPtr.Zero) }; | ||
| 485 | 490 | }); | |
| 486 | 491 | } | |
| 487 | 492 | ||
| 488 | - public static Tensor[] _convert_flows_to_tensorarrays<T>(T tensors_or_tensorarrays, Tensor[] tensors_or_flows) | ||
| 493 | + public static ITensorOrTensorArray[] _convert_flows_to_tensorarrays(ITensorOrTensorArray[] tensors_or_tensorarrays, Tensor[] tensors_or_flows) | ||
| 489 | 494 | { | |
| 490 | - // zip(tensors_or_tensorarrays, tensors_or_flows).Select((ta, t_or_flow) => ta).ToArray(); | ||
| 491 | - return tensors_or_flows; | ||
| 495 | + return zip(tensors_or_tensorarrays, tensors_or_flows).Select(x => | ||
| 496 | + { | ||
| 497 | + var (ta, t_or_flow) = (x.Item1, x.Item2); | ||
| 498 | + if (ta is TensorArray ta_1) | ||
| 499 | + return tensor_array_ops.build_ta_with_new_flow(ta_1, t_or_flow) as ITensorOrTensorArray; | ||
| 500 | + else | ||
| 501 | + return t_or_flow as ITensorOrTensorArray; | ||
| 502 | + }).ToArray(); | ||
| 492 | 503 | } | |
| 493 | 504 | ||
| 494 | 505 | /// <summary> | |
@@ -592,7 +603,7 @@ public static Tensor ZerosLikeOutsideLoop(Operation op, int index) | |||
| 592 | 603 | /// <param name="loop_vars"></param> | |
| 593 | 604 | /// <param name="i"></param> | |
| 594 | 605 | public static Tensor while_loop<TItem>(Func<TItem, Tensor> cond, Func<TItem, TItem> body, TItem loop_vars, | |
| 595 | - TensorShape shape_invariants = null, | ||
| 606 | + TensorShape[] shape_invariants = null, | ||
| 596 | 607 | int parallel_iterations = 10, | |
| 597 | 608 | bool back_prop = true, | |
| 598 | 609 | bool swap_memory = false, | |
@@ -617,8 +628,8 @@ public static Tensor while_loop<TItem>(Func<TItem, Tensor> cond, Func<TItem, TIt | |||
| 617 | 628 | var orig_body = body; | |
| 618 | 629 | ||
| 619 | 630 | LoopVar<TItem> loop_vars_1 = null; | |
| 620 | - Func<Tensor, TItem, LoopVar<TItem>> body_buildloop = null; | ||
| 621 | - Func<Tensor, TItem, Tensor> cond_buildloop = null; | ||
| 631 | + Func<LoopVar<TItem>, LoopVar<TItem>> body_buildloop = null; | ||
| 632 | + Func<LoopVar<TItem>, Tensor> cond_buildloop = null; | ||
| 622 | 633 | ||
| 623 | 634 | if (try_to_pack) | |
| 624 | 635 | { | |
@@ -627,9 +638,18 @@ public static Tensor while_loop<TItem>(Func<TItem, Tensor> cond, Func<TItem, TIt | |||
| 627 | 638 | else | |
| 628 | 639 | { | |
| 629 | 640 | loop_vars_1 = new LoopVar<TItem>(counter, loop_vars); | |
| 630 | - cond_buildloop = (i, lv) => | ||
| 631 | - math_ops.logical_and(i < maximum_iterations, orig_cond(lv)); | ||
| 632 | - body_buildloop = (i, lv) => new LoopVar<TItem>(i + 1, orig_body(lv)); | ||
| 641 | + cond_buildloop = (item) => | ||
| 642 | + { | ||
| 643 | + var (i, lv) = (item.Counter, item.Item); | ||
| 644 | + var oc = orig_cond(lv); | ||
| 645 | + return math_ops.logical_and(i < maximum_iterations, oc); | ||
| 646 | + }; | ||
| 647 | + | ||
| 648 | + body_buildloop = (item) => | ||
| 649 | + { | ||
| 650 | + var (i, lv) = (item.Counter, item.Item); | ||
| 651 | + return new LoopVar<TItem>(i + 1, orig_body(lv)); | ||
| 652 | + }; | ||
| 633 | 653 | } | |
| 634 | 654 | try_to_pack = false; | |
| 635 | 655 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -14,7 +14,9 @@ You may obtain a copy of the License at | |||
| 14 | 14 | limitations under the License. | |
| 15 | 15 | ******************************************************************************/ | |
| 16 | 16 | ||
| 17 | + using System; | ||
| 17 | 18 | using Tensorflow.Operations; | |
| 19 | + using static Tensorflow.Binding; | ||
| 18 | 20 | ||
| 19 | 21 | namespace Tensorflow | |
| 20 | 22 | { | |
@@ -53,5 +55,25 @@ public static ControlFlowContext GetOutputContext(Operation op) | |||
| 53 | 55 | ctxt = ctxt.outer_context; | |
| 54 | 56 | return ctxt; | |
| 55 | 57 | } | |
| 58 | + | ||
| 59 | + public static void CheckInputFromValidContext(Operation op, Operation input_op) | ||
| 60 | + { | ||
| 61 | + var op_ctxt = op._get_control_flow_context(); | ||
| 62 | + var input_ctxt = GetOutputContext(input_op); | ||
| 63 | + var valid = false; | ||
| 64 | + if (input_ctxt == null) | ||
| 65 | + valid = true; | ||
| 66 | + else if (op_ctxt == input_ctxt) | ||
| 67 | + valid = true; | ||
| 68 | + else | ||
| 69 | + { | ||
| 70 | + throw new NotImplementedException(""); | ||
| 71 | + } | ||
| 72 | + | ||
| 73 | + if (!valid) | ||
| 74 | + { | ||
| 75 | + throw new NotImplementedException(""); | ||
| 76 | + } | ||
| 77 | + } | ||
| 56 | 78 | } | |
| 57 | 79 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -14,6 +14,8 @@ You may obtain a copy of the License at | |||
| 14 | 14 | limitations under the License. | |
| 15 | 15 | ******************************************************************************/ | |
| 16 | 16 | ||
| 17 | + using static Tensorflow.Binding; | ||
| 18 | + | ||
| 17 | 19 | namespace Tensorflow | |
| 18 | 20 | { | |
| 19 | 21 | public static class gen_math_ops | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,33 @@ | |||
| 1 | + using System; | ||
| 2 | + using System.Collections.Generic; | ||
| 3 | + using System.Text; | ||
| 4 | + | ||
| 5 | + namespace Tensorflow | ||
| 6 | + { | ||
| 7 | + public class tensor_array_ops | ||
| 8 | + { | ||
| 9 | + /// <summary> | ||
| 10 | + /// Builds a TensorArray with a new `flow` tensor. | ||
| 11 | + /// </summary> | ||
| 12 | + /// <param name="old_ta"></param> | ||
| 13 | + /// <param name="flow"></param> | ||
| 14 | + /// <returns></returns> | ||
| 15 | + public static TensorArray build_ta_with_new_flow(TensorArray old_ta, Tensor flow) | ||
| 16 | + { | ||
| 17 | + var impl = old_ta._implementation; | ||
| 18 | + | ||
| 19 | + var new_ta = new TensorArray( | ||
| 20 | + dtype: impl.dtype, | ||
| 21 | + handle: impl.handle, | ||
| 22 | + flow: flow, | ||
| 23 | + infer_shape: impl.infer_shape, | ||
| 24 | + colocate_with_first_write_call: impl.colocate_with_first_write_call); | ||
| 25 | + | ||
| 26 | + var new_impl = new_ta._implementation; | ||
| 27 | + new_impl._dynamic_size = impl._dynamic_size; | ||
| 28 | + new_impl._colocate_with = impl._colocate_with; | ||
| 29 | + new_impl._element_shape = impl._element_shape; | ||
| 30 | + return new_ta; | ||
| 31 | + } | ||
| 32 | + } | ||
| 33 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -20,7 +20,8 @@ Building, training and infering deep learning models. | |||
| 20 | 20 | https://tensorflownet.readthedocs.io</Description> | |
| 21 | 21 | <AssemblyVersion>0.12.0.0</AssemblyVersion> | |
| 22 | 22 | <PackageReleaseNotes>Changes since v0.11.0: | |
| 23 | - </PackageReleaseNotes> | ||
| 23 | + 1: Add ICanBeFlattened for nest.flatten2. | ||
| 24 | + 2:</PackageReleaseNotes> | ||
| 24 | 25 | <LangVersion>7.3</LangVersion> | |
| 25 | 26 | <FileVersion>0.12.0.0</FileVersion> | |
| 26 | 27 | <PackageLicenseFile>LICENSE</PackageLicenseFile> | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -39,7 +39,7 @@ namespace Tensorflow | |||
| 39 | 39 | /// Internally, TensorFlow represents tensors as n-dimensional arrays of base datatypes. | |
| 40 | 40 | /// </summary> | |
| 41 | 41 | [SuppressMessage("ReSharper", "ConvertToAutoProperty")] | |
| 42 | - public partial class Tensor : DisposableObject, ITensorOrOperation, _TensorLike | ||
| 42 | + public partial class Tensor : DisposableObject, ITensorOrOperation, _TensorLike, ITensorOrTensorArray | ||
| 43 | 43 | { | |
| 44 | 44 | private readonly int _id; | |
| 45 | 45 | private readonly Operation _op; | |
@@ -178,7 +178,7 @@ public int[] _shape_tuple() | |||
| 178 | 178 | /// </summary> | |
| 179 | 179 | public void set_shape(TensorShape shape) | |
| 180 | 180 | { | |
| 181 | - this.shape = shape.rank > 0 ? shape.dims : null; | ||
| 181 | + this.shape = shape.rank >= 0 ? shape.dims : null; | ||
| 182 | 182 | } | |
| 183 | 183 | ||
| 184 | 184 | /// <summary> | |
| Back | FazBrowse Home | New Git URL |
0 commit comments